27 if (!class_exists(
'Assetic\Asset\AssetCollection')) {
29 'smarty_block_assetic requires '.
30 'Assetic. If you are using composer, add kriswallsmith/assetic '.
31 'as dependency to your project');
58 function smarty_block_assetic($params, $content, Smarty_Internal_Template $template, &$repeat) {
60 if (isset($content)) {
61 if ($params[
'debug'] ==
true) {
69 $urls = StringUtil::getUrls($content);
70 foreach ($urls as $url) {
71 $parts = pathinfo($url);
72 $extension = strtolower($parts[
'extension']);
73 $min = preg_match(
'/\.min$/', $parts[
'filename']);
74 if (!isset($resources[$extension])) {
75 $resources[$extension] = array(
'min' => array(),
'src' => array());
77 $resources[$extension][$min ?
'min' :
'src'][] = $url;
81 $config = ObjectFactory::getInstance(
'configuration');
82 $basePath = dirname(FileUtil::realpath($_SERVER[
'SCRIPT_FILENAME'])).
'/';
83 $cacheRootAbs = WCMF_BASE.$config->getValue(
'cacheDir',
'View').
'cache';
84 $cacheRootRel = URIUtil::makeRelative($cacheRootAbs, $basePath);
87 foreach ($resources as $type => $files) {
88 $filesystem =
new FilesystemCache($cacheRootAbs);
89 $writer =
new AssetWriter($cacheRootAbs);
91 $cacheFile = (isset($params[
'name']) ? $params[
'name'] : uniqid()).
'.min.'.$type;
92 $cachePathRel = $cacheRootRel.
'/'.$cacheFile;
97 $filters[] =
new CssRewriteFilter();
99 $minFilters = array_merge($filters, array(
new MinFilter($type)));
103 $minAssets = array();
104 foreach ($files[
'min'] as $file) {
105 $asset =
new FileAsset($file, $filters,
'', $file);
106 $asset->setTargetPath($cachePathRel);
107 $minAssets[] =
new StringAsset($asset->dump());
109 foreach ($files[
'src'] as $file) {
110 $asset =
new FileAsset($file, $minFilters,
'', $file);
111 $asset->setTargetPath($cachePathRel);
112 $minAssets[] =
new StringAsset($asset->dump());
116 $minCollection =
new AssetCollection($minAssets);
117 $cache =
new AssetCache($minCollection, $filesystem);
118 $cache->setTargetPath($cacheFile);
119 $writer->writeAsset($cache);
124 $tag =
'<script src="'.$cachePathRel.
'"></script>';
127 $tag =
'<link rel="stylesheet" href="'.$cachePathRel.
'">';
ConfigurationException signals an exception in the configuration.