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

Composer: From Beginner to Expert

72f8175ac13e367bb500dd4da1f1aa32?s=47 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.

72f8175ac13e367bb500dd4da1f1aa32?s=128

Jonathan Klein

September 07, 2014
Tweet

Transcript

  1. Composer: From Beginner to Expert Northeast PHP 2014 Jonathan Klein

    @jonathanklein
  2. jkle.in/composer

  3. None
  4. “The best way to use 3rd party code”

  5. “…and autoload your code”

  6. None
  7. None
  8. None
  9. A Story in Three Acts 1. Dependency Management

  10. A Story in Three Acts 1. Dependency Management

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

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

    2. Autoloading Your Code
  13. A Story in Three Acts 1. Using 3rd Party Code

    2. Autoloading Your Code 3. Publishing Packages
  14. Using 3rd Party Code

  15. Install Composer

  16. curl -sS https://getcomposer.org/installer | php ! mv composer.phar /usr/bin/composer

  17. None
  18. None
  19. None
  20. None
  21. https://www.flickr.com/photos/monkeywobble/2124397478

  22. https://www.flickr.com/photos/adrianblack/3358661327

  23. Three Ways Composer Helps

  24. Three Ways Composer Helps 1. Simple JSON Config File (composer.json)

  25. Three Ways Composer Helps 1. Simple JSON Config File (composer.json)

    2. Repository of Code (Packagist)
  26. Three Ways Composer Helps 1. Simple JSON Config File (composer.json)

    2. Repository of Code (Packagist) 3. Generated Autoload File (autoload.php)
  27. Simplest JSON Config File { }

  28. Simplest JSON Config File { "require": { "monolog/monolog": "1.10.*" }

    }
  29. Simplest JSON Config File { "require": { "monolog/monolog": "1.10.*" }

    } Vendor Name
  30. Simplest JSON Config File { "require": { "monolog/monolog": "1.10.*" }

    } Vendor Name Package Name
  31. Simplest JSON Config File { "require": { "monolog/monolog": "1.10.*" }

    } Vendor Name Package Name Version Number
  32. Composer + Packagist

  33. None
  34. None
  35. None
  36. Simplest JSON Config File { "require": { "monolog/monolog": "1.10.*" }

    } Vendor Name Package Name Version Number
  37. None
  38. None
  39. None
  40. None
  41. $ composer install

  42. Vendor Directory

  43. None
  44. Vendor Directory

  45. None
  46. None
  47. $ composer install

  48. Using The Code <?php require 'vendor/autoload.php'; ! $log = new

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

    Monolog\Logger('name'); ! …snip… ! $log->addWarning('Foo'); Generated autoload file
  50. None
  51. None
  52. None
  53. { "require": { "monolog/monolog": "1.10.*", "filp/whoops": "1.*", "respect/validation": "0.5.*", "swiftmailer/swiftmailer":

    "5.0.*" } } composer.json
  54. $ composer update

  55. composer require filp/whoops:1.* composer require respect/validation:0.5.* composer require swiftmailer/swiftmailer:5.0.*

  56. 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);
  57. 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);
  58. 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);
  59. Versioning

  60. { "require": { "monolog/monolog": "1.10.*", "filp/whoops": "1.*", "respect/validation": "0.5.*", "swiftmailer/swiftmailer":

    "5.0.*" } }
  61. composer.lock

  62. composer.lock Commit this

  63. None
  64. $ composer update

  65. None
  66. None
  67. vendor/autoload.php is regenerated

  68. vendor/autoload.php should be the only include in your application

  69. “What about MY Code?”

  70. Autoloading Your Code

  71. { "require": { "monolog/monolog": "1.10.*" }, ! "autoload": { "psr-4":

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

    {"Acme\\": "src/"} } } WTF?
  73. PSR: PHP Specification Request

  74. None
  75. Approved Standards PSR-0 Autoloading PSR-1 Basic Coding Standard PSR-2 Coding

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

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

    Style Guide PSR-3 Logger Interface PSR-4 Improved Autoloading
  78. What Improved?

  79. What Improved? 1. Underscores no longer designate directories

  80. What Improved? 1. Underscores no longer designate directories 2. Namespaces

    can map to arbitrary folders
  81. None
  82. None
  83. None
  84. { "require": { "monolog/monolog": "1.10.*" }, ! "autoload": { "psr-4":

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

    { "Acme\\": ["src/", "tests/"] } } }
  86. None
  87. Takeaway: Use PSR-4

  88. Autoloading code <?php ! require 'vendor/autoload.php'; ! $foo = new

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

    {"Acme\\": "src/Foo/Bar/"} } }
  90. Autoloading code <?php ! require 'vendor/autoload.php'; ! $foo = new

    \Acme\Baz(); // src/Foo/Bar/Baz.php
  91. <?php ! namespace Acme; ! class Baz{ ! function __construct()

    { echo "Hello, World"; } }
  92. { "require": { "monolog/monolog": "1.10.*" }, ! "autoload": { "psr-4":

    { "Acme\\": "src/", "Vendor\\Namespace\\": "" } } }
  93. “What About Functions?”

  94. { "require": { "monolog/monolog": "1.10.*" }, ! "autoload": { "psr-4":

    {"Acme\\": "src/"}, "files": ["src/functions.php"] } }
  95. None
  96. None
  97. Publishing Packages

  98. { "require": { "monolog/monolog": "1.10.*" }, ! "autoload": { "psr-4":

    {"Acme\\": "src/Foo/Bar/"} } }
  99. { "name": "acme/hello", "description": "Prints Hello, World", "require": { "monolog/monolog":

    "1.10.*" }, ! "autoload": { "psr-4": {"Acme\\": "src/Foo/Bar/"} } }
  100. None
  101. None
  102. None
  103. Private Repository with Satis

  104. Bonus: Advanced Usage

  105. { "require": { "monolog/monolog": "1.10.*" }, ! "require-dev": { "phpunit/phpunit":

    "3.7.*", "mockery/mockery": "0.7.*" } }
  106. { "scripts": { "post-update-cmd": "MyVendor\\MyClass::postUpdate", "post-package-install": [ "MyVendor\\MyClass::postPackageInstall" ], "post-install-cmd":

    [ "MyVendor\\MyClass::warmCache", "phpunit -c app/" ] } }
  107. { "scripts": { "post-update-cmd": "MyVendor\\MyClass::postUpdate", "post-package-install": [ "MyVendor\\MyClass::postPackageInstall" ], "post-install-cmd":

    [ "MyVendor\\MyClass::warmCache", "phpunit -c app/" ] } }
  108. { "scripts": { "post-update-cmd": "MyVendor\\MyClass::postUpdate", "post-package-install": [ "MyVendor\\MyClass::postPackageInstall" ], "post-install-cmd":

    [ "MyVendor\\MyClass::warmCache", "phpunit -c app/" ] } }
  109. Other Useful Commands

  110. Other Useful Commands composer init

  111. Other Useful Commands composer init composer remove <package name>

  112. Other Useful Commands composer init composer remove <package name> composer

    validate
  113. Other Useful Commands composer init composer remove <package name> composer

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

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

    validate composer self-update composer diagnose composer status
  116. Wrap Up

  117. Wrap Up • Include 3rd Party Code

  118. Wrap Up • Include 3rd Party Code • Autoload Your

    Own Code
  119. Wrap Up • Include 3rd Party Code • Autoload Your

    Own Code • Publish Packages
  120. Thanks!

  121. ! jkle.in/composer ! @jonathanklein jonathan@etsy.com www.etsy.com/careers