Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing service properties #145

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Annotation/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,19 @@ final class Service

/** @var array<string> */
public $environments = array();

/** @var boolean */
public $lazy;

/** @var string */
public $factoryService;

/** @var string */
public $factoryMethod;

/** @var string */
public $factoryClass;

/** @var array */
public $factoryMethodArguments = array();
}
14 changes: 14 additions & 0 deletions Metadata/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class ClassMetadata extends BaseClassMetadata
public $decorates;
public $decoration_inner_name;
public $deprecated;
public $lazy;
public $factoryService;
public $factoryMethod;
public $factoryClass;

/**
* @param string $env
Expand Down Expand Up @@ -76,6 +80,11 @@ public function serialize()
$this->lookupMethods,
$this->properties,
$this->initMethod,
$this->initMethods,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should add them at the end of the array to avoid BC breaks

$this->lazy,
$this->factoryService,
$this->factoryMethod,
$this->factoryClass,
parent::serialize(),
$this->environments,
$this->decorates,
Expand Down Expand Up @@ -104,11 +113,16 @@ public function unserialize($str)
$this->lookupMethods,
$this->properties,
$this->initMethod,
$this->initMethods,
$parentStr,
$this->environments,
$this->decorates,
$this->decoration_inner_name,
$this->deprecated,
$this->lazy,
$this->factoryService,
$this->factoryMethod,
$this->factoryClass,
) = $data;

parent::unserialize($parentStr);
Expand Down
15 changes: 15 additions & 0 deletions Metadata/Driver/AnnotationDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,21 @@ public function loadMetadataForClass(\ReflectionClass $class)
$metadata->decorates = $annot->decorates;
$metadata->decoration_inner_name = $annot->decoration_inner_name;
$metadata->deprecated = $annot->deprecated;
$metadata->lazy = $annot->lazy;
$metadata->factoryClass = $annot->factoryClass;
$metadata->factoryService = $annot->factoryService;
$metadata->factoryMethod = $annot->factoryMethod;
if ($annot->factoryMethodArguments !== array()) {
$params = array();
foreach ($annot->factoryMethodArguments as $key => $argument) {
if ($argument instanceof Inject) {
$params[] = $this->convertReferenceValue($key, $argument);
} else {
$params[] = $argument;
}
}
$metadata->arguments = $params;
}
} else if ($annot instanceof Tag) {
$metadata->tags[$annot->name][] = $annot->attributes;
} else if ($annot instanceof Validator) {
Expand Down
15 changes: 15 additions & 0 deletions Metadata/MetadataConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,21 @@ public function convert(ClassHierarchyMetadata $metadata)
if (null !== $classMetadata->deprecated && method_exists($definition, 'setDeprecated')) {
$definition->setDeprecated(true, $classMetadata->deprecated);
}
if (null !== $classMetadata->lazy) {
if (!method_exists($definition, 'setLazy')) {
throw new \RuntimeException(sprintf('the "lazy" attribute is not available on your Symfony version.'));
}
$definition->setLazy($classMetadata->lazy);
}
if (null !== $classMetadata->factoryClass) {
$definition->setFactoryClass($classMetadata->factoryClass);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is deprecated, you should use setFactory when possible.

}
if (null !== $classMetadata->factoryMethod) {
$definition->setFactoryMethod($classMetadata->factoryMethod);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same thing

}
if (null !== $classMetadata->factoryService) {
$definition->setFactoryService($classMetadata->factoryService);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

}

if (null === $classMetadata->id) {
$classMetadata->id = '_jms_di_extra.unnamed.service_'.$count++;
Expand Down
28 changes: 28 additions & 0 deletions Tests/Functional/Bundle/TestBundle/Factory/TestClassFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace JMS\DiExtraBundle\Tests\Functional\Bundle\TestBundle\Factory;

use JMS\DiExtraBundle\Tests\Functional\Bundle\TestBundle\Model\TestClass;
use JMS\DiExtraBundle\Annotation\Service;

/**
* JMS\DiExtraBundle\Tests\Functional\Bundle\TestBundle\Factory\TestClassFactory
*
* @Service("jms_di_extra.factory.test_factory")
*
* @author Ivan Molchanov <[email protected]>
*/
class TestClassFactory
{
/**
* @param string $message
* @return TestClass
*/
public function create($message = '')
{
$class = new TestClass();
$class->setTestMessage($message);

return $class;
}
}
45 changes: 45 additions & 0 deletions Tests/Functional/Bundle/TestBundle/Model/TestClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace JMS\DiExtraBundle\Tests\Functional\Bundle\TestBundle\Model;

use JMS\DiExtraBundle\Annotation\Service;

/**
* JMS\DiExtraBundle\Tests\Functional\Bundle\TestBundle\Model\TestClass
*
* @Service("jms_di_extra.model.test",
* factoryClass = "JMS\DiExtraBundle\Tests\Functional\Bundle\TestBundle\Factory\TestClassFactory",
* factoryMethod="create", factoryMethodArguments={
* "message" = "created by factory"
* } )
*
* @author Ivan Molchanov <[email protected]>
*/
class TestClass
{
/**
* @var string
*/
protected $testMessage;

/**
* @param string $testMessage
* @return TestClass
*/
public function setTestMessage($testMessage)
{
$this->testMessage = $testMessage;

return $this;
}

/**
* @return string
*/
public function getTestMessage()
{
return $this->testMessage;
}


}
43 changes: 43 additions & 0 deletions Tests/Functional/Bundle/TestBundle/Model/TestService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace JMS\DiExtraBundle\Tests\Functional\Bundle\TestBundle\Model;

use JMS\DiExtraBundle\Annotation\Service;

/**
* JMS\DiExtraBundle\Tests\Functional\Bundle\TestBundle\Model\TestService
*
* @Service("jms_di_extra.model.test_service",
* factoryService = "jms_di_extra.factory.test_factory",
* factoryMethod="create", factoryMethodArguments={
* "message" = "created by factory"
* } )
*
* @author Ivan Molchanov <[email protected]>
*/
class TestService
{
/**
* @var string
*/
protected $testMessage;

/**
* @param string $testMessage
* @return TestClass
*/
public function setTestMessage($testMessage)
{
$this->testMessage = $testMessage;

return $this;
}

/**
* @return string
*/
public function getTestMessage()
{
return $this->testMessage;
}
}
34 changes: 34 additions & 0 deletions Tests/Functional/FactoryPropertiesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace JMS\DiExtraBundle\Tests\Functional;

/**
* JMS\DiExtraBundle\Tests\Functional\Bundle\FactoryPropertiesTest
*
* @author Ivan Molchanov <[email protected]>
*/
class FactoryPropertiesTest extends BaseTestCase
{
/**
* @runInSeparateProcess
*/
public function testFactoryClassProperty()
{
$this->createClient();
$container = self::$kernel->getContainer();
$testClass = $container->get('jms_di_extra.model.test');
$this->assertEquals($testClass->getTestMessage(), 'created by factory');
}

/**
* @runInSeparateProcess
*/
public function testFactoryServiceProperty()
{
$this->createClient();
$container = self::$kernel->getContainer();
$testClass = $container->get('jms_di_extra.model.test_service');
$this->assertEquals($testClass->getTestMessage(), 'created by factory');
}
}