SlideShare a Scribd company logo
Avoid Version Chaos
PHP Dependency Management with Composer
Shameless Self-Promotion
Who Am I?
● David Weingart
● PHP user since 2001
● Currently Web Development Manager at the
UNH InterOperability Laboratory
● Twitter: @dweingart
● https://www.linkedin.com/in/dbweingart
What is Composer?
● Composer is a dependency manager for
PHP
o It downloads, installs, and loads dependencies you
specify in a composer.json file
● A dependency is any code that your
application requires in order to function
o Think libraries like Monolog or Doctrine, or
frameworks like Symfony or Laravel
Why use Composer?
● You could just download the libraries you
need, right?
o Sure, but Composer has the following benefits:
 Declarative dependencies
 Handles recursive dependencies
 Easy autoloading of packages
 Integration with Packagist
● Unix systems (Linux/Mac OS X)
o curl -Ss https://getcomposer.org/installer > installer.php
o vim installer.php # Verify the download is not malicious
o php installer.php --install-dir=/usr/local/bin --filename=composer
 Composer installed globally as /usr/local/bin/composer
o Don’t follow the instructions on the download page to pipe the installer through PHP
without looking at the code.
● Windows
o Download the Composer windows installer and run it
 https://getcomposer.org/Composer-Setup.exe
Installation
Hello World
$ composer init
● Init will interactively create an initial composer.json
file for you
Sample Composer.json
{
"name": "dweingart/hello-world",
"description": "Basic Composer demonstration",
"require": {
"slim/slim": "2.*",
"slim/views": "*",
"twig/twig": "1.*"
},
"license": "BSD",
"authors": [
{
"name": "David Weingart",
"email": "dweingart@pobox.com"
}
]
}
Declaring Dependencies
● Declare dependencies in the “require”
section of composer.json
o Dependencies consist of a package name and a
version specification
 Package names are vendor/package
● twig/twig is the Twig template engine, and twig/extensions is
the official Twig extensions package
o Packages are installed from a repository
 Default repository is Packagist (you can add more)
Version Specification
● Exact version: 1.2.3
● Wildcard: 1.2.*
● Range: >=1.0,<1.2
o With ranges you can exclude a known-bad release
● Next Significant Release: ~1.2
o Equivalent to >=1.2,<2.0
● Version specifications interact with the stability-flag
setting. You can also set per-package stability flags.
Composer Update
$ composer update <package>
● Will update the code in your vendor directory to the latest versions
based on your version specifications
● Example:
o Version specification: 1.2.*
o Current installed version: 1.2.3
o Latest release: 1.2.10
o Update will download and install 1.2.10
● Updates the composer.lock file with the exact versions installed
● Be careful with update as it has the potential to break your application
o Revert a bad update by reverting the lock file and running install
Composer Install
$ composer install
● Downloads and installs the exact versions of the packages defined in
the composer.lock file
o Exception: if there’s no lock file it uses composer.json and
performs an update to generate an initial lock file
● Production systems should never use composer update and should
only use composer install
Integration with VCS
● Do: Check composer.json and
composer.lock into version control
● Don’t: Check in the vendor directory
Autoloader
● Composer includes a handy autoloader for
any class that it manages
● You can also configure the autoloader to
load your own classes
o require 'vendor/autoload.php';
o $app = new SlimSlim();
o $db = new MyAppDBConnector();
Packagist
● Packagist is the main source of Composer
packages
● Pro: Anyone can submit packages
o Lots to choose from
● Con: Anyone can submit packages
o Due diligence is required
Advanced Features
● Repositories other than Packagist
o Composer can load packages from PEAR, Git,
Subversion, a private Packagist instance, or even a
zip file.
● require-dev
o Packages required only for testing (e.g. PHPUnit)
can be placed in a require-dev section and updated
separately.
Advanced Features
● Aliases
o To satisfy dependencies you can alias branch
names to versions
 "monolog/monolog": "dev-bugfix as 1.0.x-dev"
● Packages can include vendor binaries
o This is used by some frameworks to allow you to
quickly create new projects
WordPress Support
● WordPress
o No official support, but community efforts to support
installing WP core and plugins using Composer
o Resources
 Composer in WordPress
 WP Packagist
