Skip to content
Browse files

Fixed a bug in creating parent definitions for definition decorators.…

… Inserted testcase
  • Loading branch information...
1 parent 35bfb9a commit 183483ec87c4b53716e1aba7e9f27060c85d0d11 @joshiausdemwald committed Jun 2, 2011
View
11 src/Ifschleife/Bundle/AutowiringBundle/Autowiring/ClassnameMapper.php
@@ -145,12 +145,15 @@ private function resolveAliases()
{
if ($alias_definition->isPublic())
{
- /* @var $alias_definition \Symfony\Component\DependencyInjection\Alias */
- $target_definition = $this->container->findDefinition($name);
+ $target_definition = null;
- if (null === $target_definition)
+ try
{
- throw new DefinitionNotFoundException(sprintf('Service definition for alias "%s" could not be resolved.', $name));
+ $target_definition = $this->container->findDefinition($name);
+ }
+ catch(\InvalidArgumentException $e)
+ {
+ throw new DefinitionNotFoundException(sprintf('Service definition for alias "%s" could not be resolved.', $name), null, $e);
}
// SYNTHETIC DEFINITIONS HAS NO CLASSNAME
View
8 src/Ifschleife/Bundle/AutowiringBundle/Autowiring/Injector/ContainerInjector.php
@@ -42,13 +42,13 @@ protected function process(Definition $definition, \Reflector $reflector)
/**
* @param Definition $definition
* @param \Reflector $class
- * @return type
+ * @return string the service id
*/
public function injectService(\ReflectionClass $class)
{
$this->annotations = $this->createAnnotationsMap((array)$this->readAnnotations($class));
- $this->doInject($class);
+ return $this->doInject($class);
}
protected function doInject(\ReflectionClass $class)
@@ -85,8 +85,10 @@ protected function doInject(\ReflectionClass $class)
*/
protected function createDefinition(\ReflectionClass $class)
{
- if(false !== ($parentClass = $class->getParentClass()) && null !== ($parent_service_id = $this->doInject($parentClass)))
+ if(false !== ($parentClass = $class->getParentClass()))
{
+ $parent_service_id = $this->injectService($parentClass);
+
$definition = new DefinitionDecorator($parent_service_id);
}
else
View
19 src/Ifschleife/Bundle/AutowiringBundle/Autowiring/Injector/MethodInjector.php
@@ -88,17 +88,20 @@ public function guessArgumentsForMethodSignature(\ReflectionMethod $method)
// NO MATCHING SERVICE PARAMETER FOUND, CHECK FOR SCALAR VALUE
if (is_string($di_hint))
{
- if($this->container->findDefinition($di_hint))
+ try
{
- $arguments[] = $this->createReference($di_hint, $is_optional, $is_strict);
+ $arguments[] = $this->container->findDefinition($di_hint);
}
- elseif($this->container->hasParameter($di_hint))
+ catch(\InvalidArgumentException $e)
{
- $arguments[] = new Parameter($di_hint);
- }
- else
- {
- $arguments[] = new Parameter($this->addParameter($di_hint));
+ if($this->container->hasParameter($di_hint))
+ {
+ $arguments[] = new Parameter($di_hint);
+ }
+ else
+ {
+ $arguments[] = new Parameter($this->addParameter($di_hint));
+ }
}
}
else
View
12 src/Ifschleife/Bundle/AutowiringBundle/Autowiring/Injector/PropertyInjector.php
@@ -38,7 +38,7 @@
class PropertyInjector extends Injector
{
protected function process(Definition $definition, \Reflector $property)
- {
+ {
$is_optional = $this->hasAnnotation(self::ANNOTATION_OPTIONAL)
? $this->getAnnotation(self::ANNOTATION_OPTIONAL)->getIsOptional() : false;
@@ -52,7 +52,7 @@ protected function process(Definition $definition, \Reflector $property)
if (null === $di_hints || null === ($di_hint = array_pop($di_hints)))
{
- throw new UnresolvedServiceException(sprintf('Property "$%s" of class "%s" cannot be injected. Please provide a valid service id.', $property->getName(), $class->getName()));
+ throw new UnresolvedPropertyException(sprintf('Property "$%s" of class "%s" cannot be injected. Please provide a valid service id.', $property->getName(), $class->getName()));
}
$inject = null;
@@ -81,10 +81,16 @@ protected function process(Definition $definition, \Reflector $property)
// changed: from findDefinition to getDefinition
$service_id = Inflector::propertyName2ServiceId($property->getName());
- if($this->container->findDefinition($service_id))
+ try
{
+ $this->container->findDefinition($service_id);
+
$definition->setProperty($property->getName(), $this->createReference($service_id, $is_optional, $is_strict));
}
+ catch(\InvalidArgumentException $e)
+ {
+ throw new UnresolvedPropertyException(sprintf('Instance property "%s::$%s" on service could not be resolved.', $property->getDeclaringClass()->getName(), $property->getName()), null, $e);
+ }
}
}
}
View
34 src/Ifschleife/Bundle/AutowiringBundle/Autowiring/Injector/UnresolvedPropertyException.php
@@ -0,0 +1,34 @@
+<?php
+/**
+ * Copyright (c) 2011 Johannes Heinen <johannes.heinen@gmail.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+namespace Ifschleife\Bundle\AutowiringBundle\Autowiring\Injector;
+
+/**
+ * UnresolvedPropertyException
+ *
+ * @author joshi
+ */
+class UnresolvedPropertyException extends InjectorException
+{
+
+}
View
2 src/Ifschleife/Bundle/AutowiringBundle/Autowiring/Injector/UnresolvedServiceException.php
@@ -21,7 +21,7 @@
* SOFTWARE.
*/
-namespace Ifschleife\Bundle\AutowiringBundle\Autowiring;
+namespace Ifschleife\Bundle\AutowiringBundle\Autowiring\Injector;
/**
* UnresolvedServiceException
View
22 src/Ifschleife/Bundle/AutowiringBundle/Tests/Autowiring/DependencyResolverTest.php
@@ -43,15 +43,33 @@ public function testDependencyResolving(array $files)
$serviceBuilder->build();
- $container->compile();
+ $dependencyResolver = new \Ifschleife\Bundle\AutowiringBundle\Autowiring\DependencyResolver($container);
+ $dependencyResolver->resolve();
+
$this->assertTrue($container->hasDefinition('ifschleife.bundle.autowiring_bundle.tests.fixtures.create_service_testclass1'));
$this->assertTrue($container->hasDefinition('hans.wurst'));
$this->assertTrue($container->hasDefinition('ifschleife.autowiring.testclass'));
$this->assertTrue($container->hasDefinition('ifschleife.bundle.autowiring_bundle.tests.fixtures.parent_testclass'));
- $this->assertTrue($container->hasDefinition('ifschleife.bundle.autowiring_bundle.tests.fixtures.testservice'));
+ $this->assertTrue($container->hasDefinition('ifschleife.autowiring.testservice'));
$definitions = $container->getDefinitions();
+
+ $this->assertInstanceOf(
+ '\Symfony\Component\DependencyInjection\Reference',
+ $definitions['ifschleife.autowiring.testclass']->getArgument(0)
+ );
+
+ $properties = $definitions['ifschleife.autowiring.testclass']->getProperties();
+
+ $this->assertInstanceOf(
+ '\Symfony\Component\DependencyInjection\Reference',
+ $properties['ifschleifeAutowiringTestserviceService']
+ );
+
+ $this->assertInstanceOf('\Symfony\Component\DependencyInjection\DefinitionDecorator', $definitions['ifschleife.autowiring.testclass']);
+
+ $this->assertEquals('ifschleife.bundle.autowiring_bundle.tests.fixtures.parent_testclass', $definitions['ifschleife.autowiring.testclass']->getParent());
}
function files()
View
2 src/Ifschleife/Bundle/AutowiringBundle/Tests/Fixtures/ParentTestclass.php
@@ -29,8 +29,8 @@
/**
* Description of ParentTestclass
*
- * @Service
* @author joshi
+ * @Service
*/
class ParentTestclass
{
View
2 src/Ifschleife/Bundle/AutowiringBundle/Tests/Fixtures/Testservice.php
@@ -11,7 +11,7 @@
/**
* Description of Testservice
*
- * @Service
+ * @Service("ifschleife.autowiring.testservice")
* @author joshi
*/
class Testservice

0 comments on commit 183483e

Please sign in to comment.
Something went wrong with that request. Please try again.