From e320dcd1bb6bc551939f95c51c56da05053dbb7b Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Wed, 8 Oct 2014 13:52:56 +0000 Subject: [PATCH] Create AssetManager class --- src/mibew/libs/classes/Mibew/Application.php | 13 +- .../libs/classes/Mibew/Asset/AssetManager.php | 234 ++++++++++++++++++ ...ace.php => AssetManagerAwareInterface.php} | 16 +- .../Mibew/Asset/AssetManagerInterface.php | 106 ++++++++ .../UrlGenerator.php} | 10 +- .../UrlGeneratorInterface.php} | 4 +- .../Mibew/Button/Generator/ImageGenerator.php | 2 +- .../Mibew/Controller/AbstractController.php | 27 +- .../Mibew/Controller/ButtonCodeController.php | 2 +- .../Mibew/Controller/ControllerResolver.php | 38 +-- .../Mibew/Handlebars/Helper/AssetHelper.php | 13 +- 11 files changed, 405 insertions(+), 60 deletions(-) create mode 100644 src/mibew/libs/classes/Mibew/Asset/AssetManager.php rename src/mibew/libs/classes/Mibew/Asset/{AssetUrlGeneratorAwareInterface.php => AssetManagerAwareInterface.php} (65%) create mode 100644 src/mibew/libs/classes/Mibew/Asset/AssetManagerInterface.php rename src/mibew/libs/classes/Mibew/Asset/{AssetUrlGenerator.php => Generator/UrlGenerator.php} (94%) rename src/mibew/libs/classes/Mibew/Asset/{AssetUrlGeneratorInterface.php => Generator/UrlGeneratorInterface.php} (96%) diff --git a/src/mibew/libs/classes/Mibew/Application.php b/src/mibew/libs/classes/Mibew/Application.php index 8e385cd7..5753f3cc 100644 --- a/src/mibew/libs/classes/Mibew/Application.php +++ b/src/mibew/libs/classes/Mibew/Application.php @@ -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); } /** diff --git a/src/mibew/libs/classes/Mibew/Asset/AssetManager.php b/src/mibew/libs/classes/Mibew/Asset/AssetManager.php new file mode 100644 index 00000000..159f9e40 --- /dev/null +++ b/src/mibew/libs/classes/Mibew/Asset/AssetManager.php @@ -0,0 +1,234 @@ +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; + } +} diff --git a/src/mibew/libs/classes/Mibew/Asset/AssetUrlGeneratorAwareInterface.php b/src/mibew/libs/classes/Mibew/Asset/AssetManagerAwareInterface.php similarity index 65% rename from src/mibew/libs/classes/Mibew/Asset/AssetUrlGeneratorAwareInterface.php rename to src/mibew/libs/classes/Mibew/Asset/AssetManagerAwareInterface.php index eda03cfd..68efc10d 100644 --- a/src/mibew/libs/classes/Mibew/Asset/AssetUrlGeneratorAwareInterface.php +++ b/src/mibew/libs/classes/Mibew/Asset/AssetManagerAwareInterface.php @@ -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); } diff --git a/src/mibew/libs/classes/Mibew/Asset/AssetManagerInterface.php b/src/mibew/libs/classes/Mibew/Asset/AssetManagerInterface.php new file mode 100644 index 00000000..62a4fa80 --- /dev/null +++ b/src/mibew/libs/classes/Mibew/Asset/AssetManagerInterface.php @@ -0,0 +1,106 @@ +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. diff --git a/src/mibew/libs/classes/Mibew/Asset/AssetUrlGeneratorInterface.php b/src/mibew/libs/classes/Mibew/Asset/Generator/UrlGeneratorInterface.php similarity index 96% rename from src/mibew/libs/classes/Mibew/Asset/AssetUrlGeneratorInterface.php rename to src/mibew/libs/classes/Mibew/Asset/Generator/UrlGeneratorInterface.php index d45ccf7d..aaa42e0f 100644 --- a/src/mibew/libs/classes/Mibew/Asset/AssetUrlGeneratorInterface.php +++ b/src/mibew/libs/classes/Mibew/Asset/Generator/UrlGeneratorInterface.php @@ -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". diff --git a/src/mibew/libs/classes/Mibew/Button/Generator/ImageGenerator.php b/src/mibew/libs/classes/Mibew/Button/Generator/ImageGenerator.php index bb863264..507286e5 100644 --- a/src/mibew/libs/classes/Mibew/Button/Generator/ImageGenerator.php +++ b/src/mibew/libs/classes/Mibew/Button/Generator/ImageGenerator.php @@ -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; diff --git a/src/mibew/libs/classes/Mibew/Controller/AbstractController.php b/src/mibew/libs/classes/Mibew/Controller/AbstractController.php index 9fe430a4..7e01ed64 100644 --- a/src/mibew/libs/classes/Mibew/Controller/AbstractController.php +++ b/src/mibew/libs/classes/Mibew/Controller/AbstractController.php @@ -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()) ) ); diff --git a/src/mibew/libs/classes/Mibew/Controller/ButtonCodeController.php b/src/mibew/libs/classes/Mibew/Controller/ButtonCodeController.php index 5269ccfa..5f7d5f19 100644 --- a/src/mibew/libs/classes/Mibew/Controller/ButtonCodeController.php +++ b/src/mibew/libs/classes/Mibew/Controller/ButtonCodeController.php @@ -118,7 +118,7 @@ class ButtonCodeController extends AbstractController $button_generator = new ImageButtonGenerator( $this->getRouter(), - $this->getAssetUrlGenerator(), + $this->getAssetManager()->getUrlGenerator(), $button_generator_options ); diff --git a/src/mibew/libs/classes/Mibew/Controller/ControllerResolver.php b/src/mibew/libs/classes/Mibew/Controller/ControllerResolver.php index 43390ba5..159f7f18 100644 --- a/src/mibew/libs/classes/Mibew/Controller/ControllerResolver.php +++ b/src/mibew/libs/classes/Mibew/Controller/ControllerResolver.php @@ -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) { diff --git a/src/mibew/libs/classes/Mibew/Handlebars/Helper/AssetHelper.php b/src/mibew/libs/classes/Mibew/Handlebars/Helper/AssetHelper.php index 47248cf5..d5a34c1c 100644 --- a/src/mibew/libs/classes/Mibew/Handlebars/Helper/AssetHelper.php +++ b/src/mibew/libs/classes/Mibew/Handlebars/Helper/AssetHelper.php @@ -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"}} * */ -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) {