● Mirrors official WP themes and plugin directory as a
Composer repository
Drupal Support
● Drupal 8 will support composer for updating
core packages
● There exists today a Drupal 8 package in
Packagist
Security Notes
● Recommended installation method - don’t pipe
untrusted code to PHP
● Anyone can publish to Packagist without a security
review
● Falls back to regular HTTP without warning
● Packages can register scripts that execute on install
(but you can disable this)
● Does not validate SSL certificates
● No code signing yet
Resources
● Composer Documentation
● Packagist
● Presentation: Composer & You
o An opinionated look at Composer and running your
own package repository by @MrDanack
● Accelerate Drupal 8 Development

More Related Content

PDF
Development and deployment with composer and kite
PDF
Getting started with add ons
ODP
How to write patches for Vim
PDF
Cache in Chromium: Disk Cache
PDF
Chromium: NaCl and Pepper API
PDF
Madrid .NET Meetup: Microsoft open sources .NET!
PDF
Composer
PDF
Complete WPF Overview Tutorial with Example - iFour Technolab
Development and deployment with composer and kite
Getting started with add ons
How to write patches for Vim
Cache in Chromium: Disk Cache
Chromium: NaCl and Pepper API
Madrid .NET Meetup: Microsoft open sources .NET!
Composer
Complete WPF Overview Tutorial with Example - iFour Technolab

What's hot (20)

TXT
Release Notes
PPTX
.net frameworks
ODP
Introduction to Vim 8.0
ODP
Packages, Releases, QGSkel
PPTX
Visual studio code
PDF
Apache ssl
PPTX
[Vietnam Mobile Day 2013] - Giới thiệu android media framework dựa trên công ...
ODP
Cross Platform Net Development With Mono And My Sql Presentation
PPTX
Build Leaner, Faster Web Applications with ASP.NET
PDF
HKG18-217 - OpenCDM/CDMi (Multi DRM) work with WPE & Chromium
PDF
Efficient development workflows with composer
PDF
Last Month in PHP - June 2016
PDF
Efficient development workflows with composer
PDF
MongoDB World 2019 Builder's Fest - Open source command line power tools for ...
PPTX
NuGet Nuggets
PDF
Docker & PHP - Practical use case
PDF
Drupal + composer = new love !?
PPTX
Volunteering atyouseeforit services
PDF
FFmpeg - the universal multimedia toolkit
PDF
Salt and pepper — native code in the browser Browser using Google native Client
Release Notes
.net frameworks
Introduction to Vim 8.0
Packages, Releases, QGSkel
Visual studio code
Apache ssl
[Vietnam Mobile Day 2013] - Giới thiệu android media framework dựa trên công ...
Cross Platform Net Development With Mono And My Sql Presentation
Build Leaner, Faster Web Applications with ASP.NET
HKG18-217 - OpenCDM/CDMi (Multi DRM) work with WPE & Chromium
Efficient development workflows with composer
Last Month in PHP - June 2016
Efficient development workflows with composer
MongoDB World 2019 Builder's Fest - Open source command line power tools for ...
NuGet Nuggets
Docker & PHP - Practical use case
Drupal + composer = new love !?
Volunteering atyouseeforit services
FFmpeg - the universal multimedia toolkit
Salt and pepper — native code in the browser Browser using Google native Client
Ad

Viewers also liked (20)

