mirror of
https://github.com/Mibew/mibew.git
synced 2025-02-23 23:04:32 +03:00
Simplify working with Asset Manager in Handlebars helpers
This commit is contained in:
parent
db702e43c9
commit
9fef4b2871
@ -120,20 +120,6 @@ abstract class AbstractController implements
|
||||
public function setAssetManager(AssetManagerInterface $manager)
|
||||
{
|
||||
$this->assetManager = $manager;
|
||||
|
||||
// Update URL generator in the style helpers
|
||||
if (!is_null($this->style) && $this->style instanceof HandlebarsAwareInterface) {
|
||||
$handlebars = $this->style->getHandlebars();
|
||||
if ($handlebars->hasHelper('asset')) {
|
||||
$handlebars->getHelper('asset')->setAssetUrlGenerator($manager->getUrlGenerator());
|
||||
}
|
||||
if ($handlebars->hasHelper('jsAssets')) {
|
||||
$handlebars->getHelper('jsAssets')->setAssetManager($manager);
|
||||
}
|
||||
if ($handlebars->hasHelper('cssAssets')) {
|
||||
$handlebars->getHelper('cssAssets')->setAssetManager($manager);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -358,18 +344,12 @@ abstract class AbstractController implements
|
||||
$hbs->addHelper(
|
||||
'asset',
|
||||
new AssetHelper(
|
||||
$this->getAssetManager()->getUrlGenerator(),
|
||||
$this,
|
||||
array('CurrentStyle' => $style->getFilesPath())
|
||||
)
|
||||
);
|
||||
$hbs->addHelper(
|
||||
'jsAssets',
|
||||
new JsAssetsHelper($this->getAssetManager())
|
||||
);
|
||||
$hbs->addHelper(
|
||||
'cssAssets',
|
||||
new CssAssetsHelper($this->getAssetManager())
|
||||
);
|
||||
$hbs->addHelper('jsAssets', new JsAssetsHelper($this));
|
||||
$hbs->addHelper('cssAssets', new CssAssetsHelper($this));
|
||||
}
|
||||
|
||||
return $style;
|
||||
|
@ -29,37 +29,22 @@ use Mibew\Asset\AssetManagerInterface;
|
||||
/**
|
||||
* Contains for basic functionality for all helpers which renders assets lists.
|
||||
*/
|
||||
abstract class AbstractAssetsHelper implements HelperInterface, AssetManagerAwareInterface
|
||||
abstract class AbstractAssetsHelper implements HelperInterface
|
||||
{
|
||||
/**
|
||||
* @var AssetManagerInterface|null
|
||||
* @var AssetManagerAwareInterface|null
|
||||
*/
|
||||
protected $manager = null;
|
||||
protected $assetManagerContainer = null;
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*
|
||||
* @param AssetUrlGeneratorInterface $manager An instance of Asset Manager.
|
||||
* @param AssetManagerAwareInterface $manager_container An object which know
|
||||
* where to get an appropriate Asset Manager.
|
||||
*/
|
||||
public function __construct(AssetManagerInterface $manager)
|
||||
public function __construct(AssetManagerAwareInterface $manager_container)
|
||||
{
|
||||
$this->manager = $manager;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAssetManager()
|
||||
{
|
||||
return $this->manager;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setAssetManager(AssetManagerInterface $manager)
|
||||
{
|
||||
$this->manager = $manager;
|
||||
$this->assetManagerContainer = $manager_container;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -95,6 +80,17 @@ abstract class AbstractAssetsHelper implements HelperInterface, AssetManagerAwar
|
||||
return new SafeString(implode("\n", $buffer));
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts asset manager from the asset manager's container related with
|
||||
* the object.
|
||||
*
|
||||
* @return AssetManagerInterface
|
||||
*/
|
||||
protected function getAssetManager()
|
||||
{
|
||||
return $this->assetManagerContainer->getAssetManager();
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders URL of an asset.
|
||||
*
|
||||
|
@ -22,6 +22,7 @@ namespace Mibew\Handlebars\Helper;
|
||||
use Handlebars\Context;
|
||||
use Handlebars\Helper as HelperInterface;
|
||||
use Handlebars\Template;
|
||||
use Mibew\Asset\AssetManagerAwareInterface;
|
||||
use Mibew\Asset\Generator\UrlGeneratorInterface as AssetUrlGeneratorInterface;
|
||||
|
||||
/**
|
||||
@ -36,7 +37,7 @@ use Mibew\Asset\Generator\UrlGeneratorInterface as AssetUrlGeneratorInterface;
|
||||
* paths. Lets assume that the following array is passed to the constructor:
|
||||
* <code>
|
||||
* $helper = new AssetHelper(
|
||||
* $generator,
|
||||
* $asset_manager_container,
|
||||
* array('CustomStorage' => 'custom/files/storage')
|
||||
* );
|
||||
* </code>
|
||||
@ -54,22 +55,23 @@ class AssetHelper implements HelperInterface
|
||||
protected $locations = null;
|
||||
|
||||
/**
|
||||
* @var AssetUrlGeneratorInterface|null
|
||||
* @var AssetManagerAwareInterface|null
|
||||
*/
|
||||
protected $generator = null;
|
||||
protected $assetManagerContainer = null;
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*
|
||||
* @param AssetUrlGeneratorInterface $generator An instance of URL generator
|
||||
* @param AssetManagerAwareInterface $manager_container An object which
|
||||
* knows where to get an appropriate Asset Manager.
|
||||
* @param array $locations Associative array of locations that can be used
|
||||
* as prefixes for asset relative paths. The keys are prefixes and the
|
||||
* values are locations relative paths. These paths must not content
|
||||
* neither leading nor trailing slashes.
|
||||
*/
|
||||
public function __construct(AssetUrlGeneratorInterface $generator, $locations = array())
|
||||
public function __construct(AssetManagerAwareInterface $manager_container, $locations = array())
|
||||
{
|
||||
$this->generator = $generator;
|
||||
$this->assetManagerContainer = $manager_container;
|
||||
|
||||
// Strip slashes from location paths.
|
||||
foreach ($locations as $name => $path) {
|
||||
@ -77,26 +79,6 @@ class AssetHelper implements HelperInterface
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets instance of Asset URL Generator.
|
||||
*
|
||||
* @return AssetUrlGeneratorInterface
|
||||
*/
|
||||
public function getAssetUrlGenerator()
|
||||
{
|
||||
return $this->generator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets an instance of Asset URL Generator.
|
||||
*
|
||||
* @param AssetUrlGeneratorInterface $generator
|
||||
*/
|
||||
public function setAssetUrlGenerator(AssetUrlGeneratorInterface $generator)
|
||||
{
|
||||
$this->generator = $generator;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@ -120,7 +102,18 @@ class AssetHelper implements HelperInterface
|
||||
);
|
||||
}
|
||||
|
||||
return $this->generator->generate($relative_path);
|
||||
return $this->getAssetUrlGenerator()->generate($relative_path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts an instance of Asset URL Generator from the Asset Manager
|
||||
* container related with the object.
|
||||
*
|
||||
* @return AssetUrlGeneratorInterface
|
||||
*/
|
||||
protected function getAssetUrlGenerator()
|
||||
{
|
||||
return $this->assetManagerContainer->getAssetManager()->getUrlGenerator();
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user