PHP7 Registry

This commit is contained in:
Dominik Liebler
2016-09-23 10:47:24 +02:00
parent 281714cd58
commit 46e8d3cbe6
3 changed files with 49 additions and 22 deletions

View File

@ -6,7 +6,8 @@ Purpose
To implement a central storage for objects often used throughout the
application, is typically implemented using an abstract class with only
static methods (or using the Singleton pattern)
static methods (or using the Singleton pattern). Remember that this introduces
global state, which should be avoided at all times! Instead implement it using Dependency Injection!
Examples
--------

View File

@ -2,46 +2,51 @@
namespace DesignPatterns\Structural\Registry;
/**
* class Registry.
*/
abstract class Registry
{
const LOGGER = 'logger';
/**
* this introduces global state in your application which can not be mocked up for testing
* and is therefor considered an anti-pattern! Use dependency injection instead!
*
* @var array
*/
protected static $storedValues = array();
private static $storedValues = [];
/**
* @var array
*/
private static $allowedKeys = [
self::LOGGER,
];
/**
* sets a value.
*
* @param string $key
* @param mixed $value
*
* @static
*
* @return void
*/
public static function set($key, $value)
public static function set(string $key, $value)
{
if (!in_array($key, self::$allowedKeys)) {
throw new \InvalidArgumentException('Invalid key given');
}
self::$storedValues[$key] = $value;
}
/**
* gets a value from the registry.
*
* @param string $key
*
* @static
*
* @return mixed
*/
public static function get($key)
public static function get(string $key)
{
if (!in_array($key, self::$allowedKeys) || !isset(self::$storedValues[$key])) {
throw new \InvalidArgumentException('Invalid key given');
}
return self::$storedValues[$key];
}
// typically there would be methods to check if a key has already been registered and so on ...
}

View File

@ -9,12 +9,33 @@ class RegistryTest extends \PHPUnit_Framework_TestCase
public function testSetAndGetLogger()
{
$key = Registry::LOGGER;
$object = new \StdClass();
$logger = new \stdClass();
Registry::set($key, $object);
$actual = Registry::get($key);
Registry::set($key, $logger);
$storedLogger = Registry::get($key);
$this->assertEquals($object, $actual);
$this->assertInstanceOf('StdClass', $actual);
$this->assertSame($logger, $storedLogger);
$this->assertInstanceOf('stdClass', $storedLogger);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testThrowsExceptionWhenTryingToSetInvalidKey()
{
Registry::set('foobar', new \stdClass());
}
/**
* notice @runInSeparateProcess here: without it, a previous test might have set it already and
* testing would not be possible. That's why you should implement Dependency Injection where an
* injected class may easily be replaced by a mockup
*
* @runInSeparateProcess
* @expectedException \InvalidArgumentException
*/
public function testThrowsExceptionWhenTryingToGetNotSetKey()
{
Registry::get(Registry::LOGGER);
}
}