PPT
CURRICULUM VITAE
PDF
Hoivan tarvitsijoiden perusturva
PPS
Musica filme nosso_lar
PDF
Yrjö Mattila: Oikeusturva sosiaalivakuutusasioissa
PDF
Presentacio r bombardo_cpujol
PDF
Introducing A\\V ASESORES
PDF
Suvi Rasimus: Tuottaako EU-tuomioistuin oikeusturvaa sosiaalivakuutuksessa?
PDF
Jani Miettinen: Oulun SOTEa kuvaava rekisteriaineisto ja sen mahdollisuudet -...
PDF
Risto Koulu: Oikeussuojan takeet sosiaaliturvan muutoksenhaussa - esimerkkinä...
XLS
Tsahim test batchimeg
PPTX
17.2 Fish
PPTX
Vuononvirta, Tiina: Etäfysioterapia
PPT
LLM club welcoming NUS llm students
PDF
15778 sağlik sözleşmeli personel
PPT
Smartphones Presentation
PPTX
III fórum regional secopa bahia 2014
PPTX
Mikkola, Hennamari ja Ylikännö, Minna: Sosiaalibarometri antaa evästystä Kela...
PPTX
çoklu zeka
PPTX
Inanny_Final report for OAP
PPTX
CURRICULUM VITAE
Hoivan tarvitsijoiden perusturva
Musica filme nosso_lar
Yrjö Mattila: Oikeusturva sosiaalivakuutusasioissa
Presentacio r bombardo_cpujol
Introducing A\\V ASESORES
Suvi Rasimus: Tuottaako EU-tuomioistuin oikeusturvaa sosiaalivakuutuksessa?
Jani Miettinen: Oulun SOTEa kuvaava rekisteriaineisto ja sen mahdollisuudet -...
Risto Koulu: Oikeussuojan takeet sosiaaliturvan muutoksenhaussa - esimerkkinä...
Tsahim test batchimeg
17.2 Fish
Vuononvirta, Tiina: Etäfysioterapia
LLM club welcoming NUS llm students
15778 sağlik sözleşmeli personel
Smartphones Presentation
III fórum regional secopa bahia 2014
Mikkola, Hennamari ja Ylikännö, Minna: Sosiaalibarometri antaa evästystä Kela...
çoklu zeka
Inanny_Final report for OAP
Ad

Similar to Nh php may 2014 - composer (20)

PDF
Composer Best Practices
PDF
Composer Best Practices.pdf
PDF
Composer Best Practices
PDF
12 Composer #burningkeyboards
DOCX
"The Power of Composer"
PDF
Beginning with Composer - Dependency manager in php
PDF
Php Dependency Management with Composer ZendCon 2016
PPTX
Composer
PDF
Leveraging Composer in Existing Projects
PPTX
PHP Dependency Management with Composer
PDF
Php Dependency Management with Composer ZendCon 2017
PDF
composer_talk_20160209
PDF
Composer yourself: a reintroduction to composer
PDF
Composer: Dependency Manager for PHP
PPTX
Composer
PPTX
Composer namespacing
PDF
WordCamp Sacramento 2019: Modernizing Your Development Workflow Using Composer
PDF
Composer the Right Way - MM16NL
PDF
Composer - The missing package manager for PHP
Composer Best Practices
Composer Best Practices.pdf
Composer Best Practices
12 Composer #burningkeyboards
"The Power of Composer"
Beginning with Composer - Dependency manager in php
Php Dependency Management with Composer ZendCon 2016
Composer
Leveraging Composer in Existing Projects
PHP Dependency Management with Composer
Php Dependency Management with Composer ZendCon 2017
composer_talk_20160209
Composer yourself: a reintroduction to composer
Composer: Dependency Manager for PHP
Composer
Composer namespacing
WordCamp Sacramento 2019: Modernizing Your Development Workflow Using Composer
Composer the Right Way - MM16NL
Composer - The missing package manager for PHP

