Changeset 1763

Show
Ignore:
Timestamp:
01/21/2008 05:19:34 AM (12 months ago)
Author:
Geert
Message:
  • Fixed and improved CSS compressing function.
    • Fix: the old _css_compress() function would break a rule like a { border: 1px solid red} because of the double space between 1px and solid.
    • Improvement: generating even smaller CSS files now because more whitespace is being stripped.
  • Cleaning up code style: http://trac.kohanaphp.com/wiki/CodingStyle
Files:
1 modified

Legend:

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

    r1633 r1763  
    1818 */ 
    1919class Media_Controller extends Controller { 
    20         protected $use_cache = false; 
     20 
     21        protected $use_cache = FALSE; 
    2122        protected $cache_lifetime; 
    2223         
    23         protected $pack_css = false; 
    24         protected $pack_js = false; 
     24        protected $pack_css = FALSE; 
     25        protected $pack_js = FALSE; 
    2526         
    26         public function __construct() { 
     27        public function __construct() 
     28        { 
    2729                parent::__construct(); 
    2830                 
    29                 $cache = config::item('media.cache'); 
     31                $cache = Config::item('media.cache'); 
    3032                $this->use_cache = ($cache > 0); 
    3133                 
    32                 if (is_int($cache)) { 
     34                if (is_int($cache)) 
     35                { 
    3336                        $this->cache_lifetime = $cache; 
    34                 } else { 
     37                } 
     38                else 
     39                { 
    3540                        $this->cache_lifetime = config::item('cache.lifetime') OR $this->cache_lifetime = 1800; 
    3641                } 
    3742                 
    38                 if ($this->use_cache AND !isset($this->cache))  
     43                if ($this->use_cache AND ! isset($this->cache))  
    3944                { 
    4045                        $this->load->library('cache'); 
    4146                } 
    4247                 
    43                 $this->pack_css = (bool)config::item('media.pack_css'); 
     48                $this->pack_css = (bool) Config::item('media.pack_css'); 
     49                $this->pack_js = Config::item('media.pack_js'); 
    4450                 
    45                 $this->pack_js = config::item('media.pack_js'); 
    46                 if ($this->pack_js === true) $this->pack_js = 'Normal'; 
     51                if ($this->pack_js === TRUE) 
     52                { 
     53                        $this->pack_js = 'Normal'; 
     54                } 
    4755        } 
    4856         
    49         public function css() { 
     57        public function css() 
     58        { 
    5059                $filename = $orig_filename = $this->uri->segment(3); 
    51                 if (substr($filename, -4) == ".css") { 
     60                if (substr($filename, -4) == '.css') 
     61                { 
    5262                        $filename = substr($filename, 0, -4); 
    5363                } 
    5464                 
    5565                $mimetype = config::item('mimes.css'); 
    56                 $mimetype = (isset($mimetype[0]) ? $mimetype[0] : 'text/stylesheet'); 
    57                                          
     66                $mimetype = (isset($mimetype[0])) ? $mimetype[0] : 'text/stylesheet'; 
    5867                 
    5968                $this->use_cache AND $data = $this->cache->get('media.css.'.$filename); 
    6069                 
    61                 if (!isset($data) OR empty($data)) 
     70                if ( ! isset($data) OR empty($data)) 
    6271                { 
    6372                        try 
    6473                        { 
    65                                 $view = new View('media/css/'.$filename, null, 'css'); 
     74                                $view = new View('media/css/'.$filename, NULL, 'css'); 
    6675                        } 
    6776                        catch (Kohana_Exception $exception) 
    6877                        { 
    69                                 // try to load the file as a php view (eg, file.css.php)  
     78                                // Try to load the file as a php view (e.g. file.css.php)  
    7079                                try 
    7180                                { 
    7281                                        $view = new View('media/css/'.$orig_filename); 
    73                                          
    7482                                } 
    7583                                catch (Kohana_Exception $exception) 
    7684                                { 
    77                                         // not found 
     85                                        // Not found 
    7886                                        unset($view); 
    7987                                } 
    8088                        } 
    8189                         
    82                         if (isset($view)) { 
     90                        if (isset($view)) 
     91                        { 
    8392                                $data = $view->render(); 
    8493                                 
     
    103112        } 
    104113 
    105         public function js() { 
     114        public function js() 
     115        { 
    106116                $filename = $orig_filename = $this->uri->segment(3); 
    107                 if (substr($filename, -3) == ".js") { 
     117                if (substr($filename, -3) == '.js') 
     118                { 
    108119                        $filename = substr($filename, 0, -3); 
    109120                } 
    110121                 
    111                 $mimetype = config::item('mimes.js'); 
    112                 $mimetype = (isset($mimetype[0]) ? $mimetype[0] : 'text/javascript'); 
     122                $mimetype = Config::item('mimes.js'); 
     123                $mimetype = (isset($mimetype[0])) ? $mimetype[0] : 'text/javascript'; 
    113124 
    114125                 
    115126                $this->use_cache AND $data = $this->cache->get('media.js.'.$filename); 
    116127                 
    117                 if (!isset($data) OR empty($data))  
     128                if ( ! isset($data) OR empty($data))  
    118129                { 
    119130                        try 
    120131                        { 
    121                                 $view = new View('media/js/'.$filename, null, 'js'); 
     132                                $view = new View('media/js/'.$filename, NULL, 'js'); 
    122133                        } 
    123134                        catch (Kohana_Exception $exception) 
    124135                        { 
    125                                 // try to load the file as a php view (eg, file.js.php)  
     136                                // Try to load the file as a php view (eg, file.js.php)  
    126137                                try 
    127138                                { 
     
    130141                                catch (Kohana_Exception $exception) 
    131142                                { 
    132                                         // not found 
     143                                        // Not found 
    133144                                        unset($view); 
    134145                                } 
     
    160171        } 
    161172         
    162         public function _default() { 
     173        public function _default() 
     174        { 
    163175                $type = $this->uri->segment(2); 
    164176                $filename = $this->uri->segment(3); 
    165                 //TODO: finish this for generic types 
     177                // TODO: finish this for generic types 
    166178                /* issues: getting View to work with any types of files */ 
    167179        } 
    168          
    169         function _css_compress($data) { 
    170                 // from http://www.ibloomstudios.com/articles/php_css_compressor/ 
     180 
     181        // Based on http://www.ibloomstudios.com/articles/php_css_compressor/    
     182        public function _css_compress($data) 
     183        { 
     184                // Remove comments 
     185                $data = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $data); 
    171186                 
    172                 // remove comments 
    173                 $data = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $data); 
    174                 // remove tabs, spaces, newlines, etc. 
    175                 $data = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), '', $data); 
     187                // Remove tabs, spaces, newlines, etc. 
     188                $data = str_replace 
     189                ( 
     190                        array("\r\n", "\r", "\n", "\t", '  ', ' {', '{ ', ' }', '} ', ' +', '+ ', ' >', '> ', ' :', ': ', ' ;', '; ', ' ,', ', ', ';}'), 
     191                        array(' ',    ' ',  ' ',  ' ',  ' ',  '{',  '{',  '}',  '}',  '+',  '+',  '>',  '>',  ':',  ':',  ';',  ';',  ',',  ',',  '}' ), 
     192                        $data 
     193                ); 
     194                 
    176195                return $data; 
    177196        } 
     197 
    178198} // End Media_Controller 
    179  
    180