Create a base class for jsAssets and cssAssets helpers

This commit is contained in:
Dmitriy Simushev 2014-10-10 08:17:36 +00:00
parent 5b09b3e52c
commit 00bba6c05a
3 changed files with 133 additions and 148 deletions

View File

@ -0,0 +1,123 @@
<?php
/*
* This file is a part of Mibew Messenger.
*
* Copyright 2005-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Mibew\Handlebars\Helper;
use Handlebars\Context;
use Handlebars\Helper as HelperInterface;
use Handlebars\SafeString;
use Handlebars\Template;
use Mibew\Asset\AssetManagerAwareInterface;
use Mibew\Asset\AssetManagerInterface;
/**
* Contains for basic functionality for all helpers which renders assets lists.
*/
abstract class AbstractAssetsHelper implements HelperInterface, AssetManagerAwareInterface
{
/**
* @var AssetManagerInterface|null
*/
protected $manager = null;
/**
* Class constructor.
*
* @param AssetUrlGeneratorInterface $manager An instance of Asset Manager.
*/
public function __construct(AssetManagerInterface $manager)
{
$this->manager = $manager;
}
/**
* {@inheritdoc}
*/
public function getAssetManager()
{
return $this->manager;
}
/**
* {@inheritdoc}
*/
public function setAssetManager(AssetManagerInterface $manager)
{
$this->manager = $manager;
}
/**
* {@inheritdoc}
*/
public function execute(Template $template, Context $context, $args, $source)
{
$generator = $this->getAssetManager()->getUrlGenerator();
$buffer = array();
foreach ($this->getAssetsList() as $asset) {
switch ($asset['type']) {
case AssetManagerInterface::ABSOLUTE_URL:
$buffer[] = $this->renderUrl($asset['content']);
break;
case AssetManagerInterface::RELATIVE_URL:
$buffer[] = $this->renderUrl($generator->generate($asset['content']));
break;
case AssetManagerInterface::INLINE:
$buffer[] = $this->renderContent($asset['content']);
break;
default:
throw new \RuntimeException(sprintf(
'Unknown asset type "%s"',
$asset['type']
));
}
}
return new SafeString(implode("\n", $buffer));
}
/**
* Renders URL of an asset.
*
* @param string $url URL of an asset.
* @return string HTML markup.
*/
abstract protected function renderUrl($url);
/**
* Renders content of an asset.
*
* @param string $content Content of an asset.
* @return string HTML markup.
*/
abstract protected function renderContent($content);
/**
* Retrieves list of assets which should be rendered by the helper.
*
* @return array List of assets. See
* {@link \Mibew\Asset\AssetManagerInterface::getJsAssets()} and
* {@link \Mibew\Asset\AssetManagerInterface::getCssAssets()} for details
* about array's structure.
*/
abstract protected function getAssetsList();
}

View File

@ -19,13 +19,6 @@
namespace Mibew\Handlebars\Helper;
use Handlebars\Context;
use Handlebars\Helper as HelperInterface;
use Handlebars\SafeString;
use Handlebars\Template;
use Mibew\Asset\AssetManagerAwareInterface;
use Mibew\Asset\AssetManagerInterface;
/**
* A helper that generates additional CSS list from assets attached to
* Asset Manager.
@ -35,78 +28,19 @@ use Mibew\Asset\AssetManagerInterface;
* {{cssAssets}}
* </code>
*/
class CssAssetsHelper implements HelperInterface, AssetManagerAwareInterface
class CssAssetsHelper extends AbstractAssetsHelper
{
/**
* @var AssetManagerInterface|null
* {@inheritdoc}
*/
protected $manager = null;
/**
* Class constructor.
*
* @param AssetUrlGeneratorInterface $manager An instance of Asset Manager.
*/
public function __construct(AssetManagerInterface $manager)
protected function getAssetsList()
{
$this->manager = $manager;
return $this->getAssetManager()->getCssAssets();
}
/**
* {@inheritdoc}
*/
public function getAssetManager()
{
return $this->manager;
}
/**
* {@inheritdoc}
*/
public function setAssetManager(AssetManagerInterface $manager)
{
$this->manager = $manager;
}
/**
* {@inheritdoc}
*/
public function execute(Template $template, Context $context, $args, $source)
{
$generator = $this->getAssetManager()->getUrlGenerator();
$buffer = array();
foreach ($this->getAssetManager()->getCssAssets() as $asset) {
switch ($asset['type']) {
case AssetManagerInterface::ABSOLUTE_URL:
$buffer[] = $this->renderUrl($asset['content']);
break;
case AssetManagerInterface::RELATIVE_URL:
$buffer[] = $this->renderUrl($generator->generate($asset['content']));
break;
case AssetManagerInterface::INLINE:
$buffer[] = $this->renderContent($asset['content']);
break;
default:
throw new \RuntimeException(sprintf(
'Unknown asset type "%s"',
$asset['type']
));
}
}
return new SafeString(implode("\n", $buffer));
}
/**
* Renders URL of an asset.
*
* @param string $url URL of an asset.
* @return string HTML markup.
*/
protected function renderUrl($url)
{
return sprintf(
@ -116,10 +50,7 @@ class CssAssetsHelper implements HelperInterface, AssetManagerAwareInterface
}
/**
* Renders content of an asset.
*
* @param string $content Content of an asset.
* @return string HTML markup.
* {@inheritdoc}
*/
protected function renderContent($content)
{

View File

@ -19,13 +19,6 @@
namespace Mibew\Handlebars\Helper;
use Handlebars\Context;
use Handlebars\Helper as HelperInterface;
use Handlebars\SafeString;
use Handlebars\Template;
use Mibew\Asset\AssetManagerAwareInterface;
use Mibew\Asset\AssetManagerInterface;
/**
* A helper that generates additional JavaSctipts list from assets attached to
* Asset Manager.
@ -35,78 +28,19 @@ use Mibew\Asset\AssetManagerInterface;
* {{jsAssets}}
* </code>
*/
class JsAssetsHelper implements HelperInterface, AssetManagerAwareInterface
class JsAssetsHelper extends AbstractAssetsHelper
{
/**
* @var AssetManagerInterface|null
* {@inheritdoc}
*/
protected $manager = null;
/**
* Class constructor.
*
* @param AssetUrlGeneratorInterface $manager An instance of Asset Manager.
*/
public function __construct(AssetManagerInterface $manager)
protected function getAssetsList()
{
$this->manager = $manager;
return $this->getAssetManager()->getJsAssets();
}
/**
* {@inheritdoc}
*/
public function getAssetManager()
{
return $this->manager;
}
/**
* {@inheritdoc}
*/
public function setAssetManager(AssetManagerInterface $manager)
{
$this->manager = $manager;
}
/**
* {@inheritdoc}
*/
public function execute(Template $template, Context $context, $args, $source)
{
$generator = $this->getAssetManager()->getUrlGenerator();
$buffer = array();
foreach ($this->getAssetManager()->getJsAssets() as $asset) {
switch ($asset['type']) {
case AssetManagerInterface::ABSOLUTE_URL:
$buffer[] = $this->renderUrl($asset['content']);
break;
case AssetManagerInterface::RELATIVE_URL:
$buffer[] = $this->renderUrl($generator->generate($asset['content']));
break;
case AssetManagerInterface::INLINE:
$buffer[] = $this->renderContent($asset['content']);
break;
default:
throw new \RuntimeException(sprintf(
'Unknown asset type "%s"',
$asset['type']
));
}
}
return new SafeString(implode("\n", $buffer));
}
/**
* Renders URL of an asset.
*
* @param string $url URL of an asset.
* @return string HTML markup.
*/
protected function renderUrl($url)
{
return sprintf(
@ -116,10 +50,7 @@ class JsAssetsHelper implements HelperInterface, AssetManagerAwareInterface
}
/**
* Renders content of an asset.
*
* @param string $content Content of an asset.
* @return string HTML markup.
* {@inheritdoc}
*/
protected function renderContent($content)
{