Create AssetManager class

This commit is contained in:
Dmitriy Simushev 2014-10-08 13:52:56 +00:00
parent 54840e96ad
commit e320dcd1bb
11 changed files with 405 additions and 60 deletions

View File

@ -20,7 +20,8 @@
namespace Mibew;
use Mibew\AccessControl\Check\CheckResolver;
use Mibew\Asset\AssetUrlGenerator;
use Mibew\Asset\AssetManager;
use Mibew\Asset\AssetManagerInterface;
use Mibew\Authentication\AuthenticationManagerInterface;
use Mibew\Authentication\AuthenticationManagerAwareInterface;
use Mibew\Controller\ControllerResolver;
@ -67,9 +68,9 @@ class Application implements RouterAwareInterface, AuthenticationManagerAwareInt
protected $authenticationManager = null;
/**
* @var AssetUrlGenerator|null
* @var AssetManagerInterface|null
*/
protected $assetUrlGenerator = null;
protected $assetManager = null;
/**
* Class constructor.
@ -87,11 +88,11 @@ class Application implements RouterAwareInterface, AuthenticationManagerAwareInt
$driver->setOptions(array('path' => MIBEW_FS_ROOT . '/cache'));
$this->cache = new \Stash\Pool($driver);
$this->assetUrlGenerator = new AssetUrlGenerator();
$this->assetManager = new AssetManager();
$this->controllerResolver = new ControllerResolver(
$this->router,
$this->authenticationManager,
$this->assetUrlGenerator,
$this->assetManager,
$this->cache
);
$this->accessCheckResolver = new CheckResolver($this->authenticationManager);
@ -224,7 +225,7 @@ class Application implements RouterAwareInterface, AuthenticationManagerAwareInt
$authentication_manager->setOperatorFromRequest($request);
// Actualize AssetUrlGenerator
$this->assetUrlGenerator->setRequest($request);
$this->assetManager->setRequest($request);
}
/**

View File

@ -0,0 +1,234 @@
<?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\Asset;
use Mibew\Asset\Generator\UrlGenerator;
use Mibew\Asset\Generator\UrlGeneratorInterface;
use Mibew\EventDispatcher;
use Symfony\Component\HttpFoundation\Request;
/**
* The class works with assets related with the current request.
*/
class AssetManager implements AssetManagerInterface
{
/**
* @var UrlGeneratorInterface|null
*/
protected $generator = null;
/**
* @var Request|null
*/
protected $request = null;
/**
* List of attached JS assets.
*
* @var array
*/
protected $jsAssets = array();
/**
* List of attached CSS assets.
*
* @var array
*/
protected $cssAssets = array();
/**
* Sets a request which will be used as a context.
*
* You can pass null as the first argument to notify the manager that there
* is no current request.
*
* @param Request $request Request that should be used.
*/
public function setRequest(Request $request = null)
{
$this->request = $request;
$this->getUrlGenerator()->setRequest($request);
// The request has been changed thus all attaches assets are outdated
// now. Clear them all.
$this->jsAssets = array();
$this->cssAssets = array();
}
/**
* {@inheritdoc}
*/
public function setUrlGenerator(UrlGeneratorInterface $generator)
{
$this->generator = $generator;
}
/**
* {@inheritdoc}
*/
public function getUrlGenerator()
{
if (is_null($this->generator)) {
$this->generator = new UrlGenerator();
}
return $this->generator;
}
/**
* {@inheritdoc}
*/
public function addJsAsset($content, $type = AssetManagerInterface::RELATIVE_URL)
{
$this->jsAssets[] = array(
'content' => $content,
'type' => $type,
);
}
/**
* {@inheritdoc}
*/
public function getJsAssets()
{
return array_merge(
$this->jsAssets,
$this->triggerJsEvent()
);
}
/**
* {@inheritdoc}
*/
public function addCssAsset($content, $type = AssetManagerInterface::RELATIVE_URL)
{
$this->cssAssets[] = array(
'content' => $content,
'type' => $type,
);
}
/**
* {@inheritdoc}
*/
public function getCssAssets()
{
return array_merge(
$this->cssAssets,
$this->triggerCssEvent()
);
}
/**
* Returns the request which is associated with the manager.
*
* @return Request
* @throws \RuntimeException If a request was not associated with the
* manager yet.
*/
protected function getRequest()
{
if (is_null($this->request)) {
throw new \RuntimeException('Request instance was not set yet.');
}
return $this->request;
}
/**
* Triggers "pageAddJS" and "pageAddJSPluginOptions" events and prepares JS
* assets which are returned by plugins.
*
* @return array Assets list.
*/
protected function triggerJsEvent()
{
// Get additional JavaScript from plugins
$event = array(
'request' => $this->getRequest(),
'js' => array(),
);
EventDispatcher::getInstance()->triggerEvent('pageAddJS', $event);
$assets = $this->normalizeAssets($event['js']);
// Get plugins options, transform them into raw JS and attache to the
// other assets.
$event = array(
'request' => $this->getRequest(),
'plugins' => array(),
);
EventDispatcher::getInstance()->triggerEvent('pageAddJSPluginOptions', $event);
$assets[] = array(
'content' => sprintf(
'var Mibew = Mibew || {}; Mibew.PluginOptions = %s;',
json_encode($event['plugins'])
),
'type' => AssetManagerInterface::INLINE,
);
return $assets;
}
/**
* Triggers "pageAddCSS" event and prepares CSS assets which are returned by
* plugins.
*
* @return array Assets list.
*/
protected function triggerCssEvent()
{
$event = array(
'request' => $this->getRequest(),
'css' => array(),
);
EventDispatcher::getInstance()->triggerEvent('pageAddCSS', $event);
return $this->normalizeAssets($event['css']);
}
/**
* Validates passed assets lists and builds a normalized one.
*
* @param array $assets Assets list. Each item of the list can be either a
* string or an asset array. If a string is used it is treated as an
* absolute URL of the asset. If an array is used it is treated as a
* normal asset array and must have "content" and "type" items.
* @return array A list of normalized assets.
* @throws \InvalidArgumentException If the passed in assets list is not
* valid.
*/
protected function normalizeAssets($assets)
{
$normalized_assets = array();
foreach ($assets as $asset) {
if (is_string($asset)) {
$normalized_assets[] = array(
'content' => $asset,
'type' => AssetManagerInterface::ABSOLUTE_URL,
);
} elseif (is_array($asset) && !empty($asset['type']) && !empty($asset['content'])) {
$normalized_assets[] = $asset;
} else {
throw new \InvalidArgumentException('Invalid asset item');
}
}
return $normalized_assets;
}
}

