diff --git a/Imagine/CachePathResolver.php b/Imagine/CachePathResolver.php index 603eefe..a8c4679 100644 --- a/Imagine/CachePathResolver.php +++ b/Imagine/CachePathResolver.php @@ -3,30 +3,46 @@ namespace Avalanche\Bundle\ImagineBundle\Imagine; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\RouterInterface; +use Symfony\Component\HttpKernel\Util\Filesystem; class CachePathResolver { /** - * @var string + * @var Symfony\Component\HttpFoundation\Request */ - private $webRoot; + private $request; /** * @var Symfony\Component\Routing\RouterInterface */ private $router; + /** + * @var Symfony\Component\HttpKernel\Util\Filesystem + */ + private $filesystem; + + /** + * @var string + */ + private $webRoot; + /** * Constructs cache path resolver with a given web root and cache prefix * - * @param string $webRoot - * @param Symfony\Component\Routing\RouterInterface $router + * @param Symfony\Component\HttpFoundation\Request $request + * @param Symfony\Component\Routing\RouterInterface $router + * @param Symfony\Component\HttpKernel\Util\Filesystem $filesystem + * @param string $webRoot */ - public function __construct($webRoot, RouterInterface $router) + public function __construct(Request $request, RouterInterface $router, Filesystem $filesystem, $webRoot) { - $this->webRoot = $webRoot; - $this->router = $router; + $this->request = $request; + $this->router = $router; + $this->filesystem = $filesystem; + $this->webRoot = $webRoot; } /** @@ -55,4 +71,39 @@ public function getBrowserPath($path, $filter) return $path; } + + public function resolve($path, $filter) + { + //TODO: find out why I need double urldecode to get a valid path + $browserPath = urldecode(urldecode($this->getBrowserPath($path, $filter))); + $basePath = $this->request->getBaseUrl(); + + if (!empty($basePath) && 0 === strpos($browserPath, $basePath)) { + $browserPath = substr($browserPath, strlen($basePath)); + } + + // if cache path cannot be determined, return 404 + if (null === $browserPath) { + return false; + } + + $realPath = $this->webRoot.$browserPath; + + // if the file has already been cached, we're probably not rewriting + // correctly, hence make a 301 to proper location, so browser remembers + if (file_exists($realPath)) { + return new Response('', 301, array( + 'location' => $this->request->getBasePath().$browserPath + )); + } + + $dir = pathinfo($realPath, PATHINFO_DIRNAME); + if (!is_dir($dir) && !$this->filesystem->mkdir($dir)) { + throw new \RuntimeException(sprintf( + 'Could not create directory %s', $dir + )); + } + + return $realPath; + } } diff --git a/Resources/config/imagine.xml b/Resources/config/imagine.xml index 2622dcf..85cc606 100644 --- a/Resources/config/imagine.xml +++ b/Resources/config/imagine.xml @@ -45,9 +45,11 @@ - - %imagine.web_root% + + + + %imagine.web_root% @@ -87,12 +89,12 @@ - + - + diff --git a/Templating/Helper/ImagineHelper.php b/Templating/Helper/ImagineHelper.php index 1849a5c..ddca102 100644 --- a/Templating/Helper/ImagineHelper.php +++ b/Templating/Helper/ImagineHelper.php @@ -2,24 +2,24 @@ namespace Avalanche\Bundle\ImagineBundle\Templating\Helper; -use Avalanche\Bundle\ImagineBundle\Imagine\CachePathResolver; +use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\Templating\Helper\Helper; class ImagineHelper extends Helper { /** - * @var Avalanche\Bundle\ImagineBundle\Imagine\CachePathResolver + * @var Symfony\Component\DependencyInjection\ContainerInterface */ - private $cachePathResolver; + private $container; /** - * Constructs by setting $cachePathResolver + * Constructs by setting $container * - * @param Avalanche\Bundle\ImagineBundle\Imagine\CachePathResolver $cachePathResolver + * @param Symfony\Component\DependencyInjection\ContainerInterface $container */ - public function __construct(CachePathResolver $cachePathResolver) + public function __construct(ContainerInterface $container) { - $this->cachePathResolver = $cachePathResolver; + $this->container = $container; } /** @@ -32,7 +32,7 @@ public function __construct(CachePathResolver $cachePathResolver) */ public function filter($path, $filter) { - return $this->cachePathResolver->getBrowserPath($path, $filter); + return $this->container->get('imagine.cache.path.resolver')->getBrowserPath($path, $filter); } /** diff --git a/Templating/ImagineExtension.php b/Templating/ImagineExtension.php index 90ca6ea..7c4c716 100644 --- a/Templating/ImagineExtension.php +++ b/Templating/ImagineExtension.php @@ -2,24 +2,24 @@ namespace Avalanche\Bundle\ImagineBundle\Templating; -use Avalanche\Bundle\ImagineBundle\Imagine\CachePathResolver; +use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpKernel\Util\Filesystem; class ImagineExtension extends \Twig_Extension { /** - * @var Avalanche\Bundle\ImagineBundle\Imagine\CachePathResolver + * @var Symfony\Component\DependencyInjection\ContainerInterface */ - private $cachePathResolver; + private $container; /** - * Constructs by setting $cachePathResolver + * Constructs by setting $container * - * @param Avalanche\Bundle\ImagineBundle\Imagine\CachePathResolver $cachePathResolver + * @param Symfony\Component\DependencyInjection\ContainerInterface $container */ - public function __construct(CachePathResolver $cachePathResolver) + public function __construct(ContainerInterface $container) { - $this->cachePathResolver = $cachePathResolver; + $this->container = $container; } /** @@ -43,7 +43,7 @@ public function getFilters() */ public function applyFilter($path, $filter) { - return $this->cachePathResolver->getBrowserPath($path, $filter); + return $this->container->get('imagine.cache.path.resolver')->getBrowserPath($path, $filter); } /**