SlideShare une entreprise Scribd logo
1  sur  17
Télécharger pour lire hors ligne
#BURNINGKEYBOARDS
DENIS.RISTIC@PERPETUUM.HR
COMPOSER
COMPOSER
INTRO
▸ Dependency Manager for PHP
▸ It allows you to declare the libraries your project depends
on and it will manage (install/update) them for you.
▸ Composer handles dependency resolution automatically
▸ Composer handles autoloading automatically
3
COMPOSER
DEPENDENCY MANAGEMENT
▸ Dependency management is actually a pretty simple concept. Let's
assume you're creating a one-page website using the Bootstrap
framework for your Javascript and CSS needs. How do you make sure
that Bootstrap is added to your project?
▸ The usual approach is to go to the website, download the package
and place it somewhere within your project. So far so good. Now, what
do you do when you want to update to the latest version? You repeat
the same thing, overwriting the old version.
▸ Let's assume this goes on for a while and you realize something is
broken. They've changed something in Bootstrap and now you have
to roll back, but where to? You'll need to find the older versions and
start applying them until you find the right one.
4
COMPOSER
DEPENDENCY MANAGEMENT
▸ Even if you sort all that out, let's say you move onto someone else's project. Are
they using Bootstrap as well? If so, where is it installed and what version is it?
▸ These may not seem like huge issues for a small project but imagine a project
with 8-10 dependencies - which still isn't a lot. Managing everything modularly
becomes impossible or - at the very least - a waste of time.
▸ Dependency management solves these problems by automating and
standardizing. The retrieval of dependencies such as Bootstrap, jQuery, Twig,
Symphony, logging modules and so on can be done programmatically.
Preferred versions can also be designated which protects against conflicts.
▸ A dependency manager standardizes the way the packages are stored and
where they are used from. In practice this means that every project that uses
the same dependency manager will follow the same structure - at least for
dependencies.
5
COMPOSER
INSTALLING COMPOSER (WINDOWS)
▸ Using the Installer
▸ This is the easiest way to get Composer set up on your
machine.
▸ Download and run Composer-Setup.exe. It will install
the latest Composer version and set up your PATH so
that you can just call composer from any directory in
your command line.
6
COMPOSER
BASIC USAGE
▸ For our basic usage introduction, we will be installing
monolog/monolog, a logging library.
▸ To start using Composer in your project, all you need is a
composer.json file. This file describes the dependencies
of your project and may contain other metadata as well.
▸ The first (and often only) thing you specify in
composer.json is the require key. You are simply telling
Composer which packages your project depends on.
7
COMPOSER
BASIC USAGE
composer.json
{
"require": {
"monolog/monolog": "1.0.*"
}
}
8
COMPOSER
BASIC USAGE
▸ Package Names
▸ The package name consists of a vendor name and the
project's name.
▸ Package Version Constraints
▸ In our example, we are requesting the Monolog
package with the version constraint 1.0.*. This means
any version in the 1.0 development branch, or any
version that is greater than or equal to 1.0 and less than
1.1 (>=1.0 <1.1).
9
COMPOSER
BASIC USAGE
▸ Installing Dependencies
▸ To install the defined dependencies for your project, just
run the install command.
▸ composer install
▸ Updating Dependencies to their Latest Versions
▸ composer update
▸ composer update monolog/monolog
10
COMPOSER
BASIC USAGE
▸ Packagist
▸ Packagist.org is the main Composer repository.
▸ A Composer repository is basically a package source: a
place where you can get packages from.
▸ Packagist aims to be the central repository that
everybody uses.
▸ This means that you can automatically require any
package that is available there, without further specifying
where Composer should look for the package.
11
COMPOSER
BASIC USAGE
▸ Platform packages
▸ Composer has platform packages, which are virtual packages for things that are
installed on the system but are not actually installable by Composer. This includes PHP
itself, PHP extensions and some system libraries.
▸ php represents the PHP version of the user, allowing you to apply constraints, e.g.
>=5.4.0. To require a 64bit version of php, you can require the php-64bit
package.
▸ hhvm represents the version of the HHVM runtime and allows you to apply a
constraint, e.g., '>=2.3.3'.
▸ ext-<name> allows you to require PHP extensions (includes core extensions).
Versioning can be quite inconsistent here, so it's often a good idea to just set the
constraint to *. An example of an extension package name is ext-gd.
▸ lib-<name> allows constraints to be made on versions of libraries used by PHP. The
following are available: curl, iconv, icu, libxml, openssl, pcre, uuid, xsl.
12
COMPOSER
BASIC USAGE
▸ Autoloading
▸ For libraries that specify autoload information,
Composer generates a vendor/autoload.php file.
require __DIR__ . '/vendor/autoload.php';
$log = new MonologLogger('name');
$log->pushHandler(new
MonologHandlerStreamHandler('app.log',
MonologLogger::WARNING));
$log->addWarning('Foo');
13
COMPOSER
BASIC USAGE
▸ You can even add your own code to the autoloader by adding an autoload field to
composer.json.
{
"autoload": {
"psr-4": {"Acme": "src/"}
}
}
▸ Composer will register a PSR-4 autoloader for the Acme namespace.
▸ You define a mapping from namespaces to directories. The src directory would be in your
project root, on the same level as vendor directory is. An example filename would be src/
Foo.php containing an AcmeFoo class.
▸ After adding the autoload field, you have to re-run dump-autoload to re-generate the
vendor/autoload.php file.
14
COMPOSER
LIBRARIES
▸ Every project is a package
▸ As soon as you have a composer.json in a directory, that
directory is a package. When you add a require to a
project, you are making a package that depends on
other packages. The only difference between your
project and a library is that your project is a package
without a name.
▸ In order to make that package installable you need to
give it a name. You do this by adding the name property
in composer.json:
15
COMPOSER
LIBRARIES
{
"name": “acme/hello-world”,
"version": “1.0.0",
"require": {
"monolog/monolog": "1.0.*"
}
}
▸ In this case the project name is acme/hello-world, where acme
is the vendor name. Supplying a vendor name is mandatory.
16
COMPOSER
COMPOSER GLOBAL
‣ You can install packages globally rather than locally into your project.
$ composer global require phpunit/phpunit
$ composer global require phpunit/dbunit
$ composer global require phing/phing
$ composer global require phpdocumentor/phpdocumentor
$ composer global require sebastian/phpcpd
$ composer global require phploc/phploc
$ composer global require phpmd/phpmd
$ composer global require squizlabs/php_codesniffer
‣ This will install PHPUnit and all its dependencies into the ~/.composer/vendor/ directory
and, most importantly, the phpunit CLI tools are installed into ~/.composer/vendor/bin/.
‣ Simply add this directory to your PATH
17

