-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathGitHooksLoader.php
143 lines (125 loc) · 2.68 KB
/
GitHooksLoader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/**
* Class GitHooksLoader
* @package WCM\GitPHPHooks
*/
class GitHooksLoader
{
/** @type string */
private $hook;
/** @type string */
private $location;
/** @type \FilesystemIterator */
private $fetched;
/** @type \RegexIterator */
private $filtered;
/** @type \SplPriorityQueue */
private $sorted;
/**
* Set the hook name, the location and trigger the process
* @param string $hook
* @param string $location
*/
public function __construct( $hook, $location )
{
$this->setHook( $hook );
$this->setLocation( $location );
$this->prepare();
$this->load();
}
/**
* Grabs all files in a dir,
* filters them to only include PHP files
* where the file name contains the current hook name
* and sorts them by the first integer found in the file name.
*/
public function prepare()
{
$this->fetched = $this->fetch( $this->location );
$this->fetched->rewind();
$this->filtered = $this->filter( $this->fetched );
$this->filtered->rewind();
$this->sorted = $this->sort( $this->filtered );
$this->sorted->rewind();
}
/**
* Loads the pre sorted files
*/
public function load()
{
foreach ( $this->sorted as $it )
include_once $this->sorted->current();
}
/**
* Sets the current git hook name
* @param string $file
* @return $this
*/
public function setHook( $file )
{
$this->hook = pathinfo(
$file,
PATHINFO_FILENAME
);
}
/**
* Sets the folder/location where the PHP Git Hook files are located
* @param $location
*/
public function setLocation( $location )
{
$this->location = $location;
}
/**
* Fetches all files from the target folder
* @param string $location
* @return \FilesystemIterator
*/
public function fetch( $location )
{
return new \FilesystemIterator(
$location,
FilesystemIterator::SKIP_DOTS
| FilesystemIterator::FOLLOW_SYMLINKS
);
}
/**
* Filters the file list to only include PHP files
* where the file name contains the current hook name
* @param \Iterator $files
* @return \RegexIterator
*/
public function filter( \Iterator $files )
{
return new \RegexIterator(
$files,
'/('.$this->hook.')[\w-]*?\.php/i'
);
}
/**
* Sorts files by the first `int` in the file name
* and adds them to a `\SplPriorityQueue`.
* Files without a priority are ignored to allow easy disabling.
* @param \Iterator $files
* @return \SplPriorityQueue
*/
public function sort( \Iterator $files )
{
$sorted = new \SplPriorityQueue();
foreach ( $files as $file )
{
preg_match(
'/\d+/',
$files->current(),
$priority
);
if ( empty( $priority ) )
continue;
$sorted->insert(
$files->current(),
$priority[0]
);
}
return $sorted;
}
}