Skip to content

Commit

Permalink
Replaced PhpDocParser with mnapoli/PhpDocReader
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Oct 20, 2013
1 parent 3c04c79 commit 90ab878
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 522 deletions.
5 changes: 3 additions & 2 deletions composer.json
Expand Up @@ -16,8 +16,9 @@
"php": ">=5.4.0",
"doctrine/annotations": "1.*",
"doctrine/cache": "1.*",
"mnapoli/phpdocreader": "1.0.*",
"myclabs/php-enum": "1.*",
"symfony/yaml": "2.*",
"ocramius/proxy-manager": "~0.3"
"ocramius/proxy-manager": "~0.3",
"symfony/yaml": "2.*"
}
}
183 changes: 0 additions & 183 deletions src/DI/Definition/Source/Annotation/PhpDocParser.php

This file was deleted.

13 changes: 5 additions & 8 deletions src/DI/Definition/Source/AnnotationDefinitionSource.php
Expand Up @@ -17,12 +17,12 @@
use DI\Definition\Exception\DefinitionException;
use DI\Definition\MethodInjection;
use DI\Definition\PropertyInjection;
use DI\Definition\Source\Annotation\PhpDocParser;
use DI\Definition\UndefinedInjection;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\Common\Annotations\Reader;
use InvalidArgumentException;
use PhpDocReader\PhpDocReader;
use ReflectionClass;
use ReflectionMethod;
use UnexpectedValueException;
Expand All @@ -42,17 +42,14 @@ class AnnotationDefinitionSource implements DefinitionSource
private $annotationReader;

/**
* @var PhpDocParser
* @var PhpDocReader
*/
private $phpDocParser;


/**
* Constructor
*/
public function __construct()
{
$this->phpDocParser = new PhpDocParser();
$this->phpDocParser = new PhpDocReader();
}

/**
Expand Down Expand Up @@ -125,7 +122,7 @@ private function readProperties(ReflectionClass $reflectionClass, ClassDefinitio
$entryName = $annotation->getName();

// Look for @var content
$entryName = $entryName ?: $this->phpDocParser->getPropertyType($reflectionClass, $property);
$entryName = $entryName ?: $this->phpDocParser->getPropertyType($property);

if ($entryName === null) {
$value = new UndefinedInjection();
Expand Down Expand Up @@ -227,7 +224,7 @@ private function readMethodParameters(

// Look for @param tag or PHP type-hinting (only case where we use reflection)
if ($entryName === null) {
$entryName = $this->phpDocParser->getParameterType($reflectionClass, $method, $parameter);
$entryName = $this->phpDocParser->getParameterType($parameter);
}

if ($entryName === null) {
Expand Down
4 changes: 2 additions & 2 deletions src/DI/Entry.php
Expand Up @@ -14,7 +14,7 @@
use DI\DefinitionHelper\ObjectDefinitionHelper;

/**
* Helps defining a container entry
* Helps defining a container entry.
*
* @author Matthieu Napoli <matthieu@mnapoli.fr>
*/
Expand Down Expand Up @@ -42,7 +42,7 @@ public static function link($entryName)
* @param string|callable $callable Can be either the name of a static method, or a callable
* @return CallableDefinitionHelper
*/
public static function factory($callable)
public static function factory(callable $callable)
{
return new CallableDefinitionHelper($callable);
}
Expand Down
55 changes: 55 additions & 0 deletions test.php
@@ -0,0 +1,55 @@
<?php

use DI\Entry;
use DI\Scope;

return [

// Values (not classes)
'db.host' => 'localhost',
'db.port' => 5000,

// Indexed non-empty array as value
'report.recipients' => [
'bob@acme.example.com',
'alice@acme.example.com'
],

// Direct mapping (not needed if you didn't disable Reflection)
'SomeClass' => Entry::object(),

// This is not recommended: will instantiate the class even when not used, prevents caching
'SomeOtherClass' => new SomeOtherClass(1, "hello"),

// Defines an instance of My\Class
'My\Class' => [
'host' => Entry::link('db.host'),
'otherService' => Entry::link('My\OtherClass'),
],

'My\OtherClass' => [
'scope' => Scope::PROTOTYPE(),
'host' => Entry::link('db.host'),
'port' => Entry::link('db.port'),
'setFoo1' => Entry::link('My\Foo1'),
'setFoo2' => [Entry::link('My\Foo1'), Entry::link('My\Foo2')],
'setFoo3' => [
'param1' => Entry::link('My\Foo1'),
'param2' => Entry::link('My\Foo2'),
],
'bar' => Entry::link('My\Bar'),
],

// Mapping an interface to an implementation
'My\Interface' => Entry::object('My\Implementation'),

// Defining a named instance
'myNamedInstance' => Entry::object('My\Class'),

// Using an anonymous function
// not recommended: will prevent caching
'My\Stuff' => Entry::factory(function (Container $c) {
return new MyClass($c['db.host']);
}),

];
40 changes: 22 additions & 18 deletions tests/IntegrationTests/DI/PropertyInjectionTest.php
Expand Up @@ -9,9 +9,14 @@