Contenu connexe

Tendances

Docker workshop
Docker workshopDocker workshop
Docker workshop
Evans Ye
 

Tendances (20)

6 Years of Docker: The Good, the Bad and Python Packaging at PyCon.DE&PyData ...
6 Years of Docker: The Good, the Bad and Python Packaging at PyCon.DE&PyData ...6 Years of Docker: The Good, the Bad and Python Packaging at PyCon.DE&PyData ...
6 Years of Docker: The Good, the Bad and Python Packaging at PyCon.DE&PyData ...
 
PHP development with Docker
PHP development with DockerPHP development with Docker
PHP development with Docker
 
maXbox starter46 work with Wine
maXbox starter46 work with WinemaXbox starter46 work with Wine
maXbox starter46 work with Wine
 
Getting instantly up and running with Docker and Symfony
Getting instantly up and running with Docker and SymfonyGetting instantly up and running with Docker and Symfony
Getting instantly up and running with Docker and Symfony
 
Vagrant and docker
Vagrant and dockerVagrant and docker
Vagrant and docker
 
FITC - Node.js 101
FITC - Node.js 101FITC - Node.js 101
FITC - Node.js 101
 
Creating your own AtoM demo data set for re-use with Vagrant
Creating your own AtoM demo data set for re-use with VagrantCreating your own AtoM demo data set for re-use with Vagrant
Creating your own AtoM demo data set for re-use with Vagrant
 
