forked from symfony/symfony
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ResourceWatcher] created basic resource event listener object
- Loading branch information
Showing
2 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
src/Symfony/Component/ResourceWatcher/Event/EventListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\ResourceWatcher\Event; | ||
|
||
use Symfony\Component\Config\Resource\ResourceInterface; | ||
|
||
/** | ||
* Resource change listener. | ||
* | ||
* @author Konstantin Kudryashov <ever.zet@gmail.com> | ||
*/ | ||
class EventListener implements EventListenerInterface | ||
{ | ||
private $resource; | ||
private $callback; | ||
private $eventsMask; | ||
|
||
/** | ||
* Initializes listener. | ||
* | ||
* @param ResourceInterface $resource resource to listen | ||
* @param callable $callback callback to call on event | ||
* @param integer $eventsMask event types to listen | ||
*/ | ||
public function __construct(ResourceInterface $resource, $callback, $eventsMask) | ||
{ | ||
if (!is_callable($callback)) { | ||
throw new \InvalidArgumentException( | ||
'EventListener\'s second argument should be callable.' | ||
); | ||
} | ||
|
||
$this->resource = $resource; | ||
$this->callback = $callback; | ||
$this->eventsMask = $eventsMask; | ||
} | ||
|
||
/** | ||
* Returns listening resource. | ||
* | ||
* @return ResourceInterface | ||
*/ | ||
public function getResource() | ||
{ | ||
return $this->resource; | ||
} | ||
|
||
/** | ||
* Returns callback. | ||
* | ||
* @return callable | ||
*/ | ||
public function getCallback() | ||
{ | ||
return $this->callback; | ||
} | ||
|
||
/** | ||
* Checks whether listener can handle provided resource event. | ||
* | ||
* @param Event $event | ||
*/ | ||
public function handles(Event $event) | ||
{ | ||
return 0 !== ($this->eventsMask & $event->getType()); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
tests/Symfony/Tests/Component/ResourceWatcher/Event/EventListenerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Tests\Component\ResourceWatcher\Event; | ||
|
||
use Symfony\Component\Config\Resource\FileResource; | ||
use Symfony\Component\ResourceWatcher\Event\Event; | ||
use Symfony\Component\ResourceWatcher\Event\EventListener; | ||
|
||
class EventListenerTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testConstructAndGetters() | ||
{ | ||
$listener = new EventListener($res = new FileResource(__FILE__), $cb = function(){}, Event::CREATED); | ||
|
||
$this->assertSame($res, $listener->getResource()); | ||
$this->assertSame($cb, $listener->getCallback()); | ||
} | ||
|
||
public function testHandles() | ||
{ | ||
$res = new FileResource(__FILE__); | ||
$cb = function(){}; | ||
|
||
$listener = new EventListener($res, $cb, Event::CREATED); | ||
|
||
$this->assertTrue($listener->handles(new Event(1, $res, $type = Event::CREATED))); | ||
$this->assertFalse($listener->handles(new Event(1, $res, $type = Event::MODIFIED))); | ||
$this->assertFalse($listener->handles(new Event(1, $res, $type = Event::DELETED))); | ||
|
||
$listener = new EventListener($res, $cb, Event::CREATED | Event::DELETED); | ||
|
||
$this->assertTrue($listener->handles(new Event(1, $res, $type = Event::CREATED))); | ||
$this->assertFalse($listener->handles(new Event(1, $res, $type = Event::MODIFIED))); | ||
$this->assertTrue($listener->handles(new Event(1, $res, $type = Event::DELETED))); | ||
|
||
$listener = new EventListener($res, $cb, Event::ALL); | ||
|
||
$this->assertTrue($listener->handles(new Event(1, $res, $type = Event::CREATED))); | ||
$this->assertTrue($listener->handles(new Event(1, $res, $type = Event::MODIFIED))); | ||
$this->assertTrue($listener->handles(new Event(1, $res, $type = Event::DELETED))); | ||
|
||
$listener = new EventListener($res, $cb, Event::DELETED); | ||
|
||
$this->assertFalse($listener->handles(new Event(1, $res, $type = Event::CREATED))); | ||
$this->assertFalse($listener->handles(new Event(1, $res, $type = Event::MODIFIED))); | ||
$this->assertTrue($listener->handles(new Event(1, $res, $type = Event::DELETED))); | ||
} | ||
} |