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
990
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
アジャイルでの品質の進化 Agile in Motion vol.1/20241118 Hiroyuki Sato
shift_evolve
0
170
AI前提のサービス運用ってなんだろう?
ryuichi1208
8
1.4k
10XにおけるData Contractの導入について: Data Contract事例共有会
10xinc
6
660
誰も全体を知らない ~ ロールの垣根を超えて引き上げる開発生産性 / Boosting Development Productivity Across Roles
kakehashi
1
230
適材適所の技術選定 〜GraphQL・REST API・tRPC〜 / Optimal Technology Selection
kakehashi
1
690
AWS Media Services 最新サービスアップデート 2024
eijikominami
0
200
リンクアンドモチベーション ソフトウェアエンジニア向け紹介資料 / Introduction to Link and Motivation for Software Engineers
lmi
4
300k
開発生産性を上げながらビジネスも30倍成長させてきたチームの姿
kamina_zzz
2
1.7k
テストコード品質を高めるためにMutation Testingライブラリ・Strykerを実戦導入してみた話
ysknsid25
7
2.7k
組織成長を加速させるオンボーディングの取り組み
sudoakiy
2
210
日経電子版のStoreKit2フルリニューアル
shimastripe
1
140
アジャイルチームがらしさを発揮するための目標づくり / Making the goal and enabling the team
kakehashi
3
140
Featured
See All Featured
YesSQL, Process and Tooling at Scale
rocio
169
14k
BBQ
matthewcrist
85
9.3k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
42
9.2k
Six Lessons from altMBA
skipperchong
27
3.5k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
How To Stay Up To Date on Web Technology
chriscoyier
788
250k
Designing for Performance
lara
604
68k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
109
49k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
6
420
Code Reviewing Like a Champion
maltzj
520
39k
Teambox: Starting and Learning
jrom
133
8.8k
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