Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Commit 1753631

Browse files
committed
Merge pull request #64 from webimpress/feature/asset-helper
Asset helper
2 parents c8a47a5 + bddbc6b commit 1753631

File tree

9 files changed

+371
-0
lines changed

9 files changed

+371
-0
lines changed

doc/book/helpers/asset.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Asset
2+
3+
The `Asset` helper is used to translate asset names.
4+
It could be used to prevent browser caching for assets.
5+
6+
## Configuration and Basic Usage
7+
8+
`Zend\View\Helper\Service\AssetFactory` checks the application
9+
configuration, making it possible to set up the resource map through
10+
your `module.config.php`. The next example will set up `Asset` helper:
11+
12+
```php
13+
'view_helper_config' => [
14+
'asset' => [
15+
'resource_map' => [
16+
'css/style.css' => 'css/style-3a97ff4ee3.css',
17+
'js/vendor.js' => 'js/vendor-a507086eba.js',
18+
],
19+
],
20+
],
21+
```
22+
23+
Then in your view you can use:
24+
25+
```php
26+
// Usable in any of your .phtml files:
27+
echo $this->asset('css/style.css');
28+
```
29+
30+
and you would receive following output:
31+
32+
```html
33+
css/style-3a97ff4ee3.css
34+
```
35+
36+
The first argument of the `asset` helper is the regular asset name,
37+
which will be replaced by versioned asset name defined in `resource_map`
38+
of the configuration.
39+
40+
> ### Note
41+
>
42+
> When `asset` key is defined but `resource_map` is not provided or is not
43+
> an array exception `Zend\View\Exception\RuntimeException` will be
44+
thrown.
45+
>
46+
> When you call `asset` helper with parameter which is not defined on your
47+
> `resource_map` exception `Zend\View\Exception\InvalidArgumentException`
48+
> will be thrown.
49+
50+
## Resource map in JSON file
51+
52+
If you have JSON file with resource map, for example
53+
`rev-manifest.json`:
54+
55+
```javascript
56+
{
57+
"css/style.css": "css/style-3a97ff4ee3.css",
58+
"js/vendor.js": "js/vendor-a507086eba.js"
59+
}
60+
```
61+
62+
then you can have in your configuration:
63+
64+
```php
65+
'view_helper_config' => [
66+
'asset' => [
67+
'resource_map' => json_decode(file_get_contents('/path/to/rev-manifest.json'), true),
68+
],
69+
],
70+
```
71+
72+
and when you have enabled cache config this file will be also cached in
73+
compiled configuration cache, so it prevents reading the file on each
74+
page load.

doc/book/helpers/intro.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ variables. Additionally, there are a rich set of helpers for providing values
5555
for, and rendering, the various HTML `<head>` tags, such as `HeadTitle`,
5656
`HeadLink`, and `HeadScript`. The currently shipped helpers include:
5757

58+
- [Asset](asset.md)
5859
- [BasePath](base-path.md)
5960
- [Cycle](cycle.md)
6061
- [Doctype](doctype.md)

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pages:
99
- "The ViewEvent": view-event.md
1010
- Helpers:
1111
- Intro: helpers/intro.md
12+
- Asset: helpers/asset.md
1213
- BasePath: helpers/base-path.md
1314
- Cycle: helpers/cycle.md
1415
- Doctype: helpers/doctype.md

