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
Fireside Chat
paigeccino
42
3.9k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Building Adaptive Systems
keathley
44
3k
Raft: Consensus for Rubyists
vanstee
141
7.5k
Bash Introduction
62gerente
615
210k
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
210
Designing for Performance
lara
611
70k
Reflections from 52 weeks, 52 projects
jeffersonlam
356
21k
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
1
3.6k
The Illustrated Children's Guide to Kubernetes
chrisshort
51
52k
Designing Powerful Visuals for Engaging Learning
tmiket
1
400
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
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