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

Last change on this file since 264 was 264, checked in by sven, 7 years ago

Saubere Caching-Klasse eingefuehrt, um Code-Redundanzen in technikum29.php und js.php zu vermeiden und mehr/einheitliche Caching-Funktionalitaet bereitzustellen. Das Caching kann jetzt debuggt werden und so. Und ist prima dokumentiert. Ausserdem kann der Caching-Ordner jetzt einfach gewechselt werden.

  • Property svn:keywords set to Id
File size: 1.9 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 = '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
12if(!$debug) {
13        require "$lib/cache.php";
14        $js_cache = new t29Cache();
15        $js_cache->test_files = $modules;
16        $js_cache->set_cache_file($webroot, $cache_file);
17        $js_cache->try_cache_and_exit();
18        $js_cache->start_cache('minify_javascript', true);
19}
20
21$filenames = array_map('basename', $modules); // filenames like foo.js
22?>
23/*!
24 * t29v6 JavaScript Code
25 * http://technikum29.de/
26 * $Id: js.php 264 2012-05-30 23:47:32Z sven $
27 *
28 * Copyright 2012, Sven Koeppel <sven@te...29.de>
29 * Licensed under any of Apache, MIT, GPL, LGPL
30 *
31 * Depends heavily on jQuery
32 * Packed: <?php echo implode(' ', $filenames); ?> 
33 * Arguments: ?debug=true - skip cache and just cat everything
34 *            ?purge_cache=true - force rebuild of compressed cache file
35 * Date: <?php echo date('r'); ?>
36 */
37<?php
38
39$header = ob_get_contents(); // for prepending it to minified code
40
41foreach($modules as $i => $mod) {
42        $modfile = $filenames[$i];
43        if($debug) echo "\n\n/*** t29v6-RessourceLoader: Start of $modfile ***/\n\n";
44        readfile($mod);
45        echo "\n\n"; // for being sure no former "//" comment in last line wipes out code
46       
47        if($modfile == "msg.js") {
48                // special treatment of this file
49                if($debug) echo "\n/*** Auto appended ***/\n";
50                require "$lib/messages.php";
51                echo "t29.msg.data=";
52                echo t29Messages::create_json();
53                echo ";\n";
54        }
55
56        if($debug) echo "\n\n/*** t29-v6-RessourceLoader: End of $modfile ***/\n\n";
57}
58
59
60function minify_javascript($code) {
61        global $lib, $header;
62        // reduces code size about 1/2 - before: 23kB, after: 12kB
63        require "$lib/JavaScriptMinifier.php";
64        $minified = JavaScriptMinifier::minify($code);
65        return $header . $minified;
66}
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