Completed
Push — master ( 389cfb...bc3e86 )
by Michal
07:54
created

Extension   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 79.17%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 3
c 2
b 1
f 0
lcom 1
cbo 1
dl 0
loc 71
ccs 19
cts 24
cp 0.7917
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A beforeCompile() 0 23 1
A validateChoices() 0 11 2
1
<?php
2
3
namespace Webrouse\AssetMacro\DI;
4
5
use Nette\DI\CompilerExtension;
6
use Nette\Utils\Validators;
7
use Webrouse\AssetMacro\AssetMacro;
8
use Webrouse\AssetMacro\Exceptions\UnexpectedValueException;
9
10
11
class Extension extends CompilerExtension
12
{
13
14
	/**
15
	 * Default configuration
16
	 * @var array
17
	 */
18
	public $defaults = [
19
	    // Public www dir
20
		'wwwDir' => '%wwwDir%',
21
        // Assets revision manifest
22
		'manifest' => NULL,
23
        // Paths for manifest autodetection
24
		'autodetect' => [
25
            'assets.json',
26
		    'busters.json',
27
			'versions.json',
28
            'manifest.json',
29
			'rev-manifest.json',
30
		],
31
        // Check the existence of asset file
32
        'existCheck' => TRUE,
33
        // Error handling
34
		'missingAsset' => 'notice',    // exception, notice, or ignore
35
		'missingManifest' => 'notice', // exception, notice, or ignore
36
		'missingRevision' => 'ignore',  // exception, notice, or ignore
37
	];
38
39
40
	public function beforeCompile()
41
	{
42 1
		$config = $this->getContainerBuilder()->expand($this->validateConfig($this->defaults));
43
44
		// Validate configuration
45 1
        Validators::assertField($config, 'wwwDir', 'string');
46 1
        Validators::assertField($config, 'manifest', 'null|string|array');
47 1
        Validators::assertField($config, 'autodetect', 'array');
48 1
        Validators::assertField($config, 'existCheck', 'bool');
49 1
		$choices = ['exception', 'notice', 'ignore'];
50 1
		$this->validateChoices('missingAsset', $choices);
51 1
		$this->validateChoices('missingManifest', $choices);
52 1
		$this->validateChoices('missingRevision', $choices);
53
54
        // Setup macro
55 1
		$builder = $this->getContainerBuilder();
56 1
		$builder->getDefinition('latte.latteFactory')
57 1
			->addSetup("?->addProvider(?, ?)", ['@self', AssetMacro::CONFIG_PROVIDER, $config])
58 1
			->addSetup("?->onCompile[] = function(\$engine) { " .
59 1
				AssetMacro::class . "::install(\$engine->getCompiler()); }",
60 1
				['@self']
61
			);
62 1
	}
63
64
65
	/**
66
	 * @param string $key
67
	 * @param array $choices
68
	 */
69 1
	private function validateChoices($key, array $choices)
70
	{
71 1
		if ( ! in_array($this->config[$key], $choices)) {
72
			throw new UnexpectedValueException(sprintf(
73
				"Unexpected value '%s' of '%s' configuration key. Allowed values: %s.",
74
				$this->config[$key],
75
				$this->prefix($key),
76
				implode(', ', $choices)
77
			));
78
		}
79 1
	}
80
81
}
82