Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Added resolve events to cache manager #388

Merged
merged 12 commits into from
Apr 10, 2014
44 changes: 44 additions & 0 deletions Events/PostResolveEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
namespace Liip\ImagineBundle\Events;

use Symfony\Component\EventDispatcher\Event;
use Liip\ImagineBundle\Imagine\Cache\Resolver\ResolverInterface;

class PostResolveEvent extends Event
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can have only one event class, let's say CacheResolveEvent. In pre event the url will be null of cource.

You should add setters too to allow modify params in the event.

{
protected $resolver;

protected $path;

protected $filter;

protected $url;

public function __construct(ResolverInterface $resolver, $path, $filter, $url)
{
$this->resolver = $resolver;
$this->path = $path;
$this->filter = $filter;
$this->url = $url;
}

public function getResolver()
{
return $this->path;
}

public function getPath()
{
return $this->path;
}

public function getFilter()
{
return $this->filter;
}

public function getUrl()
{
return $this->url;
}
}
35 changes: 35 additions & 0 deletions Events/PreResolveEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
namespace Liip\ImagineBundle\Events;

use Symfony\Component\EventDispatcher\Event;
use Liip\ImagineBundle\Imagine\Cache\Resolver\ResolverInterface;

class PreResolveEvent extends Event
{
protected $resolver;

protected $path;

protected $filter;

public function __construct(ResolverInterface $resolver, $path, $filter)
{
$this->path = $path;
$this->filter = $filter;
}

public function getResolver()
{
return $this->path;
}

public function getPath()
{
return $this->path;
}

public function getFilter()
{
return $this->filter;
}
}
27 changes: 24 additions & 3 deletions Imagine/Cache/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\UriSigner;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\Event;
use Liip\ImagineBundle\ImagineEvents;
use Liip\ImagineBundle\Events\PreResolveEvent;
use Liip\ImagineBundle\Events\PostResolveEvent;

class CacheManager
{
Expand All @@ -31,6 +36,8 @@ class CacheManager
*/
protected $uriSigner;

protected $dispatcher;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add docblock here please


/**
* @var string
*/
Expand All @@ -44,11 +51,17 @@ class CacheManager
* @param UriSigner $uriSigner
* @param string $defaultResolver
*/
public function __construct(FilterConfiguration $filterConfig, RouterInterface $router, UriSigner $uriSigner, $defaultResolver = null)
{
public function __construct(
FilterConfiguration $filterConfig,
RouterInterface $router,
UriSigner $uriSigner,
EventDispatcherInterface $dispatcher,
$defaultResolver = null
) {
$this->filterConfig = $filterConfig;
$this->router = $router;
$this->uriSigner = $uriSigner;
$this->dispatcher = $dispatcher;
$this->defaultResolver = $defaultResolver ?: 'default';
}

Expand Down Expand Up @@ -176,7 +189,15 @@ public function resolve($path, $filter)
throw new NotFoundHttpException(sprintf("Source image was searched with '%s' outside of the defined root path", $path));
}

return $this->getResolver($filter)->resolve($path, $filter);
$resolver = $this->getResolver($filter);

$this->dispatcher->dispatch(ImagineEvents::PRE_RESOLVE, new PreResolveEvent($resolver, $path, $filter));

$url = $resolver->resolve($path, $filter);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolver, path, amd filter has to be taken from the event


$this->dispatcher->dispatch(ImagineEvents::POST_RESOLVE, new PostResolveEvent($resolver, $path, $filter, $url));

return $url;
}

/**
Expand Down
14 changes: 14 additions & 0 deletions ImagineEvents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
namespace Liip\ImagineBundle;


final class ImagineEvents
{
const PRE_RESOLVE = 'imagine.pre_resolve';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

liip_imagine.xxx


const POST_RESOLVE = 'imagine.post_resolve';

protected function __construct()
{
}
}
1 change: 1 addition & 0 deletions Resources/config/imagine.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
<argument type="service" id="liip_imagine.filter.configuration" />
<argument type="service" id="router" />
<argument type="service" id="liip_imagine.uri_signer" />
<argument type="service" id="event_dispatcher" />
<argument>%liip_imagine.cache.resolver.default%</argument>
</service>

Expand Down