View File

@ -20,21 +20,21 @@
namespace Mibew\Asset;
/**
* Indicates if a class is aware of AssetUrlGenerator class.
* Indicates if a class is aware of AssetManager class.
*/
interface AssetUrlGeneratorAwareInterface
interface AssetManagerAwareInterface
{
/**
* Gets an asset URL generator.
* Gets an asset manager.
*
* @return AssetUrlGeneratorInterface
* @return AssetManagerInterface
*/
public function getAssetUrlGenerator();
public function getAssetManager();
/**
* Sets an asset URL generator.
* Sets an asset manager.
*
* @param AssetUrlGeneratorInterface $generator
* @param AssetManagerInterface $manager
*/
public function setAssetUrlGenerator(AssetUrlGeneratorInterface $generator);
public function setAssetManager(AssetManagerInterface $manager);
}

View File

@ -0,0 +1,106 @@
<?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\Asset;
use Mibew\Asset\Generator\UrlGeneratorInterface;
/**
* This is the interface that all Asset manager classes must implement.
*/
interface AssetManagerInterface
{
/**
* Indicates that content of an asset is an absolute URL.
*/
const ABSOLUTE_URL = 'absolute';
/**
* Indicates that content of an asset is a relative URL.
*/
const RELATIVE_URL = 'relative';
/**
* Indicates that content of an asset is raw CSS or JS.
*/
const INLINE = 'inline';
/**
* Gets an instance of Assets URL Generator.
*
* @return UrlGeneratorInterface
*/
public function getUrlGenerator();
/**
* Sets an instance of Assets URL Generator.
*
* @param UrlGeneratorInterface $generator
*/
public function setUrlGenerator(UrlGeneratorInterface $generator);
/**
* Attaches a JavaScript asset.
*
* @param type $content Content of the asset. It can be a kind of URL of
* plain content depends on the second argument of the method.
* @param mixed $type Determines asset type. It can be one of
* AssetManagerInterface::ABSOLUTE_URL,
* AssetManagerInterface::RELATIVE_URL or AssetManagerInterface::INLINE
* constants.
*/
public function addJsAsset($content, $type = self::ABSOLUTE_URL);
/**
* Retrieves all attached and provided by plugins JavaScript assets.
*
* @return array List of attached assets. Each item is an array with
* the following keys:
* - content: string, can be either a kind of URL or raw JavaScript
* content.
* - type: mixed, determines asset type. It can be one of
* AssetManagerInterface::ABSOLUTE_URL,
* AssetManagerInterface::RELATIVE_URL or AssetManagerInterface::INLINE
* constants.
*/
public function getJsAssets();
/**
* Attaches a CSS asset.
*
* @param type $content Content of the asset. It can be a kind of URL of
* plain content depends on the second argument of the method.
* @param mixed $type Determines asset type. It can be one of
* AssetManagerInterface::ABSOLUTE_URL,
* AssetManagerInterface::RELATIVE_URL or AssetManagerInterface::INLINE
* constants.
*/
public function addCssAsset($content, $type = self::ABSOLUTE_URL);
/**
* Retrieves all attached and provided by plugins CSS assets.
*
* @return array List of attached assets. Each item is an array with
* the following keys:
* - content: string, can be either a kind of URL or raw CSS content.
* - type: mixed, determines asset type. It can be one of
* AssetManagerInterface::ABSOLUTE_URL,
* AssetManagerInterface::RELATIVE_URL or AssetManagerInterface::INLINE
* constants.
*/
public function getCssAssets();
}

