SlideShare a Scribd company logo
Composer
Alessandro Minoccheri
Composer is a tool for dependency
management in PHP, is not a package
manager.
It allows you to declare the dependent
libraries your project needs and it will
install them in your project for you.
Composer What is composer?
What is composer?
How does it works?
• Install composer
• composer init
Composer How to start?
How to start?
• composer install
• composer update
• composer require vendor/package:1.1.*
• composer remove vendor/package
• composer update vendor/package
Composer How to start?
Basic commands
Don’t edit
composer.json
manually
Package version: x.y.z
• x: MAJOR version when you make incompatible API changes
• y: MINOR version when you add functionality in a backwards-
compatible manner
• z: PATCH version when you make backwards-compatible bug fixes
Composer Semantic versioning
Semantic versioning
• Exact version 2.3.1
• Version range >=1.0 <1.1 || >=1.2
• Tilde version range ~1.2.3 is equivalent to >=1.2.3 <1.3.0
• Caret version range ^1.2.3 is equivalent to >=1.2.3 <2.0.0
Composer Versions and constraints
Versions and constraints
Composer generate an autoload file for all your dependencies vendor/autoload.php

Now you can create a class inside app folder like Config.php and use this namespace: AppConfig

The base directory will be src/App/Config 

The resulting file path will be: ./src/App/Config/Config.php
Composer Autoload
Autoload
{
"autoload": {
"psr-4": { "App": "src/" }
},
"autoload-dev": {
"psr-4": { "AppTests": "tests/" }
}
}
A script, in Composer's
terms, can either be a
PHP callback (defined as
a static method) or any
command-line
executable command. 
Composer Scripts
Scripts
"scripts": {
"post-update-cmd": "MyVendor
MyClass::postUpdate",
"post-install-cmd": [
"MyVendorMyClass::warmCache",
"phpunit -c app/"
],
"post-create-project-cmd": [
"php -r "copy('config/local-
example.php', 'config/local.php');""
]
}
Composer Require
Require
{
"require": {
"php": "^7.1.1"
}
}
$ php -v
PHP 5.6.10
$ composer update
Your requirements could not be resolved
to an installable set of packages.
Problem 1
This package requires php ^7.1.1 but
your PHP version (5.6.10) does not
satisfy that requirement.
$ php -v
PHP 5.6.10
$ composer update
Success
Composer Platform requirements
Platform requirements
{
"require": {
"php": "^7.1.1"
},
"config": {
"platform": {
"php": "7.1.2"
}
}
}
• Minimum-stability field defines default stability flags: for example: dev,
alpha, beta, RC or stable
• -stable, -RC ecc… suffix specifies the stability
• If the stability is not specified, composer does it:

1.2.3 become = 1.2.3.0-stable

>1.2 become > 1.2.0.0-stable

>=1.2 become 1.2.0.0-dev

<1.3 become <1.3.0.0-dev
Composer Stability flags
Stability flags
When running composer install for the first time, or when
running composer update a lock file called composer.lock
will be created with the exact versions that are installed
so they can be re-installed.



composer.lock must be committed to keep all co-workers
in the same versions as you and to make deploy faster.
Composer Composer.lock
Composer.lock
To solve composer.lock conflicts you have different ways:
• Solve conflicts on .json file, accept your or another .lock and launch
composer install
• Solve conflicts on .json file, delete composer.lock file and launch
composer update
• Accept .json and .lock files from origin and re-apply your changes



The best solution is the last to be more safe.
Composer Composer.lock conflicts
Composer.lock conflicts
Composer install
vs update
• Check for composer.lock file
• If not, auto generate composer.lock file
(Using composer update)
• Install the specified versions recorded in
the composer.lock file
Composer Composer install vs update
Composer install
• Go through the composer.json file
• Check availability of newer (latest) versions, based
on the version criteria mentioned (e.g. 1.12.*)
• Install the latest possible (according to above)
versions
• Update composer.lock file with installed versions
Composer Composer install vs update
Composer update
• Use --prefer-dist to avoid git clones

Will always download zip files if possible
(default for stable versions)
• Store ~/.composer/cache between builds

How depends on CI product/setup you use
Composer Composer install vs update
Composer install performance
—optimize-autoloader

Class map generation essentially
converts PSR-4/PSR-0 rules into
classmap rules, performance increase
from 20% to 25%
Composer Composer install vs update
Autoloader optimization
composer install ——optimize-autoloader
composer install --classmap-authoritative
—classmap-authoritative 

If something is not found in the classmap,
then it does not exist and the autoloader
should not attempt to look on the
filesystem according to PSR-4 rules.
A git patch is a commit
converted into a file that
can be applied on a
different repository.



So if you have a pull request
not merged you can use the
PR as if it were merged.



Composer Patching with Composer
Patching with Composer
{
"require": {
"symfony/symfony": "4.1.0"
},
"config": {
"preferred-install": "source"
},
"extra": {
"patches": {
"symfony/symfony": {
"Something": “https://url.com/
foo.patch“
}
}
}
}
VCS stands for version
control system. This
includes versioning
systems like git, svn, fossil
or hg. Composer has a
repository type for
installing packages from
these systems.



Composer Use Private Package
Use Private Package
"require": {
"vendor/package": "dev-master"
},
“repositories”:[
{
“type”: “vcs”,
“url” :
“git@bitbucket.org:vendor/

package.git”
}
]
• Git: git-scm.com
• Subversion: subversion.apache.org
• Mercurial: mercurial-scm.org
• Fossil: fossil-scm.org
GitHub: github.com (Git)
BitBucket: bitbucket.org (Git and Mercurial)
Composer Git alternatives
Git alternatives
Packagist is the default
Composer package repository.
It lets you find packages and
lets Composer know where to
get the code from
Composer Packagist
Packagist
Put a file named composer.json at the root of your package's repository,
containing the at least a name of the package.



Commit the file to your git or other VCS repository.



Login or register on pakagist.org site, then hit the submit button in the
menu.
Once you entered your public repository URL in there, your package will
be automatically crawled periodically. 

You just have to make sure you keep the composer.json file up to date.
Composer Publishing a Package
Publishing a Package
Grazie.
Alessandro Minoccheri

Twitter: minompi

Github: AlessandroMinoccheri

Stackoverflow: Alessandro Minoccheri

More Related Content

PDF
PDF
Open source and cross platform .net
PPTX
Fast Deployments to Multiple Golang Lambda Functions
PDF
Composer intro
PPTX
Lambda lambda-lambda
PPTX
End-to-end CI/CD deployments of containerized applications using AWS services
KEY
Advanced Server Integration with Data and Direct
PPTX
AWS elastic beanstalk
Open source and cross platform .net
Fast Deployments to Multiple Golang Lambda Functions
Composer intro
Lambda lambda-lambda
End-to-end CI/CD deployments of containerized applications using AWS services
Advanced Server Integration with Data and Direct
AWS elastic beanstalk

What's hot (18)

PPTX
London Community Summit - Habitat 2016
PDF
[Jun AWS 201] Elastic Beanstalk for Startups
PDF
Lessons learned migrating 100+ services to Kubernetes
PDF
Spinnaker 파트 1
PDF
Manage any AWS resources with Terraform 0.12 - April 2020
PDF
Raffaele Rialdi
PDF
What is new in Symfony 3,3, 3,4, 4.0, 4,1 + Flex
PDF
Ansible Configuration Management Tool 소개 및 활용
PPTX
Amazon Web Services for the .NET Developer
PPTX
goployer, 코드 기반의 배포 도구 - 송주영 (beNX) :: AWS Community Day 2020
PDF
Raffaele Rialdi
PDF
Find your data - use GraphDB capabilities in XPages applications - and beyond
PDF
Servlets made easy. 
Write once and run everywhere.
PDF
Deploy and Scale your PHP App with AWS ElasticBeanstalk and Docker- PHPTour L...
KEY
Modern Commandline Tool
PPTX
.Net Core 1.0 vs .NET Framework
PDF
Postman for Efficient Professional Services: Collaboration, Mocking Dependenc...
PPT
Version Control ThinkVitamin
London Community Summit - Habitat 2016
[Jun AWS 201] Elastic Beanstalk for Startups
Lessons learned migrating 100+ services to Kubernetes
Spinnaker 파트 1
Manage any AWS resources with Terraform 0.12 - April 2020
Raffaele Rialdi
What is new in Symfony 3,3, 3,4, 4.0, 4,1 + Flex
Ansible Configuration Management Tool 소개 및 활용
Amazon Web Services for the .NET Developer
goployer, 코드 기반의 배포 도구 - 송주영 (beNX) :: AWS Community Day 2020
Raffaele Rialdi
Find your data - use GraphDB capabilities in XPages applications - and beyond
Servlets made easy. 
Write once and run everywhere.
Deploy and Scale your PHP App with AWS ElasticBeanstalk and Docker- PHPTour L...
Modern Commandline Tool
.Net Core 1.0 vs .NET Framework
Postman for Efficient Professional Services: Collaboration, Mocking Dependenc...
Version Control ThinkVitamin
Ad

Similar to Composer (20)

PPTX
Composer
PDF
Dependency management with Composer
PDF
Composer - The missing package manager for PHP
PPTX
Composer Lightning Talk
PDF
Composer the Right Way - MM16NL
PDF
Using Composer with WordPress - 2.0
PPTX
PHP Dependency Management with Composer
PDF
Manuele Menozzi - Gestione delle dipendenze con Composer in Magento 2
PDF
Php Dependency Management with Composer ZendCon 2016
PPTX
Composer
PDF
Composer Helpdesk
PPTX
Composer namespacing
PPTX
Composer talk by Maxim Romanovsky
PDF
Composer & Drupal
PDF
WordCamp Sacramento 2019: Modernizing Your Development Workflow Using Composer
PPTX
IBM Rational Rhapsody 8.4 install guide (including Cygwin and obtaining an ev...
PDF
Composer the right way
PDF
Composer the right way [SweetlakePHP]
KEY
Composer
PDF
Php Dependency Management with Composer ZendCon 2017
Composer
Dependency management with Composer
Composer - The missing package manager for PHP
Composer Lightning Talk
Composer the Right Way - MM16NL
Using Composer with WordPress - 2.0
PHP Dependency Management with Composer
Manuele Menozzi - Gestione delle dipendenze con Composer in Magento 2
Php Dependency Management with Composer ZendCon 2016
Composer
Composer Helpdesk
Composer namespacing
Composer talk by Maxim Romanovsky
Composer & Drupal
WordCamp Sacramento 2019: Modernizing Your Development Workflow Using Composer
IBM Rational Rhapsody 8.4 install guide (including Cygwin and obtaining an ev...
Composer the right way
Composer the right way [SweetlakePHP]
Composer
Php Dependency Management with Composer ZendCon 2017
Ad

More from Alessandro Minoccheri (8)

PDF
ServerSentEventsV2.pdf
PDF
ServerSentEvents.pdf
PDF
Kotlin hexagonal-architecture
PDF
Hexagonal architecture ita
PDF
Hexagonal architecture
PDF
Smart working
ServerSentEventsV2.pdf
ServerSentEvents.pdf
Kotlin hexagonal-architecture
Hexagonal architecture ita
Hexagonal architecture
Smart working

Recently uploaded (20)

PDF
Salesforce Agentforce AI Implementation.pdf
PDF
Download FL Studio Crack Latest version 2025 ?
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Nekopoi APK 2025 free lastest update
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
iTop VPN 6.5.0 Crack + License Key 2025 (Premium Version)
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
Oracle Fusion HCM Cloud Demo for Beginners
PDF
CCleaner Pro 6.38.11537 Crack Final Latest Version 2025
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Autodesk AutoCAD Crack Free Download 2025
PPTX
L1 - Introduction to python Backend.pptx
PPTX
Computer Software and OS of computer science of grade 11.pptx
PPTX
Transform Your Business with a Software ERP System
PPTX
Monitoring Stack: Grafana, Loki & Promtail
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Salesforce Agentforce AI Implementation.pdf
Download FL Studio Crack Latest version 2025 ?
Reimagine Home Health with the Power of Agentic AI​
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Nekopoi APK 2025 free lastest update
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
iTop VPN 6.5.0 Crack + License Key 2025 (Premium Version)
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Oracle Fusion HCM Cloud Demo for Beginners
CCleaner Pro 6.38.11537 Crack Final Latest Version 2025
Design an Analysis of Algorithms I-SECS-1021-03
Autodesk AutoCAD Crack Free Download 2025
L1 - Introduction to python Backend.pptx
Computer Software and OS of computer science of grade 11.pptx
Transform Your Business with a Software ERP System
Monitoring Stack: Grafana, Loki & Promtail
Navsoft: AI-Powered Business Solutions & Custom Software Development
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus

Composer

  • 2. Composer is a tool for dependency management in PHP, is not a package manager. It allows you to declare the dependent libraries your project needs and it will install them in your project for you. Composer What is composer? What is composer?
  • 3. How does it works?
  • 4. • Install composer • composer init Composer How to start? How to start?
  • 5. • composer install • composer update • composer require vendor/package:1.1.* • composer remove vendor/package • composer update vendor/package Composer How to start? Basic commands
  • 7. Package version: x.y.z • x: MAJOR version when you make incompatible API changes • y: MINOR version when you add functionality in a backwards- compatible manner • z: PATCH version when you make backwards-compatible bug fixes Composer Semantic versioning Semantic versioning
  • 8. • Exact version 2.3.1 • Version range >=1.0 <1.1 || >=1.2 • Tilde version range ~1.2.3 is equivalent to >=1.2.3 <1.3.0 • Caret version range ^1.2.3 is equivalent to >=1.2.3 <2.0.0 Composer Versions and constraints Versions and constraints
  • 9. Composer generate an autoload file for all your dependencies vendor/autoload.php
 Now you can create a class inside app folder like Config.php and use this namespace: AppConfig
 The base directory will be src/App/Config 
 The resulting file path will be: ./src/App/Config/Config.php Composer Autoload Autoload { "autoload": { "psr-4": { "App": "src/" } }, "autoload-dev": { "psr-4": { "AppTests": "tests/" } } }
  • 10. A script, in Composer's terms, can either be a PHP callback (defined as a static method) or any command-line executable command.  Composer Scripts Scripts "scripts": { "post-update-cmd": "MyVendor MyClass::postUpdate", "post-install-cmd": [ "MyVendorMyClass::warmCache", "phpunit -c app/" ], "post-create-project-cmd": [ "php -r "copy('config/local- example.php', 'config/local.php');"" ] }
  • 11. Composer Require Require { "require": { "php": "^7.1.1" } } $ php -v PHP 5.6.10 $ composer update Your requirements could not be resolved to an installable set of packages. Problem 1 This package requires php ^7.1.1 but your PHP version (5.6.10) does not satisfy that requirement.
  • 12. $ php -v PHP 5.6.10 $ composer update Success Composer Platform requirements Platform requirements { "require": { "php": "^7.1.1" }, "config": { "platform": { "php": "7.1.2" } } }
  • 13. • Minimum-stability field defines default stability flags: for example: dev, alpha, beta, RC or stable • -stable, -RC ecc… suffix specifies the stability • If the stability is not specified, composer does it:
 1.2.3 become = 1.2.3.0-stable
 >1.2 become > 1.2.0.0-stable
 >=1.2 become 1.2.0.0-dev
 <1.3 become <1.3.0.0-dev Composer Stability flags Stability flags
  • 14. When running composer install for the first time, or when running composer update a lock file called composer.lock will be created with the exact versions that are installed so they can be re-installed.
 
 composer.lock must be committed to keep all co-workers in the same versions as you and to make deploy faster. Composer Composer.lock Composer.lock
  • 15. To solve composer.lock conflicts you have different ways: • Solve conflicts on .json file, accept your or another .lock and launch composer install • Solve conflicts on .json file, delete composer.lock file and launch composer update • Accept .json and .lock files from origin and re-apply your changes
 
 The best solution is the last to be more safe. Composer Composer.lock conflicts Composer.lock conflicts
  • 17. • Check for composer.lock file • If not, auto generate composer.lock file (Using composer update) • Install the specified versions recorded in the composer.lock file Composer Composer install vs update Composer install
  • 18. • Go through the composer.json file • Check availability of newer (latest) versions, based on the version criteria mentioned (e.g. 1.12.*) • Install the latest possible (according to above) versions • Update composer.lock file with installed versions Composer Composer install vs update Composer update
  • 19. • Use --prefer-dist to avoid git clones
 Will always download zip files if possible (default for stable versions) • Store ~/.composer/cache between builds
 How depends on CI product/setup you use Composer Composer install vs update Composer install performance
  • 20. —optimize-autoloader
 Class map generation essentially converts PSR-4/PSR-0 rules into classmap rules, performance increase from 20% to 25% Composer Composer install vs update Autoloader optimization composer install ——optimize-autoloader composer install --classmap-authoritative —classmap-authoritative 
 If something is not found in the classmap, then it does not exist and the autoloader should not attempt to look on the filesystem according to PSR-4 rules.
  • 21. A git patch is a commit converted into a file that can be applied on a different repository.
 
 So if you have a pull request not merged you can use the PR as if it were merged.
 
 Composer Patching with Composer Patching with Composer { "require": { "symfony/symfony": "4.1.0" }, "config": { "preferred-install": "source" }, "extra": { "patches": { "symfony/symfony": { "Something": “https://url.com/ foo.patch“ } } } }
  • 22. VCS stands for version control system. This includes versioning systems like git, svn, fossil or hg. Composer has a repository type for installing packages from these systems.
 
 Composer Use Private Package Use Private Package "require": { "vendor/package": "dev-master" }, “repositories”:[ { “type”: “vcs”, “url” : “[email protected]:vendor/
 package.git” } ]
  • 23. • Git: git-scm.com • Subversion: subversion.apache.org • Mercurial: mercurial-scm.org • Fossil: fossil-scm.org GitHub: github.com (Git) BitBucket: bitbucket.org (Git and Mercurial) Composer Git alternatives Git alternatives
  • 24. Packagist is the default Composer package repository. It lets you find packages and lets Composer know where to get the code from Composer Packagist Packagist
  • 25. Put a file named composer.json at the root of your package's repository, containing the at least a name of the package.
 
 Commit the file to your git or other VCS repository.
 
 Login or register on pakagist.org site, then hit the submit button in the menu. Once you entered your public repository URL in there, your package will be automatically crawled periodically. 
 You just have to make sure you keep the composer.json file up to date. Composer Publishing a Package Publishing a Package
  • 26. Grazie. Alessandro Minoccheri
 Twitter: minompi
 Github: AlessandroMinoccheri
 Stackoverflow: Alessandro Minoccheri