1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Webrouse\AssetMacro\DI; |
4
|
|
|
|
5
|
|
|
use Nette\DI\CompilerExtension; |
6
|
|
|
use Webrouse\AssetMacro\AssetMacro; |
7
|
|
|
use Webrouse\AssetMacro\Exceptions\UnexpectedValueException; |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class Extension extends CompilerExtension |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Default configuration |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
public $defaults = [ |
18
|
|
|
'wwwDir' => '%wwwDir%', |
19
|
|
|
'versions' => AssetMacro::VERSIONS_AUTODETECT, |
20
|
|
|
'autodetectPaths' => [ |
21
|
|
|
'busters.json', |
22
|
|
|
'versions.json', |
23
|
|
|
'rev-manifest.json', |
24
|
|
|
], |
25
|
|
|
'missingAsset' => 'exception', // exception, notice, or ignore |
26
|
|
|
'missingVersion' => 'ignore', // exception, notice, or ignore |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
public function beforeCompile() |
31
|
|
|
{ |
32
|
1 |
|
$config = $this->getContainerBuilder()->expand($this->validateConfig($this->defaults)); |
33
|
|
|
|
34
|
1 |
|
$choices = ['exception', 'notice', 'ignore']; |
35
|
1 |
|
$this->validateChoices('missingAsset', $choices); |
36
|
1 |
|
$this->validateChoices('missingVersion', $choices); |
37
|
|
|
|
38
|
1 |
|
$builder = $this->getContainerBuilder(); |
39
|
1 |
|
$builder->getDefinition('latte.latteFactory') |
40
|
1 |
|
->addSetup("?->addProvider(?, ?)", ['@self', AssetMacro::CONFIG_PROVIDER, $config]) |
41
|
1 |
|
->addSetup("?->onCompile[] = function(\$engine) { " . |
42
|
1 |
|
AssetMacro::class . "::install(\$engine->getCompiler()); }", |
43
|
1 |
|
['@self'] |
44
|
|
|
); |
45
|
1 |
|
} |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $key |
50
|
|
|
* @param array $choices |
51
|
|
|
*/ |
52
|
1 |
|
private function validateChoices($key, array $choices) |
53
|
|
|
{ |
54
|
1 |
|
if ( ! in_array($this->config[$key], $choices)) { |
55
|
1 |
|
throw new UnexpectedValueException(sprintf( |
56
|
1 |
|
"Unexpected value '%s' of '%s' configuration key. Allowed values: %s.", |
57
|
1 |
|
$this->config[$key], |
58
|
1 |
|
$this->prefix($key), |
59
|
1 |
|
implode(', ', $choices) |
60
|
|
|
)); |
61
|
|
|
} |
62
|
1 |
|
} |
63
|
|
|
|
64
|
|
|
} |
65
|
|
|
|