-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from batopa/feat/assets-loader
Assets strategies loader
- Loading branch information
Showing
16 changed files
with
818 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* BEdita, API-first content management framework | ||
* Copyright 2020 ChannelWeb Srl, Chialab Srl | ||
* | ||
* This file is part of BEdita: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details. | ||
*/ | ||
namespace BEdita\WebTools\Utility\Asset; | ||
|
||
use Cake\Core\InstanceConfigTrait; | ||
|
||
/** | ||
* Abstract base class for asset strategies. | ||
* Every asset strategy should extend this class or implements `AssetStrategyInterface` | ||
*/ | ||
abstract class AssetStrategy implements AssetStrategyInterface | ||
{ | ||
use InstanceConfigTrait; | ||
|
||
/** | ||
* Default configuration. | ||
* | ||
* - `manifestPath` is the file path used as manifest for assets | ||
* | ||
* @var array | ||
*/ | ||
protected $_defaultConfig = [ | ||
'manifestPath' => '', | ||
]; | ||
|
||
/** | ||
* The assets map loaded. | ||
* | ||
* @var array | ||
*/ | ||
protected $assets = []; | ||
|
||
/** | ||
* Initialize an asset strategy instance. Called after the constructor. | ||
* | ||
* - write conf | ||
* - load assets | ||
* | ||
* @param array $config The configuration for the asset strategy | ||
*/ | ||
public function __construct(array $config = []) | ||
{ | ||
$this->setConfig($config); | ||
$this->loadAssets(); | ||
} | ||
|
||
/** | ||
* Load assets map. | ||
* If no map path is passed then it uses the configured one. | ||
* | ||
* @param string|null $manifestPath The optional file path to use | ||
* @return void | ||
*/ | ||
public function loadAssets(?string $manifestPath = null): void | ||
{ | ||
$this->assets = []; | ||
if (empty($manifestPath)) { | ||
$manifestPath = $this->getConfig('manifestPath'); | ||
} | ||
if (file_exists($manifestPath)) { | ||
$this->assets = (array)json_decode(file_get_contents($manifestPath), true); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* BEdita, API-first content management framework | ||
* Copyright 2020 ChannelWeb Srl, Chialab Srl | ||
* | ||
* This file is part of BEdita: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details. | ||
*/ | ||
namespace BEdita\WebTools\Utility\Asset; | ||
|
||
/** | ||
* Interface that describe an asset strategy. | ||
*/ | ||
interface AssetStrategyInterface | ||
{ | ||
/** | ||
* Retrieve the asset corresponding to the name passed. | ||
* | ||
* @param string $name The name used to looking for the asset | ||
* @param string|null $extension Optional asset extension as 'js' or 'css' | ||
* @return string|array | ||
*/ | ||
public function get(string $name, ?string $extension = null); | ||
|
||
/** | ||
* Load assets map optionally using a file path. | ||
* | ||
* @param string|null $path The optional file path | ||
* @return void | ||
*/ | ||
public function loadAssets(?string $path = null): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* BEdita, API-first content management framework | ||
* Copyright 2020 ChannelWeb Srl, Chialab Srl | ||
* | ||
* This file is part of BEdita: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details. | ||
*/ | ||
namespace BEdita\WebTools\Utility\Asset\Strategy; | ||
|
||
use BEdita\WebTools\Utility\Asset\AssetStrategy; | ||
use Cake\Utility\Hash; | ||
|
||
/** | ||
* Entrypoints asset strategy. | ||
* This strategy is based on map produced by Webpack Encore and expects a JSON assets map like | ||
* | ||
* ``` | ||
* { | ||
* "entrypoints": { | ||
* "app": { | ||
* "js": [ | ||
* "/build/runtime.f011bcb1.js", | ||
* "/build/0.54651780.js", | ||
* "/build/app.82269f26.js" | ||
* ] | ||
* }, | ||
* "style": { | ||
* "js": [ | ||
* "/build/runtime.f011bcb1.js" | ||
* ], | ||
* "css": [ | ||
* "/build/style.12c5249c.css" | ||
* ] | ||
* } | ||
* } | ||
* } | ||
* ``` | ||
* | ||
* @see https://symfony.com/doc/current/frontend.html | ||
*/ | ||
class EntrypointsStrategy extends AssetStrategy | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected $_defaultConfig = [ | ||
'manifestPath' => WWW_ROOT . 'build' . DS . 'entrypoints.json', | ||
]; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function get(string $name, ?string $extension = null) | ||
{ | ||
$path = sprintf('entrypoints.%s', $name); | ||
if (!empty($extension)) { | ||
$path .= sprintf('.%s', $extension); | ||
} | ||
|
||
return Hash::get($this->assets, $path); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* BEdita, API-first content management framework | ||
* Copyright 2020 ChannelWeb Srl, Chialab Srl | ||
* | ||
* This file is part of BEdita: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details. | ||
*/ | ||
namespace BEdita\WebTools\Utility\Asset\Strategy; | ||
|
||
use BEdita\WebTools\Utility\Asset\AssetStrategy; | ||
|
||
/** | ||
* RevManifest asset strategy. | ||
* This strategy expects a JSON assets map like | ||
* | ||
* ``` | ||
* { | ||
* "app.js": "app.13gdr5.js", | ||
* "style.css: "style.lgcf6a.js" | ||
* } | ||
* ``` | ||
*/ | ||
class RevManifestStrategy extends AssetStrategy | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected $_defaultConfig = [ | ||
'manifestPath' => WWW_ROOT . 'rev-manifest.json', | ||
]; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function get(string $name, ?string $extension = null) | ||
{ | ||
if (!empty($extension)) { | ||
$name .= sprintf('.%s', $extension); | ||
} | ||
|
||
if (empty($this->assets[$name])) { | ||
return null; | ||
} | ||
|
||
return $this->assets[$name]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.