forked from cebe/yii2-openapi
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathModelsGenerator.php
More file actions
120 lines (111 loc) · 4.24 KB
/
Copy pathModelsGenerator.php
File metadata and controls
120 lines (111 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
/**
* @copyright Copyright (c) 2018 Carsten Brandt <mail@cebe.cc> and contributors
* @license https://github.com/cebe/yii2-openapi/blob/master/LICENSE
*/
namespace cebe\yii2openapi\lib\generators;
use cebe\yii2openapi\lib\CodeFiles;
use cebe\yii2openapi\lib\Config;
use Laminas\Code\Generator\ClassGenerator;
use Laminas\Code\Generator\FileGenerator;
use Yii;
use yii\gii\CodeFile;
class ModelsGenerator
{
/**
* @var \cebe\yii2openapi\lib\Config
*/
protected $config;
/**
* @var array|\cebe\yii2openapi\lib\items\DbModel[]
*/
protected $models;
/**
* @var CodeFiles $files
**/
protected $files;
public function __construct(Config $config, array $models)
{
$this->config = $config;
$this->models = $models;
$this->files = new CodeFiles([]);
}
/**
* @throws \yii\base\InvalidConfigException
*/
public function generate():CodeFiles
{
if (!$this->config->generateModels) {
return $this->files;
}
$modelPath = Config::getPathFromNamespace($this->config->modelNamespace);
$fakerPath = Config::getPathFromNamespace($this->config->fakerNamespace);
if ($this->config->generateModelFaker) {
$this->files->add(new CodeFile(
Yii::getAlias("$fakerPath/BaseModelFaker.php"),
$this->config->render('basefaker.php', ['namespace' => $this->config->fakerNamespace])
));
}
foreach ($this->models as $model) {
$className = $model->getClassName();
if (!empty($this->config->dbModel['scenarioDefaultDescription'])) {
$model->scenarioDefaultDescription = $this->config->dbModel['scenarioDefaultDescription'];
}
if ($model->isNotDb === false) {
$this->files->add(new CodeFile(
Yii::getAlias("$modelPath/base/$className.php"),
$this->config->render(
'dbmodel.php',
[
'model' => $model,
'namespace' => $this->config->modelNamespace . '\\base',
'relationNamespace' => $this->config->modelNamespace,
]
)
));
if ($this->config->generateModelFaker) {
$this->files->add(new CodeFile(
Yii::getAlias("$fakerPath/{$className}Faker.php"),
$this->config->render(
'faker.php',
[
'model' => $model,
'modelNamespace' => $this->config->modelNamespace,
'namespace' => $this->config->fakerNamespace,
'deps' => $model->fakerDependentModels(),
]
)
));
}
} else {
/** This case not implemented yet, just keep it **/
$this->files->add(new CodeFile(
Yii::getAlias("$modelPath/base/$className.php"),
$this->config->render(
'model.php',
[
'model' => $model,
'namespace' => $this->config->modelNamespace . '\\base',
]
)
));
}
// only generate custom classes if they do not exist, do not override
if (!file_exists(Yii::getAlias("$modelPath/$className.php"))) {
$classFileGenerator = new FileGenerator();
$reflection = new ClassGenerator(
$className,
$this->config->modelNamespace,
null,
$this->config->modelNamespace . '\\base\\' . $className
);
$classFileGenerator->setClasses([$reflection]);
$this->files->add(new CodeFile(
Yii::getAlias("$modelPath/$className.php"),
$classFileGenerator->generate()
));
}
}
return $this->files;
}
}