Skip to content

Commit 8f214e9

Browse files
committed
Refactor, adds cache usage and more
1 parent a8a2ef6 commit 8f214e9

File tree

3 files changed

+87
-34
lines changed

3 files changed

+87
-34
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,4 +530,3 @@ Professional support, consulting as well as software development services are av
530530
https://www.cebe.cc/en/contact
531531

532532
Development of this library is sponsored by [cebe.:cloud: "Your Professional Deployment Platform"](https://cebe.cloud).
533-

src/generator/ApiGenerator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,8 @@ public function hints()
372372
*/
373373
public function autoCompleteData()
374374
{
375-
return (new PathAutoCompletion())->complete();
375+
$config = $this->makeConfig();
376+
return (new PathAutoCompletion($config))->complete();
376377
}
377378

378379
/**

src/lib/PathAutoCompletion.php

Lines changed: 85 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,45 +11,28 @@
1111
use RecursiveIteratorIterator;
1212
use RegexIterator;
1313
use Throwable;
14+
use cebe\yii2openapi\lib\Config;
1415
use Yii;
1516
use yii\helpers\FileHelper;
1617

1718
class PathAutoCompletion
1819
{
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;
4321

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+
}
4526

27+
public function complete():array
28+
{
4629
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'),
5336
// 'urlConfigFile' => [
5437
// '@app/config/urls.rest.php',
5538
// ],
@@ -66,7 +49,7 @@ private function completeAlias(string $alias):array
6649
return [];
6750
}
6851
try {
69-
$dirs = FileHelper::findDirectories($path, ['except' => ['vendor/','runtime/','assets/','.git/','.svn/']]);
52+
$dirs = FileHelper::findDirectories($path, ['except' => ['vendor/','runtime/','assets/','.git/','.svn/', '/web']]);
7053
} catch (Throwable $e) {
7154
// ignore errors with file permissions
7255
Yii::error($e);
@@ -76,4 +59,74 @@ private function completeAlias(string $alias):array
7659
return str_replace('/', '\\', substr($alias, 1) . substr($dir, strlen($path)));
7760
}, $dirs);
7861
}
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+
}
79132
}

0 commit comments

Comments
 (0)