We’ve been hearing for years about WordPress’s famous 5-minute install. The plugin ecosystem makes sites infinitely extensible, and WP-CLI provides additional flexibility for the platform. What if we introduced another modern tool to the mix? In this session, I will discuss Composer, a command-line utility used to easily pull in code that your project needs, including WordPress itself.
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
WordCamp Sacramento 2019: Modernizing Your Development Workflow Using Composer
1. Modernizing Your Development
Workflow Using Composer
Jeremy Ward
WordCamp Sacramento
September 21, 2019
Slides Available at:
bit.ly/wcsac-2019-composer
2. @_jmichaelward jmichaelward.com
Hi, I'm Jeremy.
• Senior Backend Engineer at WebDevStudios
• Student of Software Architecture
• Author of OOPS-WP
• Board Game Enthusiast
• Stand-up Comedy Fan
• "I'm from St. Paul"
7. @_jmichaelward jmichaelward.com
Enter Composer
• Like Yarn/NPM in JavaScript, but for PHP.
• Define packages you need per-project.
• Version control only code specific to your project.
• Control via the command line.
11. @_jmichaelward jmichaelward.com
Why I ❤ Composer
• Install publicly-available PHP libraries, packages, frameworks, & more
• Host your own packages in public and private repos
• Ship packages with custom utility scripts
• Define installation routes based on package types
• Document information about your project
• Version control only the code you're working on
• Easily implement class autoloading
13. @_jmichaelward jmichaelward.com
How Do I Get It?
• Instructions: https://getcomposer.org/download/
• System Requirements:
• PHP 5.3.2+
• Some PHP settings and compile flags (Composer will let you know)
• Can install globally or locally on a per-project basis. For global installation:
• Copy and paste the installer commands in a terminal
• mv composer.phar /directory/in/your/path/composer
15. @_jmichaelward jmichaelward.com
composer.json
• composer.json lets you define tons of other info about your project, including:
• Project name, description, authors
• Requirements for developing the package, minimum PHP versions, PHP
extensions, etc
• Locations of source code, distributable code, wikis, support channels, and so on
16. @_jmichaelward jmichaelward.com
Command: composer init
• Interactively generate a composer.json file with values such as:
• name, description, author, type, homepage, require, require-dev, stability, license,
repository
• composer init -n generates an empty composer.json file
18. @_jmichaelward jmichaelward.com
composer.json - "require": { }
• Defines information about the dependencies for your project
• Can specify specific versions or ranges
• See https://getcomposer.org/doc/04-schema.md for more info.
19. @_jmichaelward jmichaelward.com
Command: composer install
• Installs dependencies defined in composer.lock (if exists) or composer.json
• Reads the require and require-dev sections of composer.json
• Uses the repositories section to know where to look
20. @_jmichaelward jmichaelward.com
composer.lock
• Generated after running composer install
• Locks dependencies to a specific version installed at that time
• Has no effect if the project is included as a dependency of another project - only
composer.json is read.
• Should be version controlled with your project
21. @_jmichaelward jmichaelward.com
Command: composer require
• Adds a dependency to the composer.json file and installs it
• composer require wpackagist-plugin/advanced-custom-fields
• Can specify branches or versions, and multiple packages at once
• composer require webdevstudios/wd_s:dev-master webdevstudios/oops-wp:^0.2
• Install development-only dependencies with the --dev flag
• composer require --dev phpunit/phpunit:^7.0
22.
23. @_jmichaelward jmichaelward.com
Command: composer require --dev
• --dev flag installs packages as development dependency
• Dependencies could include lots of useful items:
• PHP coding standards, PHPUnit, WP-CLI, custom migration scripts, deployment
tools
• If it helps develop the project, and it can be reused in other projects, consider
making it an installable package
• Use --no-dev for production deployments
24. @_jmichaelward jmichaelward.com
A Word About Versioning
• Versions and Constraints
• Packages may follow semantic versioning guidelines (https://semver.org)
• Determines version to install based on git tags
• Minimum stability:
• Filters packages by stability. Options include: stable, dev, alpha, beta, and RC.
25. @_jmichaelward jmichaelward.com
Command: composer update
• Updates dependencies defined in composer.json, then publishes those updated values to
composer.lock
• Installs newer versions of dependencies if defined. Removes any dependencies that are installed and
not present in composer.json.
• Can include a specific package name to update only that package
• e.g., composer update monolog/monolog
• Wildcards also accepted
• e.g., composer update symfony/*
Like composer install, with exceptions:
30. @_jmichaelward jmichaelward.com
"repositories": [ ]
• By default, Composer looks for public packages on packagist.org
• Can indicate the location of unlisted repositories in composer.json
• "repositories": [
{
"type": "composer",
"url": "https://wpackagist.org"
}
]
• Useful for both public and private repositories
31. @_jmichaelward jmichaelward.com
"repositories": [ ] cont'd
• If you work with a lot of private repositories, your list will get pretty long
• Can use a repository generator instead. Some options:
• Private Packagist (paid: https://packagist.com/)
• Satis (free and self-hosted: https://getcomposer.org/doc/articles/handling-
private-packages-with-satis.md)
35. @_jmichaelward jmichaelward.com
Autoloading
• Must require generated file: require_once __DIR__ . 'vendor/autoload.php';
• Supports PSR-4, PSR-0, classmap, and files autoloading
• Define mapping from namespaces to directories, e.g.:
{
"autoload": {
"psr-4": {"Acme": "src/"}
}
}
• Can add more namespaces via PHP after requiring the autoloader (good for unit
tests).
38. @_jmichaelward jmichaelward.com
WP-CLI Packages
• 2.0, released in July 2018, completely separated bundled commands from the
framework.
• Each command can now be installed as needed per-project.
• e.g., composer require wp-cli/entity-command:^2
• Custom commands work the same way: wp package install <name|git|path|zip>,
uses Composer behind the scenes (requires the package command).
39. @_jmichaelward jmichaelward.com
Scripting
• Define scripts directly in Composer, or install utility packages
• composer.json has a "bin" key for defining scripts to hoist to ./vendor/bin
• e.g. ./vendor/bin/wp-init from demo; ./vendor/bin/phpunit
• Other ways of triggering scripts:
• composer run-script [script-name]
• composer exec [script-name]
41. @_jmichaelward jmichaelward.com
Themes
• Define plugins required to power the theme
• During active development, track against dev-master to pull in the latest working
version (composer update my-company-name/my-theme-name)
47. @_jmichaelward jmichaelward.com
Same Library, Different Version
• https://github.com/TypistTech/imposter-plugin
• https://github.com/coenjacobs/mozart
• https://github.com/humbug/php-scoper
52. @_jmichaelward jmichaelward.com
Global keyword
• Adding the global keyword will install packages to your global installation of
composer.
• Good for things like WordPress Coding Standards
• composer global require wordpress-coding-standards/wpcs
53. @_jmichaelward jmichaelward.com
composer archive
• Can use this to create a ZIP backup of a package, including one you're presently
working on!
• Great for backups of WordPress plugins!
• composer archive wpackagist-plugin/advanced-custom-fields 5.6.8 --
format=zip
• Can also pass in a --file parameter to give it a specific name, and --dir to specify
where to save it. --format defaults to tar.
55. @_jmichaelward jmichaelward.com
Meta commands
• composer validate: validate a composer.json file
• composer dumpautoload: reconstruct the autoload file
• composer selfupdate: update Composer to the latest version!
• composer config: edit configuration settings
• composer clearcache: delete all content from cache
• composer diagnose: perform automated checks for common problems
• composer licenses: list name, version, and license of every installed package.
56. @_jmichaelward jmichaelward.com
More commands!
• show - show detailed information about a package
• composer show wpackagist-plugin/advanced-custom-fields
• outdated - show outdated packages. Alias of composer show -lo
• suggests - show suggested packages based on what's currently installed.
• depends - show which other packages depend on a current package.
• prohibits - show which packages are blocking a given package from being
installed.