Show
Ignore:
Timestamp:
02/27/2008 08:03:49 AM (11 months ago)
Author:
gregmac
Message:

Refactor code for CSS/JS files loading; Add support for generic media files (fix #392); Add 'separator' config variable;

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/modules/media/controllers/media.php

    r2171 r2176  
    1111 * 
    1212 * @package        Media Module 
    13  * @author         Kohana Team 
     13 * @author         Greg MacLellan 
    1414 * @copyright  (c) 2007-2008 Kohana Team 
    1515 * @license        http://kohanaphp.com/license.html 
    1616 */ 
    1717class Media_Controller extends Controller { 
    18  
     18        protected $separator = FALSE; 
     19         
    1920        protected $use_cache = FALSE; 
    2021        protected $cache_lifetime; 
     
    2223        protected $pack_css = FALSE; 
    2324        protected $pack_js = FALSE; 
    24  
     25         
    2526        public function __construct() 
    2627        { 
    2728                parent::__construct(); 
    28  
    29                 $cache           = Config::item('media.cache'); 
     29                 
     30                $this->separator = config::item('media.separator') OR $this->separator = ','; 
     31                 
     32                $cache = Config::item('media.cache'); 
    3033                $this->use_cache = ($cache > 0); 
    3134 
     
    3639                else 
    3740                { 
    38                         Config::item('cache.lifetime') OR $this->cache_lifetime = 1800; 
     41                        $this->cache_lifetime = Config::item('cache.lifetime') OR $this->cache_lifetime = 1800; 
    3942                } 
    4043 
     
    4548 
    4649                $this->pack_css = (bool) Config::item('media.pack_css'); 
    47                 $this->pack_js  = Config::item('media.pack_js'); 
    48  
    49                 if ($this->pack_js === TRUE) 
    50                 { 
    51                         $this->pack_js = 'Normal'; 
    52                 } 
    53         } 
    54  
    55         public function css($querystr)  
    56         { 
    57                 // Find all the individual files 
    58                 $files = explode('+', $querystr); 
    59  
     50                $this->pack_js = Config::item('media.pack_js'); 
     51 
     52                ($this->pack_js === TRUE) AND $this->pack_js = 'Normal'; 
     53        } 
     54         
     55        public function css($querystr) { 
     56                // find all the individual files 
     57                $files = explode($this->separator, $querystr); 
     58                         
    6059                $mimetype = config::item('mimes.css'); 
    61                 $mimetype = isset($mimetype[0]) ? $mimetype[0] : 'text/stylesheet'; 
    62  
    63                 $this->use_cache AND $data = $this->cache->get('media.css.'.$querystr); 
    64  
    65                 if ( ! isset($data) OR empty($data)) 
    66                 { 
    67                         $data = ''; 
    68                         foreach ($files as $orig_filename)  
    69                         { 
    70                                 $filename = $orig_filename; 
    71  
    72                                 if (substr($filename, -4) == ".css") 
     60                $mimetype = (isset($mimetype[0])) ? $mimetype[0] : 'text/stylesheet'; 
     61 
     62                $this->use_cache AND $output = $this->cache->get('media.css.'.$querystr); 
     63                 
     64                if ( ! isset($output) OR empty($output)) 
     65                { 
     66                        $output = ''; 
     67                        $filedata = $this->_load_filedata($files, 'css'); 
     68                        foreach ($filedata as $filename=>$data)  
     69                        { 
     70                                if ($this->pack_css) 
    7371                                { 
    74                                         $filename = substr($filename, 0, -4); 
     72                                        $data = $this->_css_compress($data); 
    7573                                } 
    76  
     74                                $output .= $data; 
     75                        } 
     76                         
     77                        ($this->use_cache) and $this->cache->set('media.css.'.$querystr, $data, array('media'), $this->cache_lifetime); 
     78                } 
     79                 
     80                $mimetype AND header('Content-type: '.$mimetype); 
     81                echo $output; 
     82        } 
     83         
     84        public function js($querystr) { 
     85                // find all the individual files 
     86                $files = explode($this->separator, $querystr); 
     87 
     88                $mimetype = config::item('mimes.js'); 
     89                $mimetype = (isset($mimetype[0])) ? $mimetype[0] : 'text/javascript'; 
     90 
     91                $this->use_cache AND $output = $this->cache->get('media.js.'.$querystr); 
     92 
     93                if ( ! isset($output) OR empty($output)) 
     94                { 
     95                        $output = ''; 
     96                        $filedata = $this->_load_filedata($files, 'js'); 
     97                        foreach ($filedata as $filename=>$data)  
     98                        { 
     99                                $output .= $data; 
     100                        } 
     101                         
     102                        if ($this->pack_js) 
     103                        { 
     104                                $output = $this->_js_compress($output); 
     105                        } 
     106                         
     107                        ($this->use_cache) and $this->cache->set('media.js.'.$querystr, $data, array('media'), $this->cache_lifetime); 
     108                } 
     109                 
     110                $mimetype AND header('Content-type: '.$mimetype); 
     111                echo $output; 
     112        } 
     113         
     114 
     115        public function _default() 
     116        { 
     117                $segments = $this->uri->argument_array(); 
     118                 
     119                $filename = array_pop($segments); 
     120                $type = implode('/',$segments); 
     121                 
     122                if (($pos = strrpos($filename, '.')) !== false)  
     123                { 
     124                        $extension = substr($filename, $pos+1); 
     125                        $filename = substr($filename, 0, $pos); 
     126                }  
     127                else 
     128                { 
     129                        $extension = ''; 
     130                } 
     131                 
     132                try 
     133                { 
     134                        $view = new View('media/'.$type.'/'.$filename, null, $extension); 
     135                        echo $view->render(); 
     136                } 
     137                catch (Kohana_Exception $exception) 
     138                { 
     139                        Event::run('system.404'); 
     140                } 
     141        } 
     142         
     143        public function _load_filedata($files, $resource_type)  
     144        { 
     145                $filedata = array(); 
     146                foreach ($files as $orig_filename)  
     147                { 
     148                        $filename = $orig_filename; 
     149                        if (substr($filename, -1 * strlen($resource_type) - 1) == '.'.$resource_type) 
     150                        { 
     151                                $filename = substr($filename, 0, -1 * strlen($resource_type) - 1); 
     152                        } 
     153                         
     154                        try 
     155                        { 
     156                                $view = new View('media/'.$resource_type.'/'.$filename, null, $resource_type); 
     157                        } 
     158                        catch (Kohana_Exception $exception) 
     159                        { 
     160                                // try to load the file as a php view (eg, file.css.php) 
    77161                                try 
    78162                                { 
    79                                         $view = new View('media/css/'.$filename, null, 'css'); 
     163                                        $view = new View('media/'.$resource_type.'/'.$orig_filename); 
    80164                                } 
    81165                                catch (Kohana_Exception $exception) 
    82166                                { 
    83                                         // Try to load the file as a php view (eg, file.css.php) 
    84                                         try 
    85                                         { 
    86                                                 $view = new View('media/css/'.$orig_filename); 
    87                                         } 
    88                                         catch (Kohana_Exception $exception) 
    89                                         { 
    90                                                 // Not found 
    91                                                 unset($view); 
    92                                         } 
    93                                 } 
    94  
    95                                 if (isset($view)) { 
    96                                         $filedata = $view->render(); 
    97  
    98                                         ($this->pack_css) AND $filedata = $this->_css_compress($filedata); 
    99  
    100                                         $data .= $filedata; 
    101                                 } 
    102                                 else 
    103                                 { 
    104                                         $data .= "\n/**** stylesheet ".$filename." not found ****/\n\n\n"; 
    105                                 } 
    106                         } 
    107  
    108                         ($this->use_cache) AND $this->cache->set('media.css.'.$querystr, $data, array('media'), $this->cache_lifetime); 
    109                 } 
    110  
    111                 $mimetype AND header('Content-type: '.$mimetype); 
    112                 echo $data; 
    113         } 
    114  
    115         public function js($orig_filename)  
    116         { 
    117                 $filename = $orig_filename; 
    118  
    119                 if (substr($filename, -3) == '.js') 
    120                 { 
    121                         $filename = substr($filename, 0, -3); 
    122                 } 
    123  
    124                 $mimetype = Config::item('mimes.js'); 
    125                 $mimetype = isset($mimetype[0]) ? $mimetype[0] : 'text/javascript'; 
    126  
    127                 $this->use_cache AND $data = $this->cache->get('media.js.'.$filename); 
    128  
    129                 if ( ! isset($data) OR empty($data)) 
    130                 { 
    131                         try 
    132                         { 
    133                                 $view = new View('media/js/'.$filename, NULL, 'js'); 
    134                         } 
    135                         catch (Kohana_Exception $exception) 
    136                         { 
    137                                 // Try to load the file as a php view (eg, file.js.php) 
    138                                 try 
    139                                 { 
    140                                         $view = new View('media/js/'.$orig_filename); 
    141                                 } 
    142                                 catch (Kohana_Exception $exception) 
    143                                 { 
    144                                         // Not found 
     167                                        // not found 
    145168                                        unset($view); 
    146169                                } 
    147170                        } 
    148  
    149                         if (isset($view)) 
    150                         { 
    151                                 $data = $view->render(); 
    152  
    153                                 if ($this->pack_js) 
    154                                 { 
    155                                         $packer = new JavaScriptPacker($data, $this->pack_js); 
    156                                         $data = $packer->pack(); 
    157                                 } 
    158  
    159                                 ($this->use_cache) AND $this->cache->set('media.js.'.$filename, $data, array('media'), $this->cache_lifetime); 
    160                         } 
    161                         else 
    162                         { 
    163                                 $data = '/* script not found */'; 
    164                         } 
    165                 } 
    166  
    167                 $mimetype AND header('Content-type: '.$mimetype); 
    168                 echo $data; 
    169         } 
    170  
    171         public function _default() 
    172         { 
    173                 $type = $this->uri->segment(2); 
    174                 $filename = $this->uri->segment(3); 
    175                 // TODO:   Finish this for generic types 
    176                 // ISSUES: Getting View to work with any types of files 
    177  
    178                 try 
    179                 { 
    180                         $view = new View('media/'.$type.'/'.$filename); 
    181                 } 
    182                 catch (Kohana_Exception $exception) 
    183                 { 
    184                         Event::run('system.404'); 
    185                 } 
    186         } 
    187  
    188         /** 
    189          * @based_on   http://www.ibloomstudios.com/articles/php_css_compressor/ 
    190          */ 
     171                         
     172                        (isset($view)) and $filedata[$filename] = $view->render(); 
     173                } 
     174                return $filedata; 
     175        } 
     176         
     177        // Based on http://www.ibloomstudios.com/articles/php_css_compressor/ 
    191178        public function _css_compress($data) 
    192179        { 
     
    208195                return $data; 
    209196        } 
    210  
     197         
     198        public function _js_compress($data) { 
     199                $packer = new JavaScriptPacker($data, $this->pack_js); 
     200                return $packer->pack(); 
     201        } 
    211202} // End Media_Controller