Skip to content

Commit

Permalink
Manage asset logic in service class
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Apr 28, 2022
1 parent fa3451b commit c72905f
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 8 deletions.
9 changes: 7 additions & 2 deletions config/hyde.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@

/*
|--------------------------------------------------------------------------
| Asset Locations
| Asset Locations and Versions
|--------------------------------------------------------------------------
|
| Since v0.15.0, the default Hyde styles are no longer included as
Expand All @@ -118,9 +118,14 @@
| changing the config setting below. If you set it to
| false, Hyde will load the media/app.css, which
| you compile using NPM.
|
| The CDN version is defined in the AssetService class,
| but can be changed here to a valid HydeFront tag.
|
*/

'loadTailwindFromCDN' => true,
// 'loadTailwindFromCDN' => true,
// 'cdnHydeFrontVersionOverride' => 'v1.0.0,

/*
|--------------------------------------------------------------------------
Expand Down
10 changes: 4 additions & 6 deletions src/Concerns/Internal/AssetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Hyde\Framework\Concerns\Internal;

use function config;
use Hyde\Framework\Services\AssetService;

/**
* AssetManager for the Hyde Facade.
Expand All @@ -14,24 +14,22 @@ trait AssetManager
*/
public static function tailwind(): string|false
{
return config('hyde.loadTailwindFromCDN')
? 'https://cdn.jsdelivr.net/gh/hydephp/[email protected]/dist/app.css'
: false;
return (new AssetService)->tailwindPath();
}

/**
* Return the Hyde stylesheet.
*/
public static function styles(): string
{
return 'https://cdn.jsdelivr.net/gh/hydephp/[email protected]/dist/hyde.css';
return (new AssetService)->stylePath();
}

/**
* Return the Hyde scripts.
*/
public static function scripts(): string
{
return 'https://cdn.jsdelivr.net/gh/hydephp/[email protected]/dist/hyde.js';
return (new AssetService)->scriptPath();
}
}
33 changes: 33 additions & 0 deletions src/Contracts/AssetServiceContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Hyde\Framework\Contracts;

interface AssetServiceContract
{
/**
* The HydeFront version to load.
*/
public function version(): string;

/**
* Return the Tailwind CDN if enabled in the config, else false.
*/
public function tailwindPath(): string|false;


/**
* Return the main Hyde stylesheet location/path.
*/
public function stylePath(): string;


/**
* Return the main Hyde script location/path.
*/
public function scriptPath(): string;

/**
* Construct a URI path for the CDN using the static dist version.
*/
public function cdnPathConstructor(string $file): string;
}
41 changes: 41 additions & 0 deletions src/Services/AssetService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Hyde\Framework\Services;

use Hyde\Framework\Contracts\AssetServiceContract;

class AssetService implements AssetServiceContract
{
/**
* The HydeFront version to load.
* @property string $version
*/
public string $version = 'v1.3.1';

public function version(): string
{
return config('hyde.cdnHydeFrontVersionOverride', $this->version);
}

public function tailwindPath(): string|false
{
return config('hyde.loadTailwindFromCDN', false)
? $this->cdnPathConstructor('app.css')
: false;
}

public function stylePath(): string
{
return $this->cdnPathConstructor('hyde.css');
}

public function scriptPath(): string
{
return $this->cdnPathConstructor('hyde.js');
}

public function cdnPathConstructor(string $file): string
{
return 'https://cdn.jsdelivr.net/gh/hydephp/hydefront@' . $this->version() . '/dist/' . $file;
}
}

0 comments on commit c72905f

Please sign in to comment.