1 | <?php |
---|
2 | /** |
---|
3 | * t29 ressource loading system. |
---|
4 | * |
---|
5 | * Entry point for loading javascript and css style sheet files in a bundled and |
---|
6 | * compressed manner, including caching. This file only checks up the caches and |
---|
7 | * contains the main configuration. See ressourceloader.php for all constructing |
---|
8 | * code. |
---|
9 | * |
---|
10 | **/ |
---|
11 | |
---|
12 | if(!defined('T29_GRAB_LOADER_DEFS')) { |
---|
13 | $lib = dirname(__FILE__); |
---|
14 | $webroot = realpath("$lib/../"); # file path to root of t29 web installation |
---|
15 | |
---|
16 | if(!isset($_GET['type'])) { |
---|
17 | print "<html><pre>The t29v6 Ressource loader.\n"; |
---|
18 | print "Provide ?type=js or ?type=css.\n"; |
---|
19 | print '<a href="https://labs.technikum29.de/browser/technikum29%20Website/lib/loader.php">Read my sourcecode</a>'; |
---|
20 | exit; |
---|
21 | } |
---|
22 | } |
---|
23 | |
---|
24 | $types = array('js', 'css'); // mapping position (numeric key) => $conf array position |
---|
25 | $conf = array( |
---|
26 | 'cache_file' => array('compressed.js', 'style.css'), |
---|
27 | 'module_dir' => array("$webroot/shared/js-v6/modules", "$webroot/shared/css-v6/modules"), |
---|
28 | 'page_dir' => array("$webroot/shared/js-v6/pagescripts", "$webroot/shared/css-v6/pagestyles"), |
---|
29 | 'glob_pattern' => array('*.js', '*.css'), |
---|
30 | 'content_types' => array('application/javascript', 'text/css'), |
---|
31 | 'class' => array('t29JavaScriptRessourceLoader', 't29StyleSheetRessourceLoader'), |
---|
32 | 'modules' => function($conf){ return glob($conf['module_dir'] . '/' . $conf['glob_pattern']); }, |
---|
33 | ); |
---|
34 | $conf_for_type = function($type, $debug_flag=false) use ($conf, $types) { |
---|
35 | $typepos = array_search($type, $types); |
---|
36 | if($typepos === FALSE) return null; |
---|
37 | array_walk($conf, function(&$val, $key) use($typepos) { if(is_array($val)) $val = $val[$typepos]; }); |
---|
38 | $conf['type'] = $type; |
---|
39 | $conf['modules'] = call_user_func($conf['modules'], $conf); |
---|
40 | $conf['debug'] = $debug_flag; // skip cache and just concat everything |
---|
41 | return $conf; |
---|
42 | }; |
---|
43 | |
---|
44 | if(defined('T29_GRAB_LOADER_DEFS')) { |
---|
45 | return; // just grab the vars in the local scope |
---|
46 | } |
---|
47 | |
---|
48 | $type = $_GET['type']; |
---|
49 | $conf = $conf_for_type($type, isset($_GET['debug'])); |
---|
50 | if($conf == null) |
---|
51 | die("Illegal type. Valid types are: ". implode($types)); |
---|
52 | extract($conf); // for saving long human reading times :D |
---|
53 | |
---|
54 | require "$lib/cache.php"; |
---|
55 | $js_cache = new t29Cache(); |
---|
56 | $js_cache->test_files = $modules; |
---|
57 | $js_cache->set_cache_file($webroot, $cache_file); |
---|
58 | |
---|
59 | header("Content-Type: $content_types"); |
---|
60 | if(!$debug) $js_cache->try_cache_and_exit(); |
---|
61 | |
---|
62 | # so we are still in the game |
---|
63 | require "$lib/ressourceloader.php"; |
---|
64 | $loader = new $class($conf); |
---|
65 | // TODO: there is a known bug with the filter function issue: Since t29Host variable web prefixes the |
---|
66 | // filter function for CSS also does path rewriting which doesn't work when it is in debug mode. |
---|
67 | // Anyway a page in debug mode also imports all untouched CSS files, anyway |
---|
68 | if(!$debug) |
---|
69 | $js_cache->start_cache(array( |
---|
70 | 'shutdown_func' => null, // nothing to do afterwards for the cache |
---|
71 | 'filter_func' => array($loader, 'compression_filter'), // compress if not debugging |
---|
72 | 'write_cache' => true, // write the cache if not debugging |
---|
73 | )); |
---|
74 | $loader->run(); |
---|