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\Formatter; |
15
|
|
|
use Webrouse\AssetMacro\ManifestService; |
16
|
|
|
use Webrouse\AssetMacro\Utils; |
17
|
|
|
|
18
|
|
|
|
19
|
1 |
|
class Extension extends CompilerExtension |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Default configuration |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
public const DEFAULTS = [ |
27
|
|
|
// Cache generated output |
28
|
|
|
'cache' => '%productionMode%', |
29
|
|
|
// Assets revision manifest |
30
|
|
|
'manifest' => null, |
31
|
|
|
// Paths for manifest auto-detection |
32
|
|
|
'autodetect' => [ |
33
|
|
|
'assets.json', |
34
|
|
|
'busters.json', |
35
|
|
|
'versions.json', |
36
|
|
|
'manifest.json', |
37
|
|
|
'rev-manifest.json', |
38
|
|
|
], |
39
|
|
|
// Absolute path to assets dir |
40
|
|
|
'assetsPath' => '%wwwDir%/', |
41
|
|
|
// Public path to "assetsPath" |
42
|
|
|
'publicPath' => '/', |
43
|
|
|
// Error handling (exception, notice, or ignore) |
44
|
|
|
'missingAsset' => 'notice', |
45
|
|
|
'missingManifest' => 'notice', |
46
|
|
|
'missingRevision' => 'notice', |
47
|
|
|
// Default format |
48
|
|
|
'format' => '%%url%%', |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
public function loadConfiguration() |
53
|
|
|
{ |
54
|
1 |
|
$builder = $this->getContainerBuilder(); |
55
|
1 |
|
$config = Helpers::expand($this->validateConfig(self::DEFAULTS), $builder->parameters); |
56
|
|
|
|
57
|
|
|
// Validate types |
58
|
1 |
|
Validators::assertField($config, 'assetsPath', 'string'); |
59
|
1 |
|
Validators::assertField($config, 'publicPath', 'string'); |
60
|
1 |
|
Validators::assertField($config, 'manifest', 'null|string|array'); |
61
|
1 |
|
Validators::assertField($config, 'autodetect', 'array'); |
62
|
1 |
|
Validators::assertField($config, 'format', 'string'); |
63
|
|
|
|
64
|
|
|
// Validate policies |
65
|
1 |
|
$choices = [Utils::MISSING_POLICY_IGNORE, Utils::MISSING_POLICY_NOTICE, Utils::MISSING_POLICY_EXCEPTION]; |
66
|
1 |
|
$this->validatePolicy($config, 'missingAsset', $choices); |
67
|
1 |
|
$this->validatePolicy($config, 'missingManifest', $choices); |
68
|
1 |
|
$this->validatePolicy($config, 'missingRevision', $choices); |
69
|
|
|
|
70
|
|
|
// Config |
71
|
|
|
$builder |
72
|
1 |
|
->addDefinition($this->prefix('config')) |
73
|
1 |
|
->setFactory(Config::class, [$config]); |
74
|
|
|
|
75
|
|
|
// Formatter |
76
|
|
|
$builder |
77
|
1 |
|
->addDefinition($this->prefix('formatter')) |
78
|
1 |
|
->setFactory(Formatter::class, [$this->prefix('@config')]); |
79
|
|
|
|
80
|
|
|
// Manifest service |
81
|
|
|
$builder |
82
|
1 |
|
->addDefinition($this->prefix('manifest')) |
83
|
1 |
|
->setFactory(ManifestService::class, [$this->prefix('@config'), $this->prefix('@formatter')]); |
84
|
1 |
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
public function beforeCompile() |
88
|
|
|
{ |
89
|
1 |
|
$builder = $this->getContainerBuilder(); |
90
|
|
|
|
91
|
|
|
// Setup macro |
92
|
1 |
|
$latteDefinition = $builder->getDefinition('latte.latteFactory'); |
93
|
|
|
|
94
|
|
|
// Compatibility with Nette 3.0 |
95
|
1 |
|
if (class_exists(FactoryDefinition::class) && $latteDefinition instanceof FactoryDefinition) { |
|
|
|
|
96
|
1 |
|
$latteDefinition = $latteDefinition->getResultDefinition(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** @var ServiceDefinition $latteDefinition */ |
100
|
|
|
$latteDefinition |
101
|
1 |
|
->addSetup('?->addProvider(?, ?)', ['@self', AssetMacro::MANIFEST_PROVIDER, $this->prefix('@manifest')]) |
102
|
1 |
|
->addSetup('?->onCompile[] = function($engine) { ' . |
103
|
1 |
|
AssetMacro::class . '::install($engine->getCompiler()); }', |
104
|
1 |
|
['@self'] |
105
|
|
|
); |
106
|
1 |
|
} |
107
|
|
|
|
108
|
|
|
|
109
|
1 |
|
private function validatePolicy(array &$config, $key, array $choices): void |
110
|
|
|
{ |
111
|
1 |
|
if (!in_array($config[$key], $choices, true)) { |
112
|
1 |
|
throw new UnexpectedValueException(sprintf( |
113
|
1 |
|
"Unexpected value '%s' of '%s' configuration key. Allowed values: %s.", |
114
|
1 |
|
$config[$key], |
115
|
1 |
|
$this->prefix($key), |
116
|
1 |
|
implode(', ', $choices) |
117
|
|
|
)); |
118
|
|
|
} |
119
|
1 |
|
} |
120
|
|
|
} |
121
|
|
|
|
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 thecomposer.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
orrequire-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 you have not tested against this specific condition, such errors might go unnoticed.