1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11: namespace expect;
12:
13: use ArrayIterator;
14: use expect\package\ComposerJsonNotFoundException;
15: use expect\package\MatcherClass;
16: use expect\package\ReflectionIterator;
17: use expect\matcher\ReportableMatcher;
18: use Noodlehaus\Config;
19:
20: 21: 22: 23: 24: 25:
26: final class MatcherPackage implements RegisterablePackage
27: {
28:
29: 30: 31:
32: private $namespace;
33:
34: 35: 36:
37: private $namespaceDirectory;
38:
39: 40: 41: 42: 43: 44:
45: public function __construct($namespace, $namespaceDirectory)
46: {
47: $this->namespace = $this->normalizeNamespace($namespace);
48: $this->namespaceDirectory = $namespaceDirectory;
49: }
50:
51: 52: 53:
54: public function registerTo(MatcherRegistry $registry)
55: {
56: $provideMatchers = $this->getProvideMatchers();
57:
58: foreach ($provideMatchers as $provideMatcher) {
59: $registry->register($provideMatcher);
60: }
61: }
62:
63: 64: 65:
66: private function getProvideMatchers()
67: {
68: $matchers = [];
69: $reflectionIterator = new ReflectionIterator(
70: $this->namespace,
71: $this->namespaceDirectory
72: );
73:
74: foreach ($reflectionIterator as $reflection) {
75: if ($reflection->implementsInterface(ReportableMatcher::class) === false) {
76: continue;
77: }
78:
79: $matchers[] = new MatcherClass(
80: $reflection->getNamespaceName(),
81: $reflection->getShortName()
82: );
83: }
84:
85: return new ArrayIterator($matchers);
86: }
87:
88: private function normalizeNamespace($namespace)
89: {
90: $normalizeNamespace = $namespace;
91: $lastCharAt = strlen($namespace) - 1;
92:
93: if (substr($namespace, $lastCharAt) === '\\') {
94: $normalizeNamespace = substr($namespace, 0, $lastCharAt);
95: }
96:
97: return $normalizeNamespace;
98: }
99:
100: 101: 102: 103: 104: 105: 106:
107: public static function fromPackageFile($composerJson)
108: {
109: if (file_exists($composerJson) === false) {
110: throw new ComposerJsonNotFoundException("File {$composerJson} not found.");
111: }
112:
113: $config = Config::load($composerJson);
114: $autoload = $config->get('autoload.psr-4');
115:
116: $composerJsonDirectory = dirname($composerJson);
117:
118: $keys = array_keys($autoload);
119: $namespace = array_shift($keys);
120:
121: $values = array_values($autoload);
122: $namespaceDirectory = array_shift($values);
123: $namespaceDirectory = realpath($composerJsonDirectory . '/' . $namespaceDirectory);
124:
125: return new self($namespace, $namespaceDirectory);
126: }
127: }
128: