Skip to content

Commit

Permalink
Use vendor Twill manifest when public/ is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonio Ribeiro authored and ifox committed Apr 29, 2021
1 parent dafd459 commit 5ff29af
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 34 deletions.
36 changes: 2 additions & 34 deletions src/Helpers/frontend_helpers.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Illuminate\Support\Facades\Cache;
use A17\Twill\Services\Assets\Twill as TwillAssets;

if (!function_exists('revAsset')) {
/**
Expand Down Expand Up @@ -35,40 +36,7 @@ function revAsset($file)
*/
function twillAsset($file)
{
if (app()->environment('local', 'development') && config('twill.dev_mode', false)) {
$devServerUrl = config('twill.dev_mode_url', 'http://localhost:8080');

try {
$manifest = json_decode(file_get_contents(
$devServerUrl
. '/'
. config('twill.manifest_file', 'twill-manifest.json')
), true);

} catch (\Exception $e) {
throw new \Exception('Twill dev assets manifest is missing. Make sure you are running the npm run serve command inside Twill.');
}

return $devServerUrl . ($manifest[$file] ?? ('/' . $file));
}

try {
$manifest = Cache::rememberForever('twill-manifest', function () {
return json_decode(file_get_contents(
public_path(config('twill.public_directory', 'twill'))
. '/'
. config('twill.manifest_file', 'twill-manifest.json')
), true);
});
} catch (\Exception $e) {
throw new \Exception('Twill assets manifest is missing. Make sure you published/updated Twill assets using the "php artisan twill:update" command.');
}

if (isset($manifest[$file])) {
return $manifest[$file];
}

return '/' . config('twill.public_directory', 'twill') . '/' . $file;
return app(TwillAssets::class)->asset($file);
}
}

Expand Down
92 changes: 92 additions & 0 deletions src/Services/Assets/Twill.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace A17\Twill\Services\Assets;

use Illuminate\Support\Facades\Cache;

class Twill
{
function asset($file)
{
return $this->devAsset($file) ?? $this->twillAsset($file);
}

public function twillAsset($file)
{
$manifest = $this->readManifest();

if (isset($manifest[$file])) {
return asset($manifest[$file]);
}

return asset(
'/' . config('twill.public_directory', 'twill') . '/' . $file,
);
}

public function getManifestFilename()
{
$fileName =
public_path(config('twill.public_directory', 'twill')) .
'/' .
config('twill.manifest_file', 'twill-manifest.json');

if (file_exists($fileName)) {
return $fileName;
}

return base_path(
'vendor/area17/twill/dist/assets/admin/twill-manifest.json',
);
}

public function devAsset($file)
{
if (!$this->devMode()) {
return null;
}

$devServerUrl = config('twill.dev_mode_url', 'http://localhost:8080');

try {
$manifest = $this->readJson(
$devServerUrl .
'/' .
config('twill.manifest_file', 'twill-manifest.json'),
);
} catch (\Exception $e) {
throw new \Exception(
'Twill dev assets manifest is missing. Make sure you are running the npm run serve command inside Twill.',
);
}

return $devServerUrl . ($manifest[$file] ?? '/' . $file);
}

/**
* @return mixed
*/
private function readManifest()
{
try {
return Cache::rememberForever('twill-manifest', function () {
return $this->readJson($this->getManifestFilename());
});
} catch (\Exception $e) {
throw new \Exception(
'Twill assets manifest is missing. Make sure you published/updated Twill assets using the "php artisan twill:update" command.',
);
}
}

private function readJson($fileName)
{
return json_decode(file_get_contents($fileName), true);
}

private function devMode()
{
return app()->environment('local', 'development') &&
config('twill.dev_mode', false);
}
}

0 comments on commit 5ff29af

Please sign in to comment.