Get Composer (WebElement #26)

Get Composer (WebElement #26)

1633975316e340c5f24414567a3f3679?s=128

Vladimír Kriška

February 06, 2014
Tweet

Transcript

  1. Get Composer! WebElement #26

  2. Here I am • Vladimír Kriška – common web dev

    guy – working as full stack web developer – or devop? or? it doesn't matter ... • My thoughts – at my blog: blog.ujovlado.sk – or twitter: @ujovlado
  3. Package management • Do you know Pear? Probably yes. •

    Did you ever install some packages through it? • How many? • Which ones? • MDB2, System_Daemon, XML_RPC, PHPUnit? • Installed on system, not project • Where's the list of required packages for current project? • Forget about it ...
  4. Dependency management • List of packages in your project •

    Managing dependencies from project file • Browsing installed packages directly from project • Simple installation and update • That's Composer
  5. Me and Composer • At first I was like –

    Noooo, new thing to my “well-run” development flow – I don't want nothing new, I like my “bubble” • But then – Mother of God, I can't live without it • So, don't be too conservative, try and learn new things. It's the only way how to stay in touch.
  6. Installation • Get .phar archive at https://getcomposer.org – (or) curl

    -sS https://getcomposer.org/installer | php • Make simlink to /usr/local/bin/composer or wherever sudo ln -s ~/composer.phar /usr/local/bin/composer • Nothing complicated, composer command is now available • There's also installer for Windows – Next, Yes, Next, Next, Yes, Finish
  7. Command line I. • Standard options – Help: composer -h

    – Verbose: composer -v (-vvv for more verbosity) – … • Standard exit codes – 0: OK – 1: Generic error code – 2: Dependency solving error code
  8. Command line II. • Most useful commnads: – install –

    update – search – init – self-update – create-project – ...
  9. Your first requirement • Require Yii framework 1.1.14 – Create

    composer.json file in your project root – yiisoft = vendor, yii = package, 1.1.14 = version – Run composer install – Profit!
  10. Semantic versioning • Given a version number MAJOR.MINOR.PATCH, increment the:

    – MAJOR version when you make incompatible API changes (BC breaks) – MINOR version when you add functionality in a backwards-compatible manner, and – PATCH version when you make backwards-compatible bug fixes. • For more information: http://semver.org
  11. Version Numbers I. • Standard operators (>, >=, <, <=,

    !=) – >=3.1.0, <3.2 • Match: 3.1.1, 3.1.20, 3.1.20.5 • Not match: 3.2, – >=1.3, <2 • Match: 1.4, 1.8.25, 1.9.2.16 • Not match: 2.0 • Use comma “,” to do AND • Use pipe “|” to do OR
  12. Version Numbers II. • Next significant release (~) – ~3.1.0

    (>=3.1.0, <3.2) • Match: 3.1.1, 3.1.20, 3.1.20.5 • Not match: 3.2 – ~1.3 (>=1.3, <2) • Match: 1.4, 1.8.25, 1.9.2.16 • Not match: 2.0
  13. Version Numbers III. • Wildcard (*) – 3.1.* (>=3.1.0, <3.2)

    • Match: 3.1.1, 3.1.20, 3.1.20.5 • Not match: 3.2 – 1.* (>=1.3, <2) • Match: 1.4, 1.8.25, 1.9.2.16 • Not match: 2.0
  14. Where the packages came from I. • There's package archiver:

    https://packagist.org/ – Once you register, it will scan your projects (branches and tags) • Composer's repository types: – composer (default) – vcs – pear – package (vcs, zip, tar, ...)
  15. Locking dependencies • Composer creates composer.lock file – If exists,

    will be used, if not, will be created from composer.json – Should be in your VCS • Every developer gets same packages as you
  16. Where the packages came from II. • You can install

    every VCS package • If composer.json exists
  17. Where the packages came from III. • If composer.json doesn't

    exist (1)
  18. Where the packages came from IV. • If composer.json doesn't

    exist (2)
  19. Autoloading I. • Every package has own mechanism to perform

    autoloading classes • Is impossible to manage everything manually • Composer has “special” autoloader which require all other autloaders • Only one require in bootstrap file
  20. Autoloading II. • Require files defined in autoload section •

    Or classes • Or using standards (PSR-4, PSR-0)
  21. Autoloading III. • Require files defined in autoload section –

    autoload_files.php • Or classes – autoload_classmap.php
  22. Where the packages came from V. • Your own archive

    of packages – Composer will look for packages.json – http://localhost/composer/
  23. Where the packages came from VI. • http://localhost/composer/packages.json: • Yes,

    this is your own Packagist
  24. Deployment I. • It's very easy to add Composer to

    your existing deployment process • Example (new project – first deployment): • hg clone REPOSITORY_PATH webelement-REV_ID • cd webelement-REV_ID • hg update 1.0 --clean • composer install –no-dev • cd .. • ln -s ./webelement-REV_ID/webroot ./webroot
  25. Deployment II. • Example (existing project): • hg clone REPOSITORY_PATH

    webelement-REV_ID • cd webelement-REV_ID • hg update 1.0 --clean • composer update –no-dev • cd .. • rm ./webroot && ln -s ./webelement-REV_ID/webroot ./webroot • (optional) rm -rf ./webelement-OLD_REV_ID
  26. Tips • Add SHA hash to .zip, .tar packages •

    --dev is the default behavior – Will install packages in require-dev section – Use --no-dev on production • Test installation before deployment – Next run will be loaded from cache • Set {“packagist”: false} in repositories section if you don't need any public packages • Secure your own package archiver with SSH • Read composer.json on popular PHP projects
  27. Resources • Homepage: https://getcomposer.org • Book: https://getcomposer.org/book.pdf – Nice LaTeX

    formatted PDF • Github: https://github.com/composer