|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lookyman\NetteTexy\DI; |
|
4
|
|
|
|
|
5
|
|
|
use Lookyman\NetteTexy\Configurator\ConfiguratorInterface; |
|
6
|
|
|
use Nette\DI\CompilerExtension; |
|
7
|
|
|
use Nette\DI\ContainerBuilder; |
|
8
|
|
|
use Nette\DI\ServiceCreationException; |
|
9
|
|
|
use Nette\DI\ServiceDefinition; |
|
10
|
|
|
use Texy\Texy; |
|
11
|
|
|
|
|
12
|
|
|
class TexyExtension extends CompilerExtension |
|
13
|
|
|
{ |
|
14
|
|
|
const TAG_CONFIGURATOR = 'texy.configurator'; |
|
15
|
|
|
|
|
16
|
|
|
public function loadConfiguration() |
|
17
|
|
|
{ |
|
18
|
|
|
$builder = $this->getContainerBuilder(); |
|
19
|
|
|
|
|
20
|
|
|
$builder->addDefinition($this->prefix('texy')) |
|
21
|
|
|
->setClass(Texy::class); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function beforeCompile() |
|
25
|
|
|
{ |
|
26
|
|
|
$builder = $this->getContainerBuilder(); |
|
27
|
|
|
|
|
28
|
|
|
$texy = $builder->getDefinition($this->prefix('texy')); |
|
|
|
|
|
|
29
|
|
|
$configurators = $this->getSortedConfigurators($builder); |
|
30
|
|
|
foreach ($configurators as $name => $priority) { |
|
31
|
|
|
if (!self::isOfType($builder->getDefinition($name)->getClass(), ConfiguratorInterface::class)) { |
|
32
|
|
|
throw new ServiceCreationException(); |
|
33
|
|
|
} |
|
34
|
|
|
$texy->addSetup('?->configure(?)', [ |
|
35
|
|
|
sprintf('@%s', $name), '@self' |
|
36
|
|
|
]); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$latteFactory = $builder->getByType('Nette\Bridges\ApplicationLatte\ILatteFactory'); |
|
40
|
|
|
if ($latteFactory === null || !self::isOfType($builder->getDefinition($latteFactory)->getClass(), 'Latte\Engine')) { |
|
41
|
|
|
$latteFactory = 'nette.latteFactory'; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
if ($builder->hasDefinition($latteFactory) && self::isOfType($builder->getDefinition($latteFactory)->getClass(), 'Latte\Engine')) { |
|
|
|
|
|
|
45
|
|
|
$this->registerToLatte($builder->getDefinition($latteFactory)); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if ($builder->hasDefinition('nette.latte')) { |
|
49
|
|
|
$this->registerToLatte($builder->getDefinition('nette.latte')); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param ContainerBuilder $builder |
|
55
|
|
|
* @return array |
|
56
|
|
|
*/ |
|
57
|
|
|
private function getSortedConfigurators(ContainerBuilder $builder) |
|
58
|
|
|
{ |
|
59
|
|
|
$configurators = $builder->findByTag(self::TAG_CONFIGURATOR); |
|
60
|
|
|
uasort($configurators, function ($a, $b) { |
|
|
|
|
|
|
61
|
|
|
$a = is_numeric($a) ? (float) $a : 0; |
|
62
|
|
|
$b = is_numeric($b) ? (float) $b : 0; |
|
63
|
|
|
return $a < $b ? -1 : ($a > $b ? 1 : 0); |
|
64
|
|
|
}); |
|
65
|
|
|
return $configurators; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param ServiceDefinition $def |
|
70
|
|
|
*/ |
|
71
|
|
|
private function registerToLatte(ServiceDefinition $def) |
|
72
|
|
|
{ |
|
73
|
|
|
$def->addSetup('?->onCompile[] = function (Latte\Engine $engine) { (new Texy\Bridges\Latte\TexyMacro($engine, ?))->install(); }', ['@self', $this->prefix('@texy')]) |
|
|
|
|
|
|
74
|
|
|
->addSetup('addFilter', ['texy', [$this->prefix('@texy'), 'process']]); |
|
75
|
|
|
|
|
76
|
|
|
if (method_exists('Latte\Engine', 'addProvider')) { |
|
77
|
|
|
$def->addSetup('addProvider', ['texy', $this->prefix('@texy')]); |
|
78
|
|
|
|
|
79
|
|
|
} else { |
|
80
|
|
|
$def->addSetup('?->addFilter(\'getTexy\', function () { return ?;})', ['@self', $this->prefix('@texy')]); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param string $class |
|
86
|
|
|
* @param string $type |
|
87
|
|
|
* @return bool |
|
88
|
|
|
*/ |
|
89
|
|
|
private static function isOfType($class, $type) |
|
90
|
|
|
{ |
|
91
|
|
|
return $class === $type || is_subclass_of($class, $type); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.