Bright Nucleus PHP Composter
Git Hooks Management through Composer.
, (*1)
This is a Composer plugin that manages Git pre- & post-hooks through Composer dependencies. Actions you want to be executed on Git hooks can simply be require
d as --dev
dependencies, and will immediately become active on composer install
., (*2)
Introductory post: Adding Git Hooks Through Composer Dev-Dependencies, (*3)
Table Of Contents
Installation
You should not need to install this package directly. It should come as a dependency of a package that is of type php-composter-action
., (*4)
Existing PHP Composter Actions
-
PHP Composter PHPCS PSR2, (*5)
Check your PHP source code for PSR-2 compliance before committing., (*6)
-
PHP Composter Regular Expression, (*7)
Check your commit messages against a regular expression pattern, to enforce a commit message standard., (*8)
-
PHP Composter PHPCS WordPress, (*9)
Check your PHP source code for WordPress Coding Standards compliance before committing., (*10)
Thanks to Gabor Javorszky for contributing this action., (*11)
-
PHP Composter PHPCS Drupal, (*12)
Check your source code for Drupal Coding Standards compliance before committing., (*13)
Action by Nick Wilde., (*14)
-
PHP Composter PHPUnit (coming soon), (*15)
Run a PHPUnit test suite before committing., (*16)
-
PHP Composter PHP Syntax Checker (coming soon), (*17)
Validate the PHP syntax before committing., (*18)
Creating a New PHP Composter Action
To build a new PHP Composter action, you need to proceed as follows:, (*19)
- Create a Composer Package with a Valid Name
- Extend
BaseAction
class
- Add Public Methods
- Add the Class to Composer Autoloader
- Set the Composer Package Type to
php-composter-action
- Add
php-composter/php-composter
as a dependency
- Configure Git Hooks through Composer Extra key
Create a Composer Package with a Valid Name
Create a new Composer package with the following naming pattern: <vendor>/php-composter-<action intent>
, (*20)
Example:, (*21)
composer init --name php-composter/php-composter-example
Extend BaseAction
class
Create a new class that extends PHPComposter\PHPComposter\BaseAction
., (*22)
Example:, (*23)
<?php namespace PHPComposter\PHPComposterExample;
use PHPComposter\PHPComposter\BaseAction;
class Example extends BaseAction
{
// [...]
}
Add Public Methods
PHP Composter allows you to attach PHP methods to Git hooks. These methods need to be publicly accessible, so that they can be called by the PHP-Composter bootstrapping script., (*24)
Example:, (*25)
<?php
// [...]
class Example extends BaseAction
{
/**
* Example pre-commit action method.
*
* @var string $hook Name of the hook that was triggered.
* @var string $root Root folder in which the hook was triggered.
*/
public function preCommit()
{
echo 'Example Pre-Commit Hook ' . $this->hook . ' @ ' . $this->root . PHP_EOL;
}
}
Set the Composer Package Type to php-composter-action
You need to set the type of your Composer package in your composer.json
file to php-composter-action
., (*26)
Example:, (*27)
{
"name": "php-composter/php-composter-example",
"description": "PHP Composter Example.",
"type": "php-composter-action",
"[...]": ""
}
Add the Class to Composer Autoloader
Composer's Autoloader will be initialized for each Git hook, so make sure you've registered your newly created class correctly., (*28)
Example:, (*29)
{
"[...]": "",
"autoload": {
"psr-4": {
"PHPComposter\\PHPComposterExample\\": "src/"
}
},
"[...]": ""
}
Add php-composter/php-composter
as a dependency
You need to set the type of your Composer package in your composer.json
file to php-composter-action
., (*30)
Example:, (*31)
{
"[...]": "",
"require": {
"php-composter/php-composter": "^0.1",
},
"[...]": ""
}
Finally, add a new entry "php-composter-hooks"
to the extra
key in the package's composer.json
to attach each of your methods to a specific Git hook., (*32)
Example:, (*33)
{
"[...]": "",
"extra": {
"php-composter-hooks": {
"20.pre-commit": "PHPComposter\\PHPComposterExample\\Example::preCommit"
}
}
}
Hooks can either be "<priority>.<git-hook-name>"
, or just "<git-hook-name>"
., (*34)
In the above example, the priority is 20
. It defaults to 10 if omitted. Lower priority numbers get executed before higher ones., (*35)
Supported Git Hooks:
applypatch-msg
pre-applypatch
post-applypatch
pre-commit
prepare-commit-msg
commit-msg
post-commit
pre-rebase
post-checkout
post-merge
post-update
pre-auto-gc
post-rewrite
pre-push
Using Existing PHP Composter Actions in Your Projects
To use an existing PHP Composter Action in your projects, simply require
them as --dev
dependencies:, (*36)
composer require --dev php-composter/php-composter-example
Anyone using Composer to pull in the development dependencies will automatically have your PHP Composter Actions installed into their .git
., (*37)
Skipping Installation of PHP Composter Actions
In case you want to install your the Composer dependencies of a project without activating the PHP Composter system, you can run Composer with the --no-plugins
option:, (*38)
composer install --no-plugins
Contributing
All feedback / bug reports / pull requests are welcome., (*39)