View File

@ -17,11 +17,11 @@
* limitations under the License.
*/
namespace Mibew\Asset;
namespace Mibew\Asset\Generator;
use Symfony\Component\HttpFoundation\Request;
class AssetUrlGenerator implements AssetUrlGeneratorInterface
class UrlGenerator implements UrlGeneratorInterface
{
protected $scheme;
protected $host;
@ -167,7 +167,7 @@ class AssetUrlGenerator implements AssetUrlGeneratorInterface
/**
* {@inheritdoc}
*/
public function generate($relative_path, $reference_type = AssetUrlGeneratorInterface::ABSOLUTE_PATH)
public function generate($relative_path, $reference_type = UrlGeneratorInterface::ABSOLUTE_PATH)
{
return $this->doGenerate($relative_path, $reference_type, false);
}
@ -175,7 +175,7 @@ class AssetUrlGenerator implements AssetUrlGeneratorInterface
/**
* {@inheritdoc}
*/
public function generateSecure($relative_path, $reference_type = AssetUrlGeneratorInterface::ABSOLUTE_PATH)
public function generateSecure($relative_path, $reference_type = UrlGeneratorInterface::ABSOLUTE_PATH)
{
return $this->doGenerate($relative_path, $reference_type, true);
}
@ -195,7 +195,7 @@ class AssetUrlGenerator implements AssetUrlGeneratorInterface
$need_host =
// A user wants an absolute URL
($reference_type === AssetUrlGeneratorInterface::ABSOLUTE_URL)
($reference_type === UrlGeneratorInterface::ABSOLUTE_URL)
// A scheme deffers from one from request.
|| $scheme !== $this->getScheme()
// A non-standard port is used.

View File

@ -17,13 +17,13 @@
* limitations under the License.
*/
namespace Mibew\Asset;
namespace Mibew\Asset\Generator;
/**
* UrlGeneratorInterface is the interface that all Asset URL generator classes
* must implement.
*/
interface AssetUrlGeneratorInterface
interface UrlGeneratorInterface
{
/**
* Generates an absolute URL, e.g. "http://example.com/dir/file".

View File

@ -19,7 +19,7 @@
namespace Mibew\Button\Generator;
use Mibew\Asset\AssetUrlGeneratorInterface;
use Mibew\Asset\Generator\UrlGeneratorInterface as AssetUrlGeneratorInterface;
use Mibew\Routing\Generator\SecureUrlGeneratorInterface as RouteUrlGeneratorInterface;
use Mibew\Settings;
use Mibew\Style\InvitationStyle;

View File

@ -19,8 +19,9 @@
namespace Mibew\Controller;
use Mibew\Asset\AssetUrlGeneratorAwareInterface;
use Mibew\Asset\AssetUrlGeneratorInterface;
use Mibew\Asset\AssetManagerAwareInterface;
use Mibew\Asset\AssetManagerInterface;
use Mibew\Asset\Generator\UrlGeneratorInterface as AssetUrlGeneratorInterface;
use Mibew\Authentication\AuthenticationManagerAwareInterface;
use Mibew\Authentication\AuthenticationManagerInterface;
use Mibew\Cache\CacheAwareInterface;
@ -42,7 +43,7 @@ use Symfony\Component\HttpFoundation\RedirectResponse;
abstract class AbstractController implements
RouterAwareInterface,
AuthenticationManagerAwareInterface,
AssetUrlGeneratorAwareInterface,
AssetManagerAwareInterface,
CacheAwareInterface
{
/**
@ -61,9 +62,9 @@ abstract class AbstractController implements
protected $style = null;
/**
* @var AssetUrlGeneratorInterface|null
* @var AssetManagerInterface|null
*/
protected $assetUrlGenerator = null;
protected $assetManager = null;
/**
* @var PoolInterface|null;
@ -116,15 +117,15 @@ abstract class AbstractController implements
/**
* {@inheritdoc}
*/
public function setAssetUrlGenerator(AssetUrlGeneratorInterface $generator)
public function setAssetManager(AssetManagerInterface $manager)
{
$this->assetUrlGenerator = $generator;
$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($generator);
$handlebars->getHelper('asset')->setAssetUrlGenerator($manager->getUrlGenerator());
}
}
}
@ -132,9 +133,9 @@ abstract class AbstractController implements
/**
* {@inheritdoc}
*/
public function getAssetUrlGenerator()
public function getAssetManager()
{
return $this->assetUrlGenerator;
return $this->assetManager;
}
/**
@ -227,12 +228,12 @@ abstract class AbstractController implements
* @param string $relative_path Relative path of an asset.
* @param bool|string $reference_type Indicates what type of URL should be
* generated. It is equal to one of the
* {@link \Mibew\Asset\AssetUrlGeneratorInterface} constants.
* {@link AssetUrlGeneratorInterface} constants.
* @return string Asset URL.
*/
public function asset($relative_path, $reference_type = AssetUrlGeneratorInterface::ABSOLUTE_PATH)
{
return $this->getAssetUrlGenerator()
return $this->getAssetManager()->getUrlGenerator()
->generate($relative_path, $reference_type);
}
@ -278,7 +279,7 @@ abstract class AbstractController implements
$style->getHandlebars()->addHelper(
'asset',
new AssetHelper(
$this->getAssetUrlGenerator(),
$this->getAssetManager()->getUrlGenerator(),
array('CurrentStyle' => $style->getFilesPath())
)
);

