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
99 changes: 99 additions & 0 deletions Events/CacheResolveEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php
namespace Liip\ImagineBundle\Events;

use Symfony\Component\EventDispatcher\Event;

class CacheResolveEvent 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.

test that this class is sub class of event.

shouldBeSubClassOfFOSUserUserModel

{
/**
* Resource path
Copy link
Collaborator

Choose a reason for hiding this comment

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

this is useless, please remove such comment

* @var string
*/
protected $path;

/**
* Filter name
* @var string
*/
protected $filter;

/**
* Resource url
* @var null
*/
protected $url;

/**
* Init default event state
*
* @param string $path
* @param string $filter
* @param null|string $url
*/
public function __construct($path, $filter, $url = null)
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

{
$this->path = $path;
$this->filter = $filter;
$this->url = $url;
}

/**
* Sets resource path
*
* @param $path
*/
public function setPath($path)
{
$this->path = $path;
}

/**
* Returns resource path
*
* @return string
*/
public function getPath()
{
return $this->path;
}

/**
* Sets filter name
*
* @param $filter
*/
public function setFilter($filter)
{
$this->filter = $filter;
}

/**
* Returns filter name
*
* @return string
*/
public function getFilter()
{
return $this->filter;
}

/**
* Sets resource url
*
* @param $url
*/
public function setUrl($url)
{
$this->url = $url;
}

/**
* Returns resource url
*
* @return null
*/
public function getUrl()
{
return $this->url;
}
}
29 changes: 26 additions & 3 deletions Imagine/Cache/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
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\CacheResolveEvent;

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

/**
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
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 +53,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 +191,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);
$preEvent = new CacheResolveEvent($path, $filter);
$this->dispatcher->dispatch(ImagineEvents::PRE_RESOLVE, $preEvent);

$url = $this->getResolver($preEvent->getFilter())->resolve($preEvent->getPath(), $preEvent->getFilter());

$postEvent = new CacheResolveEvent($preEvent->getPath(), $preEvent->getFilter(), $url);
$this->dispatcher->dispatch(ImagineEvents::POST_RESOLVE, $postEvent);

return $postEvent->getUrl();
}

/**
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 = 'liip_imagine.pre_resolve';

const POST_RESOLVE = 'liip_imagine.post_resolve';

private 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
6 changes: 6 additions & 0 deletions Tests/AbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Liip\ImagineBundle\Imagine\Filter\FilterConfiguration;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

abstract class AbstractTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -81,6 +82,11 @@ protected function createResolverMock()
return $this->getMock('Liip\ImagineBundle\Imagine\Cache\Resolver\ResolverInterface');
}

protected function createEventDispatcherMock()
{
return $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
}

protected function getMockImage()
{
return $this->getMock('Imagine\Image\ImageInterface');
Expand Down
105 changes: 105 additions & 0 deletions Tests/Events/CacheResolveEventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php
namespace Liip\ImagineBundle\Tests\Events;

use Liip\ImagineBundle\Events\CacheResolveEvent;

/**
* Test class for CacheResolveEvent.
*/
class CacheResolveEventTest extends \PHPUnit_Framework_TestCase
{
protected function setUp()
{

}

public function testShouldAllowSetPathInConstruct()
{
$event = new CacheResolveEvent('default_path', 'default_filter');

$this->assertAttributeEquals('default_path', 'path', $event);
}

public function testShouldAllowSetPathByMethod()
{
$event = new CacheResolveEvent('default_path', 'default_filter');
$event->setPath('new_path');

$this->assertAttributeEquals('new_path', 'path', $event);
}

public function testShouldAllowGetPathWhichWasSetInConstruct()
{
$event = new CacheResolveEvent('default_path', 'default_filter');

$this->assertEquals('default_path', $event->getPath());
}

public function testShouldAllowGetPathWhichWasSetByMethod()
{
$event = new CacheResolveEvent('default_path', 'default_filter');
$event->setPath('new_path');

$this->assertEquals('new_path', $event->getPath());
}

public function testShouldAllowSetFilterInConstruct()
{
$event = new CacheResolveEvent('default_path', 'default_filter');

$this->assertAttributeEquals('default_filter', 'filter', $event);
}

public function testShouldAllowSetFilterByMethod()
{
$event = new CacheResolveEvent('default_path', 'default_filter');
$event->setFilter('new_filter');

$this->assertAttributeEquals('new_filter', 'filter', $event);
}

public function testShouldAllowGetFilterWhichWasSetInConstruct()
{
$event = new CacheResolveEvent('default_path', 'default_filter');

$this->assertEquals('default_filter', $event->getFilter());
}

public function testShouldAllowGetFilterWhichWasSetByMethod()
{
$event = new CacheResolveEvent('default_path', 'default_filter');
$event->setFilter('new_filter');

$this->assertEquals('new_filter', $event->getFilter());
}

public function testShouldAllowSetUrlInConstruct()
{
$event = new CacheResolveEvent('default_path', 'default_filter', 'default_url');

$this->assertAttributeEquals('default_url', 'url', $event);
}

public function testShouldAllowSetUrlByMethod()
{
$event = new CacheResolveEvent('default_path', 'default_filter');
$event->setUrl('new_url');

$this->assertAttributeEquals('new_url', 'url', $event);
}

public function testShouldAllowGetUrlWhichWasSetInConstruct()
{
$event = new CacheResolveEvent('default_path', 'default_filter', 'default_url');

$this->assertEquals('default_url', $event->getUrl());
}

public function testShouldAllowGetUrlWhichWasSetByMethod()
{
$event = new CacheResolveEvent('default_path', 'default_filter');
$event->setUrl('new_url');

$this->assertEquals('new_url', $event->getUrl());
}
}
Loading