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 | // Merge extension default config |
||
65 | 1 | $this->setConfig(DI\Config\Helpers::merge($this->config, $this->getContainerBuilder()->expand($this->defaults))); |
|
66 | |||
67 | // Get container builder |
||
68 | $builder = $this->getContainerBuilder(); |
||
69 | // Get extension configuration |
||
70 | $configuration = $this->getConfig(); |
||
71 | |||
72 | /** |
||
73 | * Load packages configuration |
||
74 | */ |
||
75 | |||
76 | $builder->parameters['packages'] = []; |
||
77 | |||
78 | if (is_file($configuration['dirs']['configDir'] . DIRECTORY_SEPARATOR . 'packages.php')) { |
||
79 | $packages = require $configuration['dirs']['configDir'] . DIRECTORY_SEPARATOR . 'packages.php'; |
||
80 | |||
81 | foreach ($packages as $name => $data) { |
||
82 | $builder->parameters['packages'][$name] = $data; |
||
83 | } |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Register services |
||
88 | */ |
||
89 | |||
90 | $builder->addDefinition($this->prefix('loader')) |
||
91 | ->setType(Loaders\Loader::class) |
||
92 | ->setArguments([ |
||
93 | 'packageFiles' => $configuration['loader']['packageFiles'], |
||
94 | 'metadataSources' => $configuration['sources'], |
||
95 | 'vendorDir' => $configuration['dirs']['vendorDir'], |
||
96 | ]) |
||
97 | ->addTag('cms.packages'); |
||
98 | |||
99 | $repository = $builder->addDefinition($this->prefix('repository')) |
||
100 | ->setType(Repository\Repository::class) |
||
101 | ->addTag('cms.packages'); |
||
102 | |||
103 | if ($configuration['path']) { |
||
104 | $repository->addSetup('addPath', [$configuration['path']]); |
||
105 | } |
||
106 | |||
107 | $builder->addDefinition($this->prefix('manager')) |
||
108 | ->setType(Packages\PackagesManager::class) |
||
109 | ->setArguments([ |
||
110 | 'vendorDir' => $configuration['dirs']['vendorDir'], |
||
111 | 'configDir' => $configuration['dirs']['configDir'], |
||
112 | ]) |
||
113 | ->addTag('cms.packages'); |
||
114 | |||
115 | $builder->addDefinition($this->prefix('pathResolver')) |
||
116 | ->setType(Helpers\PathResolver::class) |
||
117 | ->addTag('cms.packages'); |
||
118 | |||
119 | $builder->addDefinition($this->prefix('scripts.configuration')) |
||
120 | ->setType(Packages\Scripts\ConfigurationScript::class) |
||
121 | ->setArguments([ |
||
122 | 'configDir' => $configuration['dirs']['configDir'], |
||
123 | 'configFile' => $configuration['configFile'], |
||
124 | ]) |
||
125 | ->addTag('cms.packages'); |
||
126 | |||
127 | // Define all console commands |
||
128 | $commands = [ |
||
129 | 'packagesSync' => Commands\SyncCommand::class, |
||
130 | 'packagesList' => Commands\ListCommand::class, |
||
131 | 'packageEnable' => Commands\EnableCommand::class, |
||
132 | 'packageDisable' => Commands\DisableCommand::class, |
||
133 | 'packageInstall' => Commands\InstallCommand::class, |
||
134 | 'packageUninstall' => Commands\UninstallCommand::class, |
||
135 | ]; |
||
136 | |||
137 | foreach ($commands as $name => $cmd) { |
||
138 | $builder->addDefinition($this->prefix('commands' . lcfirst($name))) |
||
139 | ->setType($cmd); |
||
140 | } |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * {@inheritdoc} |
||
145 | */ |
||
146 | public function beforeCompile() : void |
||
147 | { |
||
148 | parent::beforeCompile(); |
||
149 | |||
150 | // Get container builder |
||
151 | $builder = $this->getContainerBuilder(); |
||
152 | |||
153 | // Get packages manager |
||
154 | $manager = $builder->getDefinition($this->prefix('manager')); |
||
155 | |||
156 | foreach ($builder->findByType(Packages\Scripts\IScript::class) as $serviceDefinition) { |
||
157 | $manager->addSetup('addScript', [$serviceDefinition->getType(), $serviceDefinition]); |
||
158 | } |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * @param Nette\Configurator $config |
||
163 | * @param string $extensionName |
||
164 | * |
||
165 | * @return void |
||
166 | */ |
||
167 | public static function register(Nette\Configurator $config, string $extensionName = 'packages') : void |
||
173 | } |
||
174 |