Skip to content

Commit

Permalink
Merge pull request #6 from firtadokei/dev
Browse files Browse the repository at this point in the history
Fix a bug
  • Loading branch information
mihatorikei authored Jul 12, 2022
2 parents 8256a47 + 92bf6a8 commit d75affc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
18 changes: 9 additions & 9 deletions src/CodeigniterVite.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ class CodeigniterVite
/**
* @var string manifest path.
*/
private static $manifest;
private $manifest;

public function __construct()
{
self::$manifest = is_file(FCPATH . 'manifest.json') ? FCPATH . 'manifest.json' : null;
$this->manifest = is_file(FCPATH . 'manifest.json') ? FCPATH . 'manifest.json' : null;
}

/**
* Get vite entry file on running or bundled files instead.
*
* @return string single script tag on developing and much more on production
*/
public static function tags()
public function tags()
{
# Check if vite is running.
$entryFile = env('VITE_ORIGIN') . '/' . env('VITE_RESOURCES_DIR') . '/' . env('VITE_ENTRY_FILE');
Expand All @@ -30,14 +30,14 @@ public static function tags()
# React HMR fix.
if (!empty($result))
{
$result = self::getReactTag() . "$result";
$result = $this->getReactTag() . "$result";
}

# If vite isn't running, then return the compiled resources.
if (empty($result) && self::$manifest)
if (empty($result) && $this->manifest)
{
# Get the manifest content.
$manifest = file_get_contents(self::$manifest);
$manifest = file_get_contents($this->manifest);
# You look much pretty as an php object =).
$manifest = json_decode($manifest);

Expand Down Expand Up @@ -67,7 +67,7 @@ public static function tags()
*
* @return string|null a simple module script
*/
public static function getReactTag()
public function getReactTag()
{
if (env('VITE_FRAMEWORK') === 'react')
{
Expand All @@ -84,7 +84,7 @@ public static function getReactTag()
*
* @return bool true if vite is runnig or if manifest does exist, otherwise false;
*/
public static function check(): bool
public function check(): bool
{
# Check if vite is running.
$entryFile = env('VITE_ORIGIN') . '/' . env('VITE_RESOURCES_DIR') . '/' . env('VITE_ENTRY_FILE');
Expand All @@ -93,7 +93,7 @@ public static function check(): bool
{
$result = true;
}
elseif (!empty(self::$manifest))
elseif (!empty($this->manifest))
{
$result = true;
}
Expand Down
6 changes: 4 additions & 2 deletions src/Decorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ class Decorator implements ViewDecoratorInterface
{
public static function decorate(string $html): string
{
$vite = new CodeigniterVite();

# Check if vite is running or manifest is ready.
if (CodeigniterVite::check())
if ($vite->check())
{
# Get generated js and css tags.
$tags = CodeigniterVite::tags();
$tags = $vite->tags();

# Insert tags just before "</head>" tag.
$html = empty($tags) ? $html : str_replace('</head>', "\n\t$tags\n</head>", $html);
Expand Down

0 comments on commit d75affc

Please sign in to comment.