-
Notifications
You must be signed in to change notification settings - Fork 379
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
Changes from all commits
a628969
258bf9f
a913fd3
5542b0f
d3046a2
1c86ebe
ee09bc4
1a01770
6c7a4f5
25effc0
4606a93
e5f94fc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
{ | ||
/** | ||
* Resource path | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
{ | ||
|
@@ -31,6 +35,11 @@ class CacheManager | |
*/ | ||
protected $uriSigner; | ||
|
||
/** | ||
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface | ||
*/ | ||
protected $dispatcher; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add docblock here please |
||
|
||
/** | ||
* @var string | ||
*/ | ||
|
@@ -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'; | ||
} | ||
|
||
|
@@ -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(); | ||
} | ||
|
||
/** | ||
|
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() | ||
{ | ||
} | ||
} |
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()); | ||
} | ||
} |
There was a problem hiding this comment.
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