Ejemplo n.º 1
0
 /**
  * Maybe compress CSS code.
  *
  * @since 140417 Initial release.
  *
  * @param string $css Raw CSS code.
  *
  * @return string CSS code (possibly compressed).
  */
 protected function maybeCompressCssCode($css)
 {
     if ($benchmark = !empty($this->options['benchmark']) && $this->options['benchmark'] === 'details') {
         $time = microtime(true);
     }
     $css = (string) $css;
     // Force string value.
     if (isset($this->options['compress_css_code'])) {
         if (!$this->options['compress_css_code']) {
             $disabled = true;
             // Disabled flag.
         }
     }
     if (!$css || !empty($disabled)) {
         goto finale;
         // Nothing to do.
     }
     if (strlen($css) > 1000000) {
         // Exclude VERY large files. Too time-consuming.
         // Should really be compressed ahead-of-time anyway.
         goto finale;
         // Don't compress HUGE files.
     }
     try {
         // Catch CSS compression-related exceptions.
         if (!($compressed_css = \WebSharks\CssMinifier\Core::compress($css))) {
             // `E_USER_NOTICE` to avoid a show-stopping problem.
             trigger_error('CSS compression failure.', E_USER_NOTICE);
         } else {
             $css = $this->stripUtf8Bom($compressed_css);
         }
         // Use compressed CSS file.
     } catch (\Exception $exception) {
         trigger_error($exception->getMessage(), E_USER_NOTICE);
     }
     finale:
     // Target point; finale/return value.
     if ($css) {
         $css = trim($css);
     }
     if ($benchmark && !empty($time) && $css && empty($disabled)) {
         $this->benchmark->addTime(__FUNCTION__, $time, sprintf('compressing CSS w/ checksum: `%1$s`', md5($css)));
     }
     return $css;
 }
 protected function execute($string)
 {
     return Core::compress($string);
 }