29/05/2015
FEDERICO LOZADA MOSTO
Twitter: @mostofreddy
Web: mostofreddy.com.ar
Facebook: /mostofreddy
Linkedin: ar.linkedin.com/in/federicolozadamosto
Github: /mostofreddy
Composer is a tool for dependency
management in PHP.
It allows you to declare the dependent libraries
your project needs and it will install them
in your project for you
What does that mean?
Scope is per project, not global
Resolves dependencies
Runs installation tasks
What does that mean?
✓ Autoload
✓ PSR-0/PSR-4
✓ Install dependencies & binaries
✓ Scripts
✓ Create project from a package
Features
✓ Installers
✓ Auth
✓ Semantic versioning
✓ Integrate with git/svn/mercurial/etc
✓ and more...
✓ 1st release: 1/03/2012
✓ Inspired by npm and bundler
✓ 100% PHP
✓ Use Symfony components
✓ Autoload
✓ Easy to use
Miscellaneous
$ curl -sS https://getcomposer.org/installer | php
$ mv composer.phar /usr/local/bin/composer
Installing Composer
Globally
Require PHP >= 5.3.2!
How does it Works?
How does it Works?
Icons by: Ryan Beck, Pieter Smits, Kirill Ulitin from the Noun Project
composer.json
internal dependencies
external dependencies
git
svn
mercurial
etc
Packagist.org
github.com
etc
How it Work?
$ cd /var/www/myProject
$ tree vendor -L 2
vendor
├── autoload.php
├── composer
│ ├── autoload_classmap.php
│ ├── autoload_namespaces.php
│ ├── autoload_psr4.php
│ ├── autoload_real.php
│ ├── ClassLoader.php
│ └── installed.json
├── symfony
│ ├── confy
│ └── yaml
└── psr
└── log
Who’s using Composer?
and more...
Who’s using Composer?
60.125 packages registered
271.001 versions available
789.500.318 packages installed (since 2012-04-13)
Why to use composer?
Componentization
Standardization
Reuse
Speed
Agility
Basics
Basic commands
$ composer list
Display all commands
config Set config options
create-project Create new project from a package
global Allows running commands in the global composer dir
init Creates a basic composer.json file in current directory
install Installs the project dependencies
update Updates your dependencies to the latest version
self-update Updates composer.phar to the latest version
and more...
composer.json
composer.lock
✓ metadata
✓ configuration
✓ dependencies
✓ development dependencies
✓ locks versions of dependencies
✓ run the same version everywhere
{
"name": "Mostofreddy/MyProject",
"description": "My project",|
"version": "2.1.0",
"license": "MIT",
"require": {
"php": ">=5.3.0"
},
"autoload": {
"psr-4": {
"MostofreddyMyProject": "src"
}
}
}
composer.json
{
"name": "Mostofreddy/MyProject",
"description": "My project",
"version": "2.1.0",
"license": "MIT",
"require": {
"php": ">=5.3.0"
},
"autoload": {
"psr-4": {
"MostofreddyMyProject": "src"
}
}
}
composer.json
Metadata
Dependencies
Autoload
composer.json
$ composer validate
./composer.json is valid, but with a few warnings
See http://getcomposer.org/doc/04-schema.md for details on the schema
License "MIT" is not a valid SPDX license identifier, see http://www.spdx.org/licenses/ if
you use an open license.
If the software is closed-source, you may use "proprietary" as license.
The version field is present, it is recommended to leave it out if the package is published
on Packagist.
Name "Mostofreddy/MyProject" does not match the best practice (e.g. lower-cased/with-dashes).
Validate it!!
composer.lock
✓ Locks versions of dependencies
✓ Run the same version everywhere
! Is good practice to upload the file to the repository
Metadata
name
name: “Mostofreddy/MyProject”
The name of the package.
It consists of vendor name and project name, separated by /
name
name: “Mostofreddy/MyProject”
The name of the package.
It consists of vendor name and project name, separated by /
vendor
project name
version
version: “1.0.1”
The version of the package
! Semantic version format
type
type: “libray”
The type of the package. It defaults to library
! Only use a custom type if you need custom
logic during installation. It is recommended
to omit this field and have it just default to
library
licence
licence: “MIT”
The license of the package. This can be either a string or
an array of strings.
author
authors: [{...}]
The authors of the package. This is an array of objects.
✓ name: The author's name. Usually
his real name.
✓ email: The author's email address.
✓ homepage: An URL to the author's
website.
✓ role: The authors' role in the
project (e.g. developer or
translator)
{
"authors": [
{
"name": "Federico Mosto",
"email": "mosto.federico@gmail.com",
"homepage": "http://mostofreddy.com.ar",
"role": "Developer"
},
{
…
}
]
}
others
homepage: “https://www.github.com/...”
An URL to the website of the project
“keywords”: [“key1”, “key2”]
An array of keywords that the package is related to
and more
https://getcomposer.org/doc/04-schema.md
Dependencies
{
"require": {
}
}
require
{
"require": {
"php": ">=5.5.0"
}
}
require
PHP Version
{
"require": {
"php": ">=5.5.0",
"ext-gd": "*",
"lib-curl": "*"
}
}
require
PHP extension
& libraries
libraries available:
curl, iconv, libxml, openssl, pcre, uuid, xsl
Tip: composer show --platform
!
!
{
"require": {
"php": ">=5.5.0",
"ext-gd": "*",
"lib-curl": "*",
"symfony/symfony": "2.3.*",
"twig/extensions": "1.0.*",
"sensio/generator-bundle": "2.3.*",
"incenteev/composer-parameter-handler": "~2.0",
".../...": "...",
"ocramius/proxy-manager": "~0.3.1"
}
}
require
vendor project name version
require
✓ exact: 1.2.0
✓ range: >, >= <,<=, !=
✓ wildcard: 2.0.*
✓ next major release: ~ 1.5
Dependency version
require-dev
{
"require-dev": {
"phpunit/phpunit": "4.1.*",
"squizlabs/php_codesniffer": "1.*",
"satooshi/php-coveralls": "dev-master"
}
}
$ composer install --dev
$ composer install --no-dev
install without dev dependencies!
! install with dev dependencies
Autoload
Autoload
PSR-0
PSR-4
Classmap
Files
PSR-0
{
"autoload": {
"psr-0": {
"MyNamespace": ["src/"]
}
}
}
.
└── src
└── MyNamespace
└── Entity
└── Person.php
Filesystem
require_once ROOT_PATH."vendor/autoload.
php";
$person = new MyNamespaceEntityPerson();
How to use
PSR-0 definition: http://www.php-fig.org/psr/psr-0/
PSR-4
{
"autoload": {
"psr-4": {
"MyNamespace": ["src/"]
}
}
}
.
└── src
└── Entity
└── Person.php
Filesystem
require_once ROOT_PATH."vendor/autoload.
php";
$person = new MyNamespaceEntityPerson();
How to use
PSR-4 definition: http://www.php-fig.org/psr/psr-4/
Classmap
{
"autoload": {
"classmap": [
"src/", "lib/", "DB.php"
]
}
}
.
└── src
└── lib
└── DB.php
Filesystem
require_once ROOT_PATH."vendor/autoload.
php";
$person = new DB();
How to use
Files
{
"autoload": {
"files": ["classes/class.DB.php"]
}
}
.
└── classes
└── class.DB.php
Filesystem
require_once ROOT_PATH."vendor/autoload.
php";
$person = new DB();
How to use
Scripts / Events
Scripts
A script, in Composer's terms, can either be a PHP
callback (defined as a static method) or any command-
line executable command. Scripts are useful for
executing a package's custom code or package-specific
commands during the Composer execution process
Events
Composer fires the following named events during its execution process:
✓ pre-install-cmd
✓ post-install-cmd
✓ pre-update-cmd
✓ post-update-cmd
✓ pre-status-cmd
✓ post-status-cmd
✓ pre-dependencies-solving
✓ post-dependencies-solving
✓ pre-package-install
✓ post-package-install
✓ pre-package-update
✓ post-package-update
✓ pre-package-uninstall
✓ post-package-uninstall
✓ pre-autoload-dump
✓ post-autoload-dump
✓ post-root-package-install
✓ post-create-project-cmd
✓ pre-archive-cmd
✓ post-archive-cmd
throw script in events
{
"scripts": {
"post-update-cmd": "MyVendorMyClass::postUpdate",
"post-package-install": [
"MyVendorMyClass::postPackageInstall"
],
"post-install-cmd": [
"MyVendorMyClass::warmCache",
"phpunit -c tests/phpunit.xml"
],
"post-create-project-cmd" : [
"php -r "copy('config/local-example.php', 'config/local.
php');""
]
}
}
throw script in events
namespace MyVendor;
use ComposerScriptEvent;
class MyClass
{
public static function postUpdate(Event $event) {
// do stuff
}
public static function postPackageInstall(Event $event) {
// do stuff
}
public static function warmCache(Event $event) {
// make cache toasty
}
}
Composer + CI
Jenkins
<!-- COMPOSER -->
<target name="composer-install" description="Install Composer Deps"
depends="prepare" unless="${composer-lock}">
<echo message="- Running Composer Install" />
<exec executable="composer" dir="${basedir}/../php-project">
<arg value="install" />
<arg value="--verbose" />
<arg value="--no-interaction" />
<arg value="--no-plugins" />
</exec>
</target>
Travis - CI
language: php
php:
- 5.4
- 5.5
- 5.6
- hhvm
before_script:
- composer self-update
- composer install
script:
- php vendor/bin/phpcs --standard=psr2 src/
- php vendor/bin/phpunit -c tests/phpunit.xm
Web: https://travis-ci.org/
Change the
development culture
PACKAGES
LIBRARIES
COMPONENTS
REUSE!!!
Questions?
Thanks!
FEDERICO LOZADA MOSTO
TW: @mostofreddy
Web: mostofreddy.com.ar
FB: mostofreddy
In: ar.linkedin.com/in/federicolozadamosto
Git: mostofreddy

Composer

  • 2.
  • 3.
    FEDERICO LOZADA MOSTO Twitter:@mostofreddy Web: mostofreddy.com.ar Facebook: /mostofreddy Linkedin: ar.linkedin.com/in/federicolozadamosto Github: /mostofreddy
  • 4.
    Composer is atool for dependency management in PHP. It allows you to declare the dependent libraries your project needs and it will install them in your project for you
  • 5.
  • 6.
    Scope is perproject, not global Resolves dependencies Runs installation tasks What does that mean?
  • 7.
    ✓ Autoload ✓ PSR-0/PSR-4 ✓Install dependencies & binaries ✓ Scripts ✓ Create project from a package Features ✓ Installers ✓ Auth ✓ Semantic versioning ✓ Integrate with git/svn/mercurial/etc ✓ and more...
  • 8.
    ✓ 1st release:1/03/2012 ✓ Inspired by npm and bundler ✓ 100% PHP ✓ Use Symfony components ✓ Autoload ✓ Easy to use Miscellaneous
  • 9.
    $ curl -sShttps://getcomposer.org/installer | php $ mv composer.phar /usr/local/bin/composer Installing Composer Globally Require PHP >= 5.3.2!
  • 10.
  • 11.
    How does itWorks? Icons by: Ryan Beck, Pieter Smits, Kirill Ulitin from the Noun Project composer.json internal dependencies external dependencies git svn mercurial etc Packagist.org github.com etc
  • 12.
    How it Work? $cd /var/www/myProject $ tree vendor -L 2 vendor ├── autoload.php ├── composer │ ├── autoload_classmap.php │ ├── autoload_namespaces.php │ ├── autoload_psr4.php │ ├── autoload_real.php │ ├── ClassLoader.php │ └── installed.json ├── symfony │ ├── confy │ └── yaml └── psr └── log
  • 13.
  • 14.
    Who’s using Composer? 60.125packages registered 271.001 versions available 789.500.318 packages installed (since 2012-04-13)
  • 15.
    Why to usecomposer? Componentization Standardization Reuse Speed Agility
  • 16.
  • 17.
    Basic commands $ composerlist Display all commands config Set config options create-project Create new project from a package global Allows running commands in the global composer dir init Creates a basic composer.json file in current directory install Installs the project dependencies update Updates your dependencies to the latest version self-update Updates composer.phar to the latest version and more...
  • 18.
    composer.json composer.lock ✓ metadata ✓ configuration ✓dependencies ✓ development dependencies ✓ locks versions of dependencies ✓ run the same version everywhere
  • 19.
    { "name": "Mostofreddy/MyProject", "description": "Myproject",| "version": "2.1.0", "license": "MIT", "require": { "php": ">=5.3.0" }, "autoload": { "psr-4": { "MostofreddyMyProject": "src" } } } composer.json
  • 20.
    { "name": "Mostofreddy/MyProject", "description": "Myproject", "version": "2.1.0", "license": "MIT", "require": { "php": ">=5.3.0" }, "autoload": { "psr-4": { "MostofreddyMyProject": "src" } } } composer.json Metadata Dependencies Autoload
  • 21.
    composer.json $ composer validate ./composer.jsonis valid, but with a few warnings See http://getcomposer.org/doc/04-schema.md for details on the schema License "MIT" is not a valid SPDX license identifier, see http://www.spdx.org/licenses/ if you use an open license. If the software is closed-source, you may use "proprietary" as license. The version field is present, it is recommended to leave it out if the package is published on Packagist. Name "Mostofreddy/MyProject" does not match the best practice (e.g. lower-cased/with-dashes). Validate it!!
  • 22.
    composer.lock ✓ Locks versionsof dependencies ✓ Run the same version everywhere ! Is good practice to upload the file to the repository
  • 23.
  • 24.
    name name: “Mostofreddy/MyProject” The nameof the package. It consists of vendor name and project name, separated by /
  • 25.
    name name: “Mostofreddy/MyProject” The nameof the package. It consists of vendor name and project name, separated by / vendor project name
  • 26.
    version version: “1.0.1” The versionof the package ! Semantic version format
  • 27.
    type type: “libray” The typeof the package. It defaults to library ! Only use a custom type if you need custom logic during installation. It is recommended to omit this field and have it just default to library
  • 28.
    licence licence: “MIT” The licenseof the package. This can be either a string or an array of strings.
  • 29.
    author authors: [{...}] The authorsof the package. This is an array of objects. ✓ name: The author's name. Usually his real name. ✓ email: The author's email address. ✓ homepage: An URL to the author's website. ✓ role: The authors' role in the project (e.g. developer or translator) { "authors": [ { "name": "Federico Mosto", "email": "mosto.federico@gmail.com", "homepage": "http://mostofreddy.com.ar", "role": "Developer" }, { … } ] }
  • 30.
    others homepage: “https://www.github.com/...” An URLto the website of the project “keywords”: [“key1”, “key2”] An array of keywords that the package is related to and more https://getcomposer.org/doc/04-schema.md
  • 31.
  • 32.
  • 33.
  • 34.
    { "require": { "php": ">=5.5.0", "ext-gd":"*", "lib-curl": "*" } } require PHP extension & libraries libraries available: curl, iconv, libxml, openssl, pcre, uuid, xsl Tip: composer show --platform ! !
  • 35.
    { "require": { "php": ">=5.5.0", "ext-gd":"*", "lib-curl": "*", "symfony/symfony": "2.3.*", "twig/extensions": "1.0.*", "sensio/generator-bundle": "2.3.*", "incenteev/composer-parameter-handler": "~2.0", ".../...": "...", "ocramius/proxy-manager": "~0.3.1" } } require vendor project name version
  • 36.
    require ✓ exact: 1.2.0 ✓range: >, >= <,<=, != ✓ wildcard: 2.0.* ✓ next major release: ~ 1.5 Dependency version
  • 37.
    require-dev { "require-dev": { "phpunit/phpunit": "4.1.*", "squizlabs/php_codesniffer":"1.*", "satooshi/php-coveralls": "dev-master" } } $ composer install --dev $ composer install --no-dev install without dev dependencies! ! install with dev dependencies
  • 38.
  • 39.
  • 40.
    PSR-0 { "autoload": { "psr-0": { "MyNamespace":["src/"] } } } . └── src └── MyNamespace └── Entity └── Person.php Filesystem require_once ROOT_PATH."vendor/autoload. php"; $person = new MyNamespaceEntityPerson(); How to use PSR-0 definition: http://www.php-fig.org/psr/psr-0/
  • 41.
    PSR-4 { "autoload": { "psr-4": { "MyNamespace":["src/"] } } } . └── src └── Entity └── Person.php Filesystem require_once ROOT_PATH."vendor/autoload. php"; $person = new MyNamespaceEntityPerson(); How to use PSR-4 definition: http://www.php-fig.org/psr/psr-4/
  • 42.
    Classmap { "autoload": { "classmap": [ "src/","lib/", "DB.php" ] } } . └── src └── lib └── DB.php Filesystem require_once ROOT_PATH."vendor/autoload. php"; $person = new DB(); How to use
  • 43.
    Files { "autoload": { "files": ["classes/class.DB.php"] } } . └──classes └── class.DB.php Filesystem require_once ROOT_PATH."vendor/autoload. php"; $person = new DB(); How to use
  • 44.
  • 45.
    Scripts A script, inComposer's terms, can either be a PHP callback (defined as a static method) or any command- line executable command. Scripts are useful for executing a package's custom code or package-specific commands during the Composer execution process
  • 46.
    Events Composer fires thefollowing named events during its execution process: ✓ pre-install-cmd ✓ post-install-cmd ✓ pre-update-cmd ✓ post-update-cmd ✓ pre-status-cmd ✓ post-status-cmd ✓ pre-dependencies-solving ✓ post-dependencies-solving ✓ pre-package-install ✓ post-package-install ✓ pre-package-update ✓ post-package-update ✓ pre-package-uninstall ✓ post-package-uninstall ✓ pre-autoload-dump ✓ post-autoload-dump ✓ post-root-package-install ✓ post-create-project-cmd ✓ pre-archive-cmd ✓ post-archive-cmd
  • 47.
    throw script inevents { "scripts": { "post-update-cmd": "MyVendorMyClass::postUpdate", "post-package-install": [ "MyVendorMyClass::postPackageInstall" ], "post-install-cmd": [ "MyVendorMyClass::warmCache", "phpunit -c tests/phpunit.xml" ], "post-create-project-cmd" : [ "php -r "copy('config/local-example.php', 'config/local. php');"" ] } }
  • 48.
    throw script inevents namespace MyVendor; use ComposerScriptEvent; class MyClass { public static function postUpdate(Event $event) { // do stuff } public static function postPackageInstall(Event $event) { // do stuff } public static function warmCache(Event $event) { // make cache toasty } }
  • 49.
  • 50.
    Jenkins <!-- COMPOSER --> <targetname="composer-install" description="Install Composer Deps" depends="prepare" unless="${composer-lock}"> <echo message="- Running Composer Install" /> <exec executable="composer" dir="${basedir}/../php-project"> <arg value="install" /> <arg value="--verbose" /> <arg value="--no-interaction" /> <arg value="--no-plugins" /> </exec> </target>
  • 51.
    Travis - CI language:php php: - 5.4 - 5.5 - 5.6 - hhvm before_script: - composer self-update - composer install script: - php vendor/bin/phpcs --standard=psr2 src/ - php vendor/bin/phpunit -c tests/phpunit.xm Web: https://travis-ci.org/
  • 52.
  • 53.
  • 54.
    Thanks! FEDERICO LOZADA MOSTO TW:@mostofreddy Web: mostofreddy.com.ar FB: mostofreddy In: ar.linkedin.com/in/federicolozadamosto Git: mostofreddy