Skip to content
This repository has been archived by the owner on Mar 28, 2022. It is now read-only.

Commit

Permalink
Add cache injection via parser manager
Browse files Browse the repository at this point in the history
  • Loading branch information
DASPRiD committed Nov 27, 2013
1 parent 39d5c4c commit cc8e13a
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 4 deletions.
25 changes: 25 additions & 0 deletions src/Dash/Router/Http/Parser/CacheAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* Dash
*
* @link http://github.com/DASPRiD/Dash For the canonical source repository
* @copyright 2013 Ben Scholzen 'DASPRiD'
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
*/

namespace Dash\Router\Http\Parser;

use Zend\Cache\Storage\StorageInterface;

/**
* Interface for parsers which are cache aware.
*/
interface CacheAwareInterface
{
/**
* Sets a cache used by the parser.
*
* @param null|StorageInterface $cache
*/
public function setCache(StorageInterface $cache = null);
}
34 changes: 34 additions & 0 deletions src/Dash/Router/Http/Parser/ParserManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,55 @@

namespace Dash\Router\Http\Parser;

use Zend\Cache\Storage\StorageInterface;
use Zend\ServiceManager\AbstractPluginManager;
use Zend\ServiceManager\ConfigInterface;
use Zend\ServiceManager\Exception;

/**
* Default plugin manager for handling parsers.
*/
class ParserManager extends AbstractPluginManager
{
/**
* @var null|StorageInterface
*/
protected $cache;

protected $shareByDefault = false;

protected $factories = [
'pathsegment' => 'Dash\Router\Http\Parser\PathSegmentFactory',
'hostnamesegment' => 'Dash\Router\Http\Parser\HostnameSegmentFactory',
];

public function __construct(ConfigInterface $configuration = null)
{
parent::__construct($configuration);

$this->addInitializer([$this, 'injectCache']);
}

/**
* Sets a cache injected into all cache-aware parsers.
*
* @param StorageInterface $cache
*/
public function setCache(StorageInterface $cache = null)
{
$this->cache = $cache;
}

/**
* @param ParserInterface $plugin
*/
public function injectCache($plugin)
{
if ($plugin instanceof CacheAwareInterface) {
$plugin->setCache($this->cache);
}
}

public function validatePlugin($plugin)
{
if ($plugin instanceof ParserInterface) {
Expand Down
5 changes: 1 addition & 4 deletions src/Dash/Router/Http/Parser/Segment.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Dash\Router\Exception;
use Zend\Cache\Storage\StorageInterface;

class Segment implements ParserInterface
class Segment implements CacheAwareInterface, ParserInterface
{
/**#@+
* Descriptive part elements.
Expand Down Expand Up @@ -75,9 +75,6 @@ public function __construct($delimiter, $pattern, array $constraints)
$this->constraints = $constraints;
}

/**
* @param StorageInterface $cache
*/
public function setCache(StorageInterface $cache = null)
{
$this->cache = $cache;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* Dash
*
* @link http://github.com/DASPRiD/Dash For the canonical source repository
* @copyright 2013 Ben Scholzen 'DASPRiD'
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
*/

namespace DashTest\Router\Http\Parser\Asset;

use Dash\Router\Http\Parser\CacheAwareInterface;
use Dash\Router\Http\Parser\ParserInterface;

/**
* This is a workaround, since stubbing multiple interfaces is not yet in any
* stable tag.
*
* @see https://github.com/sebastianbergmann/phpunit-mock-objects/commit/73e7b0766ad6223e713ac87cfe5d4e211bd18af0
*/
interface CacheAwareParserInterface extends CacheAwareInterface, ParserInterface
{
}
15 changes: 15 additions & 0 deletions tests/DashTest/Router/Http/Parser/ParserManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,19 @@ public function testFailOnInvalidPlugin()
$parserManager = new ParserManager();
$parserManager->validatePlugin(null);
}

public function testInjectCache()
{
$cache = $this->getMock('Zend\Cache\Storage\StorageInterface');
$parser = $this->getMock('DashTest\Router\Http\Parser\Asset\CacheAwareParserInterface');
$parser
->expects($this->once())
->method('setCache')
->with($this->equalTo($cache));

$parserManager = new ParserManager();
$parserManager->setCache($cache);
$parserManager->setFactory('foo', function() use ($parser) { return $parser; });
$parserManager->get('foo');
}
}

0 comments on commit cc8e13a

Please sign in to comment.