Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
schlessera committed Mar 24, 2016
0 parents commit 946c280
Show file tree
Hide file tree
Showing 10 changed files with 716 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor/
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Change Log
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [0.1.0] -
### Added
- Initial release to GitHub.

[0.1.0]: https://github.com/brightnucleus/php-composter/compare/v0.0.0...v0.1.0
152 changes: 152 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# Bright Nucleus PHP Composter

### Git Hooks Management through Composer.

[![Latest Stable Version](https://poser.pugx.org/brightnucleus/php-composter/v/stable)](https://packagist.org/packages/brightnucleus/php-composter)
[![Total Downloads](https://poser.pugx.org/brightnucleus/php-composter/downloads)](https://packagist.org/packages/brightnucleus/php-composter)
[![Latest Unstable Version](https://poser.pugx.org/brightnucleus/php-composter/v/unstable)](https://packagist.org/packages/brightnucleus/php-composter)
[![License](https://poser.pugx.org/brightnucleus/php-composter/license)](https://packagist.org/packages/brightnucleus/php-composter)

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`.

## Table Of Contents

* [Installation](#installation)
* [Creating a New PHP Composter Action](#creating-a-new-php-composter-action)
* [Using Existing PHP Composter Actions in Your Projects](#using-existing-php-composter-actions-in-your-projects)
* [Skipping Installation of PHP Composter Actions](#skipping-installation-of-php-composter-actions)
* [Contributing](#contributing)

## 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`.

## Creating a New PHP Composter Action

To build a new PHP Composter action, you need to proceed as follows:

1. [Create a new Composer Package with Public Static Methods](#create-a-new-composer-package-with-public-static-methods)
2. [Add the Class to Composer Autoloader](#add-the-class-to-composer-autoloader)
3. [Set the Composer Package Type to `php-composter-action`](#set-the-composer-package-type-to-php-composter-action)
4. [Add `php-composter/php-composter` as a dependency](#add-php-composter-php-composter-as-a-dependency)
5. [Configure Git Hooks through Composer Extra key](#configure-git-hooks-through-composer-extra-key)

### Create a new Composer Package with Public Static Methods

PHP Composter allows you to attach PHP methods to Git hooks. These methods need to be public static methods, so that they can be called by PHP-Composter without requiring any specific context.

Example code:

```PHP
<?php namespace PHPComposter\PHPComposterExample;

class Example
{

/**
* Example pre-commit action method.
*/
public static function preCommit()
{
echo 'PHP Composter Example Pre-Commit Hook' . 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`:

```JSON
{
"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.

```JSON
{
[...]
"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`:

```JSON
{
[...]
"require": {
"php-composter/php-composter": "^0.1",
},
[...]
}
```

### Configure Git Hooks through Composer Extra key

Finally, use the `extra` key in the package's `composer.json` to attach each of your methods to a specific Git hook.

```JSON
{
[...]
"extra": {
"20.pre-commit": "PHPComposter\\PHPComposterExample\\Example::preCommit"
}
}
```

Hooks can either be `"<priority>.<git-hook-name>"`, or just `"<git-hook-name>"`.

In the above example, the priority is `20`. It defaults to 10 if omitted. Lower priority numbers get executed before higher ones.

#### 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:

```BASH
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`.

## 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:

```BASH
composer install --no-plugins
```

## Contributing

All feedback / bug reports / pull requests are welcome.
7 changes: 7 additions & 0 deletions bin/php-composter
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

hook=`basename "$0"`
php=`which php`
package_root=`pwd`
$php $package_root/.git/php-composter/includes/bootstrap.php $hook $package_root

24 changes: 24 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "php-composter/php-composter",
"description": "Git Hooks Management through Composer.",
"license": "MIT",
"authors": [
{
"name": "Alain Schlesser",
"email": "alain.schlesser@gmail.com"
}
],
"type": "composer-plugin",
"require": {
"php": ">=5.4",
"composer-plugin-api": "^1"
},
"autoload": {
"psr-4": {
"PHPComposter\\PHPComposter\\": "src/"
}
},
"extra": {
"class": "PHPComposter\\PHPComposter\\Plugin"
}
}
32 changes: 32 additions & 0 deletions includes/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Git Hooks Management through Composer.
*
* @package PHPComposter\PHPComposter
* @author Alain Schlesser <alain.schlesser@gmail.com>
* @license MIT
* @link http://www.brightnucleus.com/
* @copyright 2016 Alain Schlesser, Bright Nucleus
*/

use PHPComposter\PHPComposter\Paths;

global $argv;
$hook = $argv[1];
$root = $argv[2];

// Initialize Composer Autoloader.
if (file_exists($root . '/vendor/autoload.php')) {
require_once $root . '/vendor/autoload.php';
}

$config = include Paths::getPath('git_config');
if (array_key_exists($hook, $config)) {
$array = $config[$hook];
ksort($array);
foreach ($array as $methods) {
foreach ($methods as $method) {
$method();
}
}
}
57 changes: 57 additions & 0 deletions src/HookConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* Git Hooks Management through Composer.
*
* @package PHPComposter\PHPComposter
* @author Alain Schlesser <alain.schlesser@gmail.com>
* @license MIT
* @link http://www.brightnucleus.com/
* @copyright 2016 Alain Schlesser, Bright Nucleus
*/

namespace PHPComposter\PHPComposter;

class HookConfig
{

/**
* Internal storage of the configuration data.
*
* @var array
*
* @since 0.1.0
*/
protected static $config = array();

/**
* Add an entry to the configuration data.
*
* @since 0.1.0
*
* @param string $hook Name of the Git hook to add to.
* @param int $priority Optional. Priority of the hook. Defaults to 10.
* @param string $method Fully qualified method name to add.
*/
public static function addEntry($hook, $method, $priority = 10)
{
static::$config[$hook][$priority][] = $method;
}

/**
* Get the entries for a given Git hook.
*
* @since 0.1.0
*
* @param string $hook Git hook to retrieve the methods for.
*
* @return array Array of fully qualified method names. Empty array if none.
*/
public static function getEntries($hook)
{
if (array_key_exists($hook, static::$config)) {
return static::$config[$hook];
}

return array();
}
}

0 comments on commit 946c280

Please sign in to comment.