/** * Stash a reference to the controller on each instantiation * and install conversion helpers * * @param (class) control: CSSCompression Controller */ public function __construct(CSSCompression_Control $control) { $this->Control = $control; $this->options =& $control->Option->options; if (!self::$color2hex) { foreach (self::$files as $v => $file) { self::${$v} = CSSCompression::getJSON($file); } } }
/** * Handle $data, and return the results. * * @param string data The data to handle. * @return string * @access public */ public function input($data, $options = array()) { $default_options = array('minify' => true); $result = null; $options = array_merge($default_options, $options); try { if (strpos($data, NL) === false && is_file($data)) { $data = file_get_contents($data); if ($data === false) { throw new \Exception('Could not load CSS: ' . $data); } } require_once CURATOR_APP_DIR . DS . 'Vendors' . DS . 'css-compressor' . DS . 'dist' . DS . 'src' . DS . 'CSSCompression.php'; $result = \CSSCompression::express($data, 'sane'); } catch (\Exception $e) { \Curator\Console::stderr('** Could not handle CSS data:'); \Curator\Console::stderr(' ' . $e->getMessage()); } return $result; }
/** * Extend like function to merge an array of preferences into * the options array. * * @param (mixed) options: Array of preferences to merge into options */ public function merge($options = array()) { $modes = CSSCompression::modes(); if ($options && is_array($options) && count($options)) { $this->Control->mode = $this->custom; foreach ($this->options as $key => $value) { if (!isset($options[$key])) { continue; } else { if (strtolower($options[$key]) == 'on') { $this->options[$key] = true; } else { if (strtolower($options[$key]) == 'off') { $this->options[$key] = false; } else { $this->options[$key] = intval($options[$key]); } } } } } else { if ($options && is_string($options) && array_key_exists($options, $modes)) { $this->Control->mode = $options; // Default all to true, the mode has to force false foreach ($this->options as $key => $value) { if ($key != 'readability') { $this->options[$key] = true; } } // Merge mode into options foreach ($modes[$options] as $key => $value) { $this->options[$key] = $value; } } } return $this->options; }
/** * The Singleton access method (for those that want it) * * @param (string) name: Name of the stored instance */ public static function getInstance($name = NULL) { if ($name !== NULL) { if (!isset(self::$instances[$name])) { self::$instances[$name] = new self(); } return self::$instances[$name]; } else { if (!self::$instance) { self::$instance = new self(); } } return self::$instance; }
protected static function compressFiles() { ini_set('max_execution_time', 120); self::$_mincode['js'] = ''; self::$_mincode['css'] = ''; $curl = new CURLRequest(); $css = new CSSCompression(); $css->option('readability', CSSCompression::READ_NONE); $css->option('mode', self::$_opt['cssLevel']); foreach (self::$_files as $file) { $code = $file['data']; $hash = md5($code); $cache = self::$_cacheDir . $hash; if (file_exists($cache) === true) { self::$_mincode[$file['ext']] .= file_get_contents($cache); } else { if (self::$_opt['compressCode'] === false) { self::$_mincode[$file['ext']] .= $code; } else { if ($file['ext'] === 'js') { if (self::$_opt['useLocalJS'] === false) { if (strlen($code) / 1000 / 1000 > 1) { $file = basename($file['path']); $msg = '%s is bigger than 1000kB,'; $msg .= ' split the code into multiple files or'; $msg .= ' enable local compression for javascript.'; $msg = sprintf($msg, $file); throw new Exception($msg); } $post = array('js_code' => $code, 'compilation_level' => 'SIMPLE_OPTIMIZATIONS', 'output_format' => 'json'); // Workaround to allow multiple output_info in query. $post = http_build_query($post); $post .= '&output_info=errors&output_info=compiled_code'; $return = $curl->get('http://closure-compiler.appspot.com/compile', array(CURLOPT_RETURNTRANSFER => true, CURLOPT_POSTFIELDS => $post, CURLOPT_POST => true)); $data = json_decode($return['content'], true); if (isset($data['errors']) === true || isset($data['serverErrors']) === true) { $error = $data['errors'][0]['error']; $file = basename($file['path']); $line = $data['errors'][0]['lineno']; $msg = 'Web Service returned %s in %s on line %u.'; $msg = sprintf($msg, $error, $file, $line); throw new Exception($msg); } else { if (isset($data['compiledCode']) === true) { $code = $data['compiledCode']; self::$_mincode[$file['ext']] .= $code; file_put_contents($cache, $code); } else { throw new Exception('An unknown error has occured.'); } } //end if } //end if } else { if ($file['ext'] === 'css') { $code = trim($css->compress($code)); self::$_mincode[$file['ext']] .= $code; file_put_contents($cache, $code); } } //end if } //end if } //end if } //end foreach }