1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SoliDry\Controllers; |
4
|
|
|
|
5
|
|
|
use SoliDry\Blocks\Controllers; |
6
|
|
|
use SoliDry\Blocks\Entities; |
7
|
|
|
use SoliDry\Blocks\FileManager; |
8
|
|
|
use SoliDry\Blocks\FormRequest; |
9
|
|
|
use SoliDry\Blocks\Migrations; |
10
|
|
|
use SoliDry\Blocks\Routes; |
11
|
|
|
use SoliDry\Blocks\Tests; |
12
|
|
|
use SoliDry\Exceptions\DirectoryException; |
13
|
|
|
use SoliDry\Helpers\Console; |
14
|
|
|
use SoliDry\Types\ConsoleInterface; |
15
|
|
|
use SoliDry\Types\DefaultInterface; |
16
|
|
|
use SoliDry\Types\DirsInterface; |
17
|
|
|
use SoliDry\Types\PhpInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Trait GeneratorTrait |
21
|
|
|
* |
22
|
|
|
* @package SoliDry\Controllers |
23
|
|
|
*/ |
24
|
|
|
trait GeneratorTrait |
25
|
|
|
{ |
26
|
|
|
use HistoryTrait; |
27
|
|
|
|
28
|
|
|
// all generated entities/resources |
29
|
|
|
private $forms; |
30
|
|
|
private $mappers; |
31
|
|
|
private $routes; |
32
|
|
|
private $migrations; |
33
|
|
|
private $controllers; |
34
|
|
|
private $tests; |
35
|
|
|
|
36
|
|
|
// gen dir found in history |
37
|
|
|
private $genDir; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Standard generation |
41
|
|
|
*/ |
42
|
|
|
private function generateResources(): void |
43
|
|
|
{ |
44
|
|
|
$this->outputEntity(); |
45
|
|
|
|
46
|
|
|
// create controller |
47
|
|
|
$this->solveControllers(); |
48
|
|
|
|
49
|
|
|
// create FormRequest |
50
|
|
|
$this->solveFormRequest(); |
51
|
|
|
|
52
|
|
|
// create entities/models |
53
|
|
|
$this->solveEntities(); |
54
|
|
|
|
55
|
|
|
// create routes |
56
|
|
|
$this->routes = new Routes($this); |
57
|
|
|
$this->routes->create(); |
58
|
|
|
|
59
|
|
|
// create tests |
60
|
|
|
if (empty($this->options[ConsoleInterface::OPTION_TESTS]) === false) { |
61
|
|
|
try { |
62
|
|
|
FileManager::createPath($this->formatFuncTestsPath()); |
|
|
|
|
63
|
|
|
} catch (DirectoryException $e) { |
64
|
|
|
$this->error($e->getTraceAsString()); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->tests = new Tests($this); |
68
|
|
|
$this->tests->createEntity($this->formatFuncTestsPath(), DefaultInterface::FUNCTIONAL_POSTFIX); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$this->createMigrations(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Generation with merge option |
76
|
|
|
*/ |
77
|
|
|
private function mergeResources(): void |
78
|
|
|
{ |
79
|
|
|
$this->outputEntity(); |
80
|
|
|
$this->solveControllers(); |
81
|
|
|
|
82
|
|
|
$this->solveFormRequest(); |
83
|
|
|
|
84
|
|
|
$this->solveEntities(); |
85
|
|
|
|
86
|
|
|
// create routes |
87
|
|
|
$this->routes = new Routes($this); |
88
|
|
|
$this->routes->create(); |
89
|
|
|
|
90
|
|
|
$this->createMigrations(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Creates Controllers and leaves those generated in case of merge |
95
|
|
|
*/ |
96
|
|
|
private function solveControllers(): void |
97
|
|
|
{ |
98
|
|
|
$this->controllers = new Controllers($this); |
99
|
|
|
$controllerPath = $this->formatControllersPath(); |
|
|
|
|
100
|
|
|
|
101
|
|
|
if (empty($this->options[ConsoleInterface::OPTION_REGENERATE]) === false |
102
|
|
|
&& file_exists($this->controllers->getEntityFile($controllerPath, |
103
|
|
|
DefaultInterface::CONTROLLER_POSTFIX)) === true) { |
104
|
|
|
|
105
|
|
|
$this->controllers->recreateEntity($controllerPath, DefaultInterface::CONTROLLER_POSTFIX); |
106
|
|
|
} else { |
107
|
|
|
$this->controllers->createDefault(); |
108
|
|
|
$this->controllers->createEntity($controllerPath, DefaultInterface::CONTROLLER_POSTFIX); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
private function solveFormRequest(): void |
113
|
|
|
{ |
114
|
|
|
$this->forms = new FormRequest($this); |
115
|
|
|
$formRequestPath = $this->formatRequestsPath(); |
|
|
|
|
116
|
|
|
|
117
|
|
|
if (empty($this->options[ConsoleInterface::OPTION_REGENERATE]) === false |
118
|
|
|
&& file_exists($this->forms->getEntityFile($formRequestPath, |
119
|
|
|
DefaultInterface::FORM_REQUEST_POSTFIX)) === true) { |
120
|
|
|
$this->forms->recreateEntity($formRequestPath, DefaultInterface::FORM_REQUEST_POSTFIX); |
121
|
|
|
} else { |
122
|
|
|
$this->forms->createEntity($formRequestPath, DefaultInterface::FORM_REQUEST_POSTFIX); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Decide whether to generate new Entities or mutate existing |
128
|
|
|
*/ |
129
|
|
|
private function solveEntities(): void |
130
|
|
|
{ |
131
|
|
|
// create entities/models |
132
|
|
|
$this->mappers = new Entities($this); |
133
|
|
|
$this->mappers->createPivot(); |
134
|
|
|
$entitiesPath = $this->formatEntitiesPath(); |
|
|
|
|
135
|
|
|
|
136
|
|
|
if (empty($this->options[ConsoleInterface::OPTION_MERGE]) === false |
137
|
|
|
&& file_exists($this->forms->getEntityFile($entitiesPath)) === true) { |
138
|
|
|
$this->mappers->recreateEntity($entitiesPath); |
139
|
|
|
} else { |
140
|
|
|
$this->mappers->createEntity($entitiesPath); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
private function outputEntity(): void |
145
|
|
|
{ |
146
|
|
|
Console::out( |
147
|
|
|
'===============' . PhpInterface::SPACE . $this->objectName |
148
|
|
|
. PhpInterface::SPACE . DirsInterface::ENTITIES_DIR |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Creates migrations for every entity if there is merge option - adds additional |
154
|
|
|
*/ |
155
|
|
|
private function createMigrations(): void |
156
|
|
|
{ |
157
|
|
|
if (empty($this->options[ConsoleInterface::OPTION_MIGRATIONS]) === false) { |
158
|
|
|
$this->migrations = new Migrations($this); |
159
|
|
|
$this->migrations->create(); |
160
|
|
|
$this->migrations->createPivot(); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
} |