-
Notifications
You must be signed in to change notification settings - Fork 10
/
phpminify.min.php
2 lines (2 loc) · 2.61 KB
/
phpminify.min.php
1
2
<?php /* https://github.com/basselin/php-minify */
class PhpMinify { protected $options = array( 'source' => 'module/', 'target' => 'modulemin/', 'banner' => '', 'extensions' => array('inc', 'php', 'phtml'), 'exclusions' => array('md'), ); public function __construct(array $options = array()) { $this->options = array_merge($this->options, $options); } public function getSource() { return $this->fixSlashes($this->options['source']); } public function setSource($source) { $this->options['source'] = $source; return $this; } public function getTarget() { return $this->fixSlashes($this->options['target']); } public function setTarget($target) { $this->options['target'] = $target; return $this; } public function getBanner() { return $this->options['banner']; } public function setBanner($banner) { $this->options['banner'] = $banner; return $this; } public function getExtensions() { return $this->options['extensions']; } public function setExtensions(array $extensions) { $this->options['extensions'] = $extensions; return $this; } public function getExclusions() { return $this->options['exclusions']; } public function setExclusions(array $extensions) { $this->options['exclusions'] = $extensions; return $this; } public function minify($filename) { $string = php_strip_whitespace($filename); if ($this->getBanner()) { $string = preg_replace('/^<\?php/', '<?php ' . $this->getBanner(), $string); } return $string; } public function fixSlashes($filename) { if (DIRECTORY_SEPARATOR != '/') { return str_replace(DIRECTORY_SEPARATOR, '/', $filename); } return $filename; } public function run() { $return = array(); $dirIterator = new RecursiveDirectoryIterator($this->getSource()); $iterator = new RecursiveIteratorIterator($dirIterator, RecursiveIteratorIterator::CHILD_FIRST); foreach ($iterator as $key => $value) { if (in_array($value->getFilename(), array('..', '.DS_Store'))) { continue; } $pattern = '/^' . preg_quote($this->getSource(), '/') . '/'; $sourcePathname = $this->fixSlashes($value->getPathname()); $targetPathname = preg_replace($pattern, $this->getTarget(), $sourcePathname); if ($value->isDir()) { if ($value->getBasename() == '.') { $dirname = dirname($targetPathname); if (!is_dir($dirname)) { mkdir($dirname, 0777, true); } $return[$value->getPath()] = $dirname; } continue; } if ($value->isFile() && !in_array(strtolower($value->getExtension()), $this->getExclusions())) { if (in_array(strtolower($value->getExtension()), $this->getExtensions())) { file_put_contents($targetPathname, $this->minify($sourcePathname)); } else { copy($sourcePathname, $targetPathname); } $return[$sourcePathname] = $targetPathname; } } return $return; } }