Skip to content

Commit

Permalink
Merge 3adc708 into b84e946
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Jan 6, 2017
2 parents b84e946 + 3adc708 commit 26dd05e
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 118 deletions.
7 changes: 6 additions & 1 deletion src/DI/Cache/ArrayCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ class ArrayCache implements Cache, FlushableCache, ClearableCache

public function fetch($id)
{
return $this->contains($id) ? $this->data[$id] : false;
// isset() is required for performance optimizations, to avoid unnecessary function calls to array_key_exists.
if (isset($this->data[$id]) || array_key_exists($id, $this->data)) {
return $this->data[$id];
}

return false;
}

public function contains($id)
Expand Down
9 changes: 1 addition & 8 deletions src/DI/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,8 @@ public function __construct(
*/
public function get($name)
{
if (! is_string($name)) {
throw new InvalidArgumentException(sprintf(
'The name parameter must be of type string, %s given',
is_object($name) ? get_class($name) : gettype($name)
));
}

// Try to find the entry in the singleton map
if (array_key_exists($name, $this->singletonEntries)) {
if (isset($this->singletonEntries[$name]) || array_key_exists($name, $this->singletonEntries)) {
return $this->singletonEntries[$name];
}

Expand Down
62 changes: 35 additions & 27 deletions src/DI/Definition/Resolver/ObjectCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,25 @@ function (& $wrappedObject, $proxy, $method, $params, & $initializer) use ($defi
*/
private function createInstance(ObjectDefinition $definition, array $parameters)
{
$this->assertClassExists($definition);
// Check that the class is instantiable
if (! $definition->isInstantiable()) {
// Check that the class exists
if (! $definition->classExists()) {
throw DefinitionException::create($definition, sprintf(
'Entry "%s" cannot be resolved: the class doesn\'t exist',
$definition->getName()
));
}

throw DefinitionException::create($definition, sprintf(
'Entry "%s" cannot be resolved: the class is not instantiable',
$definition->getName()
));
}

$classname = $definition->getClassName();
$classReflection = new ReflectionClass($classname);

$this->assertClassIsInstantiable($definition);

$constructorInjection = $definition->getConstructorInjection();

try {
Expand All @@ -134,10 +146,26 @@ private function createInstance(ObjectDefinition $definition, array $parameters)
$parameters
);

if (count($args) > 0) {
$object = $classReflection->newInstanceArgs($args);
} else {
$object = new $classname;
// Optimization trick
switch (count($args)) {
case 0:
$object = new $classname;
break;
case 1:
$object = new $classname($args[0]);
break;
case 2:
$object = new $classname($args[0], $args[1]);
break;
case 3:
$object = new $classname($args[0], $args[1], $args[2]);
break;
case 4:
$object = new $classname($args[0], $args[1], $args[2], $args[3]);
break;
default:
$object = $classReflection->newInstanceArgs($args);
break;
}

$this->injectMethodsAndProperties($object, $definition);
Expand Down Expand Up @@ -224,24 +252,4 @@ private function injectProperty($object, PropertyInjection $propertyInjection)
}
$property->setValue($object, $value);
}

private function assertClassExists(ObjectDefinition $definition)
{
if (! $definition->classExists()) {
throw DefinitionException::create($definition, sprintf(
'Entry "%s" cannot be resolved: the class doesn\'t exist',
$definition->getName()
));
}
}

private function assertClassIsInstantiable(ObjectDefinition $definition)
{
if (! $definition->isInstantiable()) {
throw DefinitionException::create($definition, sprintf(
'Entry "%s" cannot be resolved: the class is not instantiable',
$definition->getName()
));
}
}
}
16 changes: 10 additions & 6 deletions src/DI/Definition/Resolver/ResolverDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public function __construct(ContainerInterface $container, ProxyFactory $proxyFa
*/
public function resolve(Definition $definition, array $parameters = [])
{
// Special case, tested early for speed
if ($definition instanceof \DI\Definition\SelfResolvingDefinition) {
return $definition->resolve($this->container);
}

$definitionResolver = $this->getDefinitionResolver($definition);

return $definitionResolver->resolve($definition, $parameters);
Expand All @@ -68,6 +73,11 @@ public function resolve(Definition $definition, array $parameters = [])
*/
public function isResolvable(Definition $definition, array $parameters = [])
{
// Special case, tested early for speed
if ($definition instanceof \DI\Definition\SelfResolvingDefinition) {
return $definition->isResolvable($this->container);
}

$definitionResolver = $this->getDefinitionResolver($definition);

return $definitionResolver->isResolvable($definition, $parameters);
Expand All @@ -84,12 +94,6 @@ public function isResolvable(Definition $definition, array $parameters = [])
private function getDefinitionResolver(Definition $definition)
{
switch (true) {
case $definition instanceof \DI\Definition\SelfResolvingDefinition:
if (! $this->selfResolvingResolver) {
$this->selfResolvingResolver = new SelfResolver($this->container);
}

return $this->selfResolvingResolver;
case $definition instanceof \DI\Definition\ObjectDefinition:
if (! $this->objectResolver) {
$this->objectResolver = new ObjectCreator($this, $this->proxyFactory);
Expand Down
46 changes: 0 additions & 46 deletions src/DI/Definition/Resolver/SelfResolver.php

This file was deleted.

11 changes: 11 additions & 0 deletions tests/UnitTest/Definition/Resolver/ResolverDispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,15 @@ public function should_throw_if_non_handled_definition()
{
$this->resolver->resolve($this->easyMock(Definition::class));
}

/**
* @test
*/
public function should_resolve_definitions()
{
$definition = new ValueDefinition('foo', 'bar');

$this->assertTrue($this->resolver->isResolvable($definition));
$this->assertEquals('bar', $this->resolver->resolve($definition));
}
}
30 changes: 0 additions & 30 deletions tests/UnitTest/Definition/Resolver/SelfResolverTest.php

This file was deleted.

0 comments on commit 26dd05e

Please sign in to comment.