Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Composer: From Beginner to Expert

Jonathan Klein
September 07, 2014

Composer: From Beginner to Expert

A talk I gave at Northeast PHP 2014. Links from this talk are at jkle.in/composer.

Jonathan Klein

September 07, 2014
Tweet

More Decks by Jonathan Klein

Other Decks in Technology

Transcript

  1. A Story in Three Acts 1. Using 3rd Party Code

    2. Autoloading Your Code 3. Publishing Packages
  2. Three Ways Composer Helps 1. Simple JSON Config File (composer.json)

    2. Repository of Code (Packagist) 3. Generated Autoload File (autoload.php)
  3. Using The Code <?php require 'vendor/autoload.php'; ! $log = new

    Monolog\Logger('name'); ! …snip… ! $log->addWarning('Foo');
  4. Using The Code <?php require 'vendor/autoload.php'; ! $log = new

    Monolog\Logger('name'); ! …snip… ! $log->addWarning('Foo'); Generated autoload file
  5. Using The Code <?php ! require 'vendor/autoload.php'; ! use Respect\Validation\Validator

    as v; ! $log = new Monolog\Logger('name'); $whoops = new \Whoops\Run; Swift::init('swiftmailer_configurator'); ! $number = 123; v::numeric()->validate($number);
  6. Using The Code <?php ! require 'vendor/autoload.php'; ! use Respect\Validation\Validator

    as v; ! $log = new Monolog\Logger('name'); $whoops = new \Whoops\Run; Swift::init('swiftmailer_configurator'); ! $number = 123; v::numeric()->validate($number);
  7. Using The Code <?php ! require 'vendor/autoload.php'; ! use Respect\Validation\Validator

    as v; ! $log = new Monolog\Logger('name'); $whoops = new \Whoops\Run; Swift::init('swiftmailer_configurator'); ! $number = 123; v::numeric()->validate($number);
  8. Approved Standards PSR-0 Autoloading PSR-1 Basic Coding Standard PSR-2 Coding

    Style Guide PSR-3 Logger Interface PSR-4 Improved Autoloading
  9. Approved Standards PSR-0 Autoloading PSR-1 Basic Coding Standard PSR-2 Coding

    Style Guide PSR-3 Logger Interface PSR-4 Improved Autoloading
  10. Approved Standards PSR-0 Autoloading PSR-1 Basic Coding Standard PSR-2 Coding

    Style Guide PSR-3 Logger Interface PSR-4 Improved Autoloading
  11. Autoloading code <?php ! require 'vendor/autoload.php'; ! $foo = new

    \Acme\Foo\Bar\Baz(); // src/Foo/Bar/Baz.php
  12. { "require": { "monolog/monolog": "1.10.*" }, ! "autoload": { "psr-4":

    { "Acme\\": "src/", "Vendor\\Namespace\\": "" } } }
  13. { "require": { "monolog/monolog": "1.10.*" }, ! "autoload": { "psr-4":

    {"Acme\\": "src/"}, "files": ["src/functions.php"] } }
  14. { "name": "acme/hello", "description": "Prints Hello, World", "require": { "monolog/monolog":

    "1.10.*" }, ! "autoload": { "psr-4": {"Acme\\": "src/Foo/Bar/"} } }
  15. Other Useful Commands composer init composer remove <package name> composer

    validate composer self-update composer diagnose
  16. Other Useful Commands composer init composer remove <package name> composer

    validate composer self-update composer diagnose composer status