| 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 | |
| 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) |
| 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) |
| 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 |
| 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/ |