forked from lubomirblazekcz/nette-vite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extension.php
158 lines (128 loc) · 4.67 KB
/
Extension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php declare(strict_types=1);
namespace MMEE\Vite\Nette;
use MMEE\Vite\AssetFilter;
use MMEE\Vite\ManifestFileDoesNotExistsException;
use MMEE\Vite\Service;
use MMEE\Vite\Tracy\VitePanel;
use Nette;
use Nette\DI\CompilerExtension;
use Nette\DI\Definitions;
use Nette\Schema;
use Tracy;
/**
* @property \stdClass $config
*/
final class Extension extends CompilerExtension
{
public function getConfigSchema(): Schema\Schema
{
return Schema\Expect::structure([
'server' => Schema\Expect::string('http://localhost:3000'),
'debugMode' => Schema\Expect::bool($this->getContainerBuilder()->parameters['debugMode'] ?? false),
'manifestFile' => Schema\Expect::string(),
'filterName' => Schema\Expect::string('asset'), // empty string is for disabled
'templateProperty' => Schema\Expect::string('_vite'), // empty string is for disabled
'wwwDir' => Schema\Expect::string($this->getContainerBuilder()->parameters['wwwDir'] ?? getcwd()),
'basePath' => Schema\Expect::string(),
]);
}
public function loadConfiguration(): void
{
$this->buildViteService();
$this->buildFilter();
}
private function buildViteService(): void
{
$manifestFile = $this->prepareManifestPath();
$this->getContainerBuilder()->addDefinition($this->prefix('service'))
->setFactory(Service::class)
->setArguments([
'viteServer' => $this->config->server,
'manifestFile' => $manifestFile,
'debugMode' => $this->config->debugMode,
'basePath' => $this->prepareBasePath($manifestFile),
]);
}
private function buildFilter(): void
{
$this->getContainerBuilder()->addDefinition($this->prefix('assetFilter'))
->setFactory(AssetFilter::class)
->setAutowired(false);
}
public function beforeCompile(): void
{
$builder = $this->getContainerBuilder();
$templateFactoryDefinition = $builder->getDefinition('latte.templateFactory');
assert($templateFactoryDefinition instanceof Definitions\ServiceDefinition);
if ($this->config->templateProperty !== '') {
$templateFactoryDefinition->addSetup(
new Nette\DI\Definitions\Statement('$onCreate[]', [
new Definitions\Statement([
self::class,
'prepareTemplate',
], [$this->config->templateProperty, $builder->getDefinition($this->prefix('service'))]),
]),
);
}
if ($this->config->filterName !== '' && $builder->hasDefinition('latte.latteFactory')) {
$definition = $builder->getDefinition('latte.latteFactory');
assert($definition instanceof Definitions\FactoryDefinition);
$definition->getResultDefinition()
->addSetup('addFilter', [
$this->config->filterName,
$builder->getDefinition($this->prefix('assetFilter')),
]);
}
$tracyClass = Tracy\Bar::class;
if ($this->config->debugMode && $builder->getByType($tracyClass)) {
$definition = $this->getContainerBuilder()
->getDefinition($this->prefix('service'));
assert($definition instanceof Definitions\ServiceDefinition);
$definition->addSetup("@$tracyClass::addPanel", [
new Definitions\Statement(VitePanel::class),
]);
}
}
public static function prepareTemplate(string $propertyName, Service $service): \Closure
{
return static function (Nette\Application\UI\Template $template) use ($propertyName, $service): void {
$template->{$propertyName} = $service;
};
}
private function prepareManifestPath(): string
{
if ($this->config->manifestFile === null) {
return $this->automaticSearchManifestFile();
}
$manifestFile = $this->config->manifestFile;
if (!is_file($manifestFile)) {
$newPath = $this->config->wwwDir . DIRECTORY_SEPARATOR . ltrim($manifestFile, '/\\');
if (!is_file($newPath)) {
throw new ManifestFileDoesNotExistsException(sprintf('Found here "%s" or "%s".', $manifestFile, $newPath));
}
$manifestFile = $newPath;
}
return Nette\Safe::realpath($manifestFile);
}
private function prepareBasePath(string $manifestFile): string
{
if ($this->config->basePath === null) {
return str_replace(Nette\Safe::realpath($this->config->wwwDir), '', dirname($manifestFile)) . '/';
}
return $this->config->basePath;
}
private function automaticSearchManifestFile(): string
{
$finder = Nette\Utils\Finder::findFiles('manifest.json')->from($this->config->wwwDir);
$files = [];
foreach ($finder as $file) {
$files[] = $file->getPathname();
}
if ($files === []) {
throw new ManifestFileDoesNotExistsException(sprintf('Define path to manifest.json, because automatic search found nothing in "%s".', $this->config->wwwDir));
} elseif (count($files) > 1) {
throw new ManifestFileDoesNotExistsException(sprintf('Define path to manifest.json, because automatic search found many manifest.json files %s.', implode(', ', $files)));
}
return reset($files);
}
}