Skip to content

Commit

Permalink
add environment switches to multiservice
Browse files Browse the repository at this point in the history
  • Loading branch information
wodka committed Feb 15, 2016
1 parent 916e517 commit 1ded33b
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 5 deletions.
6 changes: 2 additions & 4 deletions DependencyInjection/Compiler/AnnotationConfigurationPass.php
Expand Up @@ -55,14 +55,12 @@ public function process(ContainerBuilder $container)
if (null === $metadata = $factory->getMetadataForClass($className)) {
continue;
}

if (null === $metadata->getOutsideClassMetadata()->id) {
continue;
}
if ( ! $metadata->getOutsideClassMetadata()->isLoadedInEnvironment($container->getParameter('kernel.environment'))) {
continue;
}

foreach ($converter->convert($metadata) as $id => $definition) {
foreach ($converter->convert($metadata, $container->getParameter('kernel.environment')) as $id => $definition) {
$container->setDefinition($id, $definition);
}
}
Expand Down
10 changes: 10 additions & 0 deletions Metadata/ClassMetadata.php
Expand Up @@ -90,6 +90,8 @@ class ClassMetadata extends BaseClassMetadata
public $initMethod;

/**
* @deprecated use addService instead
*
* @var string[]
*/
public $environments = array();
Expand All @@ -114,6 +116,7 @@ public function addService(array $service)
$this->public = @$service['public'];
$this->scope = @$service['scope'];
$this->abstract = @$service['abstract'];
$this->environments = @$service['environments'];
// TODO update call for other tags (there are several pull requests)
}

Expand Down Expand Up @@ -143,12 +146,19 @@ public function getServices()
'public' => $this->public,
'scope' => $this->scope,
'abstract' => $this->abstract,
'environments' => $this->environments,
);
}

return $this->services;
}

/**
* @deprecated this is handled on service level
*
* @param string $env
* @return bool
*/
public function isLoadedInEnvironment($env)
{
if (empty($this->environments)) {
Expand Down
1 change: 1 addition & 0 deletions Metadata/Driver/AnnotationDriver.php
Expand Up @@ -83,6 +83,7 @@ public function loadMetadataForClass(\ReflectionClass $class)
$service['public'] = $annot->public;
$service['scope'] = $annot->scope;
$service['abstract'] = $annot->abstract;
$service['environments'] = $annot->environments;

$metadata->addService($service);
} else if ($annot instanceof Tag) {
Expand Down
17 changes: 16 additions & 1 deletion Metadata/MetadataConverter.php
Expand Up @@ -30,9 +30,10 @@ class MetadataConverter
* Converts class hierarchy metadata to definition instances.
*
* @param ClassHierarchyMetadata $metadata
* @param string $environment
* @return array an array of Definition instances
*/
public function convert(ClassHierarchyMetadata $metadata)
public function convert(ClassHierarchyMetadata $metadata, $environment = null)
{
static $count = 0;
$definitions = array();
Expand All @@ -42,6 +43,20 @@ public function convert(ClassHierarchyMetadata $metadata)
/** @var ClassMetadata $classMetadata */
foreach ($metadata->classMetadata as $classMetadata) {
foreach ($classMetadata->getServices() as $service) {
if (@$service['id'] == 'third.multi') {
//echo $environment;

//print_r($service);
}

if (isset($environment)
&& isset($service['environments'])
&& sizeof($service['environments']) > 0
&& !in_array($environment, $service['environments'])
) {
continue;
}

if (null === $previous && !isset($service['parent'])) {
$definition = new Definition();
} else {
Expand Down
2 changes: 2 additions & 0 deletions Tests/Functional/Bundle/TestBundle/Service/Multi.php
Expand Up @@ -7,6 +7,8 @@
/**
* @DI\Service("first.multi")
* @DI\Service("second.multi")
* @DI\Service("third.multi", environments={"dev"})
* @DI\Service("fourth.multi", environments={"test"})
*/
class Multi
{
Expand Down
18 changes: 18 additions & 0 deletions Tests/Functional/MultiServiceInjectTest.php
Expand Up @@ -23,5 +23,23 @@ public function testConstructorInjectionWithInheritance()

$this->assertSame($nice, $second->nice, 'all injections without restriction should be done');
$this->assertSame($bar, $second->worker);

$this->assertEquals(false, $container->has('third.multi'), 'should load third service only in dev');
}

/**
* @runInSeparateProcess
*/
public function testEnvironmentLoading()
{
// this is broken -> cannot load any other environment than test
$this->createClient(
array(
'environment' => 'dev'
)
);
$container = self::$kernel->getContainer();

$this->assertEquals(true, $container->has('fourth.multi'), 'should load fourth service only in test');
}
}

0 comments on commit 1ded33b

Please sign in to comment.