Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
4959e3b
Deprecate the AssetService class
emmadesilva Jul 26, 2024
7e2b51a
Remove mixin for deprecated AssetService
emmadesilva Jul 26, 2024
19c7d17
Remove kernel binding test for deprecated AssetService
emmadesilva Jul 26, 2024
2932340
Extend unit test case
emmadesilva Jul 26, 2024
730f177
Remove legacy facade implementation tests
emmadesilva Jul 26, 2024
3e4d023
Move unit tests to new code location
emmadesilva Jul 26, 2024
e8d9939
Add temporary test interoperability
emmadesilva Jul 26, 2024
422e966
Call tests using the facade
emmadesilva Jul 26, 2024
db2e12e
Delete AssetServiceUnitTest.php
emmadesilva Jul 26, 2024
3e53c3b
Rename test 'AssetFacadeTest' to 'AssetFacadeUnitTest'
emmadesilva Jul 26, 2024
9d393ab
Rename test 'AssetServiceTest' to 'AssetFacadeTest'
emmadesilva Jul 26, 2024
171c4ac
Update covers annotation
emmadesilva Jul 26, 2024
5d5f14d
Update test crosslinks
emmadesilva Jul 26, 2024
69b8f39
Call tests using the facade
emmadesilva Jul 26, 2024
5b48cef
Refactor class to be static
emmadesilva Jul 26, 2024
e4886a7
Remove now unused properties
emmadesilva Jul 26, 2024
50600ab
Call tests using the facade
emmadesilva Jul 26, 2024
f801942
Revert temporary test interoperability
emmadesilva Jul 26, 2024
1675dd0
Merge the AssetService into the Asset facade class
emmadesilva Jul 26, 2024
fb0e40c
Delete AssetService.php
emmadesilva Jul 26, 2024
bf257a5
Remove unused import
emmadesilva Jul 26, 2024
f0192f5
Remove AssetService container binding
emmadesilva Jul 26, 2024
83e69f8
Update internal docs for removed asset service
emmadesilva Jul 26, 2024
251ed8b
Clean up test
emmadesilva Jul 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion _ide_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
class Hyde extends \Hyde\Hyde {}
class Site extends \Hyde\Facades\Site {}
class Meta extends \Hyde\Facades\Meta {}
/** @mixin \Hyde\Framework\Services\AssetService */
class Asset extends \Hyde\Facades\Asset {}
class Author extends \Hyde\Facades\Author {}
class Features extends \Hyde\Facades\Features {}
Expand Down
4 changes: 2 additions & 2 deletions monorepo/docs/hydefront.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ This will create commits in both the monorepo and submodule. Now follow the foll
9. Update the monorepo to use the new version.
- [ ] `npm update hydefront`
- **Note:** On major/minor version bumps, **remember to update** the `package.json` in the `packages/hyde` directory to use the new version!
- **Note:** On major version bumps, **remember to update the HydeFront version in the Asset Service!**
- **Note:** On major version bumps, **remember to update the HydeFront version in the Asset Facade!**
10. Amend the monorepo commit with the updated files (package-lock.json, _media/app.css, packages\hydefront, packages\hyde\package.json)
- [ ] `git add packages/hydefront && git add packages/hyde/package.json && git add package-lock.json && git add _media/app.css && git add packages/framework/src/Framework/Services/AssetService.php && git commit --amend --no-edit`
- [ ] `git add packages/hydefront && git add packages/hyde/package.json && git add package-lock.json && git add _media/app.css && git add packages/framework/src/Facades/Asset.php && git commit --amend --no-edit`
84 changes: 71 additions & 13 deletions packages/framework/src/Facades/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,83 @@

namespace Hyde\Facades;

use Hyde\Framework\Services\AssetService;
use Illuminate\Support\Facades\Facade;
use Hyde\Hyde;
use Illuminate\Support\Str;

use function rtrim;
use function explode;
use function implode;
use function md5_file;
use function file_exists;
use function str_replace;
use function preg_replace;
use function str_contains;
use function file_get_contents;

