1
+ <?php
2
+ namespace PhpStormMetaGenerator \Classes \HostCMS ;
3
+
4
+
5
+ use InvalidArgumentException ;
6
+ use PhpStormMetaGenerator \Classes \AbstractNamespace ;
7
+ use PhpStormMetaGenerator \Interfaces \InterfaceNamespace ;
8
+
9
+ class EntitiesNamespace extends AbstractNamespace implements InterfaceNamespace
10
+ {
11
+
12
+ const SEARCH_FILE_NAME = 'model.php ' ;
13
+
14
+ /** @var string[] */
15
+ protected $ classes = [];
16
+
17
+ protected function calcClassNameByFilePath ($ path )
18
+ {
19
+ if (!file_exists ($ path ) || !is_file ($ path )) {
20
+ throw new InvalidArgumentException ('Должен быть указан путь к файлу. ' );
21
+ }
22
+
23
+ $ classNameFragments = ['Model ' ];
24
+ $ path = substr ($ path , 0 , strlen ($ path ) - strlen (DIRECTORY_SEPARATOR . self ::SEARCH_FILE_NAME ));
25
+ while ($ this ->getRoot () != $ path ) {
26
+ $ fragment = basename ($ path );
27
+ $ path = substr ($ path , 0 , strlen ($ path ) - strlen (DIRECTORY_SEPARATOR . $ fragment ));
28
+ $ classNameFragments [] = ucfirst ($ fragment );
29
+ }
30
+
31
+ return implode ('_ ' , array_reverse ($ classNameFragments ));
32
+ }
33
+
34
+ /**
35
+ * Получить все найнеднные классы в заданном пространстве имён
36
+ *
37
+ * @return string[]
38
+ */
39
+ public function getClasses ()
40
+ {
41
+ return $ this ->classes ;
42
+ }
43
+
44
+ /**
45
+ * Сканировать заданную директорию на предмет удовлетворябщих шаблону файлов
46
+ *
47
+ * @param string|null $directoryPath Путь к сканируемой директории
48
+ * @return $this
49
+ */
50
+ public function scan ($ directoryPath = null )
51
+ {
52
+ $ directoryPath = is_null ($ directoryPath ) ? $ this ->getRoot () : $ directoryPath ;
53
+ $ scannedElements = scandir ($ directoryPath );
54
+ $ scannedElements = array_diff ($ scannedElements , ['. ' , '.. ' ]);
55
+ foreach ($ scannedElements as $ element ) {
56
+ $ currentElementPath = $ directoryPath . DIRECTORY_SEPARATOR . $ element ;
57
+ if (is_dir ($ currentElementPath )) {
58
+ $ this ->scan ($ currentElementPath );
59
+ continue ;
60
+ } elseif ($ element != self ::SEARCH_FILE_NAME ) {
61
+ continue ;
62
+ }
63
+
64
+ $ this ->classes [] = $ this ->calcClassNameByFilePath ($ currentElementPath );
65
+ }
66
+
67
+ return $ this ;
68
+ }
69
+
70
+ /**
71
+ * Вывести разметку классов пространства имён
72
+ *
73
+ * @return void
74
+ */
75
+ public function render ()
76
+ {
77
+ echo ' \\Core_ORM::factory( \'\') => array( ' , PHP_EOL ;
78
+ foreach ($ this ->getClasses () as $ class ) {
79
+ echo ' \'' , substr ($ class , 0 , strlen ($ class ) - strlen ('_Model ' )), '\' instanceof \\' , $ class , ', ' , PHP_EOL ;
80
+ }
81
+ echo ' ), ' . PHP_EOL ;
82
+ }
83
+
84
+ }
0 commit comments