SlideShare ist ein Scribd-Unternehmen logo
1 von 58
Downloaden Sie, um offline zu lesen
Modernizing Your Development
Workflow Using Composer
Jeremy Ward
WordCamp Sacramento
September 21, 2019
Slides Available at:

bit.ly/wcsac-2019-composer
@_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"
@_jmichaelward jmichaelward.com
WordPress's famous
5-minute install.
Ugh.
@_jmichaelward jmichaelward.com
Better:
Installing WordPress via WP-CLI
• wp core download

• wp config create --dbuser=[user] --dbpass=[pass] --dbname=[name] --
dbhost=[host] --skip-check

• wp db create

• wp core install --url=[siteurl] --title=[title] --admin_user=[user] --admin_email=[email]
We could script this...
@_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.
@_jmichaelward jmichaelward.com
composer create-project jmichaelward/wp-starter wordcamp
@_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
@_jmichaelward jmichaelward.com
Composer's Purpose
• Make it easy to:

• install third-party code

• share your code
@_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
Defining Your Project
@_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
@_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
Installing Dependencies
@_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.
@_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
@_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
@_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
@_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
@_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.
@_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:
@_jmichaelward jmichaelward.com
composer install
vs.
composer update
*most of the time
Always* use composer install!
Discoverability
@_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

@_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)
@_jmichaelward jmichaelward.com
Command: composer search
Utility in WordPress Development
Class autoloading
@_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).
Custom Packages and Scripts
@_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).
@_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]
@_jmichaelward jmichaelward.com
Plugins
• Add development requirements (e.g., PHPUnit)

• Expose custom scripts/utilities

• Bash, PHP single-scripts

• WP-CLI extensions

• Full custom packages
@_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)
Defining installation routes
@_jmichaelward jmichaelward.com
Custom Installers
• Define where certain
package types should
get installed

• wpackagist handles
mu-plugins, plugins,
and themes by default.

• You can create your
own custom installers
(grouped editor blocks,
anyone?)
"require": {

"composer/installers": "~1.0"

},

"extra" : {

"wordpress-install-dir": "wp",

"installer-paths" : {

"wp-content/plugins/{$name}/": ["type:wordpress-plugin"],

"wp-content/mu-plugins/{$name}/": ["type:wordpress-muplugin"],

"wp-content/themes/{$name}/" : ["type:wordpress-theme"]

}

}
@_jmichaelward jmichaelward.com
Version Control
@_jmichaelward jmichaelward.com
.gitignore
• /vendor/

• /wp-content/uploads/