Introducing composer - a php dependency manager
Introducing composer  - a php dependency managerIntroducing composer  - a php dependency manager
Introducing composer - a php dependency manager
 
Installing and running Postfix within a docker container from the command line
Installing and running Postfix within a docker container from the command lineInstalling and running Postfix within a docker container from the command line
Installing and running Postfix within a docker container from the command line
 
PHP Dependency Management with Composer
PHP Dependency Management with ComposerPHP Dependency Management with Composer
PHP Dependency Management with Composer
 
Docker workshop
Docker workshopDocker workshop
Docker workshop
 
Vagrant + Docker
Vagrant + DockerVagrant + Docker
Vagrant + Docker
 
Dockerizing WordPress
Dockerizing WordPressDockerizing WordPress
Dockerizing WordPress
 
Remote Control WordPress
Remote Control WordPressRemote Control WordPress
Remote Control WordPress
 
Laravel Poznań Meetup #12 - "Speed up web API with Laravel and Swoole using ...
 Laravel Poznań Meetup #12 - "Speed up web API with Laravel and Swoole using ... Laravel Poznań Meetup #12 - "Speed up web API with Laravel and Swoole using ...
Laravel Poznań Meetup #12 - "Speed up web API with Laravel and Swoole using ...
 
Build service with_docker_in_90mins
Build service with_docker_in_90minsBuild service with_docker_in_90mins
Build service with_docker_in_90mins
 
composer_talk_20160209
composer_talk_20160209composer_talk_20160209
composer_talk_20160209
 
Puppet and Vagrant in development
Puppet and Vagrant in developmentPuppet and Vagrant in development
Puppet and Vagrant in development
 
Composer
ComposerComposer
Composer
 
Dockerfile Basics | Docker workshop #2 at twitter, 2013-11-05
Dockerfile Basics | Docker workshop #2 at twitter, 2013-11-05Dockerfile Basics | Docker workshop #2 at twitter, 2013-11-05
Dockerfile Basics | Docker workshop #2 at twitter, 2013-11-05
 

Similaire à 12 Composer #burningkeyboards

Dependency management with Composer
Dependency management with ComposerDependency management with Composer
Dependency management with Composer
Jason Grimes
 

Similaire à 12 Composer #burningkeyboards (20)

Composer namespacing
Composer namespacingComposer namespacing
Composer namespacing
 
Composer
ComposerComposer
Composer
 
Dependency management with Composer
Dependency management with ComposerDependency management with Composer
Dependency management with Composer
 
Composer Lightning Talk
Composer Lightning TalkComposer Lightning Talk
Composer Lightning Talk
 
Using Composer to create manageable WordPress websites
Using Composer to create manageable WordPress websitesUsing Composer to create manageable WordPress websites
Using Composer to create manageable WordPress websites
 
Composer Helpdesk
Composer HelpdeskComposer Helpdesk
Composer Helpdesk
 
Composer
ComposerComposer
Composer
 
Composer tools and frameworks for drupal.ppt
Composer tools and frameworks for drupal.pptComposer tools and frameworks for drupal.ppt
Composer tools and frameworks for drupal.ppt
 
Composer tools and frameworks for Drupal
Composer tools and frameworks for DrupalComposer tools and frameworks for Drupal
Composer tools and frameworks for Drupal
 
Php Dependency Management with Composer ZendCon 2017
Php Dependency Management with Composer ZendCon 2017Php Dependency Management with Composer ZendCon 2017
Php Dependency Management with Composer ZendCon 2017
 
