Changeset 273 in t29-www for lib


Ignore:
Timestamp:
Jun 2, 2012, 8:15:28 PM (11 years ago)
Author:
sven
Message:

Backend modifications:

  • ressourceloader.php can generate URLs for its own system and can instanciate an RL instance by grabbing the config from loader.php
  • template.php requests RessourceLoader for URLs (so debugging URLs can directly be injected in the template)
  • template.php and cache.php can handle header/footer seperatedly in two cache files for dynamical page content
  • suche.php and search.php as the search framework basis; they can already print a XML opensarch description.
Location:
lib
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • lib/cache.php

    r270 r273  
    115115         * header! Therefore consider using the convenience method print_cache_and_exit()
    116116         * instead of this one or exit on yourself.
    117          **/
    118         function print_cache() {
     117         *
     118         * @param $ignore_http_caching Don't check the clients HTTP cache
     119         **/
     120        function print_cache($ignore_http_caching=false) {
    119121                // make sure we already have called is_valid
    120122                if($this->mtime_cache_file === null)
     
    126128                }
    127129
    128                 if(@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $this->mtime_cache_file) {
     130                if(!$ignore_http_caching && @strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $this->mtime_cache_file) {
    129131                        // client already has page cached locally
    130132                        if($this->debug) {
     
    234236         * will be assumed (as start_cache fires it) and content will be
    235237         * extracted with ob_get_flush.
    236          **/
    237         function write_cache($content=null) {
     238         * @param $content Content to be used as cache content or OB content
     239         * @param $clear_ob_cache Use ob_get_clean instead of flushing it. If given,
     240         *                        will return $content instead of printing/keeping it.
     241         **/
     242        function write_cache($content=null, $clear_ob_cache=false) {
    238243                if(!$content)
    239                         $content = ob_get_flush();
     244                        $content = ($clear_ob_cache ? ob_get_clean() : ob_get_flush());
    240245
    241246                if($this->skip) {
     
    253258                else
    254259                        $this->print_error('Could not write page output cache to '.$this->cache_file);
     260
     261                if($clear_ob_cache)
     262                        return $content;
    255263        }
    256264       
  • lib/loader.php

    r271 r273  
    99 *
    1010 **/
     11 
     12if(!defined('T29_GRAB_LOADER_DEFS')) {
     13        $lib = dirname(__FILE__);
     14        $webroot = realpath("$lib/../");  # file path to root of t29 web installation
    1115
    12 $lib = dirname(__FILE__);
    13 $webroot = realpath("$lib/../");  # file path to root of t29 web installation
    14 
    15 if(!isset($_GET['type'])) {
    16         die("Read manual.");
     16        if(!isset($_GET['type'])) {
     17                die("Provide ?type=js or ?type=css.");
     18        }
    1719}
    1820
     
    2426        'content_types' => array('application/javascript', 'text/css'),
    2527        'class' => array('t29JavaScriptRessourceLoader', 't29StyleSheetRessourceLoader'),
     28        'modules' => function($conf){ return glob($conf['module_dir'] . '/' . $conf['glob_pattern']); },
    2629);
     30$conf_for_type = function($type, $debug_flag=false) use ($conf, $types) {
     31        $typepos = array_search($type, $types);
     32        if($typepos === FALSE) return null;
     33        array_walk($conf, function(&$val, $key) use($typepos) { if(is_array($val)) $val = $val[$typepos]; });
     34        $conf['type'] = $type;
     35        $conf['modules'] = call_user_func($conf['modules'], $conf);
     36        $conf['debug'] = $debug_flag; // skip cache and just concat everything
     37        return $conf;
     38};
     39
     40if(defined('T29_GRAB_LOADER_DEFS')) {
     41        return; // just grab the vars in the local scope
     42}
    2743
    2844$type = $_GET['type'];
    29 $typepos = array_search($type, $types);
    30 if($typepos === FALSE) {
     45$conf = $conf_for_type($type, isset($_GET['debug']));
     46if($conf == null)
    3147        die("Illegal type. Valid types are: ". implode($types));
    32 }
     48extract($conf); // for saving long human reading times :D
    3349
    34 array_walk($conf, function(&$val, $key) use($typepos) { $val = $val[$typepos]; });
    35 $conf['modules'] = glob($conf['module_dir'] . '/' . $conf['glob_pattern']);
    36 $conf['debug'] = isset($_GET['debug']); // skip cache and just concat everything
    37 extract($conf); // for saving long human reading times :D
    38 // alternative approach for direct extract in global namespace
    39 // (no more applicable because configuration is given as array to constructor):
    40 // foreach($conf as $var => $val) { $GLOBALS[$var] = $val[$typepos]; }
    4150
    4251require "$lib/cache.php";
  • lib/messages.php

    r268 r273  
    9696                'topnav-interlang-title' => array('Read this page (%s) in English', 'Diese Seite (%s) auf Deutsch lesen'),
    9797                'topnav-search-label'    => array('Suchen', 'Search'),
    98                 'topnav-search-page'     => array('/de-v6/suche.php', '/en-v6/search.php'),
     98                'topnav-search-page'     => array('/suche.php', '/search.php'),
     99                'opensearch-desc'        => array('technikum29 (de)', 'technikum29 (en)'),
    99100
    100101                'js-menu-collapse-out'   => array('Menü ausklappen', 'Expand menu'),
  • lib/ressourceloader.php

    r271 r273  
    2121 *
    2222 **/
     23 
     24/*
     25// test it:
     26$lib = dirname(__FILE__);
     27$webroot = realpath("$lib/../");
     28$js = t29RessourceLoader::create_from_type('css');
     29$js->run();
     30*/
    2331
    2432class t29RessourceLoader {
    2533        /**
    26          * expects: cache_file, module_dir, glob_pattern, content_types, class, modules, debug
     34         * expects: type, cache_file, module_dir, glob_pattern, content_types, class, modules, debug
    2735         **/
    2836        public $conf;
     37       
     38        const default_include_url = '/lib/loader.php'; // rel to webroot
    2939       
    3040        /**
    3141         * Construct with configuration array. See loader.php for contents of
    3242         * that array. See above for minimum elements which must be present.
    33          **/
    34         /// @param $conf configuration array.
     43         * @param $conf Configuration array
     44         **/
    3545        function __construct($conf) {
    3646                $this->conf = $conf;
    3747                $this->conf['filenames'] = array_map('basename', $this->conf['modules']); // filenames like foo.js
     48        }
     49       
     50        private static $conf_for_type;
     51        static function create_from_type($type, $baseconf=null) {
     52                global $lib, $webroot;
     53                if(!self::$conf_for_type) {
     54                        define('T29_GRAB_LOADER_DEFS', true);
     55                        include "$lib/loader.php";
     56                        self::$conf_for_type = $conf_for_type;
     57                }
     58
     59                $conf = call_user_func(self::$conf_for_type, $type, isset($baseconf['debug']) && $baseconf['debug']);
     60                if($conf === null) return null;
     61               
     62                return new $conf['class']($conf);
     63        }
     64       
     65        function get_urls($debug=null) {
     66                global $webroot;
     67                if(($debug !== null && $debug) || !$this->conf['debug']) {
     68                        return array(self::default_include_url . '?type=' . $this->conf['type']);
     69                } else {
     70                        $module_dir_rel2webroot = str_replace($webroot, '', $this->conf['module_dir']);
     71                        return array_map(function($i)use($module_dir_rel2webroot){ return $module_dir_rel2webroot.$i; }, $this->conf['filenames']);
     72                }
    3873        }
    3974       
  • lib/technikum29.php

    r264 r273  
    3030$page_cache = new t29Cache(false, true); // debug, verbose
    3131$page_cache->set_cache_file($webroot, $file);
    32 $page_cache->test_files = array(
     32$page_cache->test_files =  array(
    3333        __FILE__,
    3434        $_SERVER['SCRIPT_FILENAME'],
     
    4040);
    4141
    42 $page_cache->try_cache_and_exit();
     42// dynamical content:
     43$static_page = !isset($dynamischer_inhalt);
    4344
    44 // cache missed, rebuild cache
    45 require "$lib/template.php";
    46 $tmpl = new t29Template($GLOBALS);
    47 $tmpl->create_cache($page_cache);
     45if(!$static_page) {
     46        // Pages with dynamical content: only cache header and footer, seperately.
     47        // they depend on same test files, so there is only one is_valid check.
     48        $header_cache = $page_cache;
     49        $footer_cache = clone $page_cache;
     50
     51        $header_cache->set_cache_file($webroot, $file.'-header');
     52        $footer_cache->set_cache_file($webroot, $file.'-footer');
     53}
     54
     55if($page_cache->shall_use()) {
     56        if($static_page)
     57                $page_cache->print_cache_and_exit();
     58        else {
     59                $header_cache->print_cache(true);
     60                register_shutdown_function(function() use ($footer_cache) {
     61                        $footer_cache->print_cache(true);
     62                });
     63                // now print your dynamical stuff in your page, the
     64                // footer content will be automatically added afterwards.
     65        }
     66} else {
     67        // cache missed, rebuild cache
     68        require "$lib/template.php";
     69        $tmpl = new t29Template($GLOBALS);
     70        if($static_page)
     71                // rebuild complete site cache
     72                $tmpl->create_cache($page_cache);
     73        else
     74                // rebuild each header and footer cache
     75                $tmpl->create_separate_caches($header_cache, $footer_cache);
     76}
     77
    4878
    4979// end of technikum29.php
  • lib/template.php

    r271 r273  
    1010 *  $header_cache_file, $footer_cache_file.
    1111 **/
     12
     13require dirname(__FILE__) . "/ressourceloader.php";
    1214 
    1315class t29Template {
     
    2931
    3032                // fill up configuration
    31                 $this->conf['legal_pagename'] = $this->conf['lang_path'] . $this->msg->_('footer-legal-file');
     33                // Path names in messages
     34                foreach(array('footer-legal-file', 'topnav-search-page') as $msg)
     35                        $this->conf[$msg] = $this->conf['lang_path'] . $this->msg->_($msg);
    3236
    3337                // setup body classes:
     
    6064                $cache_object->start_cache(array($this, 'print_footer'));
    6165                $this->print_header();
     66        }
     67       
     68        /**
     69         * Write header and footer in separate cache files.
     70         **/
     71        function create_separate_caches($header_cache, $footer_cache) {
     72                $header_cache->start_cache();
     73                $this->print_header();
     74                $header_cache->write_cache(); // will also print out header immediately.
     75               
     76                $footer_cache->start_cache();
     77                $this->print_footer();
     78                $footer_content = $footer_cache->write_cache(null, true); // don't print footer immediately.
     79               
     80                // print footer on exit.
     81                register_shutdown_function(function() use ($footer_content) {
     82                        print $footer_content;
     83                });
    6284        }
    6385
     
    89111  ?>
    90112 
    91   <link rel="copyright" href="<?php echo $this->conf['legal_pagename']; ?>" title="<?php $p('footer-legal-link'); ?>">
     113  <link rel="copyright" href="<?php echo $this->conf['footer-legal-file']; ?>" title="<?php $p('footer-legal-link'); ?>">
     114  <link rel="search" type="application/opensearchdescription+xml" href="<?php print $this->conf['topnav-search-page'] . '?action=opensearch-desc'; ?>" title="<?php $p('opensearch-desc'); ?>">
    92115  <?php
    93116        // print interlanguage links for all languages except the active one
     
    102125 
    103126  <meta name="viewport" content="width=device-width,initial-scale=1">
    104   <link rel="stylesheet" href="/lib/loader.php?type=css">
    105   <?php
     127  <?php
     128        $csslinktmpl = '  <link rel="stylesheet" href="%s">'.PHP_EOL;
     129        foreach($this->get_ressourceloader_links('css') as $css)
     130                printf($csslinktmpl, $css);
     131 
    106132        if($this->conf['has_pagecss'])
    107                 printf('<link rel="stylesheet" href="%s">', $this->conf['pagecss']);
     133                printf($csslinktmpl, $this->conf['pagecss']);
    108134  ?>
    109135
     
    162188                                ?>
    163189                        </ul>
    164                         <form method="get" action="<?php $p('topnav-search-page'); ?>">
     190                        <form method="get" action="<?php print $this->conf['topnav-search-page']; ?>">
    165191                                <span class="no-js"><?php $p('topnav-search-label'); ?>:</span>
    166192                                <input type="text" value="" data-defaultvalue="<?php $p('topnav-search-label'); ?>" name="q" class="text">
     
    189215                        <img src="/shared/img-v6/logo.footer.png" title="technikum29 Logo" alt="Logo" class="logo">
    190216                        <?php $p('footer-copyright-tag'); ?>
    191                         <br/><?php printf('<a href="%s">%s</a>', $this->conf['legal_pagename'], $_('footer-legal-link')); ?>
     217                        <br/><?php printf('<a href="%s">%s</a>', $this->conf['footer-legal-file'], $_('footer-legal-link')); ?>
    192218                        <div class="icons">
    193                                 <a href="<?php echo $this->conf['legal_pagename']; ?>#image-copyright"><img src="/shared/img-v6/cc-icon.png"></a>
     219                                <a href="<?php echo $this->conf['footer-legal-file']; ?>#image-copyright"><img src="/shared/img-v6/cc-icon.png"></a>
    194220                                <!--<a href="http://ufopixel.de" title="Designed by Ufopixel"><img src="http://svenk.homeip.net/dropbox/Ufopixel/Ufopixel-Design/logo_90x30/ufopixel_logo_90x30_version2.png"></a>-->
    195221                        </div>
     
    208234
    209235  <script>window.t29={'conf': <?php print json_encode($this->javascript_config); ?>};</script>
    210   <script src="/lib/loader.php?type=js"></script>
     236  <?php
     237        foreach($this->get_ressourceloader_links('js') as $js)
     238                printf('  <script src="%s"></script>'.PHP_EOL, $js);
     239  ?>
    211240</div><!-- end of div id="footer-background-container" helper -->
    212241</body>
     
    221250                return isset($a['title']) ? $a['title'] : $a;
    222251        }
    223        
     252
     253        function get_ressourceloader_links($type) {
     254                $rl = t29RessourceLoader::create_from_type($type, $this->conf);
     255                return $rl->get_urls();
     256        }
     257
    224258} // class t29Template
Note: See TracChangeset for help on using the changeset viewer.
© 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