Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/asset helper #12

Merged
merged 5 commits into from
Dec 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
58 changes: 58 additions & 0 deletions src/View/Helper/AssetHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* BEdita, API-first content management framework
* Copyright 2019 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\View\Helper;

use Cake\Utility\Hash;
use Cake\View\Helper;

/**
* Asset Helper to handle asset names with signatures
*/
class AssetHelper extends Helper
{
/**
* Array having asset names as keys and revved asset names as values
*
* @var array
*/
protected $assets = [];

/**
* {@inheritDoc}
*/
public function initialize(array $config): void
{
$manifestPath = Hash::get($config, 'manifestPath', CONFIG . 'rev-manifest.json');
if (!file_exists($manifestPath)) {
return;
}

$this->assets = json_decode(file_get_contents($manifestPath), true);
}

/**
* Retrieve `revved` asset name if found in manifest or return canonical asset name otherwise
*
* @param string $name Canonical asset name (un-revved)
* @return string
*/
public function get(string $name): string
{
if (!empty($this->assets[$name])) {
$name = (string)$this->assets[$name];
}

return $name;
}
}
1 change: 0 additions & 1 deletion src/View/Helper/HtmlHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Cake\Utility\Hash;
use Cake\Utility\Inflector;
use Cake\View\Helper\HtmlHelper as CakeHtmlHelper;
use Cake\View\View;

/**
* Html helper.
Expand Down
70 changes: 70 additions & 0 deletions tests/TestCase/View/Helper/AssetHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* BEdita, API-first content management framework
* Copyright 2019 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\Test\TestCase\View\Helper;

use BEdita\WebTools\View\Helper\AssetHelper;
use Cake\TestSuite\TestCase;
use Cake\View\View;

/**
* {@see \BEdita\WebTools\View\Helper\AssetHelper} Test Case
*
* @coversDefaultClass \BEdita\WebTools\View\Helper\AssetHelper
*/
class AssetHelperTest extends TestCase
{
/**
* Data provider for `testGet` test case.
*
* @return array
*/
public function getProvider(): array
{
return [
'simple' => [
'script-h74fba9b9e.js',
'script.js',
],
'not found' => [
'functions.js',
'functions.js',
],
'custom path' => [
'script.js',
'script.js',
[
'manifestPath' => ROOT . 'mymanifestpath',
],
],
];
}

/**
* Test `get` method
*
* @dataProvider getProvider()
* @covers ::get()
* @covers ::initialize()
*
* @param string $expected The expected result
* @param string $name The asset name
* @param string $config The helper config
* @return void
*/
public function testGet(string $expected, string $name, array $config = []): void
{
$Asset = new AssetHelper(new View(), $config);
$result = $Asset->get($name);
static::assertEquals($expected, $result);
}
}
4 changes: 4 additions & 0 deletions tests/test_app/config/rev-manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"script.js": "script-h74fba9b9e.js",
"style.css": "style-f544b7c5c1.css"
}