Exemplo n.º 1
0
 public function process(AssetInterface $asset)
 {
     $hash = hash_init('sha1');
     switch ($this->strategy) {
         case self::STRATEGY_MODIFICATION:
             hash_update($hash, $asset->getLastModified());
             break;
         case self::STRATEGY_CONTENT:
             hash_update($hash, $asset->dump());
             break;
     }
     foreach ($asset as $i => $leaf) {
         if ($sourcePath = $leaf->getSourcePath()) {
             hash_update($hash, $sourcePath);
         } else {
             hash_update($hash, $i);
         }
     }
     $hash = substr(hash_final($hash), 0, 7);
     $url = $asset->getTargetPath();
     $oldExt = pathinfo($url, PATHINFO_EXTENSION);
     $newExt = '-' . $hash . '.' . $oldExt;
     if (!$oldExt || 0 < preg_match('/' . preg_quote($newExt, '/') . '$/', $url)) {
         return;
     }
     $asset->setTargetPath(substr($url, 0, (strlen($oldExt) + 1) * -1) . $newExt);
 }
Exemplo n.º 2
0
 /**
  * @param AssetInterface $asset
  * @param AssetWriter $writer
  * @param array $cachePath
  * @param array $headers
  */
 public function __construct(AssetInterface $asset, AssetWriter $writer, $cachePath, array $headers = [])
 {
     $file = $asset->getTargetPath();
     $cachePath = $cachePath . '/' . $file;
     $cached = false;
     $cacheTime = time();
     if (is_file($cachePath)) {
         $mTime = $asset->getLastModified();
         $cacheTime = filemtime($cachePath);
         if ($mTime > $cacheTime) {
             @unlink($cachePath);
             $cacheTime = $mTime;
         } else {
             $cached = true;
         }
     }
     if (!$cached) {
         $writer->writeAsset($asset);
     }
     $stream = function () use($cachePath) {
         readfile($cachePath);
     };
     $headers['Content-Length'] = filesize($cachePath);
     if (preg_match('/.+\\.([a-zA-Z0-9]+)/', $file, $matches)) {
         $ext = $matches[1];
         if (isset($this->mimeTypes[$ext])) {
             $headers['Content-Type'] = $this->mimeTypes[$ext];
         }
     }
     parent::__construct($stream, 200, $headers);
     $date = new \DateTime();
     $date->setTimestamp($cacheTime);
     $this->setLastModified($date);
 }
Exemplo n.º 3
0
 /**
  * {@inheritdoc}
  */
 public function getLastModified()
 {
     if (!$this->innerAsset) {
         $this->createInnerAsset();
     }
     return $this->innerAsset->getLastModified();
 }
 public function writeAsset(AssetInterface $asset)
 {
     foreach (VarUtils::getCombinations($asset->getVars(), $this->values) as $combination) {
         $asset->setValues($combination);
         $path = $this->dir . '/' . VarUtils::resolve($asset->getTargetPath(), $asset->getVars(), $asset->getValues());
         if (!is_dir($path) && (!file_exists($path) || filemtime($path) <= $asset->getLastModified())) {
             static::write($path, $asset->dump());
         }
     }
 }
 public function process(AssetInterface $asset, AssetFactory $factory)
 {
     $path = $asset->getTargetPath();
     $ext = pathinfo($path, PATHINFO_EXTENSION);
     $lastModified = $asset->getLastModified();
     if (null !== $lastModified) {
         $path = substr_replace($path, "{$lastModified}.{$ext}", -1 * strlen($ext));
         $asset->setTargetPath($path);
     }
 }
Exemplo n.º 6
0
 /**
  * @param AssetInterface $asset
  * @param array $options
  * @return string
  */
 protected function helper(AssetInterface $asset, array $options = array())
 {
     $path = $this->baseUrl . $this->basePath . $asset->getTargetPath();
     $extension = pathinfo($path, PATHINFO_EXTENSION);
     $extension = strtolower($extension);
     if (isset($options['addFileMTime']) && $options['addFileMTime']) {
         $path .= '?' . $asset->getLastModified();
     }
     switch ($extension) {
         case 'js':
             return $this->getScriptTag($path, $options);
         case 'css':
             return $this->getStylesheetTag($path, $options);
     }
     return '';
 }
Exemplo n.º 7
0
 private function dumpAsset(AssetInterface $asset)
 {
     // HTTP caching
     $mtime = gmdate('D, d M y H:i:s', $asset->getLastModified()) . 'GMT';
     if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && ($mtime = $_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
         header('HTTP/1.1 304 Not Modified');
         exit;
     }
     // set headers & dump asset
     $extension = pathinfo($asset->getTargetPath(), PATHINFO_EXTENSION);
     if (array_key_exists($extension, self::$extensionsToMimeTypes)) {
         $mimeType = self::$extensionsToMimeTypes[$extension];
         header("Content-Type: {$mimeType}");
         header("Last-Modified: {$mtime}");
         header('Cache-Control: public');
     }
     echo $asset->dump();
     exit;
 }
Exemplo n.º 8
0
 /**
  * @param AssetInterface $asset
  * @return string 
  */
 protected function getCacheKey(AssetInterface $asset)
 {
     $cacheKey = '';
     if ($asset instanceof AssetCollectionInterface) {
         foreach ($asset->all() as $childAsset) {
             $cacheKey .= $childAsset->getSourcePath();
             $cacheKey .= $childAsset->getLastModified();
         }
     } else {
         $cacheKey .= $asset->getSourcePath();
         $cacheKey .= $asset->getLastModified();
     }
     foreach ($asset->getFilters() as $filter) {
         if ($filter instanceof HashableInterface) {
             $cacheKey .= $filter->hash();
         } else {
             $cacheKey .= serialize($filter);
         }
     }
     return md5($cacheKey);
 }
 public function setupAsset(AssetInterface $asset)
 {
     $path = $this->getBaseUrl() . $asset->getTargetPath();
     return $this->helper($path, $asset->getLastModified());
 }
Exemplo n.º 10
0
 public function getLastModified(AssetInterface $asset)
 {
     $mtime = $asset->getLastModified();
     if (!($filters = $asset->getFilters())) {
         return $mtime;
     }
     // prepare load path
     $sourceRoot = $asset->getSourceRoot();
     $sourcePath = $asset->getSourcePath();
     $loadPath = $sourceRoot && $sourcePath ? dirname($sourceRoot . '/' . $sourcePath) : null;
     $prevFilters = array();
     foreach ($filters as $filter) {
         $prevFilters[] = $filter;
         if (!$filter instanceof DependencyExtractorInterface) {
             continue;
         }
         // extract children from asset after running all preceeding filters
         $clone = clone $asset;
         $clone->clearFilters();
         foreach (array_slice($prevFilters, 0, -1) as $prevFilter) {
             $clone->ensureFilter($prevFilter);
         }
         $clone->load();
         foreach ($filter->getChildren($this->factory, $clone->getContent(), $loadPath) as $child) {
             $mtime = max($mtime, $this->getLastModified($child));
         }
     }
     return $mtime;
 }
Exemplo n.º 11
0
 /**
  * Returns the time the current asset was last modified.
  *
  * @return integer|null A UNIX timestamp
  */
 public function getLastModified()
 {
     return $this->asset->getLastModified();
 }
Exemplo n.º 12
0
 protected function is_stale(AssetInterface $asset)
 {
     $stale = TRUE;
     $target = $asset->getTargetPath();
     if (file_exists($target)) {
         $last = filemtime($target);
         $mod = $asset->getLastModified();
         if ($mod && $last >= $mod) {
             $stale = FALSE;
         }
     }
     return $stale;
 }