Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Composer: From Beginner to Expert
Search
Jonathan Klein
September 07, 2014
Technology
1
820
Composer: From Beginner to Expert
A talk I gave at Northeast PHP 2014. Links from this talk are at jkle.in/composer.
Jonathan Klein
September 07, 2014
Tweet
Share
More Decks by Jonathan Klein
See All by Jonathan Klein
Cognitive Biases in Engineering Organizations - Craft Conf
jklein
15
340
Demystifying SPDY and HTTP/2
jklein
1
550
Demystifying SPDY and HTTP/2
jklein
2
980
Cognitive Biases in Engineering Organizations
jklein
4
1.9k
Upgrading the Web: Boston Web Performance Meetup
jklein
1
250
Upgrading the Web: Driving Support For New Standards
jklein
1
750
Northeastern Lunch and Learn
jklein
0
200
Profiling PHP With XHProf
jklein
1
970
HubSpot Tech Talk - DIY Synthetic
jklein
0
250
Other Decks in Technology
See All in Technology
SREによる隣接領域への越境とその先の信頼性
shonansurvivors
2
520
Incident Response Practices: Waroom's Features and Future Challenges
rrreeeyyy
0
160
隣接領域をBeyondするFinatextのエンジニア組織設計 / beyond-engineering-areas
stajima
1
270
いざ、BSC討伐の旅
nikinusu
2
780
【Pycon mini 東海 2024】Google Colaboratoryで試すVLM
kazuhitotakahashi
2
500
【令和最新版】AWS Direct Connectと愉快なGWたちのおさらい
minorun365
PRO
5
750
オープンソースAIとは何か? --「オープンソースAIの定義 v1.0」詳細解説
shujisado
7
810
マルチプロダクトな開発組織で 「開発生産性」に向き合うために試みたこと / Improving Multi-Product Dev Productivity
sugamasao
1
300
[CV勉強会@関東 ECCV2024 読み会] オンラインマッピング x トラッキング MapTracker: Tracking with Strided Memory Fusion for Consistent Vector HD Mapping (Chen+, ECCV24)
abemii
0
220
dev 補講: プロダクトセキュリティ / Product security overview
wa6sn
1
2.3k
OCI 運用監視サービス 概要
oracle4engineer
PRO
0
4.8k
Why App Signing Matters for Your Android Apps - Android Bangkok Conference 2024
akexorcist
0
120
Featured
See All Featured
Docker and Python
trallard
40
3.1k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
For a Future-Friendly Web
brad_frost
175
9.4k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
The Cost Of JavaScript in 2023
addyosmani
45
6.7k
Bash Introduction
62gerente
608
210k
A Modern Web Designer's Workflow
chriscoyier
693
190k
Being A Developer After 40
akosma
86
590k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
33k
Facilitating Awesome Meetings
lara
50
6.1k
Art, The Web, and Tiny UX
lynnandtonic
297
20k
The Language of Interfaces
destraynor
154
24k
Transcript
Composer: From Beginner to Expert Northeast PHP 2014 Jonathan Klein
@jonathanklein
jkle.in/composer
None
“The best way to use 3rd party code”
“…and autoload your code”
None
None
None
A Story in Three Acts 1. Dependency Management
A Story in Three Acts 1. Dependency Management
A Story in Three Acts 1. Using 3rd Party Code
A Story in Three Acts 1. Using 3rd Party Code
2. Autoloading Your Code
A Story in Three Acts 1. Using 3rd Party Code
2. Autoloading Your Code 3. Publishing Packages
Using 3rd Party Code
Install Composer
curl -sS https://getcomposer.org/installer | php ! mv composer.phar /usr/bin/composer
None
None
None
None
https://www.flickr.com/photos/monkeywobble/2124397478
https://www.flickr.com/photos/adrianblack/3358661327
Three Ways Composer Helps
Three Ways Composer Helps 1. Simple JSON Config File (composer.json)
Three Ways Composer Helps 1. Simple JSON Config File (composer.json)
2. Repository of Code (Packagist)
Three Ways Composer Helps 1. Simple JSON Config File (composer.json)
2. Repository of Code (Packagist) 3. Generated Autoload File (autoload.php)
Simplest JSON Config File { }
Simplest JSON Config File { "require": { "monolog/monolog": "1.10.*" }
}
Simplest JSON Config File { "require": { "monolog/monolog": "1.10.*" }
} Vendor Name
Simplest JSON Config File { "require": { "monolog/monolog": "1.10.*" }
} Vendor Name Package Name
Simplest JSON Config File { "require": { "monolog/monolog": "1.10.*" }
} Vendor Name Package Name Version Number
Composer + Packagist
None
None
None
Simplest JSON Config File { "require": { "monolog/monolog": "1.10.*" }
} Vendor Name Package Name Version Number
None
None
None
None
$ composer install
Vendor Directory
None
Vendor Directory
None
None
$ composer install
Using The Code <?php require 'vendor/autoload.php'; ! $log = new
Monolog\Logger('name'); ! …snip… ! $log->addWarning('Foo');
Using The Code <?php require 'vendor/autoload.php'; ! $log = new
Monolog\Logger('name'); ! …snip… ! $log->addWarning('Foo'); Generated autoload file
None
None
None
{ "require": { "monolog/monolog": "1.10.*", "filp/whoops": "1.*", "respect/validation": "0.5.*", "swiftmailer/swiftmailer":
"5.0.*" } } composer.json
$ composer update
composer require filp/whoops:1.* composer require respect/validation:0.5.* composer require swiftmailer/swiftmailer:5.0.*
Using The Code <?php ! require 'vendor/autoload.php'; ! use Respect\Validation\Validator
as v; ! $log = new Monolog\Logger('name'); $whoops = new \Whoops\Run; Swift::init('swiftmailer_configurator'); ! $number = 123; v::numeric()->validate($number);
Using The Code <?php ! require 'vendor/autoload.php'; ! use Respect\Validation\Validator
as v; ! $log = new Monolog\Logger('name'); $whoops = new \Whoops\Run; Swift::init('swiftmailer_configurator'); ! $number = 123; v::numeric()->validate($number);
Using The Code <?php ! require 'vendor/autoload.php'; ! use Respect\Validation\Validator
as v; ! $log = new Monolog\Logger('name'); $whoops = new \Whoops\Run; Swift::init('swiftmailer_configurator'); ! $number = 123; v::numeric()->validate($number);
Versioning
{ "require": { "monolog/monolog": "1.10.*", "filp/whoops": "1.*", "respect/validation": "0.5.*", "swiftmailer/swiftmailer":
"5.0.*" } }
composer.lock
composer.lock Commit this
None
$ composer update
None
None
vendor/autoload.php is regenerated
vendor/autoload.php should be the only include in your application
“What about MY Code?”
Autoloading Your Code
{ "require": { "monolog/monolog": "1.10.*" }, ! "autoload": { "psr-4":
{"Acme\\": "src/"} } }
{ "require": { "monolog/monolog": "1.10.*" }, ! "autoload": { "psr-4":
{"Acme\\": "src/"} } } WTF?
PSR: PHP Specification Request
None
Approved Standards PSR-0 Autoloading PSR-1 Basic Coding Standard PSR-2 Coding
Style Guide PSR-3 Logger Interface PSR-4 Improved Autoloading
Approved Standards PSR-0 Autoloading PSR-1 Basic Coding Standard PSR-2 Coding
Style Guide PSR-3 Logger Interface PSR-4 Improved Autoloading
Approved Standards PSR-0 Autoloading PSR-1 Basic Coding Standard PSR-2 Coding
Style Guide PSR-3 Logger Interface PSR-4 Improved Autoloading
What Improved?
What Improved? 1. Underscores no longer designate directories
What Improved? 1. Underscores no longer designate directories 2. Namespaces
can map to arbitrary folders
None
None
None
{ "require": { "monolog/monolog": "1.10.*" }, ! "autoload": { "psr-4":
{ "Acme\\": "src/" } } }
{ "require": { "monolog/monolog": "1.10.*" }, ! "autoload": { "psr-4":
{ "Acme\\": ["src/", "tests/"] } } }
None
Takeaway: Use PSR-4
Autoloading code <?php ! require 'vendor/autoload.php'; ! $foo = new
\Acme\Foo\Bar\Baz(); // src/Foo/Bar/Baz.php
{ "require": { "monolog/monolog": "1.10.*" }, ! "autoload": { "psr-4":
{"Acme\\": "src/Foo/Bar/"} } }
Autoloading code <?php ! require 'vendor/autoload.php'; ! $foo = new
\Acme\Baz(); // src/Foo/Bar/Baz.php
<?php ! namespace Acme; ! class Baz{ ! function __construct()
{ echo "Hello, World"; } }
{ "require": { "monolog/monolog": "1.10.*" }, ! "autoload": { "psr-4":
{ "Acme\\": "src/", "Vendor\\Namespace\\": "" } } }
“What About Functions?”
{ "require": { "monolog/monolog": "1.10.*" }, ! "autoload": { "psr-4":
{"Acme\\": "src/"}, "files": ["src/functions.php"] } }
None
None
Publishing Packages
{ "require": { "monolog/monolog": "1.10.*" }, ! "autoload": { "psr-4":
{"Acme\\": "src/Foo/Bar/"} } }
{ "name": "acme/hello", "description": "Prints Hello, World", "require": { "monolog/monolog":
"1.10.*" }, ! "autoload": { "psr-4": {"Acme\\": "src/Foo/Bar/"} } }
None
None
None
Private Repository with Satis
Bonus: Advanced Usage
{ "require": { "monolog/monolog": "1.10.*" }, ! "require-dev": { "phpunit/phpunit":
"3.7.*", "mockery/mockery": "0.7.*" } }
{ "scripts": { "post-update-cmd": "MyVendor\\MyClass::postUpdate", "post-package-install": [ "MyVendor\\MyClass::postPackageInstall" ], "post-install-cmd":
[ "MyVendor\\MyClass::warmCache", "phpunit -c app/" ] } }
{ "scripts": { "post-update-cmd": "MyVendor\\MyClass::postUpdate", "post-package-install": [ "MyVendor\\MyClass::postPackageInstall" ], "post-install-cmd":
[ "MyVendor\\MyClass::warmCache", "phpunit -c app/" ] } }
{ "scripts": { "post-update-cmd": "MyVendor\\MyClass::postUpdate", "post-package-install": [ "MyVendor\\MyClass::postPackageInstall" ], "post-install-cmd":
[ "MyVendor\\MyClass::warmCache", "phpunit -c app/" ] } }
Other Useful Commands
Other Useful Commands composer init
Other Useful Commands composer init composer remove <package name>
Other Useful Commands composer init composer remove <package name> composer
validate
Other Useful Commands composer init composer remove <package name> composer
validate composer self-update
Other Useful Commands composer init composer remove <package name> composer
validate composer self-update composer diagnose
Other Useful Commands composer init composer remove <package name> composer
validate composer self-update composer diagnose composer status
Wrap Up
Wrap Up • Include 3rd Party Code
Wrap Up • Include 3rd Party Code • Autoload Your
Own Code
Wrap Up • Include 3rd Party Code • Autoload Your
Own Code • Publish Packages
Thanks!
! jkle.in/composer ! @jonathanklein
[email protected]
www.etsy.com/careers