1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PackagesExtension.php |
4
|
|
|
* |
5
|
|
|
* @copyright More in license.md |
6
|
|
|
* @license https://www.ipublikuj.eu |
7
|
|
|
* @author Adam Kadlec <[email protected]> |
8
|
|
|
* @package iPublikuj:Packages! |
9
|
|
|
* @subpackage DI |
10
|
|
|
* @since 1.0.0 |
11
|
|
|
* |
12
|
|
|
* @date 27.05.16 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
declare(strict_types = 1); |
16
|
|
|
|
17
|
|
|
namespace IPub\Packages\DI; |
18
|
|
|
|
19
|
|
|
use Nette; |
20
|
|
|
use Nette\DI; |
21
|
|
|
|
22
|
|
|
use IPub\Packages; |
23
|
|
|
use IPub\Packages\Commands; |
24
|
|
|
use IPub\Packages\Events; |
25
|
|
|
use IPub\Packages\Helpers; |
26
|
|
|
use IPub\Packages\Loaders; |
27
|
|
|
use IPub\Packages\Repository; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Packages extension container |
31
|
|
|
* |
32
|
|
|
* @package iPublikuj:Packages! |
33
|
|
|
* @subpackage DI |
34
|
|
|
* |
35
|
|
|
* @author Adam Kadlec <[email protected]> |
36
|
|
|
*/ |
37
|
1 |
|
final class PackagesExtension extends DI\CompilerExtension |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* Extension default configuration |
41
|
|
|
* |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
private $defaults = [ |
45
|
|
|
'path' => NULL, // Paths where to search for packages |
46
|
|
|
'dirs' => [ // Define path to folders |
47
|
|
|
'configDir' => '%appDir%/config', // Path where is stored app configuration |
48
|
|
|
'vendorDir' => '%appDir%/../vendor', // Path to composer vendor folder |
49
|
|
|
'tempDir' => '%tempDir%', // Path to temporary folder |
50
|
|
|
], |
51
|
|
|
'configFile' => 'config.neon', // Filename with enabled packages extensions |
52
|
|
|
'loader' => [ |
53
|
|
|
'packageFiles' => [ |
54
|
|
|
'package.php', |
55
|
|
|
], |
56
|
|
|
], |
57
|
|
|
'sources' => [], |
58
|
|
|
'symfonyEvents' => FALSE, |
59
|
|
|
]; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
|
|
public function loadConfiguration() : void |
65
|
|
|
{ |
66
|
|
|
// Get container builder |
67
|
1 |
|
$builder = $this->getContainerBuilder(); |
68
|
|
|
/** @var array $configuration */ |
69
|
1 |
View Code Duplication |
if (method_exists($this, 'validateConfig')) { |
|
|
|
|
70
|
1 |
|
$configuration = $this->validateConfig($this->defaults); |
71
|
|
|
} else { |
72
|
|
|
$configuration = $this->getConfig($this->defaults); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Load packages configuration |
77
|
|
|
*/ |
78
|
|
|
|
79
|
1 |
|
$builder->parameters['packages'] = []; |
80
|
|
|
|
81
|
1 |
|
if (is_file($configuration['dirs']['configDir'] . DIRECTORY_SEPARATOR . 'packages.php')) { |
82
|
|
|
$packages = require $configuration['dirs']['configDir'] . DIRECTORY_SEPARATOR . 'packages.php'; |
83
|
|
|
|
84
|
|
|
foreach ($packages as $name => $data) { |
85
|
|
|
$builder->parameters['packages'][$name] = $data; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Register services |
91
|
|
|
*/ |
92
|
|
|
|
93
|
1 |
|
$builder->addDefinition($this->prefix('loader')) |
94
|
1 |
|
->setType(Loaders\Loader::class) |
95
|
1 |
|
->setArguments([ |
96
|
1 |
|
'packageFiles' => $configuration['loader']['packageFiles'], |
97
|
1 |
|
'metadataSources' => $configuration['sources'], |
98
|
1 |
|
'vendorDir' => $configuration['dirs']['vendorDir'], |
99
|
|
|
]) |
100
|
1 |
|
->addTag('cms.packages'); |
101
|
|
|
|
102
|
1 |
|
$repository = $builder->addDefinition($this->prefix('repository')) |
103
|
1 |
|
->setType(Repository\Repository::class) |
104
|
1 |
|
->addTag('cms.packages'); |
105
|
|
|
|
106
|
1 |
|
if ($configuration['path']) { |
107
|
|
|
$repository->addSetup('addPath', [$configuration['path']]); |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
$builder->addDefinition($this->prefix('manager')) |
111
|
1 |
|
->setType(Packages\PackagesManager::class) |
112
|
1 |
|
->setArguments([ |
113
|
1 |
|
'vendorDir' => $configuration['dirs']['vendorDir'], |
114
|
1 |
|
'configDir' => $configuration['dirs']['configDir'], |
115
|
|
|
]) |
116
|
1 |
|
->addTag('cms.packages'); |
117
|
|
|
|
118
|
1 |
|
$builder->addDefinition($this->prefix('pathResolver')) |
119
|
1 |
|
->setType(Helpers\PathResolver::class) |
120
|
1 |
|
->addTag('cms.packages'); |
121
|
|
|
|
122
|
1 |
|
$builder->addDefinition($this->prefix('scripts.configuration')) |
123
|
1 |
|
->setType(Packages\Scripts\ConfigurationScript::class) |
124
|
1 |
|
->setArguments([ |
125
|
1 |
|
'configDir' => $configuration['dirs']['configDir'], |
126
|
1 |
|
'configFile' => $configuration['configFile'], |
127
|
|
|
]) |
128
|
1 |
|
->addTag('cms.packages'); |
129
|
|
|
|
130
|
|
|
// Define all console commands |
131
|
|
|
$commands = [ |
132
|
1 |
|
'packagesSync' => Commands\SyncCommand::class, |
133
|
|
|
'packagesList' => Commands\ListCommand::class, |
134
|
|
|
'packageEnable' => Commands\EnableCommand::class, |
135
|
|
|
'packageDisable' => Commands\DisableCommand::class, |
136
|
|
|
'packageInstall' => Commands\InstallCommand::class, |
137
|
|
|
'packageUninstall' => Commands\UninstallCommand::class, |
138
|
|
|
]; |
139
|
|
|
|
140
|
1 |
|
foreach ($commands as $name => $cmd) { |
141
|
1 |
|
$builder->addDefinition($this->prefix('commands' . lcfirst($name))) |
142
|
1 |
|
->setType($cmd); |
143
|
|
|
} |
144
|
1 |
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* {@inheritdoc} |
148
|
|
|
*/ |
149
|
|
|
public function beforeCompile() : void |
150
|
|
|
{ |
151
|
1 |
|
parent::beforeCompile(); |
152
|
|
|
|
153
|
|
|
// Get container builder |
154
|
1 |
|
$builder = $this->getContainerBuilder(); |
155
|
|
|
/** @var array $configuration */ |
156
|
1 |
View Code Duplication |
if (method_exists($this, 'validateConfig')) { |
|
|
|
|
157
|
1 |
|
$configuration = $this->validateConfig($this->defaults); |
158
|
|
|
} else { |
159
|
|
|
$configuration = $this->getConfig($this->defaults); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
// Get packages manager |
163
|
1 |
|
$manager = $builder->getDefinition($this->prefix('manager')); |
164
|
|
|
|
165
|
1 |
|
foreach ($builder->findByType(Packages\Scripts\IScript::class) as $serviceDefinition) { |
166
|
1 |
|
$manager->addSetup('addScript', [$serviceDefinition->getType(), $serviceDefinition]); |
167
|
|
|
} |
168
|
|
|
|
169
|
1 |
|
if ($configuration['symfonyEvents'] === TRUE) { |
170
|
|
|
$dispatcher = $builder->getDefinition($builder->getByType(EventDispatcher\EventDispatcherInterface::class)); |
171
|
|
|
|
172
|
|
|
$packagesManager = $builder->getDefinition($builder->getByType(Packages\PackagesManager::class)); |
173
|
|
|
assert($packagesManager instanceof DI\ServiceDefinition); |
|
|
|
|
174
|
|
|
|
175
|
|
|
$packagesManager->addSetup('?->onEnable[] = function() {?->dispatch(new ?(...func_get_args()));}', [ |
176
|
|
|
'@self', |
177
|
|
|
$dispatcher, |
178
|
|
|
new Nette\PhpGenerator\PhpLiteral(Events\EnableEvent::class), |
179
|
|
|
]); |
180
|
|
|
$packagesManager->addSetup('?->onDisable[] = function() {?->dispatch(new ?(...func_get_args()));}', [ |
181
|
|
|
'@self', |
182
|
|
|
$dispatcher, |
183
|
|
|
new Nette\PhpGenerator\PhpLiteral(Events\DisableEvent::class), |
184
|
|
|
]); |
185
|
|
|
$packagesManager->addSetup('?->onUpgrade[] = function() {?->dispatch(new ?(...func_get_args()));}', [ |
186
|
|
|
'@self', |
187
|
|
|
$dispatcher, |
188
|
|
|
new Nette\PhpGenerator\PhpLiteral(Events\UpgradeEvent::class), |
189
|
|
|
]); |
190
|
|
|
$packagesManager->addSetup('?->onInstall[] = function() {?->dispatch(new ?(...func_get_args()));}', [ |
191
|
|
|
'@self', |
192
|
|
|
$dispatcher, |
193
|
|
|
new Nette\PhpGenerator\PhpLiteral(Events\InstallEvent::class), |
194
|
|
|
]); |
195
|
|
|
$packagesManager->addSetup('?->onUninstall[] = function() {?->dispatch(new ?(...func_get_args()));}', [ |
196
|
|
|
'@self', |
197
|
|
|
$dispatcher, |
198
|
|
|
new Nette\PhpGenerator\PhpLiteral(Events\UninstallEvent::class), |
199
|
|
|
]); |
200
|
|
|
$packagesManager->addSetup('?->onRegister[] = function() {?->dispatch(new ?(...func_get_args()));}', [ |
201
|
|
|
'@self', |
202
|
|
|
$dispatcher, |
203
|
|
|
new Nette\PhpGenerator\PhpLiteral(Events\RegisterEvent::class), |
204
|
|
|
]); |
205
|
|
|
$packagesManager->addSetup('?->onUnregister[] = function() {?->dispatch(new ?(...func_get_args()));}', [ |
206
|
|
|
'@self', |
207
|
|
|
$dispatcher, |
208
|
|
|
new Nette\PhpGenerator\PhpLiteral(Events\UnregisterEvent::class), |
209
|
|
|
]); |
210
|
|
|
} |
211
|
1 |
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @param Nette\Configurator $config |
215
|
|
|
* @param string $extensionName |
216
|
|
|
* |
217
|
|
|
* @return void |
218
|
|
|
*/ |
219
|
|
|
public static function register(Nette\Configurator $config, string $extensionName = 'packages') : void |
220
|
|
|
{ |
221
|
1 |
|
$config->onCompile[] = function (Nette\Configurator $config, Nette\DI\Compiler $compiler) use ($extensionName) { |
222
|
1 |
|
$compiler->addExtension($extensionName, new PackagesExtension()); |
223
|
1 |
|
}; |
224
|
1 |
|
} |
225
|
|
|
} |
226
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.