FRED™  3.0
FRED™: Framework for Rapid and Easy Development
Handler.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Rsi\Fred\Minify;
4 
5 class Handler extends \Rsi\Thing{
6 
7  protected $_minify;
8  protected $_config = null;
9 
10  public function __construct($minify,$config){
11  $this->_minify = $minify;
12  $this->configure($this->_config = $config);
13  $this->init();
14  }
15 
16  protected function init(){
17  }
18  /**
19  * Minify a source string.
20  * @param string $source Source to minify.
21  * @param string $filename Optional source filename for use in error messages.
22  * @return string Minified source.
23  */
24  public function source($source,$filename = null){
25  return trim(preg_replace('/\\s*[\\r\\n]\\s*/','',preg_replace('/\\/\\*.*?\\*\\//s','',$source)));
26  }
27  /**
28  * Minify a source file.
29  * @param string $source Source filename.
30  * @param string $target Target filename (when empty, and a cache is definied, a filename will be generated).
31  * @return string Target filename (false on error).
32  */
33  public function file($source,$target = null){
34  if(!$target) $target = $this->_minify->cacheTarget($source);
35  if(!$target) return false;
36  if($this->_minify->filemtime($target) < $this->_minify->filemtime($source)){
37  if($this->_minify->alreadyMinimized($source)) copy($source,$target);
38  else \Rsi\File::write($target,$this->source(file_get_contents($source),$source),0666);
39  }
40  return $target;
41  }
42  /**
43  * Minify and combine multiple source files.
44  * @param array $sources Source filenames.
45  * @param string $target Target filename (when empty, and a cache is definied, a filename will be generated).
46  * @return string Target filename (false on error).
47  */
48  public function files($sources,$target = null){
49  if(!$target) $target = $this->_minify->cacheTarget(implode('|',$sources));
50  if(!$target) return false;
51  $time = 0;
52  $targets = [];
53  foreach($sources as $source) $time = max($time,$this->_minify->filemtime($targets[] = $this->file($source)));
54  if($this->_minify->filemtime($target) < $time) foreach(array_values($targets) as $index => $source)
55  \Rsi\File::write($target,file_get_contents($source) . "\n",0666,$index);
56  return $target;
57  }
58 
59 }
Basic object.
Definition: Thing.php:13
configure($config)
Configure the object.
Definition: Thing.php:45
source($source, $filename=null)
Minify a source string.
Definition: Handler.php:24
file($source, $target=null)
Minify a source file.
Definition: Handler.php:33
files($sources, $target=null)
Minify and combine multiple source files.
Definition: Handler.php:48
__construct($minify, $config)
Definition: Handler.php:10