Skip to content

Instantly share code, notes, and snippets.

@zymsys
Created November 1, 2014 22:10
Show Gist options
  • Save zymsys/23c0d7824730a2aa2063 to your computer and use it in GitHub Desktop.
Save zymsys/23c0d7824730a2aa2063 to your computer and use it in GitHub Desktop.
Notes from my October 2013 Composer Talk for GTA-PHP
Assemble Johnny's dependencies from scratch using composer.
Vics-MacBook-Pro:johnnydo vic$ composer init
Welcome to the Composer config generator
This command will guide you through creating your composer.json config.
Package name (<vendor>/<name>) [root/johnnydo]: johnny/do
Description []: Johnny's To Do List App
Author [Vic Metcalfe <[email protected]>]: Johnny <[email protected]>
Minimum Stability []: dev
License []: MIT
Define your dependencies.
Would you like to define your dependencies (require) interactively [yes]? no
Would you like to define your dev dependencies (require-dev) interactively [yes]? no
{
"name": "johnny/do",
"description": "Johnny's To Do List App",
"license": "MIT",
"authors": [
{
"name": "Johnny",
"email": "[email protected]"
}
],
"minimum-stability": "dev",
"require": {
}
}
Do you confirm generation [yes]?
Look at the generated composer.json, especially minimum-stability, which can be dev, alpha, beta, RC, and stable.
Now let's install stuff. Talk about finding items on packagist.org.
Vics-MacBook-Pro:johnnydo vic$ composer require slim/slim
Please provide a version constraint for the slim/slim requirement: *
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Installing slim/slim (2.2.0)
Downloading: 100%
Writing lock file
Generating autoload files
Vics-MacBook-Pro:johnnydo vic$
Look at changes to composer.json
Look at composer.lock and explain its purpose. Mention its place in git repos.
Look at the vendor folder:
Composer's autoloader and the composer software
Folder for slim
Vics-MacBook-Pro:johnnydo vic$ composer require slim/views:*
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Installing slim/views (0.1.0)
Downloading: 100%
Writing lock file
Generating autoload files
Vics-MacBook-Pro:johnnydo vic$
Point out that this time we put the version info on the command line to avoid the prompt.
Point out that views went into slim/views since it belongs to the same vendor.
Vics-MacBook-Pro:johnnydo vic$ composer require twig/twig:*
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Installing twig/twig (dev-master d827c60)
Cloning d827c601e3afea6535fede5e39c9f91c12fc2e66
Writing lock file
Generating autoload files
Vics-MacBook-Pro:johnnydo vic$
As expected, we get a new vendor folder for twig.
Vics-MacBook-Pro:johnnydo vic$ composer require doctrine/orm:*
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Installing doctrine/lexer (dev-master bc0e1f0)
Cloning bc0e1f0cc285127a38c6c8ea88bc5dba2fd53e94
- Installing doctrine/annotations (v1.1.2)
Downloading: 100%
- Installing doctrine/collections (dev-master bcb5377)
Cloning bcb53776a096a0c64579cc8d8ec0db62f1109fbc
- Installing doctrine/cache (v1.1)
Downloading: 100%
- Installing doctrine/inflector (dev-master 8b4b3cc)
Cloning 8b4b3ccec7aafc596e2fc1e593c9f2e78f939c8c
- Installing doctrine/common (2.4.x-dev c94d6ff)
Cloning c94d6ff79e25418b1225e187c782bf4742f23a8b
- Installing doctrine/dbal (dev-master 8f119ee)
Cloning 8f119eea05d61ef85396201ae5e5df49bdc2f29f
- Installing symfony/console (dev-master 2b9e91a)
Cloning 2b9e91a60b4cac3edae1aaef23b9cb25b37fb627
- Installing doctrine/orm (dev-master bd7c7eb)
Cloning bd7c7ebaf353f038fae2f828802ecda823190759
symfony/console suggests installing symfony/event-dispatcher ()
doctrine/orm suggests installing symfony/yaml (If you want to use YAML Metadata Mapping Driver)
Writing lock file
Generating autoload files
Vics-MacBook-Pro:johnnydo vic$
Talk to Doctrine pulling in its own dependencies.
Mention the ~/.composer cache
Let's say we want symfony/yaml as doctrine suggested, and let's add it by modifying composer.json directly this time, and then running 'composer update'.
Vics-MacBook-Pro:johnnydo vic$ vi composer.json
Vics-MacBook-Pro:johnnydo vic$ composer update
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Removing doctrine/cache (v1.1)
- Installing doctrine/cache (v1.2.0)
Downloading: 100%
- Installing symfony/yaml (dev-master ef4878b)
Cloning ef4878bcca171ebe7466be0505b85a710818d788
- Updating twig/twig dev-master (d827c60 => 5d67e0e)
Checking out 5d67e0eaa42fa738ef47b9c6eb2f31bfe78435d0
- Updating doctrine/dbal dev-master (8f119ee => e0ffc8a)
Checking out e0ffc8a9cab3a5c1ca3bd8ccce19a218a38f3b5d
- Updating symfony/console dev-master (2b9e91a => e430d74)
Checking out e430d7416327a93be77b3c4844c191a10cc8c1c7
- Updating doctrine/orm dev-master (bd7c7eb => 0081879)
Checking out 008187982d27cb864ffc44120b5e14e9cd1eb2e2
Writing lock file
Generating autoload files
I waited between the above examples and this one about five days, and look at all the unexpected updates! Show the slide.
Talk about composer install vs update.
Now let's build a library.
First, add the autoloader to our composer.json:
Cheat: cat ~/composer/autoload.txt
"autoload": {
"psr-0": { "gtaphp\\ApplesAndOranges\\": "src/" }
}
Make these folders:
* src
* gtaphp
* ApplesAndOranges
Then, in the deepest folder create ApplesAndOrangesMiddleware.php like this:
Cheat: cp ~/composer/ApplesAndOrangesMiddleware.php src/gtaphp/ApplesAndOranges/
<?php
namespace gtaphp\ApplesAndOranges;
use Slim\Middleware;
class ApplesAndOrangesMiddleware extends Middleware
{
public function call()
{
$this->next->call();
$this->app->response()->body(
str_replace(
'Apples',
'Oranges',
$this->app->response()->body()
)
);
}
}
Then write an app that uses this middleware, placing app.php in the project root:
Cheat: cp ~/composer/app.php .
<?php
require_once 'vendor/autoload.php';
$app = new \Slim\Slim();
$app->add(new \gtaphp\ApplesAndOranges\ApplesAndOrangesMiddleware());
$app->get('/', function () use ($app) {
echo "I like to eat Apples and Bananas!";
});
$app->run();
Demonstrate how the autoloader can load the Middleware's php, and that the thing works:
Vics-MacBook-Pro:johnnydo vic$ php54 -S localhost:8888 app.php
PHP 5.4.18 Development Server started at Tue Oct 1 08:56:51 2013
Listening on http://localhost:8888
Document root is /Users/vic/prog/gtaphp/composer/ApplesAndOranges
Press Ctrl-C to quit.
Now let's make a separate app from the middleware.
Vics-MacBook-Pro:johnnydo vic$ cd ..
Vics-MacBook-Pro:composer vic$ mkdir ApplesAndOranges
Vics-MacBook-Pro:composer vic$ mkdir ApplesAndOrangesAppVics-MacBook-Pro:composer vic$ cd ApplesAndOrangesVics-MacBook-Pro:ApplesAndOranges vic$ git init
Initialized empty Git repository in /Users/vic/prog/gtaphp/composer/ApplesAndOranges/.git/
Vics-MacBook-Pro:ApplesAndOranges vic$ composer init
Create slim dependency interactively this time. Show how vendor can be automatically added to .gitignore.
We need to manually add the autoloader bit to our composer.json:
"autoload": {
"psr-0": { "gtaphp\\ApplesAndOranges\\": "src/" }
}
Move src folder into new folder, then add the files and do an initial commit:
Vics-MacBook-Pro:ApplesAndOranges vic$ git add composer.json
Vics-MacBook-Pro:ApplesAndOranges vic$ git add src/gtaphp/ApplesAndOranges/ApplesAndOrangesMiddleware.php
Vics-MacBook-Pro:ApplesAndOranges vic$ git commit -m "Initial Commit"
[master (root-commit) 49b7676] Initial Commit
2 files changed, 34 insertions(+), 0 deletions(-)
create mode 100644 composer.json
create mode 100644 src/gtaphp/ApplesAndOranges/ApplesAndOrangesMiddleware.php
Try to install satis the recommended way:
$ composer create-project composer/satis --stability=dev
If it barfs, cd satis and run: composer --prefer-source install
Create a satis.json:
Cheat: cp ~/composer/satis.json .
{
"name": "Demo Repository",
"homepage": "http://localhost:8889",
"repositories": [
{ "type": "vcs", "url": "/Users/vic/prog/gtaphp/composer/ApplesAndOranges" }
],
"require-all": true,
"output-dir": "web"
}
Then:
Vics-MacBook-Pro:satis vic$ bin/satis build
Scanning packages
Writing packages.json
Writing web view
Vics-MacBook-Pro:satis vic$ cd web/
Vics-MacBook-Pro:web vic$ php54 -S localhost:8889
PHP 5.4.18 Development Server started at Tue Oct 1 14:26:47 2013
Listening on http://localhost:8889
Document root is /Users/vic/prog/gtaphp/composer/satis/web
Press Ctrl-C to quit.
Show our private repository page: http://localhost:8889/
Show how it tags the version as master-dev. This is because we have no tagged versions, and all branches are created as -dev releases. Our project needs a stable release, so we need to do this:
Vics-MacBook-Pro:ApplesAndOrangesApp vic$ cd ../ApplesAndOranges
Vics-MacBook-Pro:ApplesAndOranges vic$ git tag v1.0.0Vics-MacBook-Pro:ApplesAndOranges vic$ cd ../satis/
Vics-MacBook-Pro:satis vic$ bin/satis build
Scanning packages
Writing packages.json
Writing web view
Vics-MacBook-Pro:satis vic$ cd web
Vics-MacBook-Pro:web vic$ php54 -S localhost:8889
PHP 5.4.18 Development Server started at Tue Oct 1 14:46:00 2013
Listening on http://localhost:8889
Document root is /Users/vic/prog/gtaphp/composer/satis/web
Press Ctrl-C to quit.
Refresh http://localhost:8889/ and show how we now have a v1.0.0 release available, which will satisfy our stable requirement.
Set up the app:
Vics-MacBook-Pro:composer vic$ cd ApplesAndOrangesApp/
Vics-MacBook-Pro:ApplesAndOrangesApp vic$ composer init
Add the repository on that page to the app's composer.json and then from the command line:
Vics-MacBook-Pro:ApplesAndOrangesApp vic$ composer require gtaphp/apples-and-oranges
Please provide a version constraint for the gtaphp/apples-and-oranges requirement: *
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Installing slim/slim (2.2.0)
Loading from cache
- Installing gtaphp/apples-and-oranges (v1.0.0)
Cloning 49b7676d7b9195c4833aaff5cafcf3cf471c60e4
Writing lock file
Generating autoload files
Vics-MacBook-Pro:ApplesAndOrangesApp vic$ cp ../working/app.php .
Vics-MacBook-Pro:ApplesAndOrangesApp vic$ php54 -S localhost:8888 app.php
PHP 5.4.18 Development Server started at Tue Oct 1 14:49:23 2013
Listening on http://localhost:8888
Document root is /Users/vic/prog/gtaphp/composer/ApplesAndOrangesApp
Press Ctrl-C to quit.
Browse to http://localhost:8888/ to show running app. We hope.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment