-
Notifications
You must be signed in to change notification settings - Fork 18
/
Extension.php
86 lines (65 loc) · 1.98 KB
/
Extension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
/**
* Copyright (c) dotBlue (http://dotblue.net)
*/
namespace DotBlue\WebImages;
use Nette\DI;
class Extension extends DI\CompilerExtension
{
/** @var array */
private $defaults = [
'routes' => [],
'rules' => [],
'providers' => [],
'wwwDir' => '%wwwDir%',
];
public function loadConfiguration()
{
$container = $this->getContainerBuilder();
$config = $this->getConfig($this->defaults);
$validator = $container->addDefinition($this->prefix('validator'))
->setClass('DotBlue\WebImages\Validator');
$generator = $container->addDefinition($this->prefix('generator'))
->setClass('DotBlue\WebImages\Generator', [
$config['wwwDir'],
]);
foreach ($config['rules'] as $rule) {
$validator->addSetup('$service->addRule(?, ?, ?)', [
$rule['width'],
$rule['height'],
isset($rule['algorithm']) ? $rule['algorithm'] : NULL,
]);
}
$i = 0;
foreach ($config['routes'] as $route => $defaults) {
if (!is_array($defaults)) {
$route = $defaults;
$defaults = [];
}
$route = $container->addDefinition($this->prefix('route' . $i))
->setClass('DotBlue\WebImages\Route', [
$route,
$defaults,
$this->prefix('@generator'),
])
->setAutowired(FALSE);
$container->getDefinition('router')
->addSetup('$service[] = ?', [
$this->prefix('@route' . $i),
]);
$i++;
}
if (count($config['providers']) === 0) {
throw new InvalidConfigException("You have to register at least one IProvider in '" . $this->prefix('providers') . "' directive.");
}
foreach ($config['providers'] as $name => $provider) {
$this->compiler->parseServices($container, [
'services' => [$this->prefix('provider' . $name) => $provider],
]);
$generator->addSetup('addProvider', [$this->prefix('@provider' . $name)]);
}
$latte = $container->getDefinition('nette.latteFactory');
$latte->addSetup('DotBlue\WebImages\Macros::install(?->getCompiler())', ['@self']);
}
}
class InvalidConfigException extends \Exception {}