1010
1111class DetectController extends Controller
1212{
13+ /**
14+ * Get the type of all classes in the app folder.
15+ *
16+ * @return array[]
17+ */
18+ public function detect ()
19+ {
20+ $ recursiveDirectoryIterator = new RecursiveDirectoryIterator (app_path ());
21+ $ files = new RecursiveIteratorIterator ($ recursiveDirectoryIterator );
22+ $ type = [];
23+
24+ foreach ($ files as $ file ) {
25+ if (!$ file ->isFile () || $ file ->getExtension () !== 'php ' ) {
26+ continue ;
27+ }
28+
29+ $ class = $ this ->getClassFromFile ($ file );
30+ if ($ class !== null ) {
31+ $ type [] = $ this ->getClassType ($ class );
32+ }
33+ }
34+
35+ return $ type ;
36+ }
37+
1338 /**
1439 * @param $file
1540 * @return ReflectionClass|null
@@ -21,7 +46,7 @@ public function getClassFromFile($file)
2146
2247 // Match namespace and class name
2348 preg_match ('/namespace\s+(.*?);.*?class\s+(\w+)/s ' , $ content , $ matches );
24- if (! isset ($ matches [1 ]) || ! isset ($ matches [2 ])) {
49+ if (!isset ($ matches [1 ]) || !isset ($ matches [2 ])) {
2550 return null ;
2651 }
2752
@@ -32,19 +57,63 @@ public function getClassFromFile($file)
3257 }
3358
3459 /**
60+ * Get the type of the given class.
61+ *
3562 * @param ReflectionClass $class
36- * @return bool
63+ * @return string
3764 */
38- private function dependsOnModels (ReflectionClass $ class )
65+ protected function getClassType (ReflectionClass $ class )
3966 {
40- $ dependencies = $ class ->getConstructor ()->getParameters ();
41- foreach ($ dependencies as $ dependency ) {
42- if (preg_match ('/Model$/ ' , $ dependency ->getClass ()->getName ()) === 1 ) {
43- return true ;
44- }
67+ $ type = 'other ' ;
68+
69+ switch (true ) {
70+ case $ this ->isRepositoryClass ($ class ):
71+ $ type = 'repository ' ;
72+ break ;
73+ case $ this ->isServiceClass ($ class ):
74+ $ type = 'service ' ;
75+ break ;
76+ case $ this ->isControllerClass ($ class ):
77+ $ type = 'controller ' ;
78+ break ;
79+ case $ this ->isActionClass ($ class ):
80+ $ type = 'action ' ;
81+ break ;
4582 }
4683
47- return false ;
84+ return $ type ;
85+ }
86+
87+ /**
88+ * Check if the class is a repository class
89+ * A repository class must have a name ending with "Repository" or "EloquentRepository"
90+ * and implement the CRUD methods
91+ * and have a dependency on a model.
92+ *
93+ * @param ReflectionClass $class
94+ * @return bool
95+ */
96+ public function isRepositoryClass (ReflectionClass $ class )
97+ {
98+ return $ this ->checkClassType ($ class , 'repository ' );
99+ }
100+
101+ /**
102+ * Check if the class is a class of the given type
103+ * A class of the given type must have a name ending with the given type or "Eloquent" + the given type.
104+ *
105+ * @param ReflectionClass $class
106+ * @param $type
107+ * @return bool
108+ */
109+ protected function checkClassType (ReflectionClass $ class , $ type )
110+ {
111+ $ type = ucfirst ($ type );
112+
113+ return preg_match ('/ ' .$ type .'$/ ' , $ class ->getName ()) === 1
114+ || preg_match ('/Eloquent ' .$ type .'$/ ' , $ class ->getName ()) === 1
115+ && $ this ->implementsCrudMethods ($ class )
116+ && $ this ->dependsOnModels ($ class );
48117 }
49118
50119 /**
@@ -73,17 +142,19 @@ protected function implementsCrudMethods(ReflectionClass $class)
73142 }
74143
75144 /**
76- * Check if the class is a repository class
77- * A repository class must have a name ending with "Repository" or "EloquentRepository"
78- * and implement the CRUD methods
79- * and have a dependency on a model.
80- *
81145 * @param ReflectionClass $class
82146 * @return bool
83147 */
84- public function isRepositoryClass (ReflectionClass $ class )
148+ private function dependsOnModels (ReflectionClass $ class )
85149 {
86- return $ this ->checkClassType ($ class , 'repository ' );
150+ $ dependencies = $ class ->getConstructor ()->getParameters ();
151+ foreach ($ dependencies as $ dependency ) {
152+ if (preg_match ('/Model$/ ' , $ dependency ->getClass ()->getName ()) === 1 ) {
153+ return true ;
154+ }
155+ }
156+
157+ return false ;
87158 }
88159
89160 /**
@@ -123,75 +194,4 @@ public function isActionClass(ReflectionClass $class)
123194 {
124195 return $ this ->checkClassType ($ class , 'action ' );
125196 }
126-
127- /**
128- * Check if the class is a class of the given type
129- * A class of the given type must have a name ending with the given type or "Eloquent" + the given type.
130- *
131- * @param ReflectionClass $class
132- * @param $type
133- * @return bool
134- */
135- protected function checkClassType (ReflectionClass $ class , $ type )
136- {
137- $ type = ucfirst ($ type );
138-
139- return preg_match ('/ ' .$ type .'$/ ' , $ class ->getName ()) === 1
140- || preg_match ('/Eloquent ' .$ type .'$/ ' , $ class ->getName ()) === 1
141- && $ this ->implementsCrudMethods ($ class )
142- && $ this ->dependsOnModels ($ class );
143- }
144-
145- /**
146- * Get the type of the given class.
147- *
148- * @param ReflectionClass $class
149- * @return string
150- */
151- protected function getClassType (ReflectionClass $ class )
152- {
153- $ type = 'other ' ;
154-
155- switch (true ) {
156- case $ this ->isRepositoryClass ($ class ):
157- $ type = 'repository ' ;
158- break ;
159- case $ this ->isServiceClass ($ class ):
160- $ type = 'service ' ;
161- break ;
162- case $ this ->isControllerClass ($ class ):
163- $ type = 'controller ' ;
164- break ;
165- case $ this ->isActionClass ($ class ):
166- $ type = 'action ' ;
167- break ;
168- }
169-
170- return $ type ;
171- }
172-
173- /**
174- * Get the type of all classes in the app folder.
175- *
176- * @return array[]
177- */
178- public function detect ()
179- {
180- $ recursiveDirectoryIterator = new RecursiveDirectoryIterator (app_path ());
181- $ files = new RecursiveIteratorIterator ($ recursiveDirectoryIterator );
182- $ type = [];
183-
184- foreach ($ files as $ file ) {
185- if (! $ file ->isFile () || $ file ->getExtension () !== 'php ' ) {
186- continue ;
187- }
188-
189- $ class = $ this ->getClassFromFile ($ file );
190- if ($ class !== null ) {
191- $ type [] = $ this ->getClassType ($ class );
192- }
193- }
194-
195- return $ type ;
196- }
197197}
0 commit comments