View File

@ -118,7 +118,7 @@ class ButtonCodeController extends AbstractController
$button_generator = new ImageButtonGenerator(
$this->getRouter(),
$this->getAssetUrlGenerator(),
$this->getAssetManager()->getUrlGenerator(),
$button_generator_options
);

View File

@ -19,8 +19,8 @@
namespace Mibew\Controller;
use Mibew\Asset\AssetUrlGeneratorAwareInterface;
use Mibew\Asset\AssetUrlGeneratorInterface;
use Mibew\Asset\AssetManagerAwareInterface;
use Mibew\Asset\AssetManagerInterface;
use Mibew\Authentication\AuthenticationManagerAwareInterface;
use Mibew\Authentication\AuthenticationManagerInterface;
use Mibew\Cache\CacheAwareInterface;
@ -32,7 +32,7 @@ use Symfony\Component\HttpFoundation\Request;
class ControllerResolver implements
RouterAwareInterface,
AuthenticationManagerAwareInterface,
AssetUrlGeneratorAwareInterface,
AssetManagerAwareInterface,
CacheAwareInterface
{
/**
@ -46,9 +46,9 @@ class ControllerResolver implements
protected $authenticationManager = null;
/**
* @var AssetUrlGeneratorInterface|null
* @var AssetManagerInterface|null
*/
protected $assetUrlGenerator = null;
protected $assetManager = null;
/**
* @var PoolInterface|null;
@ -59,21 +59,21 @@ class ControllerResolver implements
* Class constructor.
*
* @param RouterInterface $router Router instance.
* @param AuthenticationManagerInterface $manager Authentication manager
* instance.
* @param AssetUrlGeneratorInterface $url_generator An instance of Asset
* URL generator.
* @param AuthenticationManagerInterface $authentication_manager
* Authentication manager instance.
* @param AssetManagerInterface $asset_manager An instance of Asset
* Manager.
* @param PoolInterface $cache An instance of Cache pool.
*/
public function __construct(
RouterInterface $router,
AuthenticationManagerInterface $manager,
AssetUrlGeneratorInterface $url_generator,
AuthenticationManagerInterface $authentication_manager,
AssetManagerInterface $asset_manager,
PoolInterface $cache
) {
$this->router = $router;
$this->authenticationManager = $manager;
$this->assetUrlGenerator = $url_generator;
$this->authenticationManager = $authentication_manager;
$this->assetManager = $asset_manager;
$this->cache = $cache;
}
@ -112,17 +112,17 @@ class ControllerResolver implements
/**
* {@inheritdoc}
*/
public function setAssetUrlGenerator(AssetUrlGeneratorInterface $generator)
public function setAssetManager(AssetManagerInterface $manager)
{
$this->assetUrlGenerator = $generator;
$this->assetManager = $manager;
}
/**
* {@inheritdoc}
*/
public function getAssetUrlGenerator()
public function getAssetManager()
{
return $this->assetUrlGenerator;
return $this->assetManager;
}
/**
@ -202,8 +202,8 @@ class ControllerResolver implements
$object->setAuthenticationManager($this->getAuthenticationManager());
}
if ($object instanceof AssetUrlGeneratorAwareInterface) {
$object->setAssetUrlGenerator($this->getAssetUrlGenerator());
if ($object instanceof AssetManagerAwareInterface) {
$object->setAssetManager($this->getAssetManager());
}
if ($object instanceof CacheAwareInterface) {

View File

@ -22,8 +22,7 @@ namespace Mibew\Handlebars\Helper;
use Handlebars\Context;
use Handlebars\Helper as HelperInterface;
use Handlebars\Template;
use Mibew\Asset\AssetUrlGeneratorAwareInterface;
use Mibew\Asset\AssetUrlGeneratorInterface;
use Mibew\Asset\Generator\UrlGeneratorInterface as AssetUrlGeneratorInterface;
/**
* A helper that generates URLs for assets.
@ -47,7 +46,7 @@ use Mibew\Asset\AssetUrlGeneratorInterface;
* {{asset "@CustomStorage/images/the_best_logo.png"}}
* <code>
*/
class AssetHelper implements HelperInterface, AssetUrlGeneratorAwareInterface
class AssetHelper implements HelperInterface
{
/**
* @var array
@ -79,7 +78,9 @@ class AssetHelper implements HelperInterface, AssetUrlGeneratorAwareInterface
}
/**
* {@inheritdoc}
* Gets instance of Asset URL Generator.
*
* @return AssetUrlGeneratorInterface
*/
public function getAssetUrlGenerator()
{
@ -87,7 +88,9 @@ class AssetHelper implements HelperInterface, AssetUrlGeneratorAwareInterface
}
/**
* {@inheritdoc}
* Sets an instance of Asset URL Generator.
*
* @param AssetUrlGeneratorInterface $generator
*/
public function setAssetUrlGenerator(AssetUrlGeneratorInterface $generator)
{