fetch() 공개 메소드

Fetch the cached content
public fetch ( string $id ) : string
$id string cache id (e.g. a filename)
리턴 string
예제 #1
0
 /**
  * Get lessphp cache object
  *
  * @return array
  */
 private function getCache()
 {
     // cache for single run
     // so that getLastModified and getContent in single request do not add additional cache roundtrips (i.e memcache)
     if (isset($this->parsed)) {
         return $this->parsed;
     }
     // check from cache first
     $cache = null;
     $cacheId = $this->getCacheId();
     if ($this->cache->isValid($cacheId, 0)) {
         if ($cache = $this->cache->fetch($cacheId)) {
             $cache = unserialize($cache);
         }
     }
     $less = $this->getCompiler();
     $input = $cache ? $cache : $this->filepath;
     $cache = $less->cachedCompile($input);
     if (!is_array($input) || $cache['updated'] > $input['updated']) {
         $this->cache->store($cacheId, serialize($cache));
     }
     return $this->parsed = $cache;
 }
예제 #2
0
 /**
  * Serve a request for a minified file. 
  * 
  * Here are the available options and defaults:
  * 
  * 'isPublic' : send "public" instead of "private" in Cache-Control 
  * headers, allowing shared caches to cache the output. (default true)
  * 
  * 'quiet' : set to true to have serve() return an array rather than sending
  * any headers/output (default false)
  * 
  * 'encodeOutput' : set to false to disable content encoding, and not send
  * the Vary header (default true)
  * 
  * 'encodeMethod' : generally you should let this be determined by 
  * HTTP_Encoder (leave null), but you can force a particular encoding
  * to be returned, by setting this to 'gzip' or '' (no encoding)
  * 
  * 'encodeLevel' : level of encoding compression (0 to 9, default 9)
  * 
  * 'contentTypeCharset' : appended to the Content-Type header sent. Set to a falsey
  * value to remove. (default 'utf-8')  
  * 
  * 'maxAge' : set this to the number of seconds the client should use its cache
  * before revalidating with the server. This sets Cache-Control: max-age and the
  * Expires header. Unlike the old 'setExpires' setting, this setting will NOT
  * prevent conditional GETs. Note this has nothing to do with server-side caching.
  * 
  * 'rewriteCssUris' : If true, serve() will automatically set the 'currentDir'
  * minifier option to enable URI rewriting in CSS files (default true)
  * 
  * 'bubbleCssImports' : If true, all @import declarations in combined CSS
  * files will be move to the top. Note this may alter effective CSS values
  * due to a change in order. (default false)
  * 
  * 'debug' : set to true to minify all sources with the 'Lines' controller, which
  * eases the debugging of combined files. This also prevents 304 responses.
  * @see Minify_Lines::minify()
  *
  * 'concatOnly' : set to true to disable minification and simply concatenate the files.
  * For JS, no minifier will be used. For CSS, only URI rewriting is still performed.
  * 
  * 'minifiers' : to override Minify's default choice of minifier function for 
  * a particular content-type, specify your callback under the key of the 
  * content-type:
  * <code>
  * // call customCssMinifier($css) for all CSS minification
  * $options['minifiers'][Minify::TYPE_CSS] = 'customCssMinifier';
  * 
  * // don't minify Javascript at all
  * $options['minifiers'][Minify::TYPE_JS] = '';
  * </code>
  * 
  * 'minifierOptions' : to send options to the minifier function, specify your options
  * under the key of the content-type. E.g. To send the CSS minifier an option: 
  * <code>
  * // give CSS minifier array('optionName' => 'optionValue') as 2nd argument 
  * $options['minifierOptions'][Minify::TYPE_CSS]['optionName'] = 'optionValue';
  * </code>
  * 
  * 'contentType' : (optional) this is only needed if your file extension is not 
  * js/css/html. The given content-type will be sent regardless of source file
  * extension, so this should not be used in a Groups config with other
  * Javascript/CSS files.
  *
  * 'importWarning' : serve() will check CSS files for @import declarations that
  * appear too late in the combined stylesheet. If found, serve() will prepend
  * the output with this warning. To disable this, set this option to empty string.
  * 
  * Any controller options are documented in that controller's createConfiguration() method.
  * 
  * @param Minify_ControllerInterface $controller instance of subclass of Minify_Controller_Base
  * 
  * @param array                      $options    controller/serve options
  * 
  * @return null|array if the 'quiet' option is set to true, an array
  * with keys "success" (bool), "statusCode" (int), "content" (string), and
  * "headers" (array).
  *
  * @throws Exception
  */
 public function serve(Minify_ControllerInterface $controller, $options = array())
 {
     $options = array_merge($this->getDefaultOptions(), $options);
     $config = $controller->createConfiguration($options);
     $this->sources = $config->getSources();
     $this->selectionId = $config->getSelectionId();
     $this->options = $this->analyzeSources($config->getOptions());
     // check request validity
     if (!$this->sources) {
         // invalid request!
         if (!$this->options['quiet']) {
             $this->errorExit($this->options['badRequestHeader'], self::URL_DEBUG);
         } else {
             list(, $statusCode) = explode(' ', $this->options['badRequestHeader']);
             return array('success' => false, 'statusCode' => (int) $statusCode, 'content' => '', 'headers' => array());
         }
     }
     $this->controller = $controller;
     if ($this->options['debug']) {
         $this->setupDebug();
         $this->options['maxAge'] = 0;
     }
     // determine encoding
     if ($this->options['encodeOutput']) {
         $sendVary = true;
         if ($this->options['encodeMethod'] !== null) {
             // controller specifically requested this
             $contentEncoding = $this->options['encodeMethod'];
         } else {
             // sniff request header
             // depending on what the client accepts, $contentEncoding may be
             // 'x-gzip' while our internal encodeMethod is 'gzip'. Calling
             // getAcceptedEncoding(false, false) leaves out compress and deflate as options.
             list($this->options['encodeMethod'], $contentEncoding) = HTTP_Encoder::getAcceptedEncoding(false, false);
             $sendVary = !HTTP_Encoder::isBuggyIe();
         }
     } else {
         $this->options['encodeMethod'] = '';
         // identity (no encoding)
     }
     // check client cache
     $cgOptions = array('lastModifiedTime' => $this->options['lastModifiedTime'], 'isPublic' => $this->options['isPublic'], 'encoding' => $this->options['encodeMethod']);
     if ($this->options['maxAge'] > 0) {
         $cgOptions['maxAge'] = $this->options['maxAge'];
     } elseif ($this->options['debug']) {
         $cgOptions['invalidate'] = true;
     }
     $cg = new HTTP_ConditionalGet($cgOptions);
     if ($cg->cacheIsValid) {
         // client's cache is valid
         if (!$this->options['quiet']) {
             $cg->sendHeaders();
             return;
         } else {
             return array('success' => true, 'statusCode' => 304, 'content' => '', 'headers' => $cg->getHeaders());
         }
     } else {
         // client will need output
         $headers = $cg->getHeaders();
         unset($cg);
     }
     if ($this->options['contentType'] === self::TYPE_CSS && $this->options['rewriteCssUris']) {
         $this->setupUriRewrites();
     }
     if ($this->options['concatOnly']) {
         $this->options['minifiers'][self::TYPE_JS] = false;
         foreach ($this->sources as $key => $source) {
             if ($this->options['contentType'] === self::TYPE_JS) {
                 $source->setMinifier("");
             } elseif ($this->options['contentType'] === self::TYPE_CSS) {
                 $source->setMinifier(array('Minify_CSSmin', 'minify'));
                 $sourceOpts = $source->getMinifierOptions();
                 $sourceOpts['compress'] = false;
                 $source->setMinifierOptions($sourceOpts);
             }
         }
     }
     // check server cache
     if (!$this->options['debug']) {
         // using cache
         // the goal is to use only the cache methods to sniff the length and
         // output the content, as they do not require ever loading the file into
         // memory.
         $cacheId = $this->_getCacheId();
         $fullCacheId = $this->options['encodeMethod'] ? $cacheId . '.gz' : $cacheId;
         // check cache for valid entry
         $cacheIsReady = $this->cache->isValid($fullCacheId, $this->options['lastModifiedTime']);
         if ($cacheIsReady) {
             $cacheContentLength = $this->cache->getSize($fullCacheId);
         } else {
             // generate & cache content
             try {
                 $content = $this->combineMinify();
             } catch (Exception $e) {
                 $this->controller->log($e->getMessage());
                 if (!$this->options['quiet']) {
                     $this->errorExit($this->options['errorHeader'], self::URL_DEBUG);
                 }
                 throw $e;
             }
             $this->cache->store($cacheId, $content);
             if (function_exists('gzencode') && $this->options['encodeMethod']) {
                 $this->cache->store($cacheId . '.gz', gzencode($content, $this->options['encodeLevel']));
             }
         }
     } else {
         // no cache
         $cacheIsReady = false;
         try {
             $content = $this->combineMinify();
         } catch (Exception $e) {
             $this->controller->log($e->getMessage());
             if (!$this->options['quiet']) {
                 $this->errorExit($this->options['errorHeader'], self::URL_DEBUG);
             }
             throw $e;
         }
     }
     if (!$cacheIsReady && $this->options['encodeMethod']) {
         // still need to encode
         $content = gzencode($content, $this->options['encodeLevel']);
     }
     // add headers
     if ($cacheIsReady) {
         $headers['Content-Length'] = $cacheContentLength;
     } else {
         if (function_exists('mb_strlen') && (int) ini_get('mbstring.func_overload') & 2) {
             $headers['Content-Length'] = mb_strlen($content, '8bit');
         } else {
             $headers['Content-Length'] = strlen($content);
         }
     }
     $headers['Content-Type'] = $this->options['contentType'];
     if ($this->options['contentTypeCharset']) {
         $headers['Content-Type'] .= '; charset=' . $this->options['contentTypeCharset'];
     }
     if ($this->options['encodeMethod'] !== '') {
         $headers['Content-Encoding'] = $contentEncoding;
     }
     if ($this->options['encodeOutput'] && $sendVary) {
         $headers['Vary'] = 'Accept-Encoding';
     }
     if (!$this->options['quiet']) {
         // output headers & content
         foreach ($headers as $name => $val) {
             header($name . ': ' . $val);
         }
         if ($cacheIsReady) {
             $this->cache->display($fullCacheId);
         } else {
             echo $content;
         }
     } else {
         return array('success' => true, 'statusCode' => 200, 'content' => $cacheIsReady ? $this->cache->fetch($fullCacheId) : $content, 'headers' => $headers);
     }
 }