block.assetic.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2015 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
11 
17 
26 
27 if (!class_exists('Assetic\Asset\AssetCollection')) {
28  throw new ConfigurationException(
29  'smarty_block_assetic requires '.
30  'Assetic. If you are using composer, add kriswallsmith/assetic '.
31  'as dependency to your project');
32 }
33 
34 /*
35 * Smarty plugin
36 * -------------------------------------------------------------
37 * File: block.assetic.php
38 * Type: block
39 * Name: assetic
40 * Purpose: Deliver assets using assetic library. Files will be combined and
41 * minified. JS and CSS will be recognized by the file extension.
42 * In order to not minify minified files again, the must use .min. in
43 * the filename. The result will be cached in the smarty cache.
44 * Works only for local files.
45 * Parameters: name The name of the created file (will be appended by .min.js|.min.css)
46 * debug Boolean, if true the content will be returned as is
47 * Usage: {assetic name='header' debug=false}
48 * <link rel="stylesheet" href="css/normalize.min.css">
49 * <link rel="stylesheet" href="css/main.css">
50 *
51 * <script src="js/vendor/modernizr-2.8.3.min.js"></script>
52 * <script src="js/main.js"></script>
53 * {/assetic}
54 *
55 * Author: Ingo Herwig <ingo@wemove.com>
56 * -------------------------------------------------------------
57 */
58 function smarty_block_assetic($params, $content, Smarty_Internal_Template $template, &$repeat) {
59  if(!$repeat) {
60  if (isset($content)) {
61  if ($params['debug'] == true) {
62  return $content;
63  }
64  else {
65  $result = '';
66 
67  // parse urls and group resources by extension and minified state
68  $resources = array();
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());
76  }
77  $resources[$extension][$min ? 'min' : 'src'][] = $url;
78  }
79 
80  // setup assetic
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);
85 
86  // process resources
87  foreach ($resources as $type => $files) {
88  $filesystem = new FilesystemCache($cacheRootAbs);
89  $writer = new AssetWriter($cacheRootAbs);
90 
91  $cacheFile = (isset($params['name']) ? $params['name'] : uniqid()).'.min.'.$type;
92  $cachePathRel = $cacheRootRel.'/'.$cacheFile;
93 
94  // create filters
95  $filters = array();
96  if ($type == 'css') {
97  $filters[] = new CssRewriteFilter();
98  }
99  $minFilters = array_merge($filters, array(new MinFilter($type)));
100 
101  // create string assets from files (sourcePath and targetPath must be
102  // set correctly in order to make CssRewriteFilter work)
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());
108  }
109  foreach ($files['src'] as $file) {
110  $asset = new FileAsset($file, $minFilters, '', $file);
111  $asset->setTargetPath($cachePathRel);
112  $minAssets[] = new StringAsset($asset->dump());
113  }
114 
115  // write collected assets into cached file
116  $minCollection = new AssetCollection($minAssets);
117  $cache = new AssetCache($minCollection, $filesystem);
118  $cache->setTargetPath($cacheFile);
119  $writer->writeAsset($cache);
120 
121  // create html tag
122  switch ($type) {
123  case 'js':
124  $tag = '<script src="'.$cachePathRel.'"></script>';
125  break;
126  case 'css':
127  $tag = '<link rel="stylesheet" href="'.$cachePathRel.'">';
128  break;
129  }
130  $result .= $tag;
131  }
132  return $result;
133  }
134  }
135  }
136 }
137 ?>
ConfigurationException signals an exception in the configuration.