Composer & Drupal
Composer & DrupalComposer & Drupal
Composer & Drupal
 
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
 
Composer Tools & Frameworks for Drupal
Composer Tools & Frameworks for DrupalComposer Tools & Frameworks for Drupal
Composer Tools & Frameworks for Drupal
 
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
 
Start your adventure with docker
Start your adventure with dockerStart your adventure with docker
Start your adventure with docker
 
Composer intro
Composer introComposer intro
Composer intro
 
自分たちのコードを Composer パッケージに分割して開発する
自分たちのコードを Composer パッケージに分割して開発する自分たちのコードを Composer パッケージに分割して開発する
自分たちのコードを Composer パッケージに分割して開発する
 
Composer: Dependency Manager for PHP
Composer: Dependency Manager for PHPComposer: Dependency Manager for PHP
Composer: Dependency Manager for PHP
 

Plus de Denis Ristic

18 Git #burningkeyboards
18 Git #burningkeyboards18 Git #burningkeyboards
18 Git #burningkeyboards
Denis Ristic
 

Plus de Denis Ristic (20)

Magento Continuous Integration & Continuous Delivery @MM17HR
Magento Continuous Integration & Continuous Delivery @MM17HRMagento Continuous Integration & Continuous Delivery @MM17HR
Magento Continuous Integration & Continuous Delivery @MM17HR
 
Continuous integration & Continuous Delivery @DeVz
Continuous integration & Continuous Delivery @DeVzContinuous integration & Continuous Delivery @DeVz
Continuous integration & Continuous Delivery @DeVz
 
25 Intro to Symfony #burningkeyboards
25 Intro to Symfony #burningkeyboards25 Intro to Symfony #burningkeyboards
25 Intro to Symfony #burningkeyboards
 
24 Scrum #burningkeyboards
24 Scrum #burningkeyboards24 Scrum #burningkeyboards
24 Scrum #burningkeyboards
 
23 LAMP Stack #burningkeyboards
23 LAMP Stack #burningkeyboards23 LAMP Stack #burningkeyboards
23 LAMP Stack #burningkeyboards
 
22 REST & JSON API Design #burningkeyboards
22 REST & JSON API Design #burningkeyboards22 REST & JSON API Design #burningkeyboards
22 REST & JSON API Design #burningkeyboards
 
21 HTTP Protocol #burningkeyboards
21 HTTP Protocol #burningkeyboards21 HTTP Protocol #burningkeyboards
21 HTTP Protocol #burningkeyboards
 
20 PHP Static Analysis and Documentation Generators #burningkeyboards
20 PHP Static Analysis and Documentation Generators #burningkeyboards20 PHP Static Analysis and Documentation Generators #burningkeyboards
20 PHP Static Analysis and Documentation Generators #burningkeyboards
 
19 GitFlow #burningkeyboards
19 GitFlow #burningkeyboards19 GitFlow #burningkeyboards
19 GitFlow #burningkeyboards
 
18 Git #burningkeyboards
18 Git #burningkeyboards18 Git #burningkeyboards
18 Git #burningkeyboards
 
17 Linux Basics #burningkeyboards
17 Linux Basics #burningkeyboards17 Linux Basics #burningkeyboards
17 Linux Basics #burningkeyboards
 
16 MySQL Optimization #burningkeyboards
16 MySQL Optimization #burningkeyboards16 MySQL Optimization #burningkeyboards
16 MySQL Optimization #burningkeyboards
 
15 MySQL Basics #burningkeyboards
15 MySQL Basics #burningkeyboards15 MySQL Basics #burningkeyboards
15 MySQL Basics #burningkeyboards
 
14 Dependency Injection #burningkeyboards
14 Dependency Injection #burningkeyboards14 Dependency Injection #burningkeyboards
14 Dependency Injection #burningkeyboards
 
