Refactor retrieving assets
This commit is contained in:
parent
792062451a
commit
4a9af2d5df
|
|
@ -1,6 +1,8 @@
|
||||||
APP_DEBUG=false
|
APP_DEBUG=false
|
||||||
APP_ENV=production
|
APP_ENV=production
|
||||||
WEBPACK_ENV=production
|
|
||||||
|
ASSET_ENV=production
|
||||||
|
ASSET_URL=
|
||||||
|
|
||||||
DB_CONNECTION=mysql
|
DB_CONNECTION=mysql
|
||||||
DB_HOST=localhost
|
DB_HOST=localhost
|
||||||
|
|
|
||||||
|
|
@ -5,22 +5,42 @@ declare(strict_types=1);
|
||||||
namespace App\Services;
|
namespace App\Services;
|
||||||
|
|
||||||
use Illuminate\Support\Arr;
|
use Illuminate\Support\Arr;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
use Illuminate\Filesystem\Filesystem;
|
use Illuminate\Filesystem\Filesystem;
|
||||||
|
|
||||||
class Webpack
|
class Webpack
|
||||||
{
|
{
|
||||||
protected $manifest = [];
|
protected $manifest = [];
|
||||||
|
|
||||||
public function __construct(Filesystem $filesystem)
|
/** @var Option */
|
||||||
|
protected $options;
|
||||||
|
|
||||||
|
public function __construct(Filesystem $filesystem, Option $options)
|
||||||
{
|
{
|
||||||
$path = public_path('app/manifest.json');
|
$path = public_path('app/manifest.json');
|
||||||
if ($filesystem->exists($path)) {
|
if ($filesystem->exists($path)) {
|
||||||
$this->manifest = json_decode($filesystem->get($path), true);
|
$this->manifest = json_decode($filesystem->get($path), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->options = $options;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __get(string $path)
|
public function __get(string $path)
|
||||||
{
|
{
|
||||||
return Arr::get($this->manifest, $path, '');
|
return Arr::get($this->manifest, $path, '');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function url(string $path): string
|
||||||
|
{
|
||||||
|
if (Str::startsWith(config('app.asset.env'), 'dev')) {
|
||||||
|
$base = config('app.asset.url');
|
||||||
|
|
||||||
|
return "$base:8080/$path";
|
||||||
|
} else {
|
||||||
|
$path = $this->$path;
|
||||||
|
$cdn = $this->options->get('cdn_address');
|
||||||
|
|
||||||
|
return $cdn ? "$cdn/app/$path" : url("/app/$path");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,20 +7,9 @@ use Illuminate\Support\Arr;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
if (! function_exists('webpack_assets')) {
|
if (! function_exists('webpack_assets')) {
|
||||||
function webpack_assets(string $relativeUri): string
|
function webpack_assets(string $path): string
|
||||||
{
|
{
|
||||||
if (env('WEBPACK_ENV', 'production') == 'development') {
|
return resolve(\App\Services\Webpack::class)->url($path);
|
||||||
// @codeCoverageIgnoreStart
|
|
||||||
$host = parse_url(url('/'), PHP_URL_HOST);
|
|
||||||
|
|
||||||
return "http://$host:8080/$relativeUri";
|
|
||||||
// @codeCoverageIgnoreEnd
|
|
||||||
} else {
|
|
||||||
$path = resolve(\App\Services\Webpack::class)->$relativeUri;
|
|
||||||
$cdn = option('cdn_address');
|
|
||||||
|
|
||||||
return $cdn ? "$cdn/app/$path" : url("/app/$path");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -64,6 +64,19 @@ return [
|
||||||
|
|
||||||
'url' => env('APP_URL', 'http://localhost'),
|
'url' => env('APP_URL', 'http://localhost'),
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Assets
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| This is related to front-end assets. The asset URL is only available for
|
||||||
|
| development, not for production.
|
||||||
|
*/
|
||||||
|
'asset' => [
|
||||||
|
'env' => env('ASSET_ENV', 'production'),
|
||||||
|
'url' => env('ASSET_URL', 'http://localhost'),
|
||||||
|
],
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
| Application Timezone
|
| Application Timezone
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace Tests;
|
namespace Tests;
|
||||||
|
|
||||||
|
use App\Services\Webpack;
|
||||||
use Illuminate\Filesystem\Filesystem;
|
use Illuminate\Filesystem\Filesystem;
|
||||||
|
|
||||||
class WebpackTest extends TestCase
|
class WebpackTest extends TestCase
|
||||||
|
|
@ -19,7 +20,42 @@ class WebpackTest extends TestCase
|
||||||
->once()
|
->once()
|
||||||
->andReturn(json_encode(['a' => 'b']));
|
->andReturn(json_encode(['a' => 'b']));
|
||||||
});
|
});
|
||||||
$this->assertEquals('b', resolve(\App\Services\Webpack::class)->{'a'});
|
|
||||||
$this->assertEquals('', resolve(\App\Services\Webpack::class)->{'nope'});
|
$this->app->forgetInstance(Webpack::class);
|
||||||
|
$webpack = $this->app->make(Webpack::class);
|
||||||
|
$this->assertEquals('b', $webpack->{'a'});
|
||||||
|
$this->assertEquals('', $webpack->{'nope'});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testUrl()
|
||||||
|
{
|
||||||
|
$this->mock(Filesystem::class, function ($mock) {
|
||||||
|
$mock->shouldReceive('exists')
|
||||||
|
->with(public_path('app/manifest.json'))
|
||||||
|
->twice()
|
||||||
|
->andReturn(true);
|
||||||
|
|
||||||
|
$mock->shouldReceive('get')
|
||||||
|
->with(public_path('app/manifest.json'))
|
||||||
|
->twice()
|
||||||
|
->andReturn(json_encode(['a' => 'b']));
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->app->forgetInstance(Webpack::class);
|
||||||
|
$webpack = $this->app->make(Webpack::class);
|
||||||
|
$this->assertEquals('http://localhost/app/b', $webpack->url('a'));
|
||||||
|
|
||||||
|
$this->mock(\App\Services\Option::class, function ($mock) {
|
||||||
|
$mock->shouldReceive('get')
|
||||||
|
->with('cdn_address')
|
||||||
|
->once()
|
||||||
|
->andReturn('http://cdn.test');
|
||||||
|
});
|
||||||
|
$this->app->forgetInstance(Webpack::class);
|
||||||
|
$webpack = $this->app->make(Webpack::class);
|
||||||
|
$this->assertEquals('http://cdn.test/app/b', $webpack->url('a'));
|
||||||
|
|
||||||
|
config(['app.asset.env' => 'development']);
|
||||||
|
$this->assertEquals('http://localhost:8080/a', $webpack->url('a'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user