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
810
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
330
Demystifying SPDY and HTTP/2
jklein
1
550
Demystifying SPDY and HTTP/2
jklein
2
970
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
740
Northeastern Lunch and Learn
jklein
0
200
Profiling PHP With XHProf
jklein
1
970
HubSpot Tech Talk - DIY Synthetic
jklein
0
230
Other Decks in Technology
See All in Technology
クレジットカードを製造する技術
yutadayo
83
49k
YAPC::Hakodateの映像記録を支える技術
godan
4
390
SageMaker学習のツボ / The Key Points of Learning SageMaker
cmhiranofumio
0
220
Amplify Gen 2ではじめる 生成AIアプリ開発入門
tsukuboshi
0
280
Graph Database と Generative AI の素敵な関係
oracle4engineer
PRO
14
2.4k
Oracle Database 23ai 新機能#4 Application Continuity
oracle4engineer
PRO
0
130
軽いノリで"自動化"に取り組んではいけないという話
tetsuyaooooo
1
620
WSUSが非推奨に!? Windowsの更新管理を改めて勉強する!
ebibibi
0
360
Perlで始めるeBPF: 自作Loaderの作り方 / Getting started with eBPF in Perl_How to create your own Loader
takehaya
1
1k
ラブグラフ紹介資料 〜プロダクト解体新書〜 / Lovegraph Product Deck
lovegraph
0
14k
Slackbot × RAG で実現する社内情報検索の最適化
howdy39
2
350
普通の Web エンジニアのための様相論理入門 #yapcjapan / YAPC Hakodate 2024
ytaka23
7
1.6k
Featured
See All Featured
No one is an island. Learnings from fostering a developers community.
thoeni
19
2.9k
Practical Orchestrator
shlominoach
186
10k
Designing Experiences People Love
moore
138
23k
Making the Leap to Tech Lead
cromwellryan
131
8.9k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
228
52k
YesSQL, Process and Tooling at Scale
rocio
167
14k
Optimizing for Happiness
mojombo
375
69k
Faster Mobile Websites
deanohume
304
30k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
25
1.7k
Teambox: Starting and Learning
jrom
132
8.7k
Infographics Made Easy
chrislema
239
18k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.2k
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