MODX Revolution PHP Cross Reference Content Management Systems

Source: /manager/min/lib/Minify/HTML/Helper.php - 193 lines - 6341 bytes - Summary - Text - Print

Description: Class Minify_HTML_Helper

   1  <?php
   2  /**
   3   * Class Minify_HTML_Helper
   4   * @package Minify
   5   */
   6  
   7  /**
   8   * Helpers for writing Minfy URIs into HTML
   9   *
  10   * @package Minify
  11   * @author Stephen Clay <steve@mrclay.org>
  12   */
  13  class Minify_HTML_Helper {
  14      public $rewriteWorks = true;
  15      public $minAppUri = '/min';
  16      public $groupsConfigFile = '';
  17  
  18      /*
  19       * Get an HTML-escaped Minify URI for a group or set of files
  20       * 
  21       * @param mixed $keyOrFiles a group key or array of filepaths/URIs
  22       * @param array $opts options:
  23       *   'farExpires' : (default true) append a modified timestamp for cache revving
  24       *   'debug' : (default false) append debug flag
  25       *   'charset' : (default 'UTF-8') for htmlspecialchars
  26       *   'minAppUri' : (default '/min') URI of min directory
  27       *   'rewriteWorks' : (default true) does mod_rewrite work in min app?
  28       *   'groupsConfigFile' : specify if different
  29       * @return string
  30       */
  31      public static function getUri($keyOrFiles, $opts = array())
  32      {
  33          $opts = array_merge(array( // default options
  34              'farExpires' => true
  35              ,'debug' => false
  36              ,'charset' => 'UTF-8'
  37              ,'minAppUri' => '/min'
  38              ,'rewriteWorks' => true
  39              ,'groupsConfigFile' => ''
  40          ), $opts);
  41          $h = new self;
  42          $h->minAppUri = $opts['minAppUri'];
  43          $h->rewriteWorks = $opts['rewriteWorks'];
  44          $h->groupsConfigFile = $opts['groupsConfigFile'];
  45          if (is_array($keyOrFiles)) {
  46              $h->setFiles($keyOrFiles, $opts['farExpires']);
  47          } else {
  48              $h->setGroup($keyOrFiles, $opts['farExpires']);
  49          }
  50          $uri = $h->getRawUri($opts['farExpires'], $opts['debug']);
  51          return htmlspecialchars($uri, ENT_QUOTES, $opts['charset']);
  52      }
  53  
  54      /*
  55       * Get non-HTML-escaped URI to minify the specified files
  56       */
  57      public function getRawUri($farExpires = true, $debug = false)
  58      {
  59          $path = rtrim($this->minAppUri, '/') . '/';
  60          if (! $this->rewriteWorks) {
  61              $path .= '?';
  62          }
  63          if (null === $this->_groupKey) {
  64              // @todo: implement shortest uri
  65              $path = self::_getShortestUri($this->_filePaths, $path);
  66          } else {
  67              $path .= "g=" . $this->_groupKey;
  68          }
  69          if ($debug) {
  70              $path .= "&debug";
  71          } elseif ($farExpires && $this->_lastModified) {
  72              $path .= "&" . $this->_lastModified;
  73          }
  74          return $path;
  75      }
  76  
  77      public function setFiles($files, $checkLastModified = true)
  78      {
  79          $this->_groupKey = null;
  80          if ($checkLastModified) {
  81              $this->_lastModified = self::getLastModified($files);
  82          }
  83          // normalize paths like in /min/f=<paths>
  84          foreach ($files as $k => $file) {
  85              if (0 === strpos($file, '//')) {
  86                  $file = substr($file, 2);
  87              } elseif (0 === strpos($file, '/')
  88                        || 1 === strpos($file, ':\\')) {
  89                  $file = substr($file, strlen($_SERVER['DOCUMENT_ROOT']) + 1);
  90              }
  91              $file = strtr($file, '\\', '/');
  92              $files[$k] = $file;
  93          }
  94          $this->_filePaths = $files;
  95      }
  96  
  97      public function setGroup($key, $checkLastModified = true)
  98      {
  99          $this->_groupKey = $key;
 100          if ($checkLastModified) {
 101              if (! $this->groupsConfigFile) {
 102                  $this->groupsConfigFile = dirname(dirname(dirname(dirname(__FILE__)))) . '/groupsConfig.php';
 103              }
 104              if (is_file($this->groupsConfigFile)) {
 105                  $gc = (require $this->groupsConfigFile);
 106                  if (isset($gc[$key])) {
 107                      $this->_lastModified = self::getLastModified($gc[$key]);
 108                  }
 109              }
 110          }
 111      }
 112      
 113      public static function getLastModified($sources, $lastModified = 0)
 114      {
 115          $max = $lastModified;
 116          foreach ((array)$sources as $source) {
 117              if (is_object($source) && isset($source->lastModified)) {
 118                  $max = max($max, $source->lastModified);
 119              } elseif (is_string($source)) {
 120                  if (0 === strpos($source, '//')) {
 121                      $source = $_SERVER['DOCUMENT_ROOT'] . substr($source, 1);
 122                  }
 123                  if (is_file($source)) {
 124                      $max = max($max, filemtime($source));
 125                  }
 126              }
 127          }
 128          return $max;
 129      }
 130  
 131      protected $_groupKey = null; // if present, URI will be like g=...
 132      protected $_filePaths = array();
 133      protected $_lastModified = null;
 134  
 135      
 136      /**
 137       * In a given array of strings, find the character they all have at
 138       * a particular index
 139       *
 140       * @param array $arr array of strings
 141       * @param int $pos index to check
 142       * @return mixed a common char or '' if any do not match
 143       */
 144      protected static function _getCommonCharAtPos($arr, $pos) {
 145          $l = count($arr);
 146          $c = $arr[0][$pos];
 147          if ($c === '' || $l === 1)
 148              return $c;
 149          for ($i = 1; $i < $l; ++$i)
 150              if ($arr[$i][$pos] !== $c)
 151                  return '';
 152          return $c;
 153      }
 154  
 155      /**
 156       * Get the shortest URI to minify the set of source files
 157       *
 158       * @param array $paths root-relative URIs of files
 159       * @param string $minRoot root-relative URI of the "min" application
 160       */
 161      protected static function _getShortestUri($paths, $minRoot = '/min/') {
 162          $pos = 0;
 163          $base = '';
 164          $c;
 165          while (true) {
 166              $c = self::_getCommonCharAtPos($paths, $pos);
 167              if ($c === '') {
 168                  break;
 169              } else {
 170                  $base .= $c;
 171              }
 172              ++$pos;
 173          }
 174          $base = preg_replace('@[^/]+$@', '', $base);
 175          $uri = $minRoot . 'f=' . implode(',', $paths);
 176          
 177          if (substr($base, -1) === '/') {
 178              // we have a base dir!
 179              $basedPaths = $paths;
 180              $l = count($paths);
 181              for ($i = 0; $i < $l; ++$i) {
 182                  $basedPaths[$i] = substr($paths[$i], strlen($base));
 183              }
 184              $base = substr($base, 0, strlen($base) - 1);
 185              $bUri = $minRoot . 'b=' . $base . '&f=' . implode(',', $basedPaths);
 186  
 187              $uri = strlen($uri) < strlen($bUri)
 188                  ? $uri
 189                  : $bUri;
 190          }
 191          return $uri;
 192      }
 193  }

title

Description

title

Description

title

Description

title

title

Body