├── .gitignore ├── GitHooksLoader.php ├── README.md └── composer.json /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | Thumbs.db 3 | 4 | ## IDEs 5 | # PHPStorm 6 | /.idea/ 7 | # Sublime 8 | *.sublime-workspace 9 | 10 | /vendor/ 11 | 12 | # Let's keep some folders so people get into it without a full setup 13 | !.gitkeep -------------------------------------------------------------------------------- /GitHooksLoader.php: -------------------------------------------------------------------------------- 1 | setHook( $hook ); 32 | $this->setLocation( $location ); 33 | 34 | $this->prepare(); 35 | $this->load(); 36 | } 37 | 38 | /** 39 | * Grabs all files in a dir, 40 | * filters them to only include PHP files 41 | * where the file name contains the current hook name 42 | * and sorts them by the first integer found in the file name. 43 | */ 44 | public function prepare() 45 | { 46 | $this->fetched = $this->fetch( $this->location ); 47 | $this->fetched->rewind(); 48 | 49 | $this->filtered = $this->filter( $this->fetched ); 50 | $this->filtered->rewind(); 51 | 52 | $this->sorted = $this->sort( $this->filtered ); 53 | $this->sorted->rewind(); 54 | } 55 | 56 | /** 57 | * Loads the pre sorted files 58 | */ 59 | public function load() 60 | { 61 | foreach ( $this->sorted as $it ) 62 | include_once $this->sorted->current(); 63 | } 64 | 65 | /** 66 | * Sets the current git hook name 67 | * @param string $file 68 | * @return $this 69 | */ 70 | public function setHook( $file ) 71 | { 72 | $this->hook = pathinfo( 73 | $file, 74 | PATHINFO_FILENAME 75 | ); 76 | } 77 | 78 | /** 79 | * Sets the folder/location where the PHP Git Hook files are located 80 | * @param $location 81 | */ 82 | public function setLocation( $location ) 83 | { 84 | $this->location = $location; 85 | } 86 | 87 | /** 88 | * Fetches all files from the target folder 89 | * @param string $location 90 | * @return \FilesystemIterator 91 | */ 92 | public function fetch( $location ) 93 | { 94 | return new \FilesystemIterator( 95 | $location, 96 | FilesystemIterator::SKIP_DOTS 97 | | FilesystemIterator::FOLLOW_SYMLINKS 98 | ); 99 | } 100 | 101 | /** 102 | * Filters the file list to only include PHP files 103 | * where the file name contains the current hook name 104 | * @param \Iterator $files 105 | * @return \RegexIterator 106 | */ 107 | public function filter( \Iterator $files ) 108 | { 109 | return new \RegexIterator( 110 | $files, 111 | '/('.$this->hook.')[\w-]*?\.php/i' 112 | ); 113 | } 114 | 115 | /** 116 | * Sorts files by the first `int` in the file name 117 | * and adds them to a `\SplPriorityQueue`. 118 | * Files without a priority are ignored to allow easy disabling. 119 | * @param \Iterator $files 120 | * @return \SplPriorityQueue 121 | */ 122 | public function sort( \Iterator $files ) 123 | { 124 | $sorted = new \SplPriorityQueue(); 125 | foreach ( $files as $file ) 126 | { 127 | preg_match( 128 | '/\d+/', 129 | $files->current(), 130 | $priority 131 | ); 132 | if ( empty( $priority ) ) 133 | continue; 134 | 135 | $sorted->insert( 136 | $files->current(), 137 | $priority[0] 138 | ); 139 | } 140 | 141 | return $sorted; 142 | } 143 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Latest Stable Version](https://img.shields.io/packagist/v/wcm/git-php-hooks.svg?style=flat-square)](https://packagist.org/packages/wcm/git-php-hooks) 2 | [![Latest Git Release](https://img.shields.io/github/tag/wecodemore/GitPHPHooks.svg?style=flat-square)](https://github.com/wecodemore/GitPHPHooks/releases) 3 | [![Total Downloads](https://img.shields.io/packagist/dt/wcm/git-php-hooks.svg?style=flat-square)](https://packagist.org/packages/wcm/git-php-hooks) 4 | [![License](https://img.shields.io/packagist/l/wcm/git-php-hooks.svg?style=flat-square)](https://packagist.org/packages/wcm/git-php-hooks) 5 | [![GitHub Stars](https://img.shields.io/github/stars/wecodemore/GitPHPHooks.svg?style=flat-square)](https://github.com/wecodemore/GitPHPHooks/stargazers) 6 | 7 | 8 | ![Git Logo](http://i.imgur.com/qzvwT4C.png) 9 | 10 | # Git PHP Hooks 11 | 12 | Write your git hooks in PHP, organize them on a per project base and attach them automatically. 13 | 14 | ## Git Hooks 15 | 16 | > Hooks are little scripts you can place in `$GIT_DIR/hooks` directory to trigger action at certain points. 17 | 18 | There're two types of git hooks: 19 | 20 | 1. pre-push (runs client side) 21 | 1. post-push (runs server side) 22 | 23 | For more info on Git Hooks, please [take a look at the official docs](http://git-scm.com/docs/githooks) - they are quite good. 24 | 25 | ## How to 26 | 27 | It's really easy: 28 | 29 | 1. Add a folder to your project/repository. The name doesn't matter, as you have to specify 30 | it when triggering `GitPHPHooks`. The name in the following example is `'project-hooks'`. (Hint: Not having a name allows you to customize and organize it as you like. It also allows `git clone`ing into a project specific directory.) 31 | 1. Open your `.git/hooks` directory and add a new Git hook file. For example: `pre-commit` 32 | (without file extension). 33 | 1. Add a new PHP file to the newly created custom Git hooks folder (again, `'project-hooks'` in the example) that performs the task you want. 34 | 35 | That's it. 36 | 37 | All your Git hooks (inside `.git/hooks`) will have the same contents - only the target folder (`'project-hooks'`) name will (maybe) differ. 38 | 39 | ```php 40 | #!/usr/bin/env php 41 | We want to run PHPLint before we commit 77 | 78 | Add a new file named `pre-commit` in your `.git/hooks` directory. Then add a new directory in the 79 | root folder of your project/repository, named i.e. `project-hooks`. In there, add a new PHP file 80 | named `pre-commit_lint_10.php`. This file will automatically get added to your `pre-commit` hook 81 | where you called the `\GitHooksLoader()` like shown above. It will get added with a priority 82 | of 10. Then just put the following contents in your new file: 83 | 84 | ```php 85 | #!/usr/bin/env php 86 | =5.3" 21 | }, 22 | "suggest": { 23 | "wcm/git-php-hooks-library": "A convenient library of pre made hooks and tasks." 24 | }, 25 | "autoload" : { 26 | "classmap" : [ "" ] 27 | } 28 | } 29 | --------------------------------------------------------------------------------