Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
So You Think You Can Composer?
Search
Coen Jacobs
March 29, 2016
58
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
So You Think You Can Composer?
Coen Jacobs
March 29, 2016
More Decks by Coen Jacobs
See All by Coen Jacobs
The Love-Hate Relationship Between Composer and WordPress (WordCamp Netherlands 2015)
coenjacobs
0
270
State of WordPress and eCommerce (WordPress meetup Nijmegen september 2015)
coenjacobs
0
390
Featured
See All Featured
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
1
600
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
740
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
590
How to make the Groovebox
asonas
2
2.5k
Building Adaptive Systems
keathley
44
3.3k
Everyday Curiosity
cassininazir
0
390
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
From π to Pie charts
rasagy
1
430
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
920
KATA
mclloyd
PRO
35
16k
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4.2k
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
1k
Transcript
So You Think You Can Composer? Coen Jacobs - WordPress
meetup Enschede @CoenJacobs - #wpm053
Composer
None
Compooooooser
Composer PHP package manager
“Composer is a tool for dependency management in PHP. It
allows you to declare the libraries your project depends on and it will manage (install/ update) them for you.”
So what does it do?
The Composer magic • Manage project dependencies • Powerful autoloader
Install your dependencies Defined in composer.json Version locked in composer.lock
Where to find these dependencies?
None
Custom version control GitHub repositories (private or public) for example
"require": { "laravel/framework": “5.2.*" } What is a dependency?
"laravel/framework": "5.2.*" vendor package version constraint
/vendor/laravel/framework Installed in /vendor
Everything gets autoloaded Include /vendor/autoload.php
Manual loading require_once 'classes/class-core.php'; require_once 'includes/wpseo-local-functions.php'; require_once 'includes/ajax-functions.php'; require_once 'classes/class-core.php';
require_once 'classes/class-admin.php'; require_once 'classes/class-admin-wrappers.php'; require_once 'classes/class-metaboxes.php'; require_once 'classes/class-frontend.php'; require_once 'classes/class-storelocator.php'; require_once 'classes/class-taxonomy.php'; require_once 'widgets/widget-show-address.php'; require_once 'widgets/widget-show-map.php'; require_once 'widgets/widget-show-openinghours.php'; require_once 'widgets/widget-storelocator-form.php';
Autoloading require_once 'vendor/autoload.php';
Autoload bonus • Only include files when class is used
• Includes files from your dependencies • … and files from the dependencies of your dependencies… • … and so on
Ok, so what about WordPress?
WordPress is a dependency So are plugins and themes
WordPress itself is not a Composer package
None
"require": { "johnpbloch/wordpress": "4.4.*" } WordPress as a dependency
But wait… WordPress will always be WordPress
We need a couple tricks
WordPress is started from the index.php file
… so we can’t install it in /vendor/wordpress
That index.php does this require( dirname(__FILE__ ) . '/wp-blog-header.php' );
Custom install paths for EVERYTHING
None
"extra": { "wordpress-install-dir": "wp", } Define install directory in composer.json
None
“You can move the wp-config.php file to the directory above
your WordPress install. This means for a site installed in the root of your webspace, you can store wp-config.php outside the web-root folder.”
Custom index.php define( 'WP_USE_THEMES', true ); require( 'wp/wp-blog-header.php' );
Project structure so far
Why would you do all this?
Advantages of Composer • Dependencies declared in a single file
• Installing and updating handled by a tool • Dependencies locked on specific versions • No third party code in version control
Version control magic • Add all Composer installed directories to
.gitignore • git clone && composer install
What about themes and plugins?
Default directories wp-content/plugins wp-content/themes
Custom directory constants in wp-config.php WP_CONTENT_DIR to /content PLUGINDIR to
/content/plugins
Installer paths "extra": { "wordpress-install-dir": "wp", "installer-paths": { "vendor/package": ["dir/dir/"],
} }
Themes and plugins need to have composer.json, require composer-installers AND
have their package type set correctly Couple constraints
… but there is help!
None
Install Akismet via wpackagist.org "installer-paths": { "content/plugins/akismet/": ["wpackagist-plugin/akismet"], }
Full project structure
Full project structure and ignores
What about custom plugins and themes?
Ideally, in a separate repository And load them as any
other dependency
Ignore parts of /content
Or exclude custom work in the ignored folder
Great dependency management and clean version control
Can we make this easier?
Sure!
None
Plug & play Composer setup, boilerplate project
This is where the fun begins…
What about plugin dependencies? Maybe some use the same PHP
library?
WordPress SUCKS when it comes to dependency management Composer helps,
but doesn’t solve everything
None
Not a Composer problem
… it’s a WordPress problem
So what’s wrong?
Plugin dependency conflict • Plugin A bundles version 1.0.0 of
X • Plugin B bundles version 2.0.0 of X • Versions are not compatible with each other, but can be used at the same time
Which plugin loads first?
Alphabetical, basically wp-content/plugins/a-plugin (bundled version 2.0.0) wp-content/plugins/z-plugin (bundled version 1.0.0)
WordPress SUCKS when it comes to dependency management This can
bite you, with or without Composer
Custom namespaces can solve this - kinda
None
Downsides to workarounds • Maintenance heavy for updates • Defeats
purpose of package managers • … but it probably is the only way
Full site management via Composer solves this in 99.9% of
the cases But it doesn’t help plugin developers…
Maybe, one day…
None
Keep on dreaming :) Coen Jacobs - @CoenJacobs - coenjacobs.me
#wpm053