Skip to content

Commit

Permalink
added ability to resolve the real path to the cache path resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
lsmith77 committed Jul 21, 2011
1 parent 4b47493 commit 9ad85ed
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 27 deletions.
65 changes: 58 additions & 7 deletions Imagine/CachePathResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}
}
10 changes: 6 additions & 4 deletions Resources/config/imagine.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@

<!-- Utility services -->

<service id="imagine.cache.path.resolver" class="%imagine.cache.path.resolver.class%">
<argument>%imagine.web_root%</argument>
<service id="imagine.cache.path.resolver" class="%imagine.cache.path.resolver.class%" scope="request">
<argument type="service" id="request" />
<argument type="service" id="router" />
<argument type="service" id="filesystem" />
<argument>%imagine.web_root%</argument>
</service>

<service id="imagine.filter.manager" class="%imagine.filter.manager.class%">
Expand Down Expand Up @@ -87,12 +89,12 @@

<service id="imagine.twig.extension" class="%imagine.twig.extension.class%" public="false">
<tag name="twig.extension" />
<argument type="service" id="imagine.cache.path.resolver" />
<argument type="service" id="service_container" />
</service>

<service id="imagine.templating.helper" class="%imagine.templating.helper.class%">
<tag name="templating.helper" alias="imagine" />
<argument type="service" id="imagine.cache.path.resolver" />
<argument type="service" id="service_container" />
</service>

<!-- Filter loaders -->
Expand Down
16 changes: 8 additions & 8 deletions Templating/Helper/ImagineHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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);
}

/**
Expand Down
16 changes: 8 additions & 8 deletions Templating/ImagineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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);
}

/**
Expand Down

0 comments on commit 9ad85ed

Please sign in to comment.