/
MatcherFactory.php
253 lines (228 loc) · 7.18 KB
/
MatcherFactory.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
/**
* PHPSpec
*
* LICENSE
*
* This file is subject to the GNU Lesser General Public License Version 3
* that is bundled with this package in the file LICENSE.
* It is also available through the world-wide-web at this URL:
* http://www.gnu.org/licenses/lgpl-3.0.txt
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@phpspec.net so we can send you a copy immediately.
*
* @category PHPSpec
* @package PHPSpec
* @copyright Copyright (c) 2007-2009 Pádraic Brady, Travis Swicegood
* @copyright Copyright (c) 2010-2012 Pádraic Brady, Travis Swicegood,
* Marcello Duarte
* @license http://www.gnu.org/licenses/lgpl-3.0.txt GNU Lesser General Public Licence Version 3
*/
namespace PHPSpec\Matcher;
use PHPSpec\Matcher\InvalidMatcher;
/**
* @category PHPSpec
* @package PHPSpec
* @copyright Copyright (c) 2007-2009 Pádraic Brady, Travis Swicegood
* @copyright Copyright (c) 2010-2012 Pádraic Brady, Travis Swicegood,
* Marcello Duarte
* @license http://www.gnu.org/licenses/lgpl-3.0.txt GNU Lesser General Public Licence Version 3
*/
class MatcherFactory
{
const NAMESPACE_SEPARATOR = '\\';
/**
* Paths to matchers
*
* @var array
*/
protected $_pathsToMatchers;
/**
* List of builtin matchers
*
* @var array
*/
protected $_builtinMatchers = array();
/**
* Matchers registry
*
* @var associative array
*/
protected $_matchers = array();
/**
* Namespace for the builtin matchers
*
* @var string
*/
protected $_builtInNamespace;
/**
* Matcher factory is created with a path to matchers
*
* @param array $pathsToMatchers
*/
public function __construct(array $pathsToMatchers = array())
{
$this->_pathsToMatchers = $pathsToMatchers;
$this->_builtInNamespace = '\PHPSpec\Matcher\\';
}
/**
* Create the matcher
*
* @param string $matcherName
* @param string $expected
* @return \PHPSpec\Matcher
*/
public function create($matcherName, $expected = array())
{
if (empty($this->_matchers)) {
$this->_buildRegistry();
}
if (!is_array($expected)) {
$expected = array($expected);
}
if (!array_key_exists($matcherName, $this->_matchers)) {
throw new InvalidMatcher(
"Call to undefined method $matcherName"
);
}
if ($this->_matchers[$matcherName]['path'] !== false) {
require_once($this->_matchers[$matcherName]['path']);
}
$matcherClass = $this->_matchers[$matcherName]['namespace'] .
strtoupper($matcherName[0]) . substr($matcherName, 1);
$reflectedMatcher = new \ReflectionClass($matcherClass);
if (!$reflectedMatcher->implementsInterface('PHPSpec\Matcher')) {
throw new InvalidMatcherType(
$this->_matchers[$matcherName]['namespace'] .
strtoupper($matcherName[0]) . substr($matcherName, 1) .
" must implement PHPSpec\Matcher"
);
}
$expected = $expected === array() ? array(null) : $expected;
$matcher = $reflectedMatcher->newInstanceArgs($expected);
return $matcher;
}
/**
* Builds the matchers registry
*
* @return void
*/
private function _buildRegistry()
{
$this->_addBuiltinMatchersToRegistry();
$this->_addCustomMatchersToRegistry();
}
/**
* Adds builtin matchers to the registry
*
* @return void
*/
private function _addBuiltinMatchersToRegistry()
{
if (empty($this->_builtInMatchers)) {
$this->_builtInMatchers = $this->loadBuiltInMatchers();
}
foreach ($this->_builtInMatchers as $builtinMatcher) {
$this->_matchers[$builtinMatcher] = array(
'namespace' => $this->_builtInNamespace,
'path' => false
);
}
}
protected function loadBuiltInMatchers()
{
$matchers = array();
$dir = __DIR__;
do {
$dir = realpath(dirname($dir));
} while (basename($dir) !== 'PHPSpec');
$matcherDir = $dir . DIRECTORY_SEPARATOR . 'Matcher';
$files = glob($matcherDir . DIRECTORY_SEPARATOR . '*.php');
foreach ($files as $file) {
$content = file_get_contents($file);
if (preg_match('/implements Matcher/', $content)) {
$fileName = basename(
$matcherDir . DIRECTORY_SEPARATOR . $file, '.php'
);
$matchers[] = strtolower($fileName[0]) . substr($fileName, 1);
}
}
return $matchers;
}
/**
* Adds custom matchers to the registry
*
* @return void
*/
private function _addCustomMatchersToRegistry()
{
foreach ($this->_pathsToMatchers as $originalPath) {
$this->_recursivelyRegisterMatchersOnFolder($originalPath);
}
}
/**
* Recursively registers matchers found on folder
*
* @param string $folder
*/
private function _recursivelyRegisterMatchersOnFolder($originalPath)
{
$nameSpace = $this->_fromPathToNamespace($originalPath);
$currentPath = $this->_findMatcherPath($originalPath);
if ($currentPath !== false) {
foreach (
glob($currentPath . DIRECTORY_SEPARATOR . "*.php") as
$matcherFile
) {
$matcherName = basename($matcherFile, ".php");
$matcherName = strtolower($matcherName[0]) .
substr($matcherName, 1);
$this->_matchers[$matcherName] = array(
'namespace' => $nameSpace,
'path' => $matcherFile);
}
foreach (
glob($currentPath . DIRECTORY_SEPARATOR . "*", GLOB_ONLYDIR)
as $appendPath
) {
$this->_recursivelyRegisterMatchersOnFolder(
$originalPath . DIRECTORY_SEPARATOR . basename($appendPath)
);
}
}
}
/**
* Find a namespace based on the path for the file
*
* @param string $path
* @return string
*/
private function _fromPathToNamespace($path)
{
$nameSpace = str_replace(
DIRECTORY_SEPARATOR,
self::NAMESPACE_SEPARATOR, $path
);
if (substr($nameSpace, -1) !== self::NAMESPACE_SEPARATOR) {
$nameSpace .= self::NAMESPACE_SEPARATOR;
}
return $nameSpace;
}
/**
* Find the path to a Matcher
*
* @param string $path
* @return string|false
*/
private function _findMatcherPath($path)
{
$includePaths = explode(PATH_SEPARATOR, get_include_path());
foreach ($includePaths as $child) {
if (is_dir($child . DIRECTORY_SEPARATOR . $path)) {
return $child . DIRECTORY_SEPARATOR . $path;
}
}
return false;
}
}