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