7
7
8
8
class PSR0Locator implements ResourceLocatorInterface
9
9
{
10
+ /**
11
+ * @var string
12
+ */
10
13
private $ srcNamespace ;
11
14
15
+ /**
16
+ * @var string
17
+ */
12
18
private $ specSubNamespace ;
13
19
20
+ /**
21
+ * @var string
22
+ */
14
23
private $ srcPath ;
15
24
25
+ /**
26
+ * @var array
27
+ */
16
28
private $ specPaths = array ();
17
29
30
+ /**
31
+ * @var Filesystem
32
+ */
18
33
private $ filesystem ;
19
34
35
+ /**
36
+ * @param string $srcNamespace
37
+ * @param string $specSubNamespace
38
+ * @param string $srcPath
39
+ * @param array $specPaths
40
+ * @param Filesystem $filesystem
41
+ */
20
42
public function __construct ($ srcNamespace = '' , $ specSubNamespace = 'Spec ' , $ srcPath = 'src ' , $ specPaths = array (), Filesystem $ filesystem = null )
21
43
{
22
44
$ this ->srcNamespace = $ srcNamespace ;
@@ -26,45 +48,156 @@ public function __construct($srcNamespace = '', $specSubNamespace = 'Spec', $src
26
48
$ this ->filesystem = $ filesystem ?: new Filesystem ();
27
49
}
28
50
51
+ /**
52
+ * @param array $specPaths
53
+ *
54
+ * @return array
55
+ */
56
+ private function expandSpecPaths (array $ specPaths )
57
+ {
58
+ $ result = array ();
59
+
60
+ foreach ($ specPaths as $ specPath ) {
61
+ $ paths = glob ($ specPath , GLOB_ONLYDIR );
62
+ if (!empty ($ paths )) {
63
+ $ paths = array_filter (array_map ('realpath ' , $ paths ));
64
+ $ result = array_merge ($ result , $ paths );
65
+ }
66
+ }
67
+
68
+ return $ result ;
69
+ }
70
+
71
+ /**
72
+ * @return array
73
+ */
29
74
public function getAllResources ()
30
75
{
31
- if (empty ($ this ->specPaths )) {
76
+ return $ this ->findResources ($ this ->srcPath );
77
+ }
78
+
79
+ /**
80
+ * @param string $query
81
+ *
82
+ * @return boolean
83
+ */
84
+ public function supportsQuery ($ query )
85
+ {
86
+ $ path = rtrim (realpath ($ query ), DIRECTORY_SEPARATOR );
87
+
88
+ return 0 === strpos ($ path , rtrim ($ this ->srcPath , DIRECTORY_SEPARATOR ));
89
+ }
90
+
91
+ /**
92
+ * @param string $query
93
+ *
94
+ * @return PSR0Resource[]
95
+ */
96
+ public function findResources ($ query )
97
+ {
98
+ $ path = realpath ($ query );
99
+
100
+ if (!$ path ) {
32
101
return array ();
33
102
}
34
103
35
- $ files = $ this ->filesystem ->findPhpFilesIn ($ this ->specPaths );
36
- $ resources = array ();
104
+ if ('.php ' === substr ($ path , -4 )) {
105
+ return array ($ this ->createResourceFromSpecFile ($ path ));
106
+ }
37
107
38
- foreach ($ files as $ file ) {
39
- $ path = $ file ->getRealPath ();
40
- $ relative = substr ($ path , strlen ($ this ->srcPath ), -4 );
41
- $ relative = str_replace ('Spec ' , '' , $ relative );
108
+ return $ this ->findResourcesInSpecPaths ($ path );
109
+ }
110
+
111
+ /**
112
+ * @param string $path
113
+ *
114
+ * @return PSR0Resource|null
115
+ */
116
+ private function createResourceFromSpecFile ($ path )
117
+ {
118
+ $ relativePath = substr ($ path , strlen ($ this ->srcPath ), -4 );
119
+ $ relativePath = str_replace ('Spec ' , '' , $ relativePath );
120
+
121
+ return $ this ->createResource ($ relativePath );
122
+ }
42
123
43
- $ resources [] = $ this ->createResource ($ relative );
124
+ /**
125
+ * @param string $path
126
+ *
127
+ * @return array
128
+ */
129
+ private function findResourcesInSpecPaths ($ path )
130
+ {
131
+ $ paths = $ this ->filterSpecPaths ($ path );
132
+
133
+ if (empty ($ paths )) {
134
+ return array ();
44
135
}
45
136
46
- return $ resources ;
137
+ $ files = $ this ->filesystem ->findPhpFilesIn ($ paths );
138
+
139
+ return $ this ->createResourcesFromSpecFiles ($ files );
47
140
}
48
141
49
- public function supportsQuery ($ query )
142
+ /**
143
+ * Filters out the spec paths which are not child or parent of the path.
144
+ *
145
+ * @param string $path
146
+ *
147
+ * @return array
148
+ */
149
+ private function filterSpecPaths ($ path )
50
150
{
51
- $ path = rtrim (realpath ($ query ), DIRECTORY_SEPARATOR );
151
+ $ specPaths = array_map (
152
+ function ($ value ) use ($ path ) {
153
+ return 0 === strpos ($ path , $ value ) ? $ path : $ value ;
154
+ },
155
+ $ this ->specPaths
156
+ );
157
+
158
+ $ specPaths = array_filter (
159
+ $ specPaths ,
160
+ function ($ value ) use ($ path ) {
161
+ return 0 === strpos ($ value , $ path );
162
+ }
163
+ );
52
164
53
- return 0 === strpos ( $ path , rtrim ( $ this -> srcPath , DIRECTORY_SEPARATOR )) ;
165
+ return $ specPaths ;
54
166
}
55
167
56
- public function findResources ($ query )
168
+ /**
169
+ * @param array $files
170
+ *
171
+ * @return PSR0Resource[]
172
+ */
173
+ private function createResourcesFromSpecFiles (array $ files )
57
174
{
58
- // @todo: Implement findResources() method.
175
+ $ resources = array ();
176
+
177
+ foreach ($ files as $ file ) {
178
+ $ resources [] = $ this ->createResourceFromSpecFile ($ file ->getRealPath ());
179
+ }
180
+
181
+ return $ resources ;
59
182
}
60
183
184
+ /**
185
+ * @param string $classname
186
+ *
187
+ * @return boolean
188
+ */
61
189
public function supportsClass ($ classname )
62
190
{
63
191
$ classname = str_replace ('/ ' , '\\' , $ classname );
64
192
65
193
return '' === $ this ->srcNamespace || 0 === strpos ($ classname , $ this ->srcNamespace );
66
194
}
67
195
196
+ /**
197
+ * @param string $classname
198
+ *
199
+ * @return PSR0Resource|null
200
+ */
68
201
public function createResource ($ classname )
69
202
{
70
203
$ classname = str_replace ('/ ' , '\\' , $ classname );
@@ -77,28 +210,11 @@ public function createResource($classname)
77
210
return null ;
78
211
}
79
212
80
- public function getPriority ()
81
- {
82
- return 0 ;
83
- }
84
-
85
213
/**
86
- * @param array $specPaths
87
- *
88
- * @return array
214
+ * @return integer
89
215
*/
90
- private function expandSpecPaths ( array $ specPaths )
216
+ public function getPriority ( )
91
217
{
92
- $ result = array ();
93
-
94
- foreach ($ specPaths as $ specPath ) {
95
- $ paths = glob ($ specPath , GLOB_ONLYDIR );
96
- if (!empty ($ paths )) {
97
- $ paths = array_filter (array_map ('realpath ' , $ paths ));
98
- $ result = array_merge ($ result , $ paths );
99
- }
100
- }
101
-
102
- return $ result ;
218
+ return 0 ;
103
219
}
104
220
}
0 commit comments