Skip to content

Commit

Permalink
Revert "move factories into theirs own container"
Browse files Browse the repository at this point in the history
This reverts commit c7a9bc2.
  • Loading branch information
Ondřej Ešler committed Jul 10, 2020
1 parent c7a9bc2 commit 99361b9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 55 deletions.
37 changes: 19 additions & 18 deletions src/ServiceContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,23 @@

namespace IW;

use IW\ServiceContainer\AliasFactory;
use IW\ServiceContainer\CallableFactory;
use IW\ServiceContainer\ClassnameFactory;
use IW\ServiceContainer\EmptyResultFromFactory;
use IW\ServiceContainer\FactoryContainer;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Throwable;

class ServiceContainer implements ContainerInterface
{
/** @var FactoryContainer */
private $factories;
/** @var callable[] */
private $factories = [];

/** @var mixed[] */
private $instances = [];

public function __construct()
{
$this->factories = new FactoryContainer();
}

/**
* Sets an alias for a dependency, it's useful for binding implementations
* to a interface. Container will resolve alias as late as possible.
Expand All @@ -43,7 +40,7 @@ public function __construct()
*/
public function alias(string $alias, string $id): void
{
$this->factories->alias($alias, $id);
$this->factories[$alias] = new AliasFactory($id);
}

/**
Expand All @@ -58,27 +55,32 @@ public function alias(string $alias, string $id): void
*/
public function bind(string $id, callable $factory): void
{
$this->factories->bind($id, $factory);
$this->factories[$id] = new CallableFactory($factory);
}

public function factory(string $id): callable
{
return $this->factories->get($id);
if (isset($this->factories[$id])) {
return $this->factories[$id];
}

return $this->factories[$id] = new ClassnameFactory($id);
}

/**
* Finds an entry of the container by its identifier and returns it.
*
* @template T
* @param class-string<T> $id Identifier of the entry to look for.
*
* @return T
* @return mixed Entry.
*
* @throws NotFoundExceptionInterface No entry was found for **this** identifier.
* @throws ContainerExceptionInterface Error while retrieving the entry.
*
* @template T
* @return T
*/
public function get($id) // phpcs:ignore SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint,SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint,Generic.Files.LineLength.TooLong
public function get($id) // phpcs:ignore SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
{
if (isset($this->instances[$id])) {
return $this->instances[$id]; // try load a singleton if saved
Expand Down Expand Up @@ -108,7 +110,7 @@ public function has($id) // phpcs:ignore SlevomatCodingStandard.TypeHints.Parame
}

// a factory exists
if ($this->factories->has($id)) {
if (isset($this->factories[$id])) {
return true;
}

Expand All @@ -135,13 +137,12 @@ public function singleton(string $id)
/**
* Makes a new instance of a service. Dependencies are resolved from the container.
*
* @template T
* @param class-string<T> $id ID of entry we want to create new instance of
*
* @return T
*
* @template T
*/
public function make(string $id) // phpcs:ignore SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint,Generic.Files.LineLength.TooLong
public function make(string $id)
{
$instance = $this->factory($id)($this);

Expand Down
35 changes: 0 additions & 35 deletions src/ServiceContainer/FactoryContainer.php

This file was deleted.

4 changes: 2 additions & 2 deletions tests/ServiceContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function testBindingCustomFactory(): void
*/
public function testHasMethod(string $id, bool $has): void
{
$container = $this->createTestProxy(ServiceContainer::class, ['make']);
$container = $this->createPartialMock(ServiceContainer::class, ['make']);
$container->expects($this->never())->method('make');

if ($has) {
Expand All @@ -131,7 +131,7 @@ public function testHasASingleton(): void

public function testHasAFactory(): void
{
$container = $this->createTestProxy(ServiceContainer::class, ['make', 'factory']);
$container = $this->createPartialMock(ServiceContainer::class, ['make', 'factory']);
$container->expects($this->never())->method('make');
$container->expects($this->never())->method('factory');

Expand Down

0 comments on commit 99361b9

Please sign in to comment.