Completed
Push — master ( 60e59a...334051 )
by Adam
06:35
created

PackagesExtensions   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
c 1
b 0
f 1
lcom 1
cbo 5
dl 0
loc 133
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B loadConfiguration() 0 75 5
A beforeCompile() 0 14 2
A register() 0 6 1
1
<?php
2
/**
3
 * PackagesExtensions.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        http://www.ipublikuj.eu
7
 * @author         Adam Kadlec http://www.ipublikuj.eu
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
use Nette\Utils;
22
use Nette\Neon;
23
use Nette\PhpGenerator as Code;
24
25
use Kdyby\Console;
26
27
use IPub;
28
use IPub\Packages;
29
use IPub\Packages\Commands;
30
use IPub\Packages\Entities;
31
use IPub\Packages\Exceptions;
32
use IPub\Packages\Helpers;
33
use IPub\Packages\Installers;
34
use IPub\Packages\Loaders;
35
use IPub\Packages\Repository;
36
37
/**
38
 * Packages extension container
39
 *
40
 * @package        iPublikuj:Packages!
41
 * @subpackage     DI
42
 *
43
 * @author         Adam Kadlec <[email protected]>
44
 */
45
final class PackagesExtensions extends DI\CompilerExtension
46
{
47
	/**
48
	 * Extension default configuration
49
	 *
50
	 * @var array
51
	 */
52
	private $defaults = [
53
		'path'       => NULL,                        // Paths where to search for packages
54
		'dirs'       => [                            // Define path to folders
55
			'configDir' => '%appDir%/config',        // Path where is stored app configuration
56
			'vendorDir' => '%appDir%/../vendor',     // Path to composer vendor folder
57
			'tempDir'   => '%tempDir%',              // Path to temporary folder
58
		],
59
		'configFile' => 'config.neon',                // Filename with enabled packages extensions
60
		'loader'     => [
61
			'packageFiles' => [
62
				'package.php',
63
			],
64
		],
65
		'sources'    => [
66
			'https://raw.github.com/ipublikuj/packages-metadata/master/metadata.json'
67
		],
68
	];
69
70
	/**
71
	 * @return void
72
	 */
73
	public function loadConfiguration()
74
	{
75
		// Get container builder
76
		$builder = $this->getContainerBuilder();
77
		// Get extension configuration
78
		$configuration = $this->getConfig($this->defaults);
79
80
		/**
81
		 * Load packages configuration
82
		 */
83
84
		$builder->parameters['packages'] = [];
85
86
		if (is_file($configuration['dirs']['configDir'] . DIRECTORY_SEPARATOR . 'packages.php')) {
87
			$packages = require $configuration['dirs']['configDir'] . DIRECTORY_SEPARATOR . 'packages.php';
88
89
			foreach ($packages as $name => $data) {
90
				$builder->parameters['packages'][$name] = $data;
91
			}
92
		}
93
94
		/**
95
		 * Register services
96
		 */
97
98
		$builder->addDefinition($this->prefix('loader'))
99
			->setClass(Loaders\Loader::CLASS_NAME, [
100
				'packageFiles'    => $configuration['loader']['packageFiles'],
101
				'metadataSources' => $configuration['sources'],
102
				'vendorDir'       => $configuration['dirs']['vendorDir'],
103
			])
104
			->addTag('cms.packages');
105
106
		$repository = $builder->addDefinition($this->prefix('repository'))
107
			->setClass(Repository\Repository::CLASS_NAME)
108
			->addTag('cms.packages');
109
110
		if ($configuration['path']) {
111
			$repository->addSetup('addPath', [$configuration['path']]);
112
		}
113
114
		$builder->addDefinition($this->prefix('manager'))
115
			->setClass(Packages\PackagesManager::CLASS_NAME, [
116
				'vendorDir' => $configuration['dirs']['vendorDir'],
117
				'configDir' => $configuration['dirs']['configDir'],
118
			])
119
			->addTag('cms.packages');
120
121
		$builder->addDefinition($this->prefix('pathResolver'))
122
			->setClass(Helpers\PathResolver::CLASS_NAME)
123
			->addTag('cms.packages');
124
125
		$builder->addDefinition($this->prefix('scripts.configuration'))
126
			->setClass(Packages\Scripts\ConfigurationScript::CLASS_NAME, [
127
				'configDir'  => $configuration['dirs']['configDir'],
128
				'configFile' => $configuration['configFile'],
129
			])
130
			->addTag('cms.packages');
131
132
		// Define all console commands
133
		$commands = [
134
			'packagesSync'     => Commands\SyncCommand::class,
135
			'packagesList'     => Commands\ListCommand::class,
136
			'packageEnable'    => Commands\EnableCommand::class,
137
			'packageDisable'   => Commands\DisableCommand::class,
138
			'packageInstall'   => Commands\InstallCommand::class,
139
			'packageUninstall' => Commands\UninstallCommand::class,
140
		];
141
142
		foreach ($commands as $name => $cmd) {
143
			$builder->addDefinition($this->prefix('commands' . lcfirst($name)))
144
				->setClass($cmd)
145
				->addTag(Console\DI\ConsoleExtension::TAG_COMMAND);
146
		}
147
	}
148
149
	/**
150
	 * {@inheritdoc}
151
	 */
152
	public function beforeCompile()
153
	{
154
		parent::beforeCompile();
155
156
		// Get container builder
157
		$builder = $this->getContainerBuilder();
158
159
		// Get packages manager
160
		$manager = $builder->getDefinition($this->prefix('manager'));
161
162
		foreach ($builder->findByType(Packages\Scripts\IScript::INTERFACE_NAME) as $serviceDefinition) {
163
			$manager->addSetup('addScript', [$serviceDefinition->getClass(), $serviceDefinition]);
164
		}
165
	}
166
167
	/**
168
	 * @param Nette\Configurator $config
169
	 * @param string $extensionName
170
	 */
171
	public static function register(Nette\Configurator $config, string $extensionName = 'packages')
172
	{
173
		$config->onCompile[] = function (Nette\Configurator $config, Nette\DI\Compiler $compiler) use ($extensionName) {
174
			$compiler->addExtension($extensionName, new PackagesExtensions());
175
		};
176
	}
177
}
178