• /wp-content/plugins/*

• !/wp-content/plugins/my-one-off-project-plugin/

• /wp-content/themes/

• !/wp-content/themes/my-custom-project-theme/
Composer & WordPress: Caveats
@_jmichaelward jmichaelward.com
Same Library, Different Version
• https://github.com/TypistTech/imposter-plugin

• https://github.com/coenjacobs/mozart

• https://github.com/humbug/php-scoper
@_jmichaelward jmichaelward.com
Class Autoloading in Multisite
@_jmichaelward jmichaelward.com
Deployments
@_jmichaelward jmichaelward.com
Auto Updates
Other Composer Commands
@_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
@_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.
@_jmichaelward jmichaelward.com
Command: composer remove
• Remove a package from the filesystem and composer.json.
@_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.
@_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.
Summary
@_jmichaelward jmichaelward.com
Questions?
• Email: jeremy@jmichaelward.com

• Twitter: @_jmichaelward

• Web: https://jmichaelward.com

• Work: https://webdevstudios.com
Project Repository:
https://github.com/jmichaelward/wordcamp-example-wp-composer

Weitere ähnliche Inhalte

Was ist angesagt?

Dependency management with Composer
Dependency management with ComposerDependency management with Composer
Dependency management with Composer
Jason Grimes
 
Composer
ComposerComposer
Composer
cmodijk
 
Composer for Busy Developers - php|tek13
Composer for Busy Developers - php|tek13Composer for Busy Developers - php|tek13
Composer for Busy Developers - php|tek13
Rafael Dohms
 
Stress Free Deployment - Confoo 2011
Stress Free Deployment  - Confoo 2011Stress Free Deployment  - Confoo 2011
Stress Free Deployment - Confoo 2011
Bachkoutou Toutou
 

Was ist angesagt? (20)

Dependency management with Composer
Dependency management with ComposerDependency management with Composer
Dependency management with Composer
 
Installing AtoM with Ansible
Installing AtoM with AnsibleInstalling AtoM with Ansible
Installing AtoM with Ansible
 
Varying WordPress Development Environment WordCamp Columbus 2016
Varying WordPress Development Environment WordCamp Columbus 2016Varying WordPress Development Environment WordCamp Columbus 2016
Varying WordPress Development Environment WordCamp Columbus 2016
 
Composer
ComposerComposer
Composer
 
Introduction to WP-CLI: Manage WordPress from the command line
Introduction to WP-CLI: Manage WordPress from the command lineIntroduction to WP-CLI: Manage WordPress from the command line
Introduction to WP-CLI: Manage WordPress from the command line
 
Debugging webOS applications
Debugging webOS applicationsDebugging webOS applications
Debugging webOS applications
 
Handling 10k requests per second with Symfony and Varnish - SymfonyCon Berlin...
Handling 10k requests per second with Symfony and Varnish - SymfonyCon Berlin...Handling 10k requests per second with Symfony and Varnish - SymfonyCon Berlin...
Handling 10k requests per second with Symfony and Varnish - SymfonyCon Berlin...
 
Internals - Exploring the webOS Browser and JavaScript
Internals - Exploring the webOS Browser and JavaScriptInternals - Exploring the webOS Browser and JavaScript
Internals - Exploring the webOS Browser and JavaScript
 
Php Dependency Management with Composer ZendCon 2016
Php Dependency Management with Composer ZendCon 2016Php Dependency Management with Composer ZendCon 2016
Php Dependency Management with Composer ZendCon 2016
 
Write php deploy everywhere tek11
Write php deploy everywhere   tek11Write php deploy everywhere   tek11
Write php deploy everywhere tek11
 
Symfony Under Control by Maxim Romanovsky
Symfony Under Control by Maxim RomanovskySymfony Under Control by Maxim Romanovsky
Symfony Under Control by Maxim Romanovsky
 
Composer for Busy Developers - php|tek13
Composer for Busy Developers - php|tek13Composer for Busy Developers - php|tek13
Composer for Busy Developers - php|tek13
 
Cfml features modern_coding
Cfml features modern_codingCfml features modern_coding
Cfml features modern_coding
 
Locking Down CF Servers
Locking Down CF ServersLocking Down CF Servers
Locking Down CF Servers
 
Instant ColdFusion with Vagrant
Instant ColdFusion with VagrantInstant ColdFusion with Vagrant
Instant ColdFusion with Vagrant
 
Stress Free Deployment - Confoo 2011
Stress Free Deployment  - Confoo 2011Stress Free Deployment  - Confoo 2011
Stress Free Deployment - Confoo 2011
 
Save Time by Managing WordPress from the Command Line
Save Time by Managing WordPress from the Command LineSave Time by Managing WordPress from the Command Line
Save Time by Managing WordPress from the Command Line
 
Composer
ComposerComposer
Composer
 
Composer
ComposerComposer
Composer
 
Developing High Performance and Scalable ColdFusion Application Using Terraco...
Developing High Performance and Scalable ColdFusion Application Using Terraco...Developing High Performance and Scalable ColdFusion Application Using Terraco...
Developing High Performance and Scalable ColdFusion Application Using Terraco...
 

Ähnlich wie WordCamp Sacramento 2019: Modernizing Your Development Workflow Using Composer

Gnubs pres-foss-cdac-sem
Gnubs pres-foss-cdac-semGnubs pres-foss-cdac-sem
Gnubs pres-foss-cdac-sem
Sagun Baijal
 
walkmod: An open source tool for coding conventions
walkmod: An open source tool for coding conventionswalkmod: An open source tool for coding conventions
walkmod: An open source tool for coding conventions
walkmod
 

Ähnlich wie WordCamp Sacramento 2019: Modernizing Your Development Workflow Using Composer (20)

WordPress Development Environments
WordPress Development Environments WordPress Development Environments
WordPress Development Environments
 
Mastering composer
Mastering composerMastering composer
Mastering composer
 
Gnubs-pres-foss-cdac-sem
Gnubs-pres-foss-cdac-semGnubs-pres-foss-cdac-sem
Gnubs-pres-foss-cdac-sem
 
Gnubs pres-foss-cdac-sem
Gnubs pres-foss-cdac-semGnubs pres-foss-cdac-sem
Gnubs pres-foss-cdac-sem
 
Docker advance topic
Docker advance topicDocker advance topic
Docker advance topic
 
walkmod: An open source tool for coding conventions
walkmod: An open source tool for coding conventionswalkmod: An open source tool for coding conventions
walkmod: An open source tool for coding conventions
 
Managing a WordPress Site as a Composer Project by Rahul Bansal @ WordCamp Na...
Managing a WordPress Site as a Composer Project by Rahul Bansal @ WordCamp Na...Managing a WordPress Site as a Composer Project by Rahul Bansal @ WordCamp Na...
Managing a WordPress Site as a Composer Project by Rahul Bansal @ WordCamp Na...
 
Docker advance1
Docker advance1Docker advance1
Docker advance1
 
Wordpress development: A Modern Approach
Wordpress development:  A Modern ApproachWordpress development:  A Modern Approach
Wordpress development: A Modern Approach
 
Composer
ComposerComposer
Composer
 
Leveraging Composer in Existing Projects
Leveraging Composer in Existing ProjectsLeveraging Composer in Existing Projects
Leveraging Composer in Existing Projects
 
Composer JSON kills make files
Composer JSON kills make filesComposer JSON kills make files
Composer JSON kills make files
 
Composer & Drupal
Composer & DrupalComposer & Drupal
Composer & Drupal
 
Docker Compose to Production with Docker Swarm
Docker Compose to Production with Docker SwarmDocker Compose to Production with Docker Swarm
Docker Compose to Production with Docker Swarm
 
Composer
ComposerComposer
Composer
 
Extracting twitter data using apache flume
Extracting twitter data using apache flumeExtracting twitter data using apache flume
Extracting twitter data using apache flume
 
Deep Dive into the AOSP
Deep Dive into the AOSPDeep Dive into the AOSP
Deep Dive into the AOSP
 
Create your very own Development Environment with Vagrant and Packer
Create your very own Development Environment with Vagrant and PackerCreate your very own Development Environment with Vagrant and Packer
Create your very own Development Environment with Vagrant and Packer
 
Varying wordpressdevelopmentenvironment wp-campus2016
Varying wordpressdevelopmentenvironment wp-campus2016Varying wordpressdevelopmentenvironment wp-campus2016
Varying wordpressdevelopmentenvironment wp-campus2016
 
Akeebalize Your Extensions
Akeebalize Your ExtensionsAkeebalize Your Extensions
Akeebalize Your Extensions
 

Kürzlich hochgeladen

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Kürzlich hochgeladen (20)

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 

WordCamp Sacramento 2019: Modernizing Your Development Workflow Using Composer