Skip to content

Commit

Permalink
add 'weight' feature to page assets to allow basic management of orde…
Browse files Browse the repository at this point in the history
…r.refs #2596, #1324
  • Loading branch information
craigh committed Sep 25, 2015
1 parent 3fb70bc commit bc179e4
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 31 deletions.
28 changes: 18 additions & 10 deletions src/lib/Zikula/Bundle/CoreBundle/EventListener/ThemeListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
]
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<argument type="service" id="zikula_core.common.theme.assets_js" />
<argument type="service" id="zikula_core.common.theme.assets_css" />
<argument type="service" id="zikula_core.common.theme.pagevars" />
<argument>%env%</argument>
</service>

<service id="zikula_core.common.theme.assets_js" class="%zikula_core.common.theme.assets.js.class%" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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__);
}

Expand All @@ -308,21 +310,21 @@ 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);
return;
}

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]);
}
}

Expand Down
31 changes: 21 additions & 10 deletions src/lib/Zikula/Core/Theme/AssetBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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()
Expand All @@ -40,7 +50,8 @@ public function clear()

public function all()
{
return $this->assets;
// returns sorted asset
return array_keys($this->assets);
}

/**
Expand Down
12 changes: 8 additions & 4 deletions src/lib/Zikula/Core/Theme/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit bc179e4

Please sign in to comment.