1 | <?php |
||
36 | 1 | final class PackagesExtension extends DI\CompilerExtension |
|
37 | { |
||
38 | /** |
||
39 | * Extension default configuration |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | private $defaults = [ |
||
44 | 'path' => NULL, // Paths where to search for packages |
||
45 | 'dirs' => [ // Define path to folders |
||
46 | 'configDir' => '%appDir%/config', // Path where is stored app configuration |
||
47 | 'vendorDir' => '%appDir%/../vendor', // Path to composer vendor folder |
||
48 | 'tempDir' => '%tempDir%', // Path to temporary folder |
||
49 | ], |
||
50 | 'configFile' => 'config.neon', // Filename with enabled packages extensions |
||
51 | 'loader' => [ |
||
52 | 'packageFiles' => [ |
||
53 | 'package.php', |
||
54 | ], |
||
55 | ], |
||
56 | 'sources' => [], |
||
57 | ]; |
||
58 | |||
59 | /** |
||
60 | * @return void |
||
61 | */ |
||
62 | public function loadConfiguration() : void |
||
63 | { |
||
64 | // Get container builder |
||
65 | 1 | $builder = $this->getContainerBuilder(); |
|
66 | // Get extension configuration |
||
67 | 1 | $configuration = $this->getConfig($this->defaults); |
|
68 | |||
69 | /** |
||
70 | * Load packages configuration |
||
71 | */ |
||
72 | |||
73 | 1 | $builder->parameters['packages'] = []; |
|
74 | |||
75 | 1 | if (is_file($configuration['dirs']['configDir'] . DIRECTORY_SEPARATOR . 'packages.php')) { |
|
76 | $packages = require $configuration['dirs']['configDir'] . DIRECTORY_SEPARATOR . 'packages.php'; |
||
77 | |||
78 | foreach ($packages as $name => $data) { |
||
79 | $builder->parameters['packages'][$name] = $data; |
||
80 | } |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Register services |
||
85 | */ |
||
86 | |||
87 | $builder->addDefinition($this->prefix('loader')) |
||
88 | ->setType(Loaders\Loader::class) |
||
89 | ->setArguments([ |
||
90 | 'packageFiles' => $configuration['loader']['packageFiles'], |
||
91 | 'metadataSources' => $configuration['sources'], |
||
92 | 'vendorDir' => $configuration['dirs']['vendorDir'], |
||
93 | ]) |
||
94 | ->addTag('cms.packages'); |
||
95 | |||
96 | $repository = $builder->addDefinition($this->prefix('repository')) |
||
97 | ->setType(Repository\Repository::class) |
||
98 | ->addTag('cms.packages'); |
||
99 | |||
100 | if ($configuration['path']) { |
||
101 | $repository->addSetup('addPath', [$configuration['path']]); |
||
102 | } |
||
103 | |||
104 | $builder->addDefinition($this->prefix('manager')) |
||
105 | ->setType(Packages\PackagesManager::class) |
||
106 | ->setArguments([ |
||
107 | 'vendorDir' => $configuration['dirs']['vendorDir'], |
||
108 | 'configDir' => $configuration['dirs']['configDir'], |
||
109 | ]) |
||
110 | ->addTag('cms.packages'); |
||
111 | |||
112 | $builder->addDefinition($this->prefix('pathResolver')) |
||
113 | ->setType(Helpers\PathResolver::class) |
||
114 | ->addTag('cms.packages'); |
||
115 | |||
116 | $builder->addDefinition($this->prefix('scripts.configuration')) |
||
117 | ->setType(Packages\Scripts\ConfigurationScript::class) |
||
118 | ->setArguments([ |
||
119 | 'configDir' => $configuration['dirs']['configDir'], |
||
120 | 'configFile' => $configuration['configFile'], |
||
121 | ]) |
||
122 | ->addTag('cms.packages'); |
||
123 | |||
124 | // Define all console commands |
||
125 | $commands = [ |
||
126 | 'packagesSync' => Commands\SyncCommand::class, |
||
127 | 'packagesList' => Commands\ListCommand::class, |
||
128 | 'packageEnable' => Commands\EnableCommand::class, |
||
129 | 'packageDisable' => Commands\DisableCommand::class, |
||
130 | 'packageInstall' => Commands\InstallCommand::class, |
||
131 | 'packageUninstall' => Commands\UninstallCommand::class, |
||
132 | ]; |
||
133 | |||
134 | foreach ($commands as $name => $cmd) { |
||
135 | $builder->addDefinition($this->prefix('commands' . lcfirst($name))) |
||
136 | ->setType($cmd); |
||
137 | } |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * {@inheritdoc} |
||
142 | */ |
||
143 | public function beforeCompile() : void |
||
157 | |||
158 | /** |
||
159 | * @param Nette\Configurator $config |
||
160 | * @param string $extensionName |
||
161 | * |
||
162 | * @return void |
||
163 | */ |
||
164 | public static function register(Nette\Configurator $config, string $extensionName = 'packages') : void |
||
170 | } |
||
171 |