namespace IntegrationTests\DI;

use \DI\Container;
use \IntegrationTests\DI\Fixtures\PropertyInjectionTest\LazyInjectionClass;
use \IntegrationTests\DI\Fixtures\PropertyInjectionTest\NamedBean;
use DI\Container;
use IntegrationTests\DI\Fixtures\PropertyInjectionTest\Class2;
use IntegrationTests\DI\Fixtures\PropertyInjectionTest\Issue1\AnotherIssue1;
use IntegrationTests\DI\Fixtures\PropertyInjectionTest\Issue1\Dependency;
use IntegrationTests\DI\Fixtures\PropertyInjectionTest\Issue1;
use IntegrationTests\DI\Fixtures\PropertyInjectionTest\NamedBean;
use IntegrationTests\DI\Fixtures\PropertyInjectionTest\NamedInjectionClass;
use IntegrationTests\DI\Fixtures\PropertyInjectionTest\NotFoundVarAnnotation;

/**
* Test class for bean injection
Expand All @@ -32,10 +37,10 @@ public function testNamedInjection()
$bean2->nameForTest = 'namedDependency2';
$container->set('namedDependency2', $bean2);
// Test
$class = $container->get('IntegrationTests\DI\Fixtures\PropertyInjectionTest\NamedInjectionClass');
$class = $container->get(NamedInjectionClass::class);
$dependency = $class->getDependency();
$this->assertNotNull($dependency);
$this->assertInstanceOf('IntegrationTests\DI\Fixtures\PropertyInjectionTest\NamedBean', $dependency);
$this->assertInstanceOf(NamedBean::class, $dependency);
$this->assertEquals('namedDependency', $dependency->nameForTest);
$this->assertSame($bean, $dependency);
$this->assertNotSame($bean2, $dependency);
Expand All @@ -48,7 +53,7 @@ public function testNamedInjectionNotFound()
{
// Exception (bean not defined)
$container = new Container();
$container->get('IntegrationTests\DI\Fixtures\PropertyInjectionTest\NamedInjectionClass');
$container->get(NamedInjectionClass::class);
}

/**
Expand All @@ -57,26 +62,25 @@ public function testNamedInjectionNotFound()
public function testIssue1()
{
$container = new Container();
/** @var $object \IntegrationTests\DI\Fixtures\PropertyInjectionTest\Issue1 */
$object = $container->get('IntegrationTests\DI\Fixtures\PropertyInjectionTest\Issue1');
$this->assertInstanceOf('IntegrationTests\DI\Fixtures\PropertyInjectionTest\Class2', $object->class2);
$this->assertInstanceOf('IntegrationTests\DI\Fixtures\PropertyInjectionTest\Class2', $object->alias);
$this->assertInstanceOf('IntegrationTests\DI\Fixtures\PropertyInjectionTest\Class2', $object->namespaceAlias);
/** @var $object Issue1 */
$object = $container->get(Issue1::class);
$this->assertInstanceOf(Class2::class, $object->class2);
$this->assertInstanceOf(Class2::class, $object->alias);
$this->assertInstanceOf(Class2::class, $object->namespaceAlias);

/** @var $object \IntegrationTests\DI\Fixtures\PropertyInjectionTest\Issue1\AnotherIssue1 */
$object = $container->get('IntegrationTests\DI\Fixtures\PropertyInjectionTest\Issue1\AnotherIssue1');
$this->assertInstanceOf('IntegrationTests\DI\Fixtures\PropertyInjectionTest\Class2', $object->dependency);
$this->assertInstanceOf('IntegrationTests\DI\Fixtures\PropertyInjectionTest\Issue1\Dependency', $object->sameNamespaceDependency);
/** @var $object AnotherIssue1 */
$object = $container->get(AnotherIssue1::class);
$this->assertInstanceOf(Class2::class, $object->dependency);
$this->assertInstanceOf(Dependency::class, $object->sameNamespaceDependency);
}

/**
* Check error cases
* @expectedException \DI\Definition\Exception\AnnotationException
* @expectedException \PhpDocReader\AnnotationException
*/
public function testNotFoundVarAnnotation()
{
$container = new Container();
/** @var $object \IntegrationTests\DI\Fixtures\PropertyInjectionTest\NotFoundVarAnnotation */
$container->get('IntegrationTests\DI\Fixtures\PropertyInjectionTest\NotFoundVarAnnotation');
$container->get(NotFoundVarAnnotation::class);
}
}

0 comments on commit 90ab878

Please sign in to comment.