SlideShare ist ein Scribd-Unternehmen logo
1 von 20
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

Weitere ähnliche Inhalte

Was ist angesagt?

Release Notes
Release NotesRelease Notes
Release Notescosmeli
 
Introduction to Vim 8.0
Introduction to Vim 8.0Introduction to Vim 8.0
Introduction to Vim 8.0k-takata
 
Packages, Releases, QGSkel
Packages, Releases, QGSkelPackages, Releases, QGSkel
Packages, Releases, QGSkelQuintagroup
 
Visual studio code
Visual studio codeVisual studio code
Visual studio codefizmhd
 
[Vietnam Mobile Day 2013] - Giới thiệu android media framework dựa trên công ...
[Vietnam Mobile Day 2013] - Giới thiệu android media framework dựa trên công ...[Vietnam Mobile Day 2013] - Giới thiệu android media framework dựa trên công ...
[Vietnam Mobile Day 2013] - Giới thiệu android media framework dựa trên công ...AiTi Education
 
Cross Platform Net Development With Mono And My Sql Presentation
Cross Platform  Net Development With Mono And My Sql PresentationCross Platform  Net Development With Mono And My Sql Presentation
Cross Platform Net Development With Mono And My Sql Presentationsnowland nk
 
Build Leaner, Faster Web Applications with ASP.NET
Build Leaner, Faster Web Applications with  ASP.NETBuild Leaner, Faster Web Applications with  ASP.NET
Build Leaner, Faster Web Applications with ASP.NETLohith Goudagere Nagaraj
 
HKG18-217 - OpenCDM/CDMi (Multi DRM) work with WPE & Chromium
HKG18-217 - OpenCDM/CDMi (Multi DRM) work with WPE & ChromiumHKG18-217 - OpenCDM/CDMi (Multi DRM) work with WPE & Chromium
HKG18-217 - OpenCDM/CDMi (Multi DRM) work with WPE & ChromiumLinaro
 
Efficient development workflows with composer
Efficient development workflows with composerEfficient development workflows with composer
Efficient development workflows with composernuppla
 
Last Month in PHP - June 2016
Last Month in PHP - June 2016Last Month in PHP - June 2016
Last Month in PHP - June 2016Eric Poe
 
Efficient development workflows with composer
Efficient development workflows with composerEfficient development workflows with composer
Efficient development workflows with composernuppla
 
MongoDB World 2019 Builder's Fest - Open source command line power tools for ...
MongoDB World 2019 Builder's Fest - Open source command line power tools for ...MongoDB World 2019 Builder's Fest - Open source command line power tools for ...
MongoDB World 2019 Builder's Fest - Open source command line power tools for ...Stennie Steneker
 
Docker & PHP - Practical use case
Docker & PHP - Practical use caseDocker & PHP - Practical use case
Docker & PHP - Practical use caserjsmelo
 
Drupal + composer = new love !?
Drupal + composer = new love !?Drupal + composer = new love !?
Drupal + composer = new love !?nuppla
 
Volunteering atyouseeforit services
Volunteering atyouseeforit servicesVolunteering atyouseeforit services
Volunteering atyouseeforit servicesYouSee
 
FFmpeg - the universal multimedia toolkit
FFmpeg - the universal multimedia toolkitFFmpeg - the universal multimedia toolkit
FFmpeg - the universal multimedia toolkitStefano Sabatini
 
Salt and pepper — native code in the browser Browser using Google native Client
Salt and pepper — native code in the browser Browser using Google native ClientSalt and pepper — native code in the browser Browser using Google native Client
Salt and pepper — native code in the browser Browser using Google native ClientMayflower GmbH
 

Was ist angesagt? (20)

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

Andere mochten auch

Musica filme nosso_lar
Musica filme nosso_larMusica filme nosso_lar
Musica filme nosso_larrjcacupe
 
Presentacio r bombardo_cpujol
Presentacio r bombardo_cpujolPresentacio r bombardo_cpujol
Presentacio r bombardo_cpujolCTFC
 
Introducing A\\V ASESORES
Introducing A\\V ASESORESIntroducing A\\V ASESORES
Introducing A\\V ASESORESvagarno
 
Suvi Rasimus: Tuottaako EU-tuomioistuin oikeusturvaa sosiaalivakuutuksessa?
Suvi Rasimus: Tuottaako EU-tuomioistuin oikeusturvaa sosiaalivakuutuksessa?Suvi Rasimus: Tuottaako EU-tuomioistuin oikeusturvaa sosiaalivakuutuksessa?
Suvi Rasimus: Tuottaako EU-tuomioistuin oikeusturvaa sosiaalivakuutuksessa? Kelan tutkimus / Research at Kela
 
Jani Miettinen: Oulun SOTEa kuvaava rekisteriaineisto ja sen mahdollisuudet -...
Jani Miettinen: Oulun SOTEa kuvaava rekisteriaineisto ja sen mahdollisuudet -...Jani Miettinen: Oulun SOTEa kuvaava rekisteriaineisto ja sen mahdollisuudet -...
Jani Miettinen: Oulun SOTEa kuvaava rekisteriaineisto ja sen mahdollisuudet -...Kelan tutkimus / Research at Kela
 
Risto Koulu: Oikeussuojan takeet sosiaaliturvan muutoksenhaussa - esimerkkinä...
Risto Koulu: Oikeussuojan takeet sosiaaliturvan muutoksenhaussa - esimerkkinä...Risto Koulu: Oikeussuojan takeet sosiaaliturvan muutoksenhaussa - esimerkkinä...
Risto Koulu: Oikeussuojan takeet sosiaaliturvan muutoksenhaussa - esimerkkinä...Kelan tutkimus / Research at Kela
 
Tsahim test batchimeg
Tsahim test batchimegTsahim test batchimeg
Tsahim test batchimegchimgee92
 
17.2 Fish
17.2 Fish17.2 Fish
17.2 FishColfax
 
LLM club welcoming NUS llm students
LLM club welcoming NUS llm studentsLLM club welcoming NUS llm students
LLM club welcoming NUS llm studentsidanilina
 
15778 sağlik sözleşmeli personel
15778 sağlik sözleşmeli personel15778 sağlik sözleşmeli personel
15778 sağlik sözleşmeli personelAlimBey
 
Smartphones Presentation
Smartphones PresentationSmartphones Presentation
Smartphones PresentationTamsanqa Ncube
 
III fórum regional secopa bahia 2014
III fórum regional secopa bahia 2014III fórum regional secopa bahia 2014
III fórum regional secopa bahia 2014Marcos DA Aurora
 
Mikkola, Hennamari ja Ylikännö, Minna: Sosiaalibarometri antaa evästystä Kela...
Mikkola, Hennamari ja Ylikännö, Minna: Sosiaalibarometri antaa evästystä Kela...Mikkola, Hennamari ja Ylikännö, Minna: Sosiaalibarometri antaa evästystä Kela...
Mikkola, Hennamari ja Ylikännö, Minna: Sosiaalibarometri antaa evästystä Kela...Kelan tutkimus / Research at Kela
 
Inanny_Final report for OAP
Inanny_Final report for OAPInanny_Final report for OAP
Inanny_Final report for OAPidanilina
 

Andere mochten auch (20)

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

Ähnlich wie Nh php may 2014 - composer

Composer Best Practices
Composer Best PracticesComposer Best Practices
Composer Best PracticesAbid Malik
 
Composer Best Practices
Composer Best PracticesComposer Best Practices
Composer Best PracticesAbid Malik
 
Composer Best Practices.pdf
Composer Best Practices.pdfComposer Best Practices.pdf
Composer Best Practices.pdfAbid Malik
 
Composer & Drupal
Composer & DrupalComposer & Drupal
Composer & Drupaldrubb
 
Beginning with Composer - Dependency manager in php
Beginning with Composer  - Dependency manager in php Beginning with Composer  - Dependency manager in php
Beginning with Composer - Dependency manager in php Yogesh Salvi
 
Dependency management with Composer
Dependency management with ComposerDependency management with Composer
Dependency management with ComposerJason Grimes
 
Magento Docker Setup.pdf
Magento Docker Setup.pdfMagento Docker Setup.pdf
Magento Docker Setup.pdfAbid Malik
 
PHP Dependency Management with Composer
PHP Dependency Management with ComposerPHP Dependency Management with Composer
PHP Dependency Management with ComposerAdam Englander
 
Prizm Installation Guide
Prizm Installation GuidePrizm Installation Guide
Prizm Installation Guidevjvarenya
 
Composer yourself: a reintroduction to composer
Composer yourself:  a reintroduction to composerComposer yourself:  a reintroduction to composer
Composer yourself: a reintroduction to composerEric Poe
 
Using Composer with WordPress - 2.0
Using Composer with WordPress - 2.0Using Composer with WordPress - 2.0
Using Composer with WordPress - 2.0Micah Wood
 
Wonderful World of Maven
Wonderful World of MavenWonderful World of Maven
Wonderful World of MavenJustin J. Moses
 
Create a PHP Library the right way
Create a PHP Library the right wayCreate a PHP Library the right way
Create a PHP Library the right wayChristian Varela
 
Build and deploy scientific Python Applications
Build and deploy scientific Python Applications  Build and deploy scientific Python Applications
Build and deploy scientific Python Applications Ramakrishna Reddy
 
Php Dependency Management with Composer ZendCon 2016
Php Dependency Management with Composer ZendCon 2016Php Dependency Management with Composer ZendCon 2016
Php Dependency Management with Composer ZendCon 2016Clark Everetts
 

Ähnlich wie Nh php may 2014 - composer (20)

Composer Best Practices
Composer Best PracticesComposer Best Practices
Composer Best Practices
 
Composer Best Practices
Composer Best PracticesComposer Best Practices
Composer Best Practices
 
Composer Best Practices.pdf
Composer Best Practices.pdfComposer Best Practices.pdf
Composer Best Practices.pdf
 
Composer & Drupal
Composer & DrupalComposer & Drupal
Composer & Drupal
 
Composer
ComposerComposer
Composer
 
Beginning with Composer - Dependency manager in php
Beginning with Composer  - Dependency manager in php Beginning with Composer  - Dependency manager in php
Beginning with Composer - Dependency manager in php
 
Dependency management with Composer
Dependency management with ComposerDependency management with Composer
Dependency management with Composer
 
Magento Docker Setup.pdf
Magento Docker Setup.pdfMagento Docker Setup.pdf
Magento Docker Setup.pdf
 
PHP Dependency Management with Composer
PHP Dependency Management with ComposerPHP Dependency Management with Composer
PHP Dependency Management with Composer
 
Composer namespacing
Composer namespacingComposer namespacing
Composer namespacing
 
Prizm Installation Guide
Prizm Installation GuidePrizm Installation Guide
Prizm Installation Guide
 
Composer yourself: a reintroduction to composer
Composer yourself:  a reintroduction to composerComposer yourself:  a reintroduction to composer
Composer yourself: a reintroduction to composer
 
Using Composer with WordPress - 2.0
Using Composer with WordPress - 2.0Using Composer with WordPress - 2.0
Using Composer with WordPress - 2.0
 
Composer
ComposerComposer
Composer
 
Weblogic12 c installation guide
Weblogic12 c installation guideWeblogic12 c installation guide
Weblogic12 c installation guide
 
Wonderful World of Maven
Wonderful World of MavenWonderful World of Maven
Wonderful World of Maven
 
Composer
ComposerComposer
Composer
 
Create a PHP Library the right way
Create a PHP Library the right wayCreate a PHP Library the right way
Create a PHP Library the right way
 
Build and deploy scientific Python Applications
Build and deploy scientific Python Applications  Build and deploy scientific Python Applications
Build and deploy scientific Python Applications
 
Php Dependency Management with Composer ZendCon 2016
Php Dependency Management with Composer ZendCon 2016Php Dependency Management with Composer ZendCon 2016
Php Dependency Management with Composer ZendCon 2016
 

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