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
So You Think You Can Composer?
Search
Coen Jacobs
March 29, 2016
53
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
260
State of WordPress and eCommerce (WordPress meetup Nijmegen september 2015)
coenjacobs
0
370
Featured
See All Featured
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
210
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
34
2.8k
エンジニアに許された特別な時間の終わり
watany
107
250k
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
450
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
200
Ten Tips & Tricks for a 🌱 transition
stuffmc
0
150
Are puppies a ranking factor?
jonoalderson
1
3.7k
Measuring & Analyzing Core Web Vitals
bluesmoon
9
870
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
320
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
1.2k
RailsConf 2023
tenderlove
30
1.5k
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