source: t29-www/lib/js.php @ 262

Last change on this file since 262 was 262, checked in by sven, 12 years ago

Added svn:keywords = Id Property to de-v6, en-v6, lib. and ignore all files in shared/cache.

  • Property svn:keywords set to Id
File size: 2.4 KB
Line 
1<?php
2header("Content-Type: application/javascript");
3
4$lib = dirname(__FILE__);
5$webroot = realpath("$lib/../");  # file path to root of t29 web installation
6$cache_file = "$webroot/shared/cache/compressed.js";
7$module_dir = "$webroot/shared/js-v6/modules";
8$modules = glob("$module_dir/*.js");
9
10$debug = isset($_GET['debug']); // skip cache and just concat everything
11$purge_cache = isset($_GET['purge_cache']); // rebuild cache file (compressed)
12
13if(!$debug && !$purge_cache) {
14        // check cache and all input files
15        $filem_cache = @filemtime($cache_file);
16        $filem_moddir = @filemtime($module_dir); // if new modules were added
17        $filem_modules = array_reduce(array_map(function($x){return @filemtime($x);}, $modules), 'max');
18        $cache_valid = $filem_cache && $filem_modules < $filem_cache && $filem_moddir < $filem_cache;
19
20        if($cache_valid) {
21                header("Last-Modified: ".gmdate("D, d M Y H:i:s", $filem_cache)." GMT");
22                if(@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $filem_cache)
23                        // client already has page cached locally
24                        header("HTTP/1.1 304 Not Modified");
25                else
26                        readfile($cache_file);
27                exit;
28        }
29}
30
31if(!$debug) ob_start();
32$filenames = array_map('basename', $modules); // filenames like foo.js
33?>
34/*!
35 * t29v6 JavaScript Code
36 * http://technikum29.de/
37 * $Id: js.php 262 2012-05-28 23:35:30Z sven $
38 *
39 * Copyright 2012, Sven Koeppel <sven@te...29.de>
40 * Licensed under any of Apache, MIT, GPL, LGPL
41 *
42 * Depends heavily on jQuery
43 * Packed: <?php echo implode(' ', $filenames); ?> 
44 * Arguments: ?debug=true - skip cache and just cat everything
45 *            ?purge_cache=true - force rebuild of compressed cache file
46 * Date: <?php echo date('r'); ?>
47 */
48<?php
49
50foreach($modules as $i => $mod) {
51        $modfile = $filenames[$i];
52        if($debug) echo "\n\n/*** t29v6-RessourceLoader: Start of $modfile ***/\n\n";
53        readfile($mod);
54        echo "\n\n"; // for being sure no former "//" comment in last line wipes out code
55       
56        if($modfile == "msg.js") {
57                // special treatment of this file
58                if($debug) echo "\n/*** Auto appended ***/\n";
59                require "$lib/messages.php";
60                echo "t29.msg.data=";
61                echo t29Messages::create_json();
62                echo ";\n";
63        }
64
65        if($debug) echo "\n\n/*** t29-v6-RessourceLoader: End of $modfile ***/\n\n";
66}
67
68if($debug) exit; // just here without compressing etc.
69
70$code = ob_get_flush();
71
72// reduces code size about 1/2 - before: 23kB, after: 12kB
73require "$lib/JavaScriptMinifier.php";
74$code = JavaScriptMinifier::minify($code);
75
76// write out file.
77file_put_contents($cache_file, $code);
78
79print $code;
Note: See TracBrowser for help on using the repository browser.
© 2008 - 2013 technikum29 • Sven Köppel • Some rights reserved
Powered by Trac
Expect where otherwise noted, content on this site is licensed under a Creative Commons 3.0 License