src/Helper/Asset.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* @see https://github.com/zendframework/zend-view for the canonical source repository
4+
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (http://www.zend.com)
5+
* @license https://github.com/zendframework/zend-view/blob/master/LICENSE.md New BSD License
6+
*/
7+
8+
namespace Zend\View\Helper;
9+
10+
use Zend\View\Exception;
11+
12+
/**
13+
* View helper plugin to fetch asset from resource map.
14+
*/
15+
class Asset extends AbstractHelper
16+
{
17+
/**
18+
* @var array
19+
*/
20+
protected $resourceMap = [];
21+
22+
/**
23+
* @param string $asset
24+
* @return string
25+
* @throws Exception\InvalidArgumentException
26+
*/
27+
public function __invoke($asset)
28+
{
29+
if (! array_key_exists($asset, $this->resourceMap)) {
30+
throw new Exception\InvalidArgumentException('Asset is not defined.');
31+
}
32+
33+
return $this->resourceMap[$asset];
34+
}
35+
36+
/**
37+
* @param array $resourceMap
38+
* @return $this
39+
*/
40+
public function setResourceMap(array $resourceMap)
41+
{
42+
$this->resourceMap = $resourceMap;
43+
44+
return $this;
45+
}
46+
47+
/**
48+
* @return array
49+
*/
50+
public function getResourceMap()
51+
{
52+
return $this->resourceMap;
53+
}
54+
}

src/Helper/Service/AssetFactory.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* @see https://github.com/zendframework/zend-view for the canonical source repository
4+
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (http://www.zend.com)
5+
* @license https://github.com/zendframework/zend-view/blob/master/LICENSE.md New BSD License
6+
*/
7+
8+
namespace Zend\View\Helper\Service;
9+
10+
use Interop\Container\ContainerInterface;
11+
use Zend\ServiceManager\FactoryInterface;
12+
use Zend\ServiceManager\ServiceLocatorInterface;
13+
use Zend\View\Exception;
14+
use Zend\View\Helper\Asset;
15+
16+
class AssetFactory implements FactoryInterface
17+
{
18+
/**
19+
* {@inheritDoc}
20+
*
21+
* @param ContainerInterface $container
22+
* @param string $name
23+
* @param null|array $options
24+
* @return Asset
25+
* @throws Exception\RuntimeException
26+
*/
27+
public function __invoke(ContainerInterface $container, $name, array $options = null)
28+
{
29+
// test if we are using Zend\ServiceManager v2 or v3
30+
if (! method_exists($container, 'configure')) {
31+
$container = $container->getServiceLocator();
32+
}
33+
$helper = new Asset();
34+
35+
$config = $container->get('config');
36+
if (isset($config['view_helper_config']['asset'])) {
37+
$configHelper = $config['view_helper_config']['asset'];
38+
if (isset($configHelper['resource_map']) && is_array($configHelper['resource_map'])) {
39+
$helper->setResourceMap($configHelper['resource_map']);
40+
} else {
41+
throw new Exception\RuntimeException('Invalid resource map configuration.');
42+
}
43+
}
44+
45+
return $helper;
46+
}
47+
48+
/**
49+
* Create service
50+
*
51+
* @param ServiceLocatorInterface $serviceLocator
52+
* @param string|null $rName
53+
* @param string|null $cName
54+
* @return Asset
55+
*/
56+
public function createService(ServiceLocatorInterface $serviceLocator, $rName = null, $cName = null)
57+
{
58+
return $this($serviceLocator, $cName);
59+
}
60+
}

src/HelperPluginManager.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class HelperPluginManager extends AbstractPluginManager
3737
* @var string[]
3838
*/
3939
protected $aliases = [
40+
'asset' => Helper\Asset::class,
41+
'Asset' => Helper\Asset::class,
4042
'basePath' => Helper\BasePath::class,
4143
'BasePath' => Helper\BasePath::class,
4244
'basepath' => Helper\BasePath::class,
@@ -148,6 +150,7 @@ class HelperPluginManager extends AbstractPluginManager
148150
* @var array
149151
*/
150152
protected $factories = [
153+
Helper\Asset::class => Helper\Service\AssetFactory::class,
151154
Helper\FlashMessenger::class => Helper\Service\FlashMessengerFactory::class,
152155
Helper\Identity::class => Helper\Service\IdentityFactory::class,
153156
Helper\BasePath::class => InvokableFactory::class,
@@ -186,6 +189,7 @@ class HelperPluginManager extends AbstractPluginManager
186189

187190
// v2 canonical FQCNs
188191

192+
'zendviewhelperasset' => Helper\Service\AssetFactory::class,
189193
'zendviewhelperflashmessenger' => Helper\Service\FlashMessengerFactory::class,
190194
'zendviewhelperidentity' => Helper\Service\IdentityFactory::class,
191195
'zendviewhelperbasepath' => InvokableFactory::class,

src/Renderer/PhpRenderer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
*
3333
* Convenience methods for build in helpers (@see __call):
3434
*
35+
* @method string asset($asset)
3536
* @method string|null basePath($file = null)
3637
* @method \Zend\View\Helper\Cycle cycle(array $data = array(), $name = \Zend\View\Helper\Cycle::DEFAULT_NAME)
3738
* @method \Zend\View\Helper\DeclareVars declareVars()

test/Helper/AssetTest.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
/**
3+
* @see https://github.com/zendframework/zend-view for the canonical source repository
4+
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (http://www.zend.com)
5+
* @license https://github.com/zendframework/zend-view/blob/master/LICENSE.md New BSD License
6+
*/
7+
8+
namespace ZendTest\View\Helper;
9+
10+
use PHPUnit_Framework_TestCase as TestCase;
11+
use Zend\ServiceManager\ServiceManager;
12+
use Zend\View\Exception;
13+
use Zend\View\Helper\Asset;
14+
use Zend\View\HelperPluginManager;
15+
16+
class AssetTest extends TestCase
17+
{
18+
/** @var array */
19+
protected $resourceMap = [
20+
'css/style.css' => 'css/style-3a97ff4ee3.css',
21+
'js/vendor.js' => 'js/vendor-a507086eba.js',
22+
];
23+
24+
/** @var Asset */
25+
protected $asset;
26+
27+
protected function setUp()
28+
{
29+
parent::setUp();
30+
31+
$this->asset = new Asset();
32+
$this->asset->setResourceMap($this->resourceMap);
33+
}
34+
35+
public function testHelperPluginManagerReturnsAssetHelper()
36+
{
37+
$helpers = $this->getHelperPluginManager();
38+
$asset = $helpers->get('asset');
39+
40+
$this->assertInstanceOf(Asset::class, $asset);
41+
}
42+
43+
public function testHelperPluginManagerReturnsAssetHelperByClassName()
44+
{
45+
$helpers = $this->getHelperPluginManager();
46+
$asset = $helpers->get(Asset::class);
47+
48+
$this->assertInstanceOf(Asset::class, $asset);
49+
}
50+
51+
public function testInvalidAssetName()
52+
{
53+
$this->setExpectedException(Exception\InvalidArgumentException::class, 'Asset is not defined.');
54+
55+
$this->asset->__invoke('unknown');
56+
}
57+
58+
/**
59+
* @dataProvider assets
60+
*
61+
* @param string $name
62+
* @param string $expected
63+
*/
64+
public function testInvokeResult($name, $expected)
65+
{
66+
$result = $this->asset->__invoke($name);
67+
68+
$this->assertEquals($expected, $result);
69+
}
70+
71+
public function assets()
72+
{
73+
$data = [];
74+
foreach ($this->resourceMap as $key => $value) {
75+
$data[] = [$key, $value];
76+
}
77+
return $data;
78+
}
79+
80+
protected function getHelperPluginManager(array $config = [])
81+
{
82+
$services = $this->prophesize(ServiceManager::class);
83+
$services->get('config')->willReturn($config);
84+
85+
return new HelperPluginManager($services->reveal());
86+
}
87+
}

0 commit comments

Comments
 (0)