Composer for Beginners: Save time and focus on what matters

Say goodbye to waisting tons of time working with manual dependency resolution and welcome a less time-consuming PHP workflow powered by Composer.

tl;drGitHub

It is really hard to write code that is not dependent on anyone but us. Sometimes we need to use trusted external libraries just to speed up the work and we need to find a way to manage them somehow. So it's time to talk about Composer and the way in which it allows me to save a lot of time and focus on what matters.

If you are WordPress plugins or custom themes author and you've never heard about it, I insist you check out this article and check how it can help you too! I'll try to answer what is that, help you decide if you really need this and show how it can be used.


What is that?

Composer is a tool for dependency management in PHP. It allows me to declare the libraries my project depends on and it will manage them for me. It provides many useful features but I'll describe only the most important ones for me.

Life without Composer

Dependency Management

Imagine you're planning to bake a cake, and you need specific ingredients like flour, sugar and eggs. Without Composer, you would have to go to different stores, search for each ingredient, and bring them back to your kitchen. Boring and time-consuming.

Composer is like your personal assistant. You provide a list of ingredients (dependencies) you need in a shopping list (composer.json), then it goes to a central supermarket (Packagist), fetches the required ones, checks their compatibility, and brings them to your kitchen (codebase). Sounds delicious.

Autoloading

Imagine a doctor and nurse performing a medical operation. The doctor communicates the list of required tools that the nurse carefully organizes to be accessed when needed. During the operation, the doctor doesn't need to go and search for them. They just ask for a specific tool and the nurse gives it.

That's how I see Composer's autoloading feature. When a class is needed, it loads the required file and makes the class available for use. Just like the nurse providing the necessary tools to the doctor. Sounds perfect!

Life with Composer


Do you need this?

Let's assume that I want to use mPDF library in my application for generating PDF files.

Manual

Manually, I'd have to download the source to my codebase, download all the dependency dependencies, store them in the repository and load all the required classes manually. What classes are needed? I also need to figure it out manually. Installation and version management also must be handled manually for each single dependency.

require FM_PATH . '/inc/mpdf/MPDF.php';
require FM_PATH . '/inc/fpdi/FPDI.php';
// And many more...

$pdf = new \Mpdf\Mpdf();

Sounds like a lot of inefficient work. Even the authors write the following in the docs:

Generally, there is no real reason why you couldn’t use Composer (...) If you do not want to use Composer anyway, prepare to do a significant amount of work that Composer would have done for you.

Automatic

I just need to run composer require mpdf/mpdf in the terminal and Composer takes care of everything. I can immediately use the tool in my application without struggling with manual work.

composer require mpdf/mpdf
$pdf = new \Mpdf\Mpdf();

Need to switch dependency version? It can be done with one command. A huge repository? That's not the problem because external code is excluded. Dependencies are handled by composer.json. I just need to run one command and everything is handled.


How to use it?

Composer is a pretty simple tool that should not bring any understanding issues, but If you want to know more, I insist to check out the basic guide in the official docs.

Installation

The proces is described in the official docs, but I'm lazy so I use Homebrew. I just run brew install composer in my terminal and that's all.

brew install composer

Initialization

Composer works based on the config provided in the composer.json file placed in the project's root. So I need to create it manually, or by using composer init command.

{
    "name": "tentypdev/footmate",
    "description": "FootMATE Theme",
    "license": "GPLv3",
    "autoload": {
        "psr-4": {
            "FM\\": "app/"
        }
    },
    "authors": [
        {
            "name": "tentyp.dev",
            "email": "tentypdev@gmail.com"
        }
    ],
    "require": {
        "php": "^7.4|^8.0"
    }
}

By default, it loads dependencies from vendor directory, so I additionaly need to include my code namespace in the autoloading process (lines 5-9). In simple words, autoloader will search for FM namespace files in the app directory.

Usage

Once the configuration is done, I run composer install to install all the dependencies and create autoloader that, I'll include into my app. I'm wrapping the autoloader in the file_exists function to show notice about required initialization if not already done.

composer install
$composer = FM_PATH . '/vendor/autoload.php';

if (! file_exists($composer)) {
    wp_die(__('Error locating autoloader. Please run <code>composer install</code>.', 'fm'));
}

require $composer;

Ignoring

Dependencies are installed in the vendor directory based on the composer.json config so there is no need to include this directory in the repository. I add the the vendor directory to .gitignore.

vendor

Summary

I have no time and I'm lazy. I don't want to spend a lot of time working with things that are boring and repetative. My client's also want to pay less, and that's possible only when implementations take less time. So Composer is just natural and default choice. It saves tons of hours and allows me to focus on things that truly maters.

I won't be forcing you to using this, but If you just want to find a time for doing more interesting things instead of struggling with manual boring work, just try it on your own. You won't regret this.

Feedback

How satisfied you are after reading this article?