/**
* Handles the retrieval of core asset files, either from the HydeFront CDN or from the local media folder.
*
* @see \Hyde\Framework\Services\AssetService
*
* @method static string version()
* @method static string cdnLink(string $file)
* @method static string mediaLink(string $file)
* @method static bool hasMediaFile(string $file)
* @method static string injectTailwindConfig()
* This class provides static methods for interacting with versioned files,
* as well as the HydeFront CDN service and the media directories.
*/
class Asset extends Facade
class Asset
{
/** @psalm-return AssetService::class */
protected static function getFacadeAccessor(): string
/** @var string The default HydeFront SemVer tag to load. This constant is set to match the styles used for the installed framework version. */
final protected const HYDEFRONT_VERSION = 'v3.4';

/** @var string The default HydeFront CDN path pattern. The Blade-style placeholders are replaced with the proper values. */
final protected const HYDEFRONT_CDN_URL = 'https://cdn.jsdelivr.net/npm/hydefront@{{ $version }}/dist/{{ $file }}';

public static function version(): string
{
return static::HYDEFRONT_VERSION;
}

public static function cdnLink(string $file): string
{
return static::constructCdnPath($file);
}

public static function mediaLink(string $file): string
{
return Hyde::mediaLink($file).static::getCacheBustKey($file);
}

public static function hasMediaFile(string $file): bool
{
return file_exists(Hyde::mediaPath($file));
}

public static function injectTailwindConfig(): string
{
if (! file_exists(Hyde::path('tailwind.config.js'))) {
return '';
}

$config = Str::between(file_get_contents(Hyde::path('tailwind.config.js')), '{', '}');

// Remove the plugins array, as it is not used in the frontend.
if (str_contains($config, 'plugins: [')) {
$tokens = explode('plugins: [', $config, 2);
$tokens[1] = Str::after($tokens[1], ']');
$config = implode('', $tokens);
}

return preg_replace('/\s+/', ' ', "/* tailwind.config.js */ \n".rtrim($config, ",\n\r"));
}

protected static function constructCdnPath(string $file): string
{
return str_replace(
['{{ $version }}', '{{ $file }}'], [static::version(), $file],
static::HYDEFRONT_CDN_URL
);
}

protected static function getCacheBustKey(string $file): string
{
return AssetService::class;
return Config::getBool('hyde.enable_cache_busting', true)
? '?v='.md5_file(Hyde::mediaPath("$file"))
: '';
}
}
2 changes: 0 additions & 2 deletions packages/framework/src/Framework/HydeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Hyde\Pages\MarkdownPost;
use Hyde\Foundation\HydeKernel;
use Hyde\Pages\DocumentationPage;
use Hyde\Framework\Services\AssetService;
use Hyde\Framework\Services\BuildTaskService;
use Hyde\Framework\Concerns\RegistersFileLocations;
use Illuminate\Support\ServiceProvider;
Expand All @@ -29,7 +28,6 @@ public function register(): void
{
$this->kernel = HydeKernel::getInstance();

$this->app->singleton(AssetService::class, AssetService::class);
$this->app->singleton(BuildTaskService::class, BuildTaskService::class);

$this->kernel->setSourceRoot(Config::getString('hyde.source_root', ''));
Expand Down
94 changes: 0 additions & 94 deletions packages/framework/src/Framework/Services/AssetService.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,28 @@

namespace Hyde\Framework\Testing\Feature;

use Hyde\Framework\Services\AssetService;
use Hyde\Facades\Asset;
use Hyde\Hyde;
use Hyde\Testing\TestCase;

/**
* @covers \Hyde\Framework\Services\AssetService
* @covers \Hyde\Facades\Asset
*
* @see \Hyde\Framework\Testing\Unit\AssetServiceUnitTest
* @see \Hyde\Framework\Testing\Unit\Facades\AssetFacadeUnitTest
*/
class AssetServiceTest extends TestCase
class AssetFacadeTest extends TestCase
{
public function testMediaLinkReturnsMediaPathWithCacheKey()
{
$service = new AssetService();

$this->assertIsString($path = $service->mediaLink('app.css'));
$this->assertIsString($path = Asset::mediaLink('app.css'));
$this->assertSame('media/app.css?v='.md5_file(Hyde::path('_media/app.css')), $path);
}

public function testMediaLinkReturnsMediaPathWithoutCacheKeyIfCacheBustingIsDisabled()
{
config(['hyde.enable_cache_busting' => false]);

$service = new AssetService();
$path = $service->mediaLink('app.css');
$path = Asset::mediaLink('app.css');

$this->assertIsString($path);
$this->assertSame('media/app.css', $path);
Expand All @@ -41,8 +38,7 @@ public function testMediaLinkSupportsCustomMediaDirectories()

Hyde::setMediaDirectory('_assets');

$service = new AssetService();
$path = $service->mediaLink('app.css');
$path = Asset::mediaLink('app.css');

$this->assertIsString($path);
$this->assertSame('assets/app.css?v='.md5_file(Hyde::path('_assets/app.css')), $path);
Expand Down
8 changes: 0 additions & 8 deletions packages/framework/tests/Feature/HydeServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Illuminate\Contracts\Container\BindingResolutionException;
use Hyde\Console\ConsoleServiceProvider;
use Hyde\Framework\HydeServiceProvider;
use Hyde\Framework\Services\AssetService;
use Hyde\Framework\Services\BuildTaskService;
use Hyde\Foundation\HydeCoreExtension;
use Hyde\Hyde;
Expand Down Expand Up @@ -54,13 +53,6 @@ public function testProviderHasBootMethod()
$this->assertTrue(method_exists($this->provider, 'boot'));
}

public function testProviderRegistersAssetServiceAsSingleton()
{
$this->assertTrue($this->app->bound(AssetService::class));
$this->assertInstanceOf(AssetService::class, $this->app->make(AssetService::class));
$this->assertSame($this->app->make(AssetService::class), $this->app->make(AssetService::class));
}

public function testProviderRegistersBuildTaskServiceAsSingleton()
{
$this->assertTrue($this->app->bound(BuildTaskService::class));
Expand Down
31 changes: 0 additions & 31 deletions packages/framework/tests/Unit/Facades/AssetFacadeTest.php

This file was deleted.

Loading