Skip to content

Commit 9ad85ed

Browse files
committed
added ability to resolve the real path to the cache path resolver
1 parent 4b47493 commit 9ad85ed

File tree

4 files changed

+80
-27
lines changed

4 files changed

+80
-27
lines changed

Imagine/CachePathResolver.php

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,46 @@
33
namespace Avalanche\Bundle\ImagineBundle\Imagine;
44

55
use Symfony\Component\HttpFoundation\Request;
6+
use Symfony\Component\HttpFoundation\Response;
67
use Symfony\Component\Routing\RouterInterface;
8+
use Symfony\Component\HttpKernel\Util\Filesystem;
79

810
class CachePathResolver
911
{
1012
/**
11-
* @var string
13+
* @var Symfony\Component\HttpFoundation\Request
1214
*/
13-
private $webRoot;
15+
private $request;
1416

1517
/**
1618
* @var Symfony\Component\Routing\RouterInterface
1719
*/
1820
private $router;
1921

22+
/**
23+
* @var Symfony\Component\HttpKernel\Util\Filesystem
24+
*/
25+
private $filesystem;
26+
27+
/**
28+
* @var string
29+
*/
30+
private $webRoot;
31+
2032
/**
2133
* Constructs cache path resolver with a given web root and cache prefix
2234
*
23-
* @param string $webRoot
24-
* @param Symfony\Component\Routing\RouterInterface $router
35+
* @param Symfony\Component\HttpFoundation\Request $request
36+
* @param Symfony\Component\Routing\RouterInterface $router
37+
* @param Symfony\Component\HttpKernel\Util\Filesystem $filesystem
38+
* @param string $webRoot
2539
*/
26-
public function __construct($webRoot, RouterInterface $router)
40+
public function __construct(Request $request, RouterInterface $router, Filesystem $filesystem, $webRoot)
2741
{
28-
$this->webRoot = $webRoot;
29-
$this->router = $router;
42+
$this->request = $request;
43+
$this->router = $router;
44+
$this->filesystem = $filesystem;
45+
$this->webRoot = $webRoot;
3046
}
3147

3248
/**
@@ -55,4 +71,39 @@ public function getBrowserPath($path, $filter)
5571

5672
return $path;
5773
}
74+
75+
public function resolve($path, $filter)
76+
{
77+
//TODO: find out why I need double urldecode to get a valid path
78+
$browserPath = urldecode(urldecode($this->getBrowserPath($path, $filter)));
79+
$basePath = $this->request->getBaseUrl();
80+
81+
if (!empty($basePath) && 0 === strpos($browserPath, $basePath)) {
82+
$browserPath = substr($browserPath, strlen($basePath));
83+
}
84+
85+
// if cache path cannot be determined, return 404
86+
if (null === $browserPath) {
87+
return false;
88+
}
89+
90+
$realPath = $this->webRoot.$browserPath;
91+
92+
// if the file has already been cached, we're probably not rewriting
93+
// correctly, hence make a 301 to proper location, so browser remembers
94+
if (file_exists($realPath)) {
95+
return new Response('', 301, array(
96+
'location' => $this->request->getBasePath().$browserPath
97+
));
98+
}
99+
100+
$dir = pathinfo($realPath, PATHINFO_DIRNAME);
101+
if (!is_dir($dir) && !$this->filesystem->mkdir($dir)) {
102+
throw new \RuntimeException(sprintf(
103+
'Could not create directory %s', $dir
104+
));
105+
}
106+
107+
return $realPath;
108+
}
58109
}

Resources/config/imagine.xml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,11 @@
4545

4646
<!-- Utility services -->
4747

48-
<service id="imagine.cache.path.resolver" class="%imagine.cache.path.resolver.class%">
49-
<argument>%imagine.web_root%</argument>
48+
<service id="imagine.cache.path.resolver" class="%imagine.cache.path.resolver.class%" scope="request">
49+
<argument type="service" id="request" />
5050
<argument type="service" id="router" />
51+
<argument type="service" id="filesystem" />
52+
<argument>%imagine.web_root%</argument>
5153
</service>
5254

5355
<service id="imagine.filter.manager" class="%imagine.filter.manager.class%">
@@ -87,12 +89,12 @@
8789

8890
<service id="imagine.twig.extension" class="%imagine.twig.extension.class%" public="false">
8991
<tag name="twig.extension" />
90-
<argument type="service" id="imagine.cache.path.resolver" />
92+
<argument type="service" id="service_container" />
9193
</service>
9294

9395
<service id="imagine.templating.helper" class="%imagine.templating.helper.class%">
9496
<tag name="templating.helper" alias="imagine" />
95-
<argument type="service" id="imagine.cache.path.resolver" />
97+
<argument type="service" id="service_container" />
9698
</service>
9799

98100
<!-- Filter loaders -->

Templating/Helper/ImagineHelper.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@
22

33
namespace Avalanche\Bundle\ImagineBundle\Templating\Helper;
44

5-
use Avalanche\Bundle\ImagineBundle\Imagine\CachePathResolver;
5+
use Symfony\Component\DependencyInjection\ContainerInterface;
66
use Symfony\Component\Templating\Helper\Helper;
77

88
class ImagineHelper extends Helper
99
{
1010
/**
11-
* @var Avalanche\Bundle\ImagineBundle\Imagine\CachePathResolver
11+
* @var Symfony\Component\DependencyInjection\ContainerInterface
1212
*/
13-
private $cachePathResolver;
13+
private $container;
1414

1515
/**
16-
* Constructs by setting $cachePathResolver
16+
* Constructs by setting $container
1717
*
18-
* @param Avalanche\Bundle\ImagineBundle\Imagine\CachePathResolver $cachePathResolver
18+
* @param Symfony\Component\DependencyInjection\ContainerInterface $container
1919
*/
20-
public function __construct(CachePathResolver $cachePathResolver)
20+
public function __construct(ContainerInterface $container)
2121
{
22-
$this->cachePathResolver = $cachePathResolver;
22+
$this->container = $container;
2323
}
2424

2525
/**
@@ -32,7 +32,7 @@ public function __construct(CachePathResolver $cachePathResolver)
3232
*/
3333
public function filter($path, $filter)
3434
{
35-
return $this->cachePathResolver->getBrowserPath($path, $filter);
35+
return $this->container->get('imagine.cache.path.resolver')->getBrowserPath($path, $filter);
3636
}
3737

3838
/**

Templating/ImagineExtension.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@
22

33
namespace Avalanche\Bundle\ImagineBundle\Templating;
44

5-
use Avalanche\Bundle\ImagineBundle\Imagine\CachePathResolver;
5+
use Symfony\Component\DependencyInjection\ContainerInterface;
66
use Symfony\Component\HttpKernel\Util\Filesystem;
77

88
class ImagineExtension extends \Twig_Extension
99
{
1010
/**
11-
* @var Avalanche\Bundle\ImagineBundle\Imagine\CachePathResolver
11+
* @var Symfony\Component\DependencyInjection\ContainerInterface
1212
*/
13-
private $cachePathResolver;
13+
private $container;
1414

1515
/**
16-
* Constructs by setting $cachePathResolver
16+
* Constructs by setting $container
1717
*
18-
* @param Avalanche\Bundle\ImagineBundle\Imagine\CachePathResolver $cachePathResolver
18+
* @param Symfony\Component\DependencyInjection\ContainerInterface $container
1919
*/
20-
public function __construct(CachePathResolver $cachePathResolver)
20+
public function __construct(ContainerInterface $container)
2121
{
22-
$this->cachePathResolver = $cachePathResolver;
22+
$this->container = $container;
2323
}
2424

2525
/**
@@ -43,7 +43,7 @@ public function getFilters()
4343
*/
4444
public function applyFilter($path, $filter)
4545
{
46-
return $this->cachePathResolver->getBrowserPath($path, $filter);
46+
return $this->container->get('imagine.cache.path.resolver')->getBrowserPath($path, $filter);
4747
}
4848

4949
/**

0 commit comments

Comments
 (0)