Skip to content

Commit

Permalink
Fixes #409 Remove scopes
Browse files Browse the repository at this point in the history
The goal is to simplify the package for the v6. Scopes are useless: I don't see a valid use case for the "prototype" scope, if needed then it should be replaced by an actual factory.

By removing scopes the API would be simpler as well as the internals of PHP-DI.
  • Loading branch information
mnapoli committed Jun 1, 2016
1 parent 854a6d8 commit 8b7fb34
Show file tree
Hide file tree
Showing 57 changed files with 52 additions and 793 deletions.
4 changes: 4 additions & 0 deletions change-log.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change log

## 6.0

- [#409](https://github.com/PHP-DI/PHP-DI/issues/409): Scopes are removed (by [@mnapoli](https://github.com/mnapoli))

## 5.3

Read the [news entry](news/19-php-di-5-3-released.md).
Expand Down
3 changes: 0 additions & 3 deletions couscous.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,6 @@ menu:
performances:
text: Performances
url: doc/performances.html
scopes:
text: Scopes
url: doc/scopes.html
lazy-injection:
text: Lazy injection
url: doc/lazy-injection.html
Expand Down
1 change: 0 additions & 1 deletion doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ title: Documentation index
### Advanced topics

* [Performances](performances.md)
* [Scopes](scopes.md)
* [Lazy injection](lazy-injection.md)
* [Inject on an existing instance](inject-on-instance.md)
* [Injections depending on the environment](environments.md)
Expand Down
2 changes: 1 addition & 1 deletion doc/annotations.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ The `@Injectable` annotation lets you set options on injectable classes:

```php
/**
* @Injectable(scope="prototype", lazy=true)
* @Injectable(lazy=true)
*/
class Example
{
Expand Down
9 changes: 1 addition & 8 deletions doc/php-definitions.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,14 +277,7 @@ return [
];
```

By default each entry will be created once and the same instance will be injected everywhere it is used (singleton instance). You can use the "prototype" [scope](scopes.md) if you want a new instance to be created every time it is injected:

```php
return [
'FormBuilder' => DI\object()
->scope(Scope::PROTOTYPE),
];
```
Each entry will be resolved once and the same instance will be injected everywhere it is used.

### Aliases

Expand Down
103 changes: 0 additions & 103 deletions doc/scopes.md

This file was deleted.

26 changes: 0 additions & 26 deletions src/DI/Annotation/Injectable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace DI\Annotation;

use DI\Scope;
use UnexpectedValueException;

/**
* "Injectable" annotation.
*
Expand All @@ -18,12 +15,6 @@
*/
final class Injectable
{
/**
* The scope of an class: prototype, singleton.
* @var string|null
*/
private $scope;

/**
* Should the object be lazy-loaded.
* @var bool|null
Expand All @@ -35,28 +26,11 @@ final class Injectable
*/
public function __construct(array $values)
{
if (isset($values['scope'])) {
if ($values['scope'] === 'prototype') {
$this->scope = Scope::PROTOTYPE;
} elseif ($values['scope'] === 'singleton') {
$this->scope = Scope::SINGLETON;
} else {
throw new UnexpectedValueException(sprintf("Value '%s' is not a valid scope", $values['scope']));
}
}
if (isset($values['lazy'])) {
$this->lazy = (bool) $values['lazy'];
}
}

/**
* @return string|null
*/
public function getScope()
{
return $this->scope;
}

/**
* @return bool|null
*/
Expand Down
40 changes: 18 additions & 22 deletions src/DI/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
class Container implements ContainerInterface, FactoryInterface, \DI\InvokerInterface
{
/**
* Map of entries with Singleton scope that are already resolved.
* Map of entries that are already resolved.
* @var array
*/
private $singletonEntries = [];
private $resolvedEntries = [];

/**
* @var DefinitionSource
Expand Down Expand Up @@ -85,9 +85,9 @@ public function __construct(
$this->definitionResolver = new ResolverDispatcher($this->wrapperContainer, $proxyFactory);

// Auto-register the container
$this->singletonEntries[self::class] = $this;
$this->singletonEntries[FactoryInterface::class] = $this;
$this->singletonEntries[InvokerInterface::class] = $this;
$this->resolvedEntries[self::class] = $this;
$this->resolvedEntries[FactoryInterface::class] = $this;
$this->resolvedEntries[InvokerInterface::class] = $this;
}

/**
Expand All @@ -109,9 +109,9 @@ public function get($name)
));
}

// Try to find the entry in the singleton map
if (array_key_exists($name, $this->singletonEntries)) {
return $this->singletonEntries[$name];
// If the entry is already resolved we return it
if (array_key_exists($name, $this->resolvedEntries)) {
return $this->resolvedEntries[$name];
}

$definition = $this->definitionSource->getDefinition($name);
Expand All @@ -121,20 +121,16 @@ public function get($name)

$value = $this->resolveDefinition($definition);

// If the entry is singleton, we store it to always return it without recomputing it
if ($definition->getScope() === Scope::SINGLETON) {
$this->singletonEntries[$name] = $value;
}
$this->resolvedEntries[$name] = $value;

return $value;
}

/**
* Build an entry of the container by its name.
*
* This method behave like get() except it forces the scope to "prototype",
* which means the definition of the entry will be re-evaluated each time.
* For example, if the entry is a class, then a new instance will be created each time.
* This method behave like get() except resolves the entry again every time.
* For example if the entry is a class then a new instance will be created each time.
*
* This method makes the container behave like a factory.
*
Expand All @@ -159,9 +155,9 @@ public function make($name, array $parameters = [])

$definition = $this->definitionSource->getDefinition($name);
if (! $definition) {
// Try to find the entry in the singleton map
if (array_key_exists($name, $this->singletonEntries)) {
return $this->singletonEntries[$name];
// If the entry is already resolved we return it
if (array_key_exists($name, $this->resolvedEntries)) {
return $this->resolvedEntries[$name];
}

throw new NotFoundException("No entry or class found for '$name'");
Expand All @@ -187,7 +183,7 @@ public function has($name)
));
}

if (array_key_exists($name, $this->singletonEntries)) {
if (array_key_exists($name, $this->resolvedEntries)) {
return true;
}

Expand Down Expand Up @@ -255,7 +251,7 @@ public function set($name, $value)
if ($value instanceof Definition) {
$this->setDefinition($name, $value);
} else {
$this->singletonEntries[$name] = $value;
$this->resolvedEntries[$name] = $value;
}
}

Expand Down Expand Up @@ -305,8 +301,8 @@ private function setDefinition($name, Definition $definition)
}

// Clear existing entry if it exists
if (array_key_exists($name, $this->singletonEntries)) {
unset($this->singletonEntries[$name]);
if (array_key_exists($name, $this->resolvedEntries)) {
unset($this->resolvedEntries[$name]);
}

$this->definitionSource->addDefinition($definition);
Expand Down
9 changes: 0 additions & 9 deletions src/DI/Definition/AliasDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace DI\Definition;

use DI\Scope;
use Interop\Container\ContainerInterface;

/**
Expand Down Expand Up @@ -42,14 +41,6 @@ public function getName()
return $this->name;
}

/**
* {@inheritdoc}
*/
public function getScope()
{
return Scope::PROTOTYPE;
}

/**
* @return string
*/
Expand Down
9 changes: 0 additions & 9 deletions src/DI/Definition/ArrayDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace DI\Definition;

use DI\Definition\Helper\DefinitionHelper;
use DI\Scope;

/**
* Definition of an array containing values or references.
Expand Down Expand Up @@ -42,14 +41,6 @@ public function getName()
return $this->name;
}

/**
* {@inheritdoc}
*/
public function getScope()
{
return Scope::SINGLETON;
}

/**
* @return array
*/
Expand Down
7 changes: 0 additions & 7 deletions src/DI/Definition/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@ interface Definition extends RequestedEntry
*/
public function getName();

/**
* Returns the scope of the entry.
*
* @return string
*/
public function getScope();

/**
* Definitions can be cast to string for debugging information.
*
Expand Down
3 changes: 0 additions & 3 deletions src/DI/Definition/Dumper/ObjectDefinitionDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ public function dump(ObjectDefinition $definition)
}
$str = sprintf(' class = %s%s', $warning, $className);

// Scope
$str .= PHP_EOL . ' scope = ' . $definition->getScope();

// Lazy
$str .= PHP_EOL . ' lazy = ' . var_export($definition->isLazy(), true);

Expand Down
Loading

0 comments on commit 8b7fb34

Please sign in to comment.