Completed
Push — master ( 2b133d...acf90e )
by Michal
05:36
created

Extension::validateChoices()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Webrouse\AssetMacro\DI;
5
6
use Nette\DI\CompilerExtension;
7
use Nette\DI\Definitions\FactoryDefinition;
8
use Nette\DI\Definitions\ServiceDefinition;
9
use Nette\DI\Helpers;
10
use Nette\Utils\Validators;
11
use Webrouse\AssetMacro\AssetMacro;
12
use Webrouse\AssetMacro\Config;
13
use Webrouse\AssetMacro\Exceptions\UnexpectedValueException;
14
use Webrouse\AssetMacro\ManifestService;
15
use Webrouse\AssetMacro\Utils;
16
17
18 1
class Extension extends CompilerExtension
19
{
20
21
	/**
22
	 * Default configuration
23
	 * @var array
24
	 */
25
	public const DEFAULTS = [
26
		// Cache generated output
27
		'cache' => '%productionMode%',
28
		// Assets revision manifest
29
		'manifest' => null,
30
		// Paths for manifest auto-detection
31
		'autodetect' => [
32
			'assets.json',
33
			'busters.json',
34
			'versions.json',
35
			'manifest.json',
36
			'rev-manifest.json',
37
		],
38
		// Absolute path to assets dir
39
		'assetsPath' => '%wwwDir%/',
40
		// Public path to "assetsPath"
41
		'publicPath' => '/',
42
		// Error handling (exception, notice, or ignore)
43
		'missingAsset' => 'notice',
44
		'missingManifest' => 'notice',
45
		'missingRevision' => 'notice',
46
		// Default format
47
		'format' => '%%url%%',
48
	];
49
50
51
	public function loadConfiguration()
52
	{
53 1
		$builder = $this->getContainerBuilder();
54 1
		$config = Helpers::expand($this->validateConfig(self::DEFAULTS), $builder->parameters);
55
56
		// Validate types
57 1
		Validators::assertField($config, 'assetsPath', 'string');
58 1
		Validators::assertField($config, 'publicPath', 'string');
59 1
		Validators::assertField($config, 'manifest', 'null|string|array');
60 1
		Validators::assertField($config, 'autodetect', 'array');
61 1
		Validators::assertField($config, 'format', 'string');
62
63
		// Validate policies
64 1
		$choices = [Utils::MISSING_POLICY_IGNORE, Utils::MISSING_POLICY_NOTICE, Utils::MISSING_POLICY_EXCEPTION];
65 1
		$this->validatePolicy($config, 'missingAsset', $choices);
66 1
		$this->validatePolicy($config, 'missingManifest', $choices);
67 1
		$this->validatePolicy($config, 'missingRevision', $choices);
68
69
		// Config
70
		$builder
71 1
			->addDefinition($this->prefix('config'))
72 1
			->setFactory(Config::class, [$config]);
73
74
		// Manifest service
75
		$builder
76 1
			->addDefinition($this->prefix('manifest'))
77 1
			->setFactory(ManifestService::class, [$this->prefix('@config')]);
78 1
	}
79
80
81
	public function beforeCompile()
82
	{
83 1
		$builder = $this->getContainerBuilder();
84
85
		// Setup macro
86 1
		$latteDefinition = $builder->getDefinition('latte.latteFactory');
87
88
		// Compatibility with Nette 3.0
89 1
		if (class_exists(FactoryDefinition::class) && $latteDefinition instanceof FactoryDefinition) {
0 ignored issues
show
Bug introduced by
The class Nette\DI\Definitions\FactoryDefinition does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
90 1
			$latteDefinition = $latteDefinition->getResultDefinition();
91
		}
92
93
		/** @var ServiceDefinition $latteDefinition */
94
		$latteDefinition
95 1
			->addSetup('?->addProvider(?, ?)', ['@self', AssetMacro::MANIFEST_PROVIDER, $this->prefix('@manifest')])
96 1
			->addSetup('?->onCompile[] = function($engine) { ' .
97 1
				AssetMacro::class . '::install($engine->getCompiler()); }',
98 1
				['@self']
99
			);
100 1
	}
101
102
103 1
	private function validatePolicy(array &$config, $key, array $choices): void
104
	{
105 1
		if (!in_array($config[$key], $choices, true)) {
106 1
			throw new UnexpectedValueException(sprintf(
107 1
				"Unexpected value '%s' of '%s' configuration key. Allowed values: %s.",
108 1
				$config[$key],
109 1
				$this->prefix($key),
110 1
				implode(', ', $choices)
111
			));
112
		}
113 1
	}
114
}
115