Skip to Content
Course content

171: Composer Scripts and Autoload Optimization

Click on the "Edit" button in the top corner of the screen to edit your slide content.

A few years ago, I was reviewing a junior dev's deployment process for a mid-sized e-commerce API. I watched him manually run a sequence of five different terminal commands—PHPUnit for tests, PHPCS for styling, a custom cache-clear script, and a manual directory cleanup—every single time he pushed to staging. It was a ritual. He’d sometimes miss the third command, the tests would pass, but the app would crash because of a stale cache. Then, we looked at the production logs and noticed a surprising amount of filesystem I/O. The app was spending an unnecessary amount of time just searching for where classes lived on the disk. He was doing things the hard way, and the server was paying the price.

Turning Rituals into Composer Scripts

You shouldn't be memorizing long strings of terminal commands or keeping a readme.txt file open just to remember how to run your test suite. Composer has a scripts section in the composer.json file that lets you define shortcuts for these tasks. It's essentially a task runner built right into your dependency manager.

I usually set up my scripts to handle the "boring" parts of the dev cycle. For example, instead of typing ./vendor/bin/phpunit and ./vendor/bin/phpcs separately, I'll map them to simple aliases. Here is how that looks in practice:

{
    "scripts": {
        "test": "phpunit",
        "lint": "phpcs src tests",
        "analyze": "phpstan analyze src",
        "check-all": [
            "@lint",
            "@analyze",
            "@test"
        ]
    }
}

Notice the @ symbol in check-all. That's a key detail—it tells Composer to run another script defined within the same file. Now, instead of five commands, I just run composer check-all. It keeps the team aligned because the "correct" way to test the project is codified in the JSON file, not stored in one person's bash history.

The Hidden Cost of PSR-4

Now, let's talk about that filesystem lag I mentioned. We all love PSR-4 because it allows us to add new classes without updating a giant list of files. But there's a trade-off. In a standard PSR-4 setup, when you call new User(), Composer has to check the filesystem: "Does src/User.php exist? No? How about src/Models/User.php?"

On your local machine, with a fast SSD and a small project, you'll never notice this. But on a production server with thousands of classes and high traffic, those thousands of file_exists checks add up. It's a death by a thousand cuts. This is where autoload optimization comes in.

Flattening the Map for Production

When you run composer dump-autoload -o (or --optimize), Composer stops guessing. It scans your entire application and builds a massive associative array—a Class Map—that maps every single class name directly to a file path. Instead of searching the disk, PHP just looks at the map and goes straight to the file.

I always include this in my CI/CD pipeline. If you aren't optimizing your autoloader in production, you're leaving free performance on the table. You can also use --classmap-authoritative, which tells Composer: "If a class isn't in the map, don't even bother looking at the filesystem; it definitely doesn't exist." This is the fastest possible configuration, though it means you can't add new classes at runtime (which, in a modern app, you shouldn't be doing anyway).

Just remember: you only do this during deployment. You don't want to run -o during active development, because every time you create a new class, you'd have to re-run the command to make the autoloader aware of it.




📋 Practical Task

Hardening the "PaymentGateway" Project Pipeline

You've inherited a project called PaymentGateway. The current workflow is messy, and the production environment is sluggish. Your task is to modernize the composer.json and optimize the loading process.

  • Part 1: Open composer.json and add a scripts section. Create a shortcut called "verify" that runs phpunit and a shortcut called "style" that runs phpcs. Then, create a master script called "ci" that executes both verify and style.
  • Part 2: Using the terminal, simulate a production deployment. Run the specific Composer command that generates an optimized class map to eliminate PSR-4 filesystem lookups.
  • Part 3: Verify that the vendor/composer/autoload_classmap.php file has been populated with the actual paths to the classes in the src/ directory.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.