How do I hook into Composer events using scripts?
Jul 26, 2025 am 07:52 AMTo run custom logic, use Composer scripts, first add scripts blocks in composer.json and bind events. The main steps are: 1. Understand the built-in events of Composer such as pre-install-cmd, post-install-cmd, etc.; 2. Set scripts block, specify commands or script arrays as needed, and execute them in order; 3. Use class processing to achieve more complex control, and receive Event and IO interfaces through static methods; 4. Manually run Composer commands during testing and check the output and return code to ensure that the script works normally.
When you want to run custom logic during Composer operations—like installing, updating, or dumping autoloads—you can hook into Composer events using the scripts
section in your composer.json
. This lets you automate tasks like clearing caches, running build steps, or triggering notifications based on what Composer is doing.
Here's how to actually make use of Composer scripts and respond to its built-in events.
Understanding Composer Events and Script Names
Composer fires off a number of predefined events during its lifecycle. These include:
-
pre-install-cmd
-
post-install-cmd
-
pre-update-cmd
-
post-update-cmd
-
pre-autoload-dump
-
post-autoload-dump
-
post-package-install
-
post-package-update
-
post-root-package-install
Each of these script names corresponds to an event that happens during a Composer command. For example, post-install-cmd
runs after composer install
, and post-update-cmd
runs after composer update
.
You don't have to use all of them, but knowing which ones exist helps you choose the right moment to inject your own logic.
Setting Up Scripts in composer.json
To hook into any of these events, just add a scripts
block to your composer.json
. Here's a basic example:
{ "scripts": { "post-install-cmd": [ "echo 'Composer install complete!'", "php bin/notify_dev_team.php" ], "post-update-cmd": "php bin/clear_cache.php" } }
In this case:
- After
composer install
, it prints a message and triggers a notification script. - After
composer update
, it clears the cache by calling a PHP file.
You can also call shell scripts, artisan commands (in Laravel), or even npm scripts if needed. Just make sure the paths are correct and executable.
A few tips:
- You can list multiple commands per event as an array.
- Each command is executed in order.
- If one fails, Composer will stop unless you prepend
@
to ignore failures:@php something-that-might-fail.php
Using Event Classes for More Control
If you need more flexibility or want to pass parameters from Composer to your code, you can use a class-based handler instead of just a shell command.
For example:
{ "scripts": { "post-install-cmd": "My\\Script\\Handler::onPostInstall" } }
This assumes you've written a class My\Script\Handler
with a static method onPostInstall()
. The method receives two arguments:
- An instance of
Composer\Script\Event
- The Composer IO interface (
Composer\IO\IOInterface
)
This gives you access to things like package names, options passed to the command, and allows for more robust scripting inside your app context.
Make sure your autoloader includes the namespace where your handler lives so Composer can find and execute it.
Testing and Debugging Your Scripts
It's easy to break things with custom scripts, especially when deploying to production or CI environments. To test locally:
- Run
composer install
orupdate
manually and watch output. - Use
echo
or logging to see if your script ran. - Check return codes—if a script exits with non-zero status, Composer treats it as a failure.
Also, keep in mind:
- Scripts are inherited by dependent packages, so be careful not to override someone else's without good reason.
- Avoid long-running processes unless necessary; Composer expects scripts to finish quickly.
Basically that's it. Hooking into Composer events via scripts is straightforward once you know the event names and how to structure the composer.json
. Whether you're calling simple shell commands or full PHP classes, it's a powerful way to tie your project's behavior into dependency management.
The above is the detailed content of How do I hook into Composer events using scripts?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The essential Laravel extension packages for 2024 include: 1. LaravelDebugbar, used to monitor and debug code; 2. LaravelTelescope, providing detailed application monitoring; 3. LaravelHorizon, managing Redis queue tasks. These expansion packs can improve development efficiency and application performance.

The steps to build a Laravel environment on different operating systems are as follows: 1.Windows: Use XAMPP to install PHP and Composer, configure environment variables, and install Laravel. 2.Mac: Use Homebrew to install PHP and Composer and install Laravel. 3.Linux: Use Ubuntu to update the system, install PHP and Composer, and install Laravel. The specific commands and paths of each system are different, but the core steps are consistent to ensure the smooth construction of the Laravel development environment.

The main differences between Laravel and Yii are design concepts, functional characteristics and usage scenarios. 1.Laravel focuses on the simplicity and pleasure of development, and provides rich functions such as EloquentORM and Artisan tools, suitable for rapid development and beginners. 2.Yii emphasizes performance and efficiency, is suitable for high-load applications, and provides efficient ActiveRecord and cache systems, but has a steep learning curve.

Integrating Sentry and Bugsnag in Laravel can improve application stability and performance. 1. Add SentrySDK in composer.json. 2. Add Sentry service provider in config/app.php. 3. Configure SentryDSN in the .env file. 4. Add Sentry error report in App\Exceptions\Handler.php. 5. Use Sentry to catch and report exceptions and add additional context information. 6. Add Bugsnag error report in App\Exceptions\Handler.php. 7. Use Bugsnag monitoring

Article Summary: Yii Framework is an efficient and flexible PHP framework for creating dynamic and scalable web applications. It is known for its high performance, lightweight and easy to use features. This article will provide a comprehensive tutorial on the Yii framework, covering everything from installation to configuration to development of applications. This guide is designed to help beginners and experienced developers take advantage of the power of Yii to build reliable and maintainable web solutions.

Composer is a dependency management tool for PHP, and manages project dependencies through composer.json file. 1) parse composer.json to obtain dependency information; 2) parse dependencies to form a dependency tree; 3) download and install dependencies from Packagist to the vendor directory; 4) generate composer.lock file to lock the dependency version to ensure team consistency and project maintainability.

Integrating social media login in the Laravel framework can be achieved by using the LaravelSocialite package. 1. Install the Socialite package: use composerrequirelaravel/socialite. 2. Configure the service provider and alias: add relevant configuration in config/app.php. 3. Set API credentials: Configure social media API credentials in .env and config/services.php. 4. Write controller method: Add redirection and callback methods to handle social media login process. 5. Handle FAQs: Ensure user uniqueness, data synchronization, security and error handling. 6. Optimization practice:

Free and open source PHP Web Framework Yii Getting Started Guide Installation Prerequisites: PHP 7.2, Composer Create Application: composer create-project yiisoft/yii2-app-basic your-app-name Project Structure: Contains assets, config, controllers, models, runtime, views, web, etc. Create Controller: Create classes in the controllers directory, such as SiteController, define operation methods and create models: In models
