Created
September 16, 2014 10:40
-
-
Save nojimage/dc325069b4c79265daf8 to your computer and use it in GitHub Desktop.
ServiceRegistry for CakePHP 3.x
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
<?php | |
namespace App\Service\Exception; | |
use Cake\Core\Exception\Exception; | |
class MissingServiceException extends Exception { | |
protected $_messageTemplate = 'Service class %s could not be found.'; | |
} |
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
<?php | |
namespace App\Service; | |
use Cake\Core\App; | |
use Cake\Core\ObjectRegistry; | |
use App\Service\Exception\MissingServiceException; | |
class ServiceRegistry extends ObjectRegistry { | |
protected static $_instances = []; | |
/** | |
* | |
* @return ServiceRegistry | |
*/ | |
public static function getInstance() { | |
$key = get_called_class(); | |
if (!isset(static::$_instances[$key])) { | |
static::$_instances[$key] = new static(); | |
} | |
return static::$_instances[$key]; | |
} | |
public static function get($objectName, $config = []) { | |
return static::getInstance()->load($objectName, $config); | |
} | |
public static function register($objectName, $object) { | |
return static::getInstance()->set($objectName, $object); | |
} | |
public static function unregister($objectName) { | |
return static::getInstance()->unload($objectName); | |
} | |
public static function clear() { | |
return static::getInstance()->reset(); | |
} | |
/** | |
* Create the service instance. | |
* | |
* Part of the template method for Cake\Core\ObjectRegistry::load() | |
* | |
* @param string $class The classname to create. | |
* @param string $alias The alias of the service. | |
* @param array $config An array of settings to use for the service. | |
* @return mixed The constructed service class. | |
*/ | |
protected function _create($class, $alias, $config) { | |
return new $class($config); | |
} | |
protected function _resolveClassName($class) { | |
return App::className($class, 'Service'); | |
} | |
/** | |
* | |
* @param string $class | |
* @param string $plugin | |
* @throws MissingServiceException | |
*/ | |
protected function _throwMissingClassError($class, $plugin) { | |
throw new MissingServiceException([ | |
'class' => $class, | |
'plugin' => $plugin | |
]); | |
} | |
} |
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
<?php | |
namespace App\Service; | |
use Cake\TestSuite\TestCase; | |
use App\Service\ServiceRegistry; | |
class ServuceRegistryTestService { | |
} | |
class ServuceRegistryTestAlterService { | |
} | |
class ServiceRegistryTest extends TestCase { | |
/** | |
* @var ServiceRegistry | |
*/ | |
protected $services; | |
public function setUp() { | |
parent::setUp(); | |
$this->services = new ServiceRegistry; | |
} | |
public function tearDown() { | |
unset($this->services); | |
ServiceRegistry::clear(); | |
parent::tearDown(); | |
} | |
/** | |
* test triggering callbacks on loaded services | |
* | |
* @return void | |
*/ | |
public function testLoad() { | |
$result = $this->services->load('ServuceRegistryTestService'); | |
$this->assertInstanceOf('App\Service\ServuceRegistryTestService', $result); | |
$this->assertInstanceOf('App\Service\ServuceRegistryTestService', $this->services->ServuceRegistryTestService); | |
$result = $this->services->loaded(); | |
$this->assertEquals(['ServuceRegistryTestService'], $result, 'loaded() results are wrong.'); | |
} | |
/** | |
* test missingtask exception | |
* | |
* @expectedException \App\Service\Exception\MissingServiceException | |
* @return void | |
*/ | |
public function testLoadMissingService() { | |
$this->services->load('ThisServiceShouldAlwaysBeMissing'); | |
} | |
/** | |
* Tests loading as an alias | |
* | |
* @return void | |
*/ | |
public function testLoadWithAlias() { | |
$result = $this->services->load('ServiceAliased', array('className' => 'ServuceRegistryTestService')); | |
$this->assertInstanceOf('App\Service\ServuceRegistryTestService', $result); | |
$this->assertInstanceOf('App\Service\ServuceRegistryTestService', $this->services->ServiceAliased); | |
$result = $this->services->loaded(); | |
$this->assertEquals(array('ServiceAliased'), $result, 'loaded() results are wrong.'); | |
} | |
/** | |
* test triggering callbacks on loaded services | |
* | |
* @return void | |
*/ | |
public function testGet() { | |
$result = ServiceRegistry::get('ServuceRegistryTestService'); | |
$this->assertInstanceOf('App\Service\ServuceRegistryTestService', $result); | |
$this->assertInstanceOf('App\Service\ServuceRegistryTestService', ServiceRegistry::getInstance()->ServuceRegistryTestService); | |
$result = ServiceRegistry::getInstance()->loaded(); | |
$this->assertEquals(['ServuceRegistryTestService'], $result, 'loaded() results are wrong.'); | |
} | |
/** | |
* test triggering callbacks on loaded services | |
* | |
* @return void | |
*/ | |
public function testRegister() { | |
ServiceRegistry::register('ServuceRegistryTestService', new ServuceRegistryTestAlterService); | |
$this->assertInstanceOf('App\Service\ServuceRegistryTestAlterService', ServiceRegistry::getInstance()->ServuceRegistryTestService); | |
$result = ServiceRegistry::getInstance()->loaded(); | |
$this->assertEquals(['ServuceRegistryTestService'], $result, 'loaded() results are wrong.'); | |
} | |
/** | |
* test overload services | |
* | |
* @return void | |
*/ | |
public function testOverload() { | |
$result = ServiceRegistry::get('ServuceRegistryTestService'); | |
$this->assertInstanceOf('App\Service\ServuceRegistryTestService', $result); | |
$this->assertInstanceOf('App\Service\ServuceRegistryTestService', ServiceRegistry::getInstance()->ServuceRegistryTestService); | |
// overload | |
ServiceRegistry::register('ServuceRegistryTestService', new ServuceRegistryTestAlterService); | |
$this->assertInstanceOf('App\Service\ServuceRegistryTestAlterService', ServiceRegistry::getInstance()->ServuceRegistryTestService); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
フレームワーク側で汎用的なDIコンテナ用意して欲しぃ。。