Nh php may 2014 - composer

  • 1. Avoid Version Chaos PHP Dependency Management with Composer
  • 2. Shameless Self-Promotion Who Am I? ● David Weingart ● PHP user since 2001 ● Currently Web Development Manager at the UNH InterOperability Laboratory ● Twitter: @dweingart ● https://www.linkedin.com/in/dbweingart
  • 3. What is Composer? ● Composer is a dependency manager for PHP o It downloads, installs, and loads dependencies you specify in a composer.json file ● A dependency is any code that your application requires in order to function o Think libraries like Monolog or Doctrine, or frameworks like Symfony or Laravel
  • 4. Why use Composer? ● You could just download the libraries you need, right? o Sure, but Composer has the following benefits:  Declarative dependencies  Handles recursive dependencies  Easy autoloading of packages  Integration with Packagist
  • 5. ● Unix systems (Linux/Mac OS X) o curl -Ss https://getcomposer.org/installer > installer.php o vim installer.php # Verify the download is not malicious o php installer.php --install-dir=/usr/local/bin --filename=composer  Composer installed globally as /usr/local/bin/composer o Don’t follow the instructions on the download page to pipe the installer through PHP without looking at the code. ● Windows o Download the Composer windows installer and run it  https://getcomposer.org/Composer-Setup.exe Installation
  • 6. Hello World $ composer init ● Init will interactively create an initial composer.json file for you
  • 7. Sample Composer.json { "name": "dweingart/hello-world", "description": "Basic Composer demonstration", "require": { "slim/slim": "2.*", "slim/views": "*", "twig/twig": "1.*" }, "license": "BSD", "authors": [ { "name": "David Weingart", "email": "dweingart@pobox.com" } ] }
  • 8. Declaring Dependencies ● Declare dependencies in the “require” section of composer.json o Dependencies consist of a package name and a version specification  Package names are vendor/package ● twig/twig is the Twig template engine, and twig/extensions is the official Twig extensions package o Packages are installed from a repository  Default repository is Packagist (you can add more)
  • 9. Version Specification ● Exact version: 1.2.3 ● Wildcard: 1.2.* ● Range: >=1.0,<1.2 o With ranges you can exclude a known-bad release ● Next Significant Release: ~1.2 o Equivalent to >=1.2,<2.0 ● Version specifications interact with the stability-flag setting. You can also set per-package stability flags.
  • 10. Composer Update $ composer update <package> ● Will update the code in your vendor directory to the latest versions based on your version specifications ● Example: o Version specification: 1.2.* o Current installed version: 1.2.3 o Latest release: 1.2.10 o Update will download and install 1.2.10 ● Updates the composer.lock file with the exact versions installed ● Be careful with update as it has the potential to break your application o Revert a bad update by reverting the lock file and running install
  • 11. Composer Install $ composer install ● Downloads and installs the exact versions of the packages defined in the composer.lock file o Exception: if there’s no lock file it uses composer.json and performs an update to generate an initial lock file ● Production systems should never use composer update and should only use composer install
  • 12. Integration with VCS ● Do: Check composer.json and composer.lock into version control ● Don’t: Check in the vendor directory
  • 13. Autoloader ● Composer includes a handy autoloader for any class that it manages ● You can also configure the autoloader to load your own classes o require 'vendor/autoload.php'; o $app = new SlimSlim(); o $db = new MyAppDBConnector();
  • 14. Packagist ● Packagist is the main source of Composer packages ● Pro: Anyone can submit packages o Lots to choose from ● Con: Anyone can submit packages o Due diligence is required
  • 15. Advanced Features ● Repositories other than Packagist o Composer can load packages from PEAR, Git, Subversion, a private Packagist instance, or even a zip file. ● require-dev o Packages required only for testing (e.g. PHPUnit) can be placed in a require-dev section and updated separately.
  • 16. Advanced Features ● Aliases o To satisfy dependencies you can alias branch names to versions  "monolog/monolog": "dev-bugfix as 1.0.x-dev" ● Packages can include vendor binaries o This is used by some frameworks to allow you to quickly create new projects
  • 17. WordPress Support ● WordPress o No official support, but community efforts to support installing WP core and plugins using Composer o Resources  Composer in WordPress  WP Packagist ● Mirrors official WP themes and plugin directory as a Composer repository
  • 18. Drupal Support ● Drupal 8 will support composer for updating core packages ● There exists today a Drupal 8 package in Packagist
  • 19. Security Notes ● Recommended installation method - don’t pipe untrusted code to PHP ● Anyone can publish to Packagist without a security review ● Falls back to regular HTTP without warning ● Packages can register scripts that execute on install (but you can disable this) ● Does not validate SSL certificates ● No code signing yet
  • 20. Resources ● Composer Documentation ● Packagist ● Presentation: Composer & You o An opinionated look at Composer and running your own package repository by @MrDanack ● Accelerate Drupal 8 Development