Commit 9b17f88d authored by Josh Pollock's avatar Josh Pollock
Browse files

Added lazy-loaded singletons

parent ec7f9876
This diff is collapsed.
...@@ -80,6 +80,19 @@ class ServiceContainerTest extends TestCase ...@@ -80,6 +80,19 @@ class ServiceContainerTest extends TestCase
$container->singleton( $classRef, new \calderawp\CalderaContainers\Tests\Mocks\Something()); $container->singleton( $classRef, new \calderawp\CalderaContainers\Tests\Mocks\Something());
$this->assertSame( $container->make($classRef), $container->make($classRef)); $this->assertSame( $container->make($classRef), $container->make($classRef));
}
} /**
* Test that we can use a function to create a lazy-loaded singleton
*/
public function testLazySingleton()
{
$container = new \calderawp\CalderaContainers\Service\Container();
$container->singleton( 'X', function (){
$x = new stdClass();
$x->sivan = 'Roy';
return $x;
});
$this->assertSame( $container->make('X'), $container->make('X' ) );
}
} }
\ No newline at end of file
{ {
"name": "calderawp/caldera-containers", "name": "calderawp/caldera-containers",
"type": "libary", "type": "libary",
"license": "GPL-2.0", "license": "GPL-2.0",
"authors": [ "authors": [
{ {
"name": "josh", "name": "josh",
"email": "josh@calderawp.com" "email": "josh@calderawp.com"
} }
], ],
"minimum-stability": "dev", "minimum-stability": "dev",
"require": { "require": {
"php": "^5.6|^7.0", "php": "^5.6|^7.0",
"pimple/pimple": "^3.2", "pimple/pimple": "^3.2",
"psr/container": "^1.0@dev" "psr/container": "^1.0@dev"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"calderawp\\CalderaContainers\\": "src" "calderawp\\CalderaContainers\\": "src"
} }
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "5.0.*", "phpunit/phpunit": "5.0.*",
"squizlabs/php_codesniffer": "^3.2", "squizlabs/php_codesniffer": "^3.2",
"jakub-onderka/php-parallel-lint": "^1.0" "jakub-onderka/php-parallel-lint": "^1.0"
}, },
"autoload-dev": { "autoload-dev": {
"psr-4": { "psr-4": {
"calderawp\\CalderaContainers\\Tests\\": "Tests" "calderawp\\CalderaContainers\\Tests\\": "Tests"
} }
}, },
"scripts": {
"scripts" : { "tests": "composer lints && composer fixes && composer test",
"tests" : "composer lints && composer fixes && composer test", "test": "phpunit Tests",
"test" : "phpunit Tests", "tests-now": "phpunit Tests --group=now",
"tests-now" : "phpunit Tests --group=now", "sniffs": "phpcs src/",
"sniffs" : "phpcs src/", "fixes": "phpcbf src/",
"fixes" : "phpcbf src/", "lints": "parallel-lint ./src --blame --exclude vendor && parallel-lint ./Tests --blame --exclude vendor"
"lints" : "parallel-lint ./src --blame --exclude vendor && parallel-lint ./Tests --blame --exclude vendor" }
}
} }
...@@ -18,6 +18,11 @@ use calderawp\CalderaContainers\Interfaces\ServiceContainer; ...@@ -18,6 +18,11 @@ use calderawp\CalderaContainers\Interfaces\ServiceContainer;
class Container implements ServiceContainer class Container implements ServiceContainer
{ {
/**
* @var array
*/
protected $unBoundSingletons = [];
/** /**
* @var ProvidesService[] * @var ProvidesService[]
*/ */
...@@ -42,9 +47,15 @@ class Container implements ServiceContainer ...@@ -42,9 +47,15 @@ class Container implements ServiceContainer
/** @inheritdoc */ /** @inheritdoc */
public function make($alias) public function make($alias)
{ {
if( $this->isUnBoundSingleton( $alias ) ){
$binding = $this->unBoundSingletons[$alias];
$this->singleton($alias, $binding() );
}
if (! isset($this->services[$alias])) { if (! isset($this->services[$alias])) {
return $this->resolve($alias); return $this->resolve($alias);
} }
if (is_callable($this->services[$alias])) { if (is_callable($this->services[$alias])) {
return call_user_func_array($this->services[$alias], array($this)); return call_user_func_array($this->services[$alias], array($this));
} }
...@@ -60,10 +71,23 @@ class Container implements ServiceContainer ...@@ -60,10 +71,23 @@ class Container implements ServiceContainer
return $this->resolve($alias); return $this->resolve($alias);
} }
private function isUnBoundSingleton($alias)
{
return ! empty( $this->unBoundSingletons ) && array_key_exists( $alias, $this->unBoundSingletons );
}
/** @inheritdoc */ /** @inheritdoc */
public function singleton($alias, $binding) public function singleton($alias, $binding)
{ {
$this->services[$alias] = $binding; if( is_callable( $binding ) ){
$this->unBoundSingletons[ $alias ] = $binding;
}else{
if( $this->isUnBoundSingleton( $alias ) ){
unset( $this->unBoundSingletons[$alias]);
}
$this->services[$alias] = $binding;
}
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment