source: t29-www/lib/ressourceloader.php @ 269

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

CSS modular sauber verteilt und JS-Loading-Framework generalisiert zu einem OOP RessourceLoader, der sowohl JS als auch CSS vom Verzeichnis einlesen kann, Cachefile erzeugt und mit Hooks sowie Postprozessor verarbeiten kann (etwa fuer Minify, LESS, usw.). Noch nicht ausgiebig getestet.

  • Property svn:keywords set to Id
File size: 3.2 KB
RevLine 
[260]1<?php
[269]2/**
3 * t29REssourceLoader classes.
4 * This file behaves to loader.php like template.php behaves to technikum29.php.
5 *
6 * classes t29JavaScriptRessourceLoader and t29StyleSheetRessourceLoader are also defined in
7 * this class.
8 *
9 * For minification the files JavaScriptMinifier.php and CSSMin.php are included on need.
10 **/
[260]11
[269]12class t29RessourceLoader {
13        public $conf;
14       
15        /// @param $conf configuration array.
16        function __construct($conf) {
17                $this->conf = $conf;
18                $this->conf['filenames'] = array_map('basename', $this->conf['modules']); // filenames like foo.js
19        }
20       
21        function print_debug($string) {
22                if($this->conf['debug'])
23                        echo $string;
24        }
25       
26        function print_before_file($mod_filename, $dir_index) {
27                $this->print_debug("\n\n/*** t29v6-RessourceLoader[$dir_index]: Start of $mod_filename ***/\n\n");
28        }
29       
30        function print_after_file($mod_filename, $dir_index) {
31                echo "\n\n"; // JS: for being sure no former "//" comment in last line wipes out code
32                $this->print_debug("\n\n/*** t29v6-RessourceLoader[$dir_index]: End of $mod_filename ***/\n\n");
33        }
34       
35        function print_header($title=null) {
36                if(!$title) $title = __CLASS__;
37                ?>
[260]38/*!
[269]39 * t29v6 <?=$title; ?> - http://technikum29.de/
[260]40 * $Id: ressourceloader.php 269 2012-06-01 17:19:51Z sven $
41 *
42 * Copyright 2012, Sven Koeppel <sven@te...29.de>
43 * Licensed under any of Apache, MIT, GPL, LGPL
44 *
[269]45 * Packed: <?php echo implode(' ', $this->conf['filenames']); ?> 
[260]46 * Arguments: ?debug=true - skip cache and just cat everything
47 *            ?purge_cache=true - force rebuild of compressed cache file
[269]48 * Gen Date: <?php echo date('r'), PHP_EOL;
49                if($title == __CLASS__) print " **/\n";
50        }
51       
52        function run() {
53                $this->print_header();
54                $this->conf['header'] = ob_get_contents(); // for prepending it to minified code
[260]55
[269]56                foreach($this->conf['modules'] as $i => $mod) {
57                        $modfile = $this->conf['filenames'][$i];
58                        $this->print_before_file($modfile, $i);
59                        readfile($mod);
60                        $this->print_after_file($modfile, $i);
61                }
62        } // run
[264]63
[269]64        function compression_filter($output) {
65                return $output;
66        }
67} // class t29RessourceLoader
68
69class t29JavaScriptRessourceLoader extends t29RessourceLoader {
70        function print_after_file($mod_filename, $dir_index) {
71                global $lib;
72                parent::print_after_file($mod_filename, $dir_index);
73                if($mod_filename == "msg.js") {
74                        // special treatment of this file
75                        $this->print_debug("\n/*** Auto appended ***/\n");
76                        require "$lib/messages.php";
77                        echo "t29.msg.data=";
78                        echo t29Messages::create_json();
79                        echo ";\n";
80                }
81        }
82
83        function print_header() {
84                parent::print_header('JavaScript Code');
85                echo " * Depends heavily on jQuery\n **/\n";
86        }
[260]87       
[269]88        function compression_filter($code) {
89                global $lib;
90                // reduces code size about 1/2 - before: 23kB, after: 12kB
91                require "$lib/JavaScriptMinifier.php";
92                $minified = JavaScriptMinifier::minify($code);
93                return $this->conf['header'] . $minified;
[260]94        }
[269]95} // class t29JavaScriptRessourceLoader
[260]96
[269]97class t29StyleSheetRessourceLoader extends t29RessourceLoader {
98        function print_header() {
99                parent::print_header('StyleSheet Code');
100                echo " **/\n";
101        }
[260]102
[269]103        function compression_filter($code) {
104                global $lib;
105                require "$lib/CSSMin.php";
106                return "foo";
107                $minified = CSSMin::minify($code);
108                return $this->conf['header'] . $minified;
109               
110        }
111} // t29StyleSheetRessourceLoader
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