diff --git a/src/lib/Zikula/Bundle/CoreBundle/EventListener/ThemeListener.php b/src/lib/Zikula/Bundle/CoreBundle/EventListener/ThemeListener.php index 0e497e3370..d675710e27 100644 --- a/src/lib/Zikula/Bundle/CoreBundle/EventListener/ThemeListener.php +++ b/src/lib/Zikula/Bundle/CoreBundle/EventListener/ThemeListener.php @@ -32,14 +32,16 @@ class ThemeListener implements EventSubscriberInterface private $cssAssetBag; private $jsAssetBag; private $pageVars; + private $env; - function __construct(\Twig_Loader_Filesystem $loader, Engine $themeEngine, AssetBag $jsAssetBag, AssetBag $cssAssetBag, ParameterBag $pageVars) + function __construct(\Twig_Loader_Filesystem $loader, Engine $themeEngine, AssetBag $jsAssetBag, AssetBag $cssAssetBag, ParameterBag $pageVars, $env) { $this->loader = $loader; $this->themeEngine = $themeEngine; $this->jsAssetBag = $jsAssetBag; $this->cssAssetBag = $cssAssetBag; $this->pageVars = $pageVars; + $this->env = $env; } public function onKernelResponse(FilterResponseEvent $event) @@ -95,23 +97,29 @@ public function setDefaultPageAssets(GetResponseEvent $event) return; } $basePath = $event->getRequest()->getBasePath(); + $jquery = $this->env == 'prod' ? 'jquery.min.js' : 'jquery.js'; // add default javascripts to jsAssetBag - $this->jsAssetBag->add(array( - $basePath . '/web/jquery/jquery.min.js', - $basePath . '/web/bootstrap/js/bootstrap.min.js', - $basePath . '/javascript/helpers/bootstrap-zikula.js', + $this->jsAssetBag->add( + [ + $basePath . "/web/jquery/$jquery" => 0, + $basePath . '/web/bootstrap/js/bootstrap.min.js' => 1, + $basePath . '/javascript/helpers/bootstrap-zikula.js' => 2, + $basePath . '/web/html5shiv/dist/html5shiv.js' => 3, // $basePath . '/javascript/helpers/Zikula.js', // @todo legacy remove at Core 2.0 // the following currently added by JCSSUtil in Engine::filter() // $basePath . '/web/bundles/fosjsrouting/js/router.js', // $basePath . '/web/js/fos_js_routes.js', - )); + ] + ); // add default stylesheets to cssAssetBag - $this->cssAssetBag->add(array( - $basePath . '/web/bootstrap-font-awesome.css', - $basePath . '/style/core.css', - )); + $this->cssAssetBag->add( + [ + $basePath . '/web/bootstrap-font-awesome.css' => 0, + $basePath . '/style/core.css' => 1, + ] + ); } /** diff --git a/src/lib/Zikula/Bundle/CoreBundle/Resources/config/theme.xml b/src/lib/Zikula/Bundle/CoreBundle/Resources/config/theme.xml index 895b9c3c53..be51b470a9 100644 --- a/src/lib/Zikula/Bundle/CoreBundle/Resources/config/theme.xml +++ b/src/lib/Zikula/Bundle/CoreBundle/Resources/config/theme.xml @@ -42,6 +42,7 @@ + %env% diff --git a/src/lib/Zikula/Bundle/CoreBundle/Twig/Extension/CoreExtension.php b/src/lib/Zikula/Bundle/CoreBundle/Twig/Extension/CoreExtension.php index 9b3af4eefc..fd14f50732 100644 --- a/src/lib/Zikula/Bundle/CoreBundle/Twig/Extension/CoreExtension.php +++ b/src/lib/Zikula/Bundle/CoreBundle/Twig/Extension/CoreExtension.php @@ -17,6 +17,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface; use Zikula\Bundle\CoreBundle\Twig; use Zikula\Bundle\CoreBundle\Twig\Extension\SimpleFunction\AdminMenuPanelSimpleFunction; +use Zikula\Core\Theme\AssetBag; class CoreExtension extends \Twig_Extension { @@ -291,13 +292,14 @@ public function pageAddVar($name, $value) * * @param string $type * @param string $value + * @param int $weight */ - public function pageAddAsset($type, $value) + public function pageAddAsset($type, $value, $weight = AssetBag::DEFAULT_WEIGHT) { if (empty($type) || empty($value)) { throw new \InvalidArgumentException(__('Empty argument at') . ':' . __FILE__ . '::' . __LINE__); } - if (!in_array($type, ['stylesheet', 'javascript', 'header', 'footer'])) { + if (!in_array($type, ['stylesheet', 'javascript', 'header', 'footer']) || !is_numeric($weight)) { throw new \InvalidArgumentException(__('Invalid argument at') . ':' . __FILE__ . '::' . __LINE__); } @@ -308,7 +310,7 @@ public function pageAddAsset($type, $value) $features = null; } - // remove this code block at Core-2.0 because all themes are twig based + // @todo remove this code block at Core-2.0 because all themes are twig based $themeBundle = $this->container->get('zikula_core.common.theme_engine')->getTheme(); if (!$themeBundle->isTwigBased()) { \PageUtil::addVar($type, $value); @@ -316,13 +318,13 @@ public function pageAddAsset($type, $value) } if ('stylesheet' == $type) { - $this->container->get('zikula_core.common.theme.assets_css')->add($value); + $this->container->get('zikula_core.common.theme.assets_css')->add([$value => $weight]); } elseif ('javascript' == $type) { - $this->container->get('zikula_core.common.theme.assets_js')->add($value); + $this->container->get('zikula_core.common.theme.assets_js')->add([$value => $weight]); } elseif ('header' == $type) { - $this->container->get('zikula_core.common.theme.assets_header')->add($value); + $this->container->get('zikula_core.common.theme.assets_header')->add([$value => $weight]); } elseif ('footer' == $type) { - $this->container->get('zikula_core.common.theme.assets_footer')->add($value); + $this->container->get('zikula_core.common.theme.assets_footer')->add([$value => $weight]); } } diff --git a/src/lib/Zikula/Core/Theme/AssetBag.php b/src/lib/Zikula/Core/Theme/AssetBag.php index eda7723ac5..b08efcae05 100644 --- a/src/lib/Zikula/Core/Theme/AssetBag.php +++ b/src/lib/Zikula/Core/Theme/AssetBag.php @@ -4,11 +4,17 @@ class AssetBag implements \IteratorAggregate, \Countable { - private $assets; + const DEFAULT_WEIGHT = 100; - public function __construct(array $assets = array()) + /** + * array format: + * $assets = [value => weight, value => weight, value => weight] + * @var array + */ + private $assets = array(); + + public function __construct() { - $this->assets = $assets; } /** @@ -19,18 +25,22 @@ public function __construct(array $assets = array()) */ public function add($asset) { + // ensure value is an array if (!is_array($asset)) { - $asset = array($asset); + $asset = [$asset => self::DEFAULT_WEIGHT]; } - $this->assets = array_merge($this->assets, $asset); - $this->assets = array_unique($this->assets); + foreach ($asset as $source => $weight) { + if ((isset($this->assets[$source]) && $this->assets[$source] > $weight) || !isset($this->assets[$source])) { + // keep original weight if lighter. set if not set already. + $this->assets[$source] = $weight; + } + } + asort($this->assets); // put array in order by weight } public function remove($var) { - if ($key = array_search($var, $this->assets)) { - unset($this->assets[$key]); - } + unset($this->assets[$var]); } public function clear() @@ -40,7 +50,8 @@ public function clear() public function all() { - return $this->assets; + // returns sorted asset + return array_keys($this->assets); } /** diff --git a/src/lib/Zikula/Core/Theme/Engine.php b/src/lib/Zikula/Core/Theme/Engine.php index eb45a28cbf..251c57c14f 100644 --- a/src/lib/Zikula/Core/Theme/Engine.php +++ b/src/lib/Zikula/Core/Theme/Engine.php @@ -340,17 +340,21 @@ private function filter(Response $response) { // @todo START legacy block - remove at Core-2.0 $baseUri = \System::getBaseUri(); + $jsAssets = []; $javascripts = \JCSSUtil::prepareJavascripts(\PageUtil::getVar('javascript')); - foreach ($javascripts as $key => $javascript) { - $javascripts[$key] = (false === strpos($javascript, $baseUri)) ? $baseUri . '/' . $javascript : $javascript; + foreach ($javascripts as $javascript) { + $javascript = (false === strpos($javascript, $baseUri)) ? $baseUri . '/' . $javascript : $javascript; + $jsAssets[$javascript] = 50; // add before pageAddAsset default weight (100) } + $cssAssets = []; $stylesheets = \PageUtil::getVar('stylesheet'); foreach ($stylesheets as $key => $stylesheet) { - $stylesheets[$key] = $baseUri . '/' . $stylesheet; + $stylesheet = $baseUri . '/' . $stylesheet; + $cssAssets[$stylesheet] = 50; // add before pageAddAsset default weight (100) } // @todo END legacy block - remove at Core-2.0 - $filteredContent = $this->filterService->filter($response->getContent(), $javascripts, $stylesheets); + $filteredContent = $this->filterService->filter($response->getContent(), $jsAssets, $cssAssets); $response->setContent($filteredContent); return $response; }