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
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
740
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
20241031_AWS_生成AIハッカソン_GenMuck
tsumita
0
100
CI/CDやテスト自動化の開発プロジェクトへの適用
megascus
3
730
日経電子版におけるリアルタイムレコメンドシステム開発の事例紹介/nikkei-realtime-recommender-system
yng87
1
460
Figma Dev Modeで進化するデザインとエンジニアリングの協働 / figma-with-engineering
cyberagentdevelopers
PRO
1
420
VPC間の接続方法を整理してみた #自治体クラウド勉強会
non97
1
570
CAMERA-Suite: 広告文生成のための評価スイート / ai-camera-suite
cyberagentdevelopers
PRO
3
260
Autify Company Deck
autifyhq
1
39k
「最高のチューニング」をしないために / hack@delta 24.10
fujiwara3
20
3.3k
オーティファイ会社紹介資料 / Autify Company Deck
autifyhq
9
120k
Vueで Webコンポーネントを作って Reactで使う / 20241030-cloudsign-vuefes_after_night
bengo4com
4
2.5k
初心者に Vue.js を 教えるには
tsukuha
5
380
新卒1年目が向き合う生成AI事業の開発を加速させる技術選定 / ai-web-launcher
cyberagentdevelopers
PRO
7
1.5k
Featured
See All Featured
Practical Orchestrator
shlominoach
186
10k
10 Git Anti Patterns You Should be Aware of
lemiorhan
654
59k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
41
2.1k
Why Our Code Smells
bkeepers
PRO
334
57k
Building Adaptive Systems
keathley
38
2.2k
Gamification - CAS2011
davidbonilla
80
5k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.6k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.4k
Designing for Performance
lara
604
68k
Facilitating Awesome Meetings
lara
49
6k
What's in a price? How to price your products and services
michaelherold
243
12k
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