Skip to content

Commit 4e3934f

Browse files
committed
Update CodeigniterVite Class
1 parent 92bf6a8 commit 4e3934f

File tree

2 files changed

+20
-27
lines changed

2 files changed

+20
-27
lines changed

src/CodeigniterVite.php

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,14 @@ class CodeigniterVite
88
/**
99
* @var string manifest path.
1010
*/
11-
private $manifest;
12-
13-
public function __construct()
14-
{
15-
$this->manifest = is_file(FCPATH . 'manifest.json') ? FCPATH . 'manifest.json' : null;
16-
}
11+
private static $manifest = FCPATH . 'manifest.json';
1712

1813
/**
1914
* Get vite entry file on running or bundled files instead.
2015
*
2116
* @return string single script tag on developing and much more on production
2217
*/
23-
public function tags()
18+
public static function tags(): ?string
2419
{
2520
# Check if vite is running.
2621
$entryFile = env('VITE_ORIGIN') . '/' . env('VITE_RESOURCES_DIR') . '/' . env('VITE_ENTRY_FILE');
@@ -30,14 +25,14 @@ public function tags()
3025
# React HMR fix.
3126
if (!empty($result))
3227
{
33-
$result = $this->getReactTag() . "$result";
28+
$result = self::getReactTag() . "$result";
3429
}
3530

3631
# If vite isn't running, then return the compiled resources.
37-
if (empty($result) && $this->manifest)
32+
if (empty($result) && is_file(self::$manifest))
3833
{
3934
# Get the manifest content.
40-
$manifest = file_get_contents($this->manifest);
35+
$manifest = file_get_contents(self::$manifest);
4136
# You look much pretty as an php object =).
4237
$manifest = json_decode($manifest);
4338

@@ -67,7 +62,7 @@ public function tags()
6762
*
6863
* @return string|null a simple module script
6964
*/
70-
public function getReactTag()
65+
public static function getReactTag(): ?string
7166
{
7267
if (env('VITE_FRAMEWORK') === 'react')
7368
{
@@ -84,22 +79,23 @@ public function getReactTag()
8479
*
8580
* @return bool true if vite is runnig or if manifest does exist, otherwise false;
8681
*/
87-
public function check(): bool
82+
public static function check(): bool
8883
{
8984
# Check if vite is running.
9085
$entryFile = env('VITE_ORIGIN') . '/' . env('VITE_RESOURCES_DIR') . '/' . env('VITE_ENTRY_FILE');
9186

92-
if (@file_get_contents($entryFile))
93-
{
94-
$result = true;
95-
}
96-
elseif (!empty($this->manifest))
97-
{
98-
$result = true;
99-
}
100-
else
87+
switch (true)
10188
{
102-
$result = false;
89+
case @file_get_contents($entryFile):
90+
$result = true;
91+
break;
92+
case !empty(self::$manifest):
93+
$result = true;
94+
break;
95+
96+
default:
97+
$result = false;
98+
break;
10399
}
104100

105101
return $result;

src/Decorator.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@ class Decorator implements ViewDecoratorInterface
88
{
99
public static function decorate(string $html): string
1010
{
11-
$vite = new CodeigniterVite();
12-
1311
# Check if vite is running or manifest is ready.
14-
if ($vite->check())
12+
if (CodeigniterVite::check())
1513
{
1614
# Get generated js and css tags.
17-
$tags = $vite->tags();
15+
$tags = CodeigniterVite::tags();
1816

1917
# Insert tags just before "</head>" tag.
2018
$html = empty($tags) ? $html : str_replace('</head>', "\n\t$tags\n</head>", $html);
@@ -28,7 +26,6 @@ public static function decorate(string $html): string
2826
}
2927
}
3028

31-
# If not, then just return the html as it is.
3229
return $html;
3330
}
3431
}

0 commit comments

Comments
 (0)