13 PHPUnit #burningkeyboards
13 PHPUnit #burningkeyboards13 PHPUnit #burningkeyboards
13 PHPUnit #burningkeyboards
 
11 PHP Security #burningkeyboards
11 PHP Security #burningkeyboards11 PHP Security #burningkeyboards
11 PHP Security #burningkeyboards
 
10 PHP Design Patterns #burningkeyboards
10 PHP Design Patterns #burningkeyboards10 PHP Design Patterns #burningkeyboards
10 PHP Design Patterns #burningkeyboards
 
09 Object Oriented Programming in PHP #burningkeyboards
09 Object Oriented Programming in PHP #burningkeyboards09 Object Oriented Programming in PHP #burningkeyboards
09 Object Oriented Programming in PHP #burningkeyboards
 
08 Advanced PHP #burningkeyboards
08 Advanced PHP #burningkeyboards08 Advanced PHP #burningkeyboards
08 Advanced PHP #burningkeyboards
 
07 Introduction to PHP #burningkeyboards
07 Introduction to PHP #burningkeyboards07 Introduction to PHP #burningkeyboards
07 Introduction to PHP #burningkeyboards
 

Dernier

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Dernier (20)

DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 

12 Composer #burningkeyboards

  • 3. COMPOSER INTRO ▸ Dependency Manager for PHP ▸ It allows you to declare the libraries your project depends on and it will manage (install/update) them for you. ▸ Composer handles dependency resolution automatically ▸ Composer handles autoloading automatically 3
  • 4. COMPOSER DEPENDENCY MANAGEMENT ▸ Dependency management is actually a pretty simple concept. Let's assume you're creating a one-page website using the Bootstrap framework for your Javascript and CSS needs. How do you make sure that Bootstrap is added to your project? ▸ The usual approach is to go to the website, download the package and place it somewhere within your project. So far so good. Now, what do you do when you want to update to the latest version? You repeat the same thing, overwriting the old version. ▸ Let's assume this goes on for a while and you realize something is broken. They've changed something in Bootstrap and now you have to roll back, but where to? You'll need to find the older versions and start applying them until you find the right one. 4
  • 5. COMPOSER DEPENDENCY MANAGEMENT ▸ Even if you sort all that out, let's say you move onto someone else's project. Are they using Bootstrap as well? If so, where is it installed and what version is it? ▸ These may not seem like huge issues for a small project but imagine a project with 8-10 dependencies - which still isn't a lot. Managing everything modularly becomes impossible or - at the very least - a waste of time. ▸ Dependency management solves these problems by automating and standardizing. The retrieval of dependencies such as Bootstrap, jQuery, Twig, Symphony, logging modules and so on can be done programmatically. Preferred versions can also be designated which protects against conflicts. ▸ A dependency manager standardizes the way the packages are stored and where they are used from. In practice this means that every project that uses the same dependency manager will follow the same structure - at least for dependencies. 5
  • 6. COMPOSER INSTALLING COMPOSER (WINDOWS) ▸ Using the Installer ▸ This is the easiest way to get Composer set up on your machine. ▸ Download and run Composer-Setup.exe. It will install the latest Composer version and set up your PATH so that you can just call composer from any directory in your command line. 6
  • 7. COMPOSER BASIC USAGE ▸ For our basic usage introduction, we will be installing monolog/monolog, a logging library. ▸ To start using Composer in your project, all you need is a composer.json file. This file describes the dependencies of your project and may contain other metadata as well. ▸ The first (and often only) thing you specify in composer.json is the require key. You are simply telling Composer which packages your project depends on. 7
  • 9. COMPOSER BASIC USAGE ▸ Package Names ▸ The package name consists of a vendor name and the project's name. ▸ Package Version Constraints ▸ In our example, we are requesting the Monolog package with the version constraint 1.0.*. This means any version in the 1.0 development branch, or any version that is greater than or equal to 1.0 and less than 1.1 (>=1.0 <1.1). 9
  • 10. COMPOSER BASIC USAGE ▸ Installing Dependencies ▸ To install the defined dependencies for your project, just run the install command. ▸ composer install ▸ Updating Dependencies to their Latest Versions ▸ composer update ▸ composer update monolog/monolog 10
  • 11. COMPOSER BASIC USAGE ▸ Packagist ▸ Packagist.org is the main Composer repository. ▸ A Composer repository is basically a package source: a place where you can get packages from. ▸ Packagist aims to be the central repository that everybody uses. ▸ This means that you can automatically require any package that is available there, without further specifying where Composer should look for the package. 11
  • 12. COMPOSER BASIC USAGE ▸ Platform packages ▸ Composer has platform packages, which are virtual packages for things that are installed on the system but are not actually installable by Composer. This includes PHP itself, PHP extensions and some system libraries. ▸ php represents the PHP version of the user, allowing you to apply constraints, e.g. >=5.4.0. To require a 64bit version of php, you can require the php-64bit package. ▸ hhvm represents the version of the HHVM runtime and allows you to apply a constraint, e.g., '>=2.3.3'. ▸ ext-<name> allows you to require PHP extensions (includes core extensions). Versioning can be quite inconsistent here, so it's often a good idea to just set the constraint to *. An example of an extension package name is ext-gd. ▸ lib-<name> allows constraints to be made on versions of libraries used by PHP. The following are available: curl, iconv, icu, libxml, openssl, pcre, uuid, xsl. 12
  • 13. COMPOSER BASIC USAGE ▸ Autoloading ▸ For libraries that specify autoload information, Composer generates a vendor/autoload.php file. require __DIR__ . '/vendor/autoload.php'; $log = new MonologLogger('name'); $log->pushHandler(new MonologHandlerStreamHandler('app.log', MonologLogger::WARNING)); $log->addWarning('Foo'); 13
  • 14. COMPOSER BASIC USAGE ▸ You can even add your own code to the autoloader by adding an autoload field to composer.json. { "autoload": { "psr-4": {"Acme": "src/"} } } ▸ Composer will register a PSR-4 autoloader for the Acme namespace. ▸ You define a mapping from namespaces to directories. The src directory would be in your project root, on the same level as vendor directory is. An example filename would be src/ Foo.php containing an AcmeFoo class. ▸ After adding the autoload field, you have to re-run dump-autoload to re-generate the vendor/autoload.php file. 14
  • 15. COMPOSER LIBRARIES ▸ Every project is a package ▸ As soon as you have a composer.json in a directory, that directory is a package. When you add a require to a project, you are making a package that depends on other packages. The only difference between your project and a library is that your project is a package without a name. ▸ In order to make that package installable you need to give it a name. You do this by adding the name property in composer.json: 15
  • 16. COMPOSER LIBRARIES { "name": “acme/hello-world”, "version": “1.0.0", "require": { "monolog/monolog": "1.0.*" } } ▸ In this case the project name is acme/hello-world, where acme is the vendor name. Supplying a vendor name is mandatory. 16
  • 17. COMPOSER COMPOSER GLOBAL ‣ You can install packages globally rather than locally into your project. $ composer global require phpunit/phpunit $ composer global require phpunit/dbunit $ composer global require phing/phing $ composer global require phpdocumentor/phpdocumentor $ composer global require sebastian/phpcpd $ composer global require phploc/phploc $ composer global require phpmd/phpmd $ composer global require squizlabs/php_codesniffer ‣ This will install PHPUnit and all its dependencies into the ~/.composer/vendor/ directory and, most importantly, the phpunit CLI tools are installed into ~/.composer/vendor/bin/. ‣ Simply add this directory to your PATH 17
  • 18. COMPOSER COMPOSER RESOURCES ‣ Composer ‣ https://getcomposer.org/doc/ ‣ Building maintainable PHP apps using Composer ‣ https://laravel-news.com/building-apps-composer 18