CMS  Version 3.9
optimize_manager.inc
Go to the documentation of this file.
1 <?php
7 /**************************************************************
8 
9  Copyright (c) 2010 Sonjara, Inc
10 
11  Permission is hereby granted, free of charge, to any person
12  obtaining a copy of this software and associated documentation
13  files (the "Software"), to deal in the Software without
14  restriction, including without limitation the rights to use,
15  copy, modify, merge, publish, distribute, sublicense, and/or sell
16  copies of the Software, and to permit persons to whom the
17  Software is furnished to do so, subject to the following
18  conditions:
19 
20  The above copyright notice and this permission notice shall be
21  included in all copies or substantial portions of the Software.
22 
23  Except as contained in this notice, the name(s) of the above
24  copyright holders shall not be used in advertising or otherwise
25  to promote the sale, use or other dealings in this Software
26  without prior written authorization.
27 
28  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
30  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
32  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
33  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
34  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
35  OTHER DEALINGS IN THE SOFTWARE.
36 
37 *****************************************************************/
38 
39 Fakoli::using("settings", "optimize");
40 Fakoli::usingFeature("cache");
41 
42 interface IMinify
43 {
44 
45  function minifyJavascript($js);
46  function minifyCSS($css);
47 }
48 
50 {
51  static $zipped = false;
52 
53  static $minifier = null;
54 
56  {
58  }
59 
60  static function onInitialize()
61  {
62  global $css_cache_timestamp;
63  $css_cache_timestamp = Cache::get("css_cache_timestamp");
64  }
65 
66  static function clearCache()
67  {
68  Cache::invalidate("script_cache");
69  Cache::invalidate("script_cache_timestamp");
70  Cache::invalidate("css_cache");
71  Cache::invalidate("css_cache_timestamp");
72  Settings::setDefaultValue("optimize", "optimize_scripts", "", Boolean, "Combine site Javascript into a single file", "Javascript", null, 1);
73  Settings::setDefaultValue("optimize", "optimize_styles", "", Boolean, "Combine site CSS into a single file", "CSS", null, 1);
74 
75  Settings::setDefaultValue("optimize", "compress_javascript", "", Boolean, "Enable zip compression when serving Javascript", "Javascript", null, 2);
76  Settings::setDefaultValue("optimize", "compress_styles", "", Boolean, "Enable zip compression when serving CSS", "CSS", null, 2);
77 
78  Settings::setDefaultValue("optimize", "compress_page_content", "", Boolean, "Enable zip compression when serving page content", "Page Content", null, 1);
79  Settings::setDefaultValue("optimize", "append_timestamp", false, Boolean, "Specifies whether to include a timestamp in the generated link for optimized scripts and styles", "Page Content", null, 2);
80  }
81 
82  static function preprocessScripts($scripts)
83  {
84  if (Settings::getValue("optimize", "optimize_scripts"))
85  {
87  {
88  ComponentManager::fireEvent("RegisterMinifier");
89  }
90 
91  $cachedScripts = Cache::get("script_cache");
92 
93  if (!$cachedScripts)
94  {
95  foreach($scripts as $script)
96  {
97  $resource = Fakoli::resolveResource($script);
98  if ($resource)
99  {
100  $file_modified = filemtime($resource);
101  if ($file_modified > $last_modified)
102  {
103  $last_modified = $file_modified;
104  }
105 
106  $cachedScripts .= file_get_contents($resource);
107  $cachedScripts .= "\r\n";
108  trace("Added $script", 3);
109  }
110  else
111  {
112  trace("Could not resolve script $script", 2);
113  }
114  }
115 
116  if (OptimizeManager::$minifier && Settings::getValue("optimize", "minify_javascript"))
117  {
118  $cachedScripts = OptimizeManager::$minifier->minifyJavascript($cachedScripts);
119  }
120 
121  Cache::put("script_cache", $cachedScripts);
122  Cache::put("script_cache_timestamp", $last_modified);
123  }
124 
125  $scriptLink = Settings::getValue("optimize", "append_timestamp") ? "/action/optimize/scripts?".Cache::get("script_cache_timestamp") : "/action/optimize/scripts";
126 
127  $scripts = array($scriptLink);
128  }
129 
130  return $scripts;
131  }
132 
133  static function getScriptsContent()
134  {
135  $content = Cache::get("script_cache");
136  if (!$content)
137  {
138  $scripts = Fakoli::loadScripts();
140  $content = Cache::get("script_cache");
141  }
142 
143  return $content;
144  }
145 
146  static function preProcessStyles($styles)
147  {
149  {
150  ComponentManager::fireEvent("RegisterMinifier");
151  }
152 
153  if (Settings::getValue("optimize", "optimize_styles"))
154  {
155  $cachedStyles = Cache::get("css_cache");
156 
157  if (!$cachedStyles)
158  {
159  foreach($styles as $style)
160  {
161  $resource = Fakoli::resolveResource($style);
162  if ($resource)
163  {
164  $file_modified = filemtime($resource);
165  if ($file_modified > $last_modified)
166  {
167  $last_modified = $file_modified;
168  }
169 
170  $cachedStyles .= file_get_contents($resource);
171  $cachedStyles .= "\r\n";
172  }
173  }
174 
175  $cachedStyles = preg_replace("/^@CHARSET.*$/mi", "", $cachedStyles);
176  $cachedStyles = str_replace("\r\n", "\n", $cachedStyles);
177  $cachedStyles = str_replace("\n", "\r\n", $cachedStyles);
178 
179  if (OptimizeManager::$minifier && Settings::getValue("optimize", "minify_styles"))
180  {
181  $cachedStyles = OptimizeManager::$minifier->minifyCSS($cachedStyles);
182  }
183 
184  Cache::put("css_cache", $cachedStyles);
185  Cache::put("css_cache_timestamp", $last_modified);
186  }
187 
188  $styleLink = Settings::getValue("optimize", "append_timestamp") ? "/action/optimize/styles?".Cache::get("css_cache_timestamp") : "/action/optimize/styles";
189 
190  $styles = array($styleLink);
191  }
192 
193  return $styles;
194  }
195 
196  static function getStylesContent()
197  {
198  $cachedStyles = Cache::get("css_cache");
199 
200  if (!$cachedStyles)
201  {
204  $cachedStyles = Cache::get("css_cache");
205  }
206 
207  return $cachedStyles;
208  }
209 
210  static function preProcessPage($template)
211  {
212  if (Settings::getValue("optimize", "compress_page_content") && substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip'))
213  {
214  ob_start("ob_gzhandler");
215  $level = ob_get_level();
216  trace("ZIP output buffer level: $level", 3);
218  }
219  return $template;
220  }
221 
222  static function preAction($action)
223  {
224  trace("PreAction: $action", 3);
225  switch($action)
226  {
227  case "scripts":
228  if (Settings::getValue("optimize", "compress_javascript") && substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip'))
229  {
230  trace("Compressing scripts", 3);
231  OptimizeManager::$zipped = true;
232  }
233  break;
234 
235  case "styles":
236 
237  if (Settings::getValue("optimize", "compress_styles") && substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip'))
238  {
239  trace("Compressing styles", 3);
240  OptimizeManager::$zipped = true;
241  }
242  break;
243 
244  default:
245  }
246  }
247 
248  static function startZip()
249  {
251  {
252  if (extension_loaded("zlib") && (ini_get("output_handler") != "ob_gzhandler"))
253  {
254  trace("Setting zlib.output_compression to On", 3);
255 
256  @ob_end_clean();
257  @ini_set("zlib.output_compression", 'On');
258  @ini_set("zlib.output_compression_level", 9);
259  }
260 
261 // ob_start();
262 // if (!ob_start("ob_gzhandler"))
263 // {
264 // trace("Zip handler ignored", 3);
265 // }
266  }
267 
268  trace("zlib.output_compression = ".ini_get("zlib.output_compression"), 3);
269  }
270 
271  static function flushZip()
272  {
273 // if (OptimizeManager::$zipped)
274 // {
275 // trace("Flushing zipped content", 3);
276 // trace(print_r(ob_list_handlers(), true), 3);
277 // ob_end_flush();
278 // header("Content-Length: ".ob_get_length());
279 // ob_end_flush();
280 // trace(print_r(ob_list_handlers(), true), 3);
281 // }
282  }
283 
284  static function postAction($action)
285  {
286  }
287 }
288 ?>
static fireEvent($event, $parameter=null, $mustBeConsumed=false)
Fire an event to all subscribers as detailed in their manifests.
static resolveResource($resource, $component="")
Resolves the path to a web resource based on the PHP include path.
Definition: core.inc:850
static usingFeature()
Uses the specified framework feature(s).
Definition: core.inc:388
static $outputBufferLevel
Records the level at which gzip output buffering began.
Definition: core.inc:83
static loadStyles()
Load the list of CSS files from the manifests of all registered, active components.
Definition: core.inc:625
static loadScripts()
Load the list of JS scripts from the manifests of all registered, active components.
Definition: core.inc:581
static using()
Import the datamodels, views and manifest for the specified component(s).
Definition: core.inc:116
registerMinifier($minifier)
static preProcessPage($template)
static postAction($action)
static preAction($action)
static getScriptsContent()
static preProcessStyles($styles)
static preprocessScripts($scripts)
static getValue($component, $name)
Retrieve the value of the specified Setting.
Definition: settings.inc:104
static setDefaultValue($component, $name, $value, $field_type="String", $annotation="", $category="", $options="", $weight=0)
Sets the default value of the given component setting.
Definition: settings.inc:174
minifyCSS($css)
minifyJavascript($js)
$styles
$action
Definition: run.php:41
while(ob_get_level()) $last_modified
Definition: scripts.inc:7
if(array_key_exists("HTTP_IF_MODIFIED_SINCE", $_SERVER)) $content
Definition: styles.css.inc:24