11
11
use RecursiveIteratorIterator ;
12
12
use RegexIterator ;
13
13
use Throwable ;
14
+ use cebe \yii2openapi \lib \Config ;
14
15
use Yii ;
15
16
use yii \helpers \FileHelper ;
16
17
17
18
class PathAutoCompletion
18
19
{
19
- public function complete ():array
20
- {
21
- $ vendor = Yii::getAlias ('@vendor ' );
22
- $ app = Yii::getAlias ('@app ' );
23
- $ runtime = Yii::getAlias ('@runtime ' );
24
- $ paths = [];
25
- $ pathIterator = new RecursiveDirectoryIterator ($ app );
26
- $ recursiveIterator = new RecursiveIteratorIterator ($ pathIterator );
27
- $ files = new RegexIterator ($ recursiveIterator , '~.+\.(json|yaml|yml)$~i ' , RegexIterator::GET_MATCH );
28
- foreach ($ files as $ file ) {
29
- if (strpos ($ file [0 ], $ vendor ) === 0 ) {
30
- $ file = FileHelper::normalizePath ('@vendor ' . substr ($ file [0 ], strlen ($ vendor )));
31
- } elseif (strpos ($ file [0 ], $ runtime ) === 0 ) {
32
- $ file = null ;
33
- } elseif (strpos ($ file [0 ], $ app ) === 0 ) {
34
- $ file = FileHelper::normalizePath ('@app ' . substr ($ file [0 ], strlen ($ app )));
35
- } else {
36
- $ file = $ file [0 ];
37
- }
38
-
39
- if ($ file !== null ) {
40
- $ paths [] = $ file ;
41
- }
42
- }
20
+ private ?Config $ _config = null ;
43
21
44
- $ namespaces = array_merge (...array_map ([$ this , 'completeAlias ' ], array_keys (Yii::$ aliases )));
22
+ public function __construct (?Config $ config = null )
23
+ {
24
+ $ this ->_config = $ config ;
25
+ }
45
26
27
+ public function complete ():array
28
+ {
46
29
return [
47
- 'openApiPath ' => $ paths ,
48
- 'controllerNamespace ' => $ namespaces ,
49
- 'modelNamespace ' => $ namespaces ,
50
- 'fakerNamespace ' => $ namespaces ,
51
- 'migrationNamespace ' => $ namespaces ,
52
- 'transformerNamespace ' => $ namespaces ,
30
+ 'openApiPath ' => $ this -> computePaths ( $ this -> _config ) ,
31
+ 'controllerNamespace ' => $ this -> computeNamesapces ( ' controllerNamespace ' ) ,
32
+ 'modelNamespace ' => $ this -> computeNamesapces ( ' modelNamespace ' ) ,
33
+ 'fakerNamespace ' => $ this -> computeNamesapces ( ' fakerNamespace ' ) ,
34
+ 'migrationNamespace ' => $ this -> computeNamesapces ( ' migrationNamespace ' ) ,
35
+ 'transformerNamespace ' => $ this -> computeNamesapces ( ' transformerNamespace ' ) ,
53
36
// 'urlConfigFile' => [
54
37
// '@app/config/urls.rest.php',
55
38
// ],
@@ -66,7 +49,7 @@ private function completeAlias(string $alias):array
66
49
return [];
67
50
}
68
51
try {
69
- $ dirs = FileHelper::findDirectories ($ path , ['except ' => ['vendor/ ' ,'runtime/ ' ,'assets/ ' ,'.git/ ' ,'.svn/ ' ]]);
52
+ $ dirs = FileHelper::findDirectories ($ path , ['except ' => ['vendor/ ' ,'runtime/ ' ,'assets/ ' ,'.git/ ' ,'.svn/ ' , ' /web ' ]]);
70
53
} catch (Throwable $ e ) {
71
54
// ignore errors with file permissions
72
55
Yii::error ($ e );
@@ -76,4 +59,74 @@ private function completeAlias(string $alias):array
76
59
return str_replace ('/ ' , '\\' , substr ($ alias , 1 ) . substr ($ dir , strlen ($ path )));
77
60
}, $ dirs );
78
61
}
62
+
63
+ private function computeNamesapces (string $ property ): array
64
+ {
65
+ $ config = $ this ->_config ;
66
+ if ($ config && $ config ->$ property ) {
67
+ return [$ config ->$ property ];
68
+ }
69
+
70
+ $ key = 'cebe-yii2-openapi-autocompletion-data-namespaces ' ;
71
+ $ list = Yii::$ app ->cache ->get ($ key );
72
+ if ($ list !== false ) {
73
+ return $ list ;
74
+ }
75
+ $ list = array_merge (...array_map ([$ this , 'completeAlias ' ], array_keys (Yii::$ aliases )));
76
+ Yii::$ app ->cache ->set ($ key , $ list , 3 *24 *60 *60 ); // 3 days
77
+ return $ list ;
78
+ }
79
+
80
+ private function computePaths (): array
81
+ {
82
+ $ config = $ this ->_config ;
83
+
84
+ // First priority will be given to values present in config (example) to be shown in form fields.
85
+ // Second to default values present in class cebe\yii2openapi\generator\ApiGenerator
86
+ // Third will be given to values produced by PathAutoCompletion class
87
+
88
+ if ($ config && $ config ->openApiPath ) {
89
+ return [$ config ->openApiPath ];
90
+ }
91
+
92
+ // check it is present in cache
93
+ $ key = 'cebe-yii2-openapi-autocompletion-data-paths ' ;
94
+ // use cache
95
+ $ list = Yii::$ app ->cache ->get ($ key );
96
+ if ($ list !== false ) {
97
+ return $ list ;
98
+ }
99
+
100
+ // 3rd priority
101
+ $ vendor = Yii::getAlias ('@vendor ' );
102
+ $ webroot = Yii::getAlias ('@webroot ' );
103
+ $ tests = Yii::getAlias ('@app/tests ' );
104
+ $ app = Yii::getAlias ('@app ' );
105
+ $ runtime = Yii::getAlias ('@runtime ' );
106
+ $ paths = [];
107
+ $ pathIterator = new RecursiveDirectoryIterator ($ app );
108
+ $ recursiveIterator = new RecursiveIteratorIterator ($ pathIterator );
109
+ $ files = new RegexIterator ($ recursiveIterator , '~.+\.(json|yaml|yml)$~i ' , RegexIterator::GET_MATCH );
110
+ foreach ($ files as $ file ) {
111
+ if (strpos ($ file [0 ], $ vendor ) === 0 ) {
112
+ $ file = null ;
113
+ } elseif (strpos ($ file [0 ], $ tests ) === 0 ) {
114
+ $ file = null ;
115
+ } elseif (strpos ($ file [0 ], $ webroot ) === 0 ) {
116
+ $ file = null ;
117
+ } elseif (strpos ($ file [0 ], $ runtime ) === 0 ) {
118
+ $ file = null ;
119
+ } elseif (strpos ($ file [0 ], $ app ) === 0 ) {
120
+ $ file = FileHelper::normalizePath ('@app ' . substr ($ file [0 ], strlen ($ app )));
121
+ } else {
122
+ $ file = $ file [0 ];
123
+ }
124
+
125
+ if ($ file !== null ) {
126
+ $ paths [] = $ file ;
127
+ }
128
+ }
129
+ Yii::$ app ->cache ->set ($ key , $ paths , 3 *24 *60 *60 ); // 3 days
130
+ return $ paths ;
131
+ }
79
132
}
0 commit comments