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
830
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
350
Demystifying SPDY and HTTP/2
jklein
1
560
Demystifying SPDY and HTTP/2
jklein
2
1k
Cognitive Biases in Engineering Organizations
jklein
4
1.9k
Upgrading the Web: Boston Web Performance Meetup
jklein
1
260
Upgrading the Web: Driving Support For New Standards
jklein
1
750
Northeastern Lunch and Learn
jklein
0
200
Profiling PHP With XHProf
jklein
1
980
HubSpot Tech Talk - DIY Synthetic
jklein
0
260
Other Decks in Technology
See All in Technology
KubeCon NA 2024 Recap / Running WebAssembly (Wasm) Workloads Side-by-Side with Container Workloads
z63d
1
240
複雑性の高いオブジェクト編集に向き合う: プラガブルなReactフォーム設計
righttouch
PRO
0
110
kargoの魅力について伝える
magisystem0408
0
200
第3回Snowflake女子会_LT登壇資料(合成データ)_Taro_CCCMK
tarotaro0129
0
180
LINE Developersプロダクト(LIFF/LINE Login)におけるフロントエンド開発
lycorptech_jp
PRO
0
120
サイバー攻撃を想定したセキュリティガイドライン 策定とASM及びCNAPPの活用方法
syoshie
3
1.2k
PHPからGoへのマイグレーション for DMMアフィリエイト
yabakokobayashi
1
170
watsonx.ai Dojo #5 ファインチューニングとInstructLAB
oniak3ibm
PRO
0
160
Qiita埋め込み用スライド
naoki_0531
0
860
AWS re:Invent 2024 ふりかえり
kongmingstrap
0
130
[Ruby] Develop a Morse Code Learning Gem & Beep from Strings
oguressive
1
150
Turing × atmaCup #18 - 1st Place Solution
hakubishin3
0
470
Featured
See All Featured
Testing 201, or: Great Expectations
jmmastey
40
7.1k
Building Better People: How to give real-time feedback that sticks.
wjessup
365
19k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Automating Front-end Workflow
addyosmani
1366
200k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
Visualization
eitanlees
146
15k
Why Our Code Smells
bkeepers
PRO
335
57k
A Modern Web Designer's Workflow
chriscoyier
693
190k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
29
2k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
Designing for humans not robots
tammielis
250
25k
Building Applications with DynamoDB
mza
91
6.1k
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