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

Commit 11e9437

Browse files
committed
added new View Helper - Asset helper
1 parent bace31c commit 11e9437

File tree

8 files changed

+369
-0
lines changed

8 files changed

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

src/Helper/Service/AssetFactory.php

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

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,

test/Helper/AssetTest.php

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

0 commit comments

Comments
 (0)