Changed Container to Illuminate/Container
@see https://laravel.com/docs/5.5/container @see https://davejamesmiller.com/2017/06/15/laravel-illuminate-container-in-depth
This commit is contained in:
parent
945fcb079a
commit
212760d4c9
|
@ -15,11 +15,12 @@
|
|||
],
|
||||
"require": {
|
||||
"php": ">=7.0.0",
|
||||
"erusev/parsedown": "1.6.*",
|
||||
"twbs/bootstrap": "^3.3",
|
||||
"symfony/http-foundation": "^3.3",
|
||||
"erusev/parsedown": "^1.6",
|
||||
"illuminate/container": "^5.*",
|
||||
"psr/container": "^1.0",
|
||||
"psr/log": "^1.0"
|
||||
"psr/log": "^1.0",
|
||||
"symfony/http-foundation": "^3.3",
|
||||
"twbs/bootstrap": "^3.3"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^6.3"
|
||||
|
|
|
@ -106,8 +106,8 @@ Db::getPdo()->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
|
|||
*/
|
||||
$logger = new EngelsystemLogger();
|
||||
$app->instance('logger', $logger);
|
||||
$app->instance(LoggerInterface::class, $logger);
|
||||
$app->instance(EngelsystemLogger::class, $logger);
|
||||
$app->bind(LoggerInterface::class, 'logger');
|
||||
$app->bind(EngelsystemLogger::class, 'logger');
|
||||
|
||||
|
||||
/**
|
||||
|
|
|
@ -14,12 +14,12 @@ class Application extends Container
|
|||
|
||||
protected function registerBaseBindings()
|
||||
{
|
||||
self::setInstance($this);
|
||||
static::setInstance($this);
|
||||
Container::setInstance($this);
|
||||
$this->instance('app', $this);
|
||||
$this->instance('container', $this);
|
||||
$this->instance(Container::class, $this);
|
||||
$this->instance(Application::class, $this);
|
||||
$this->instance(ContainerInterface::class, $this);
|
||||
$this->bind(ContainerInterface::class, Application::class);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,115 +2,9 @@
|
|||
|
||||
namespace Engelsystem\Container;
|
||||
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Illuminate\Container\Container as IlluminateContainer;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
|
||||
class Container implements ContainerInterface
|
||||
class Container extends IlluminateContainer implements ContainerInterface
|
||||
{
|
||||
/**
|
||||
* The globally available container
|
||||
*
|
||||
* @var static
|
||||
*/
|
||||
protected static $instance;
|
||||
|
||||
/**
|
||||
* Contains the shared instances
|
||||
*
|
||||
* @var mixed[]
|
||||
*/
|
||||
protected $instances = [];
|
||||
|
||||
/**
|
||||
* Finds an entry of the container by its identifier and returns it
|
||||
*
|
||||
* @param string $id Identifier of the entry to look for
|
||||
*
|
||||
* @throws NotFoundExceptionInterface No entry was found for **this** identifier
|
||||
* @throws ContainerExceptionInterface Error while retrieving the entry
|
||||
*
|
||||
* @return mixed Entry
|
||||
*/
|
||||
public function get($id)
|
||||
{
|
||||
if ($this->has($id)) {
|
||||
return $this->resolve($id);
|
||||
}
|
||||
|
||||
throw new NotFoundException(sprintf('The entry with the id "%s" could not be found', $id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a shared entry in the container
|
||||
*
|
||||
* @param string $abstract Identifier of the entry to set
|
||||
* @param mixed $instance Entry
|
||||
*/
|
||||
public function instance($abstract, $instance)
|
||||
{
|
||||
$this->singleton($abstract, $instance);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a shared entry as singleton in the container
|
||||
*
|
||||
* @param string $abstract
|
||||
* @param mixed $instance
|
||||
*/
|
||||
public function singleton($abstract, $instance)
|
||||
{
|
||||
$this->instances[$abstract] = $instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the container can return an entry for the given identifier
|
||||
* Returns false otherwise
|
||||
*
|
||||
* `has($id)` returning true does not mean that `get($id)` will not throw an exception
|
||||
* It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`
|
||||
*
|
||||
* @param string $id Identifier of the entry to look for
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function has($id)
|
||||
{
|
||||
return isset($this->instances[$id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the requested object
|
||||
*
|
||||
* @param string $abstract
|
||||
* @return mixed
|
||||
*/
|
||||
protected function resolve($abstract)
|
||||
{
|
||||
return $this->instances[$abstract];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the globally available instance of the container
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public static function getInstance()
|
||||
{
|
||||
if (is_null(static::$instance)) {
|
||||
static::$instance = new static;
|
||||
}
|
||||
|
||||
return static::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the globally available instance of the container
|
||||
*
|
||||
* @param Container $container
|
||||
*/
|
||||
public static function setInstance(Container $container)
|
||||
{
|
||||
static::$instance = $container;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,11 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Engelsystem\Container;
|
||||
|
||||
use Exception;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
|
||||
class ContainerException extends Exception implements ContainerExceptionInterface
|
||||
{
|
||||
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Engelsystem\Container;
|
||||
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
|
||||
class NotFoundException extends ContainerException implements NotFoundExceptionInterface
|
||||
{
|
||||
|
||||
}
|
|
@ -24,6 +24,7 @@ class ApplicationTest extends TestCase
|
|||
$this->assertSame($app, $app->get(Container::class));
|
||||
$this->assertSame($app, $app->get(Application::class));
|
||||
$this->assertSame($app, $app->get(ContainerInterface::class));
|
||||
$this->assertSame($app, Application::getInstance());
|
||||
$this->assertSame($app, Container::getInstance());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,104 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Config;
|
||||
|
||||
use Engelsystem\Container\Container;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ContainerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @covers \Engelsystem\Container\Container::get
|
||||
*/
|
||||
public function testGet()
|
||||
{
|
||||
$container = new Container();
|
||||
$class = new class
|
||||
{
|
||||
};
|
||||
|
||||
$container->instance('foo', $class);
|
||||
$this->assertSame($class, $container->get('foo'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Container\Container::get
|
||||
* @expectedException \Engelsystem\Container\NotFoundException
|
||||
*/
|
||||
public function testGetException()
|
||||
{
|
||||
$container = new Container();
|
||||
|
||||
$container->get('not.registered.service');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Container\Container::instance
|
||||
* @covers \Engelsystem\Container\Container::resolve
|
||||
*/
|
||||
public function testInstance()
|
||||
{
|
||||
$container = new Container();
|
||||
$class = new class
|
||||
{
|
||||
};
|
||||
|
||||
$container->instance('foo', $class);
|
||||
$this->assertSame($class, $container->get('foo'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Container\Container::has
|
||||
*/
|
||||
public function testHas()
|
||||
{
|
||||
$container = new Container();
|
||||
|
||||
$this->assertFalse($container->has('test'));
|
||||
|
||||
$class = new class
|
||||
{
|
||||
};
|
||||
|
||||
$container->instance('test', $class);
|
||||
$this->assertTrue($container->has('test'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Container\Container::singleton
|
||||
*/
|
||||
public function testSingleton()
|
||||
{
|
||||
$container = new Container();
|
||||
$class = new class
|
||||
{
|
||||
};
|
||||
|
||||
$container->singleton('foo', $class);
|
||||
$this->assertSame($class, $container->get('foo'));
|
||||
$this->assertSame($class, $container->get('foo'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Container\Container::setInstance
|
||||
* @covers \Engelsystem\Container\Container::getInstance
|
||||
*/
|
||||
public function testContainerSingleton()
|
||||
{
|
||||
// Ensure that no container has been initialized
|
||||
$reflection = new \ReflectionProperty(Container::class, 'instance');
|
||||
$reflection->setAccessible(true);
|
||||
$reflection->setValue(null, null);
|
||||
$reflection->setAccessible(false);
|
||||
|
||||
$container0 = new Container();
|
||||
$container = Container::getInstance();
|
||||
|
||||
$this->assertNotSame($container0, $container);
|
||||
|
||||
$container1 = new Container;
|
||||
Container::setInstance($container1);
|
||||
|
||||
$this->assertSame($container1, Container::getInstance());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue