This repository has been archived by the owner on Jan 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
Asset helper #64
Merged
weierophinney
merged 8 commits into
zendframework:develop
from
michalbundyra:feature/asset-helper
Mar 21, 2017
Merged
Asset helper #64
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
11e9437
added new View Helper - Asset helper
michalbundyra dd23c07
Updated docs
michalbundyra 3e08baf
Added helper in PHPDocs for code completion in IDEs
michalbundyra e0ea1ab
Added missing throws tags in PHPDocs
michalbundyra edf8686
CS fix - single space after `!`
michalbundyra 4630c41
Added missing PHPDocs
michalbundyra 363bbf9
Added typehint for parameter
michalbundyra bddbc6b
Updated license headers
michalbundyra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,74 @@ | ||
# Asset | ||
|
||
The `Asset` helper is used to translate asset names. | ||
It could be used to prevent browser caching for assets. | ||
|
||
## Configuration and Basic Usage | ||
|
||
`Zend\View\Helper\Service\AssetFactory` checks the application | ||
configuration, making it possible to set up the resource map through | ||
your `module.config.php`. The next example will set up `Asset` helper: | ||
|
||
```php | ||
'view_helper_config' => [ | ||
'asset' => [ | ||
'resource_map' => [ | ||
'css/style.css' => 'css/style-3a97ff4ee3.css', | ||
'js/vendor.js' => 'js/vendor-a507086eba.js', | ||
], | ||
], | ||
], | ||
``` | ||
|
||
Then in your view you can use: | ||
|
||
```php | ||
// Usable in any of your .phtml files: | ||
echo $this->asset('css/style.css'); | ||
``` | ||
|
||
and you would receive following output: | ||
|
||
```html | ||
css/style-3a97ff4ee3.css | ||
``` | ||
|
||
The first argument of the `asset` helper is the regular asset name, | ||
which will be replaced by versioned asset name defined in `resource_map` | ||
of the configuration. | ||
|
||
> ### Note | ||
> | ||
> When `asset` key is defined but `resource_map` is not provided or is not | ||
> an array exception `Zend\View\Exception\RuntimeException` will be | ||
thrown. | ||
> | ||
> When you call `asset` helper with parameter which is not defined on your | ||
> `resource_map` exception `Zend\View\Exception\InvalidArgumentException` | ||
> will be thrown. | ||
|
||
## Resource map in JSON file | ||
|
||
If you have JSON file with resource map, for example | ||
`rev-manifest.json`: | ||
|
||
```javascript | ||
{ | ||
"css/style.css": "css/style-3a97ff4ee3.css", | ||
"js/vendor.js": "js/vendor-a507086eba.js" | ||
} | ||
``` | ||
|
||
then you can have in your configuration: | ||
|
||
```php | ||
'view_helper_config' => [ | ||
'asset' => [ | ||
'resource_map' => json_decode(file_get_contents('/path/to/rev-manifest.json'), true), | ||
], | ||
], | ||
``` | ||
|
||
and when you have enabled cache config this file will be also cached in | ||
compiled configuration cache, so it prevents reading the file on each | ||
page load. |
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
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
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 | ||
/** | ||
* @see https://github.com/zendframework/zend-view for the canonical source repository | ||
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license https://github.com/zendframework/zend-view/blob/master/LICENSE.md New BSD License | ||
*/ | ||
|
||
namespace Zend\View\Helper; | ||
|
||
use Zend\View\Exception; | ||
|
||
/** | ||
* View helper plugin to fetch asset from resource map. | ||
*/ | ||
class Asset extends AbstractHelper | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
protected $resourceMap = []; | ||
|
||
/** | ||
* @param string $asset | ||
* @return string | ||
* @throws Exception\InvalidArgumentException | ||
*/ | ||
public function __invoke($asset) | ||
{ | ||
if (! array_key_exists($asset, $this->resourceMap)) { | ||
throw new Exception\InvalidArgumentException('Asset is not defined.'); | ||
} | ||
|
||
return $this->resourceMap[$asset]; | ||
} | ||
|
||
/** | ||
* @param array $resourceMap | ||
* @return $this | ||
*/ | ||
public function setResourceMap(array $resourceMap) | ||
{ | ||
$this->resourceMap = $resourceMap; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getResourceMap() | ||
{ | ||
return $this->resourceMap; | ||
} | ||
} |
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,60 @@ | ||
<?php | ||
/** | ||
* @see https://github.com/zendframework/zend-view for the canonical source repository | ||
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license https://github.com/zendframework/zend-view/blob/master/LICENSE.md New BSD License | ||
*/ | ||
|
||
namespace Zend\View\Helper\Service; | ||
|
||
use Interop\Container\ContainerInterface; | ||
use Zend\ServiceManager\FactoryInterface; | ||
use Zend\ServiceManager\ServiceLocatorInterface; | ||
use Zend\View\Exception; | ||
use Zend\View\Helper\Asset; | ||
|
||
class AssetFactory implements FactoryInterface | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
* | ||
* @param ContainerInterface $container | ||
* @param string $name | ||
* @param null|array $options | ||
* @return Asset | ||
* @throws Exception\RuntimeException | ||
*/ | ||
public function __invoke(ContainerInterface $container, $name, array $options = null) | ||
{ | ||
// test if we are using Zend\ServiceManager v2 or v3 | ||
if (! method_exists($container, 'configure')) { | ||
$container = $container->getServiceLocator(); | ||
} | ||
$helper = new Asset(); | ||
|
||
$config = $container->get('config'); | ||
if (isset($config['view_helper_config']['asset'])) { | ||
$configHelper = $config['view_helper_config']['asset']; | ||
if (isset($configHelper['resource_map']) && is_array($configHelper['resource_map'])) { | ||
$helper->setResourceMap($configHelper['resource_map']); | ||
} else { | ||
throw new Exception\RuntimeException('Invalid resource map configuration.'); | ||
} | ||
} | ||
|
||
return $helper; | ||
} | ||
|
||
/** | ||
* Create service | ||
* | ||
* @param ServiceLocatorInterface $serviceLocator | ||
* @param string|null $rName | ||
* @param string|null $cName | ||
* @return Asset | ||
*/ | ||
public function createService(ServiceLocatorInterface $serviceLocator, $rName = null, $cName = null) | ||
{ | ||
return $this($serviceLocator, $cName); | ||
} | ||
} |
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
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
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,87 @@ | ||
<?php | ||
/** | ||
* @see https://github.com/zendframework/zend-view for the canonical source repository | ||
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license https://github.com/zendframework/zend-view/blob/master/LICENSE.md New BSD License | ||
*/ | ||
|
||
namespace ZendTest\View\Helper; | ||
|
||
use PHPUnit_Framework_TestCase as TestCase; | ||
use Zend\ServiceManager\ServiceManager; | ||
use Zend\View\Exception; | ||
use Zend\View\Helper\Asset; | ||
use Zend\View\HelperPluginManager; | ||
|
||
class AssetTest extends TestCase | ||
{ | ||
/** @var array */ | ||
protected $resourceMap = [ | ||
'css/style.css' => 'css/style-3a97ff4ee3.css', | ||
'js/vendor.js' => 'js/vendor-a507086eba.js', | ||
]; | ||
|
||
/** @var Asset */ | ||
protected $asset; | ||
|
||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
$this->asset = new Asset(); | ||
$this->asset->setResourceMap($this->resourceMap); | ||
} | ||
|
||
public function testHelperPluginManagerReturnsAssetHelper() | ||
{ | ||
$helpers = $this->getHelperPluginManager(); | ||
$asset = $helpers->get('asset'); | ||
|
||
$this->assertInstanceOf(Asset::class, $asset); | ||
} | ||
|
||
public function testHelperPluginManagerReturnsAssetHelperByClassName() | ||
{ | ||
$helpers = $this->getHelperPluginManager(); | ||
$asset = $helpers->get(Asset::class); | ||
|
||
$this->assertInstanceOf(Asset::class, $asset); | ||
} | ||
|
||
public function testInvalidAssetName() | ||
{ | ||
$this->setExpectedException(Exception\InvalidArgumentException::class, 'Asset is not defined.'); | ||
|
||
$this->asset->__invoke('unknown'); | ||
} | ||
|
||
/** | ||
* @dataProvider assets | ||
* | ||
* @param string $name | ||
* @param string $expected | ||
*/ | ||
public function testInvokeResult($name, $expected) | ||
{ | ||
$result = $this->asset->__invoke($name); | ||
|
||
$this->assertEquals($expected, $result); | ||
} | ||
|
||
public function assets() | ||
{ | ||
$data = []; | ||
foreach ($this->resourceMap as $key => $value) { | ||
$data[] = [$key, $value]; | ||
} | ||
return $data; | ||
} | ||
|
||
protected function getHelperPluginManager(array $config = []) | ||
{ | ||
$services = $this->prophesize(ServiceManager::class); | ||
$services->get('config')->willReturn($config); | ||
|
||
return new HelperPluginManager($services->reveal()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add the helper in the DocBlock of
PhpRenderer
for code completion in IDEs.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done 3e08baf