Composer: WTF



Stef Horner

History


PHP
PECL
PEAR

Background: Autoloading


include / require hell
lazy / eager loading

Background: Namespaces


PHP 5.3








http://daylerees.com/php-namespaces-explained
(game of thrones knowledge required)

composer.json


Every project that uses composer is technically a package.

  • name
  • description
  • ...
  • autoload
  • require
  • require-dev
  • scripts
  • repositories

composer.json: autoload


What classes and files to autoload from this package

more on this later

composer.json: require


a map of classes that this project requires
{ "vendor/package": "version" }// eg.
{ "carbon/carbon": "1.8.*" }

versions correspond to git tags
http://semver.org/

versions can also be branch names (prefix with 'dev-')
{ "mockery/mockery": "dev-master" } 

composer.json: require-dev


Requirements specifically for development - ie. not the production environment


testing, seeding, debugging, utilities etc.

composer.json: scripts


commands that will be run at given points in the composer process:

{
  "pre-update-cmd": [ "git pull" ],
  "post-update-cmd": [ "grunt", "grunt clear-cache" ]
} 

list of hooks:

http://getcomposer.org/doc/articles/scripts.md

composer.json: scripts II


the commands can either be a terminal command or a PHP class/method combo:

"post-update-cmd": [ "grunt", "Namespace\\Class::migrate" ] 

This method gets passed a Composer\Script\Event object

http://getcomposer.org/doc/articles/scripts.md

composer.json: repositories


The default composer repository is packagist.org

You can add your own repository, or just individual packages, if you don't want to publish them on packagist (private packages, or packages in development)


"repositories": [
  {
    "type": "git",
    "url": "git@bitbucket.org:architectagency/utilities.git"
  }
]

composer install


Composer will download all the required packages (and their dependancies)

It will generate an autoloader

All you have to do is:

require __DIR__ . 'vendor/autoload.php' 

at the top of your code.

composer update

composer.lock


generated by install and update

will lock future installs to the exact versions and packages

only composer install should be run on server

Frameworks & CMSs


Laravel
Silex
Slim
Symfony
Zend

Joomla
Silverstripe
Wordpress (not officially)


Autoloading


Files
Classmap
PSR-0
PSR-4
Lazy Loading

Files


An array of files to require_once

  • Globals
  • Functions
  • Constants


"autoload": {
  "files": [
    "some/functions.php", "morefunctions.php"
  ]
} 

Classmap


An array of files and folders to map for loading

Scans folders for .php files
Tries to resolve namespaces and classes
Maps those FQNs to those files

"autoload": {
  "classmap": [
    "src/controllers", "src/utilities", "src/phpclass.php"
  ]
} 

PSR-0

A project structure standard

Namespaces = Directories
Classes = Files
"autoload": {
  "psr-0": {
    "Project\\": "src/"
  }
}
src /  IrrelaventFile.php, AnotherIrrelaventFile.json
  Project /    ClassOne.php    ClassTwo.php    AnotherNamespace /      ClassOne.php

PSR-4

Cleaner than PSR-0

More Implicit

"autoload": {
  "psr-4": {
    "Project\\": "src"
  }
} 
src /
  ClassOne.php    # these files must all be in the Project namespace
  ClassTwo.php
  SubNamespace /
    ClassOne.php 

composer dump-autoload


PSR-0 && classmap && files

Will re-map all classes
Adds new classes
Removes old classes


PSR-4

Only maps namespaces

Laravel's Lazy Loading


Outside of Composer

" Sort of " PSR-0

It maps at runtime
(no composer dump-autoload required)

composer init


Will interactively create a composer.json file

composer create-project


Dry-Clones the project
Installs
Runs separate hook

composer create-project laravel/laravel new-project 

Made with Slides.com