Introduction to Composer - Dependency Manager for PHP

Introduction to Composer - Dependency Manager for PHP

Often when we create applications that depend on other components, libraries or extensions.

Composer is the dependency manager for PHP applications and libraries for high code reusability and ease of dependency management.

This was presented to the Singapore PHP User Group Nov 2012 Meetup at Microsoft Singapore.

Get Composer: http://getcomposer.org/

4749d60af0d45cc6ed8eaf014edb1f65?s=128

Sam-Mauris Yong

November 28, 2012
Tweet

Transcript

  1. Dependency Manager for PHP Sam-Mauris Yong HeartCode Singapore sam@mauris.sg http://mauris.sg/

    @_mauris Sam-Mauris Yong, Singapore PHP User Group 28 November 2012 1
  2. You use dependencies! It’s a lie to say your PHP

    application has no dependency! 28 November 2012 Sam-Mauris Yong, Singapore PHP User Group 2 Your PHP Application PHP
  3. Dependency can be a mess 28 November 2012 Sam-Mauris Yong,

    Singapore PHP User Group 3 Your PHP Application PHP 5.3 PHP 5.4 PHP 5.1/5.2 Luracast Restler Monolog
  4. Traditionally… • Download & Extract • No version management •

    No dependency checks • Updates are troublesome PHP Library website Library Archive App Library Folder Sam-Mauris Yong, Singapore PHP User Group 28 November 2012 4
  5. Software Reusability • Develop modularly • High Cohesion, Low Coupling

    • Focus on objective of software 28 November 2012 Sam-Mauris Yong, Singapore PHP User Group 5
  6. Composer • Manages dependencies and their versions • Ensures compatibility

    through version check • Ease of dependency resolve and updates • Helps to keep focus on Software Reusability 28 November 2012 Sam-Mauris Yong, Singapore PHP User Group 6
  7. Managed Dependencies • CLI Tool, runs on PHP • Install

    dependencies per-project • Easy dependency updates • Lightweight and easy to use 28 November 2012 Sam-Mauris Yong, Singapore PHP User Group 7
  8. Defining Dependencies composer.json • At project root directory • Defines

    your project dependencies 28 November 2012 Sam-Mauris Yong, Singapore PHP User Group 8 { "require": { "php": ">=5.3.0", "monolog/monolog": "1.*", "gabordemooij/redbean": "3.3.5", "pear-pear.phpunit.de/PHPUnit": "3.7.x" } } composer.json
  9. Dependency versions • "php": ">=5.3.0" – PHP 5.3.8 – PHP

    5.3.10 – PHP 5.4 • "monolog/monolog": "1.*" – Monolog 1.2.1 – Monolog 1.3 • "packfire/packfire": "2.0.*" – Packfire 2.0.1 – Packfire 2.0.3 28 November 2012 Sam-Mauris Yong, Singapore PHP User Group 9
  10. Where are the packages? • "monolog/monolog"? • "gabordemooij/redbean"? 28 November

    2012 Sam-Mauris Yong, Singapore PHP User Group 10
  11. Packagist 28 November 2012 Sam-Mauris Yong, Singapore PHP User Group

    11
  12. Other Sources • PEAR, Git, SVN, Mercurial repositories • Repositories

    with composer.json can be used as a dependency! 28 November 2012 Sam-Mauris Yong, Singapore PHP User Group 12 { "name": "acme/blog", "repositories": [ { "type": "vcs", "url": "https://github.com/composer/hello-world" } ], "require": { "acme/hello-world": "dev-master" } }
  13. Archive Sources • Zip, Phar, etc. • http://getcomposer.org/doc/05-repositories.md 28 November

    2012 Sam-Mauris Yong, Singapore PHP User Group 13 { "repositories": [ { "type": "package", "package": { "name": "smarty/smarty", "version": "3.1.7", "dist": { "url": "http://www.smarty.net/files/Smarty-3.1.7.zip", "type": "zip" } } } ], "require": { "smarty/smarty": "3.1.*" } }
  14. How to install dependencies? • Download Composer: • Install Dependencies:

    • Update Dependencies: 28 November 2012 Sam-Mauris Yong, Singapore PHP User Group 14 $ curl -s https://getcomposer.org/installer | php $ php composer.phar install $ php composer.phar update
  15. composer.lock • Created at installation if does not exist •

    If it exists, Composer will install versions of the dependencies specified in this file. • Commit with VCS – Everyone works with the same dependency versions – Deployment ensures same dependency versions • Updating dependencies will update the version numbers in composer.lock 28 November 2012 Sam-Mauris Yong, Singapore PHP User Group 15
  16. Composer Autoloader • Simple PSR-0 autoloader included 28 November 2012

    Sam-Mauris Yong, Singapore PHP User Group 16 <?php require 'vendor'/autoload.php'; use Monolog\Logger; use Monolog\Handler\StreamHandler; // create a log channel $log = new Logger('name'); $log->pushHandler(new StreamHandler('path/to/your.log', Logger::WARNING)); // add records to the log $log->addWarning('Foo'); $log->addError('Bar'); index.php
  17. Packfire Framework • Uses Composer for link between App and

    Framework • Ensures tested compatibility 28 November 2012 Sam-Mauris Yong, Singapore PHP User Group 17 Packfire Application PHP Requires Packfire 2.0.* Requires PHP >= 5.3.0
  18. Use in Travis CI • .travis.yml • Result of a

    Build Job with Composer Use: https://travis-ci.org/packfire/packfire- framework/jobs/3013917 28 November 2012 Sam-Mauris Yong, Singapore PHP User Group 18 before_script: - composer install
  19. Making project a library • Specify name, version and other

    metadata in composer.json • Publish to Packagist, Github, BitBucket etc. • http://getcomposer.org/doc/02-libraries.md 28 November 2012 Sam-Mauris Yong, Singapore PHP User Group 19
  20. Get Composer http://getcomposer.org/ • Look around • Make modular projects

    • Share reusable code • Have fun with PHP 28 November 2012 Sam-Mauris Yong, Singapore PHP User Group 20
  21. Questions? 28 November 2012 Sam-Mauris Yong, Singapore PHP User Group

    21
  22. Anchor App for Windows 28 November 2012 Sam-Mauris Yong, Singapore

    PHP User Group 22 http://heartcode.sg/apps/anchor
  23. Thank you! 28 November 2012 Sam-Mauris Yong, Singapore PHP User

    Group 23