1- <?php
2-
3- declare (strict_types=1 );
1+ <?php declare (strict_types = 1 );
42
53namespace Contributte \Vite \Nette ;
64
75use Contributte \Vite \AssetFilter ;
86use Contributte \Vite \ManifestFileDoesNotExistsException ;
97use Contributte \Vite \Service ;
108use Contributte \Vite \Tracy \VitePanel ;
11- use Nette ;
129use 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 ;
1619
1720/**
18- * @property \ stdClass $config
21+ * @property stdClass $config
1922 */
2023final class Extension extends CompilerExtension
2124{
22- public function getConfigSchema (): Schema \Schema
25+
26+ public function getConfigSchema (): Schema
2327 {
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 (),
3337 ]);
3438 }
3539
@@ -39,39 +43,18 @@ public function loadConfiguration(): void
3943 $ this ->buildFilter ();
4044 }
4145
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-
6346 public function beforeCompile (): void
6447 {
6548 $ builder = $ this ->getContainerBuilder ();
6649
6750 $ templateFactoryDefinition = $ builder ->getDefinition ('latte.templateFactory ' );
68- assert ($ templateFactoryDefinition instanceof Definitions \ ServiceDefinition);
51+ assert ($ templateFactoryDefinition instanceof ServiceDefinition);
6952
7053 if ($ this ->config ->templateProperty !== '' ) {
7154 $ 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,
7558 'prepareTemplate ' ,
7659 ], [$ this ->config ->templateProperty , $ builder ->getDefinition ($ this ->prefix ('service ' ))]),
7760 ]),
@@ -80,32 +63,45 @@ public function beforeCompile(): void
8063
8164 if ($ this ->config ->filterName !== '' && $ builder ->hasDefinition ('latte.latteFactory ' )) {
8265 $ definition = $ builder ->getDefinition ('latte.latteFactory ' );
83- assert ($ definition instanceof Definitions \ FactoryDefinition);
66+ assert ($ definition instanceof FactoryDefinition);
8467 $ definition ->getResultDefinition ()
8568 ->addSetup ('addFilter ' , [
8669 $ this ->config ->filterName ,
8770 $ builder ->getDefinition ($ this ->prefix ('assetFilter ' )),
8871 ]);
8972 }
9073
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 ) {
9575 $ definition = $ this ->getContainerBuilder ()
9676 ->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),
10082 ]);
10183 }
10284 }
10385
104- public static function prepareTemplate ( string $ propertyName , Service $ service ): \ Closure
86+ private function buildViteService ( ): void
10587 {
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 );
109105 }
110106
111107 private function prepareManifestPath (): string
@@ -124,25 +120,26 @@ private function prepareManifestPath(): string
124120 $ manifestFile = $ newPath ;
125121 }
126122
127- return Nette \ Safe::realpath ($ manifestFile );
123+ return Safe::realpath ($ manifestFile );
128124 }
129125
130126 private function prepareBasePath (string $ manifestFile ): string
131127 {
132128 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 )) . '/ ' ;
134130 }
135131
136132 return $ this ->config ->basePath ;
137133 }
138134
139135 private function automaticSearchManifestFile (): string
140136 {
141- $ finder = Nette \ Utils \ Finder::findFiles ('manifest.json ' )->from ($ this ->config ->wwwDir );
137+ $ finder = Finder::findFiles ('manifest.json ' )->from ($ this ->config ->wwwDir );
142138 $ files = [];
143139 foreach ($ finder as $ file ) {
144140 $ files [] = $ file ->getPathname ();
145141 }
142+
146143 if ($ files === []) {
147144 throw new ManifestFileDoesNotExistsException (sprintf ('Define path to manifest.json, because automatic search found nothing in "%s". ' , $ this ->config ->wwwDir ));
148145 } elseif (count ($ files ) > 1 ) {
@@ -151,4 +148,5 @@ private function automaticSearchManifestFile(): string
151148
152149 return reset ($ files );
153150 }
151+
154152}
0 commit comments