Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/di-compiler-runtime-definition-config' of https…
Browse files Browse the repository at this point in the history
…://github.com/SocalNick/zf2 into feature/di-compiler-runtime-definition
  • Loading branch information
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 5 deletions.
22 changes: 20 additions & 2 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,28 @@ public function configureDefinition(Di $di, $definition)
foreach ($definition as $definitionType => $definitionData) {
switch ($definitionType) {
case 'compiler':
// @todo
foreach ($definitionData as $filename) {
if (is_readable($filename)) {
$di->definitions()->addDefinition(new \Zend\Di\Definition\ArrayDefinition(include $filename), false);
}
}
break;
case 'runtime':
// @todo
if (isset($definitionData['enabled']) && !$definitionData['enabled']) {
// Remove runtime from definition list if not enabled
$definitions = array();
foreach ($di->definitions() as $definition) {
if (!$definition instanceof \Zend\Di\Definition\RuntimeDefinition) {
$definitions[] = $definition;
}
}
$definitions = new DefinitionList($definitions);
$di->setDefinitionList($definitions);
} elseif (isset($definitionData['use_annotations']) && $definitionData['use_annotations']) {
$di->definitions()->getDefinitionByType('\Zend\Di\Definition\RuntimeDefinition')
->getIntrospectionStrategy()
->setUseAnnotations(true);
}
break;
case 'class':
foreach ($definitionData as $className => $classData) {
Expand Down
48 changes: 46 additions & 2 deletions test/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,51 @@ public function testConfigurationCanConfigureBuilderDefinitionFromIni()
);

}



public function testConfigurationCanConfigureRuntimeDefinitionDefaultFromIni()
{
$ini = ConfigFactory::fromFile(__DIR__ . '/_files/sample.ini', true)->get('section-c');
$config = new Configuration($ini->di);
$di = new Di();
$di->configure($config);
$definition = $di->definitions()->getDefinitionByType('Zend\Di\Definition\RuntimeDefinition');
$this->assertInstanceOf('Zend\Di\Definition\RuntimeDefinition', $definition);
$this->assertFalse($definition->getIntrospectionStrategy()->getUseAnnotations());
}

public function testConfigurationCanConfigureRuntimeDefinitionDisabledFromIni()
{
$ini = ConfigFactory::fromFile(__DIR__ . '/_files/sample.ini', true)->get('section-d');
$config = new Configuration($ini->di);
$di = new Di();
$di->configure($config);
$definition = $di->definitions()->getDefinitionByType('Zend\Di\Definition\RuntimeDefinition');
$this->assertFalse($definition);
}

public function testConfigurationCanConfigureRuntimeDefinitionUseAnnotationFromIni()
{
$ini = ConfigFactory::fromFile(__DIR__ . '/_files/sample.ini', true)->get('section-e');
$config = new Configuration($ini->di);
$di = new Di();
$di->configure($config);
$definition = $di->definitions()->getDefinitionByType('Zend\Di\Definition\RuntimeDefinition');
$this->assertTrue($definition->getIntrospectionStrategy()->getUseAnnotations());
}

public function testConfigurationCanConfigureCompiledDefinition()
{
$config = ConfigFactory::fromFile(__DIR__ . '/_files/sample.php', true);
$config = new Configuration($config->di);
$di = new Di();
$di->configure($config);
$definition = $di->definitions()->getDefinitionByType('Zend\Di\Definition\ArrayDefinition');
$this->assertInstanceOf('Zend\Di\Definition\ArrayDefinition', $definition);
$this->assertTrue($di->definitions()->hasClass('My\DbAdapter'));
$this->assertTrue($di->definitions()->hasClass('My\EntityA'));
$this->assertTrue($di->definitions()->hasClass('My\Mapper'));
$this->assertTrue($di->definitions()->hasClass('My\RepositoryA'));
$this->assertTrue($di->definitions()->hasClass('My\RepositoryB'));
$this->assertFalse($di->definitions()->hasClass('My\Foo'));
}
}
14 changes: 13 additions & 1 deletion test/_files/sample.ini
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,16 @@ di.definitions.1.class = Zend\Di\Definition\BuilderDefinition
di.definitions.1.My\DbAdapter.methods.__construct.username = NULL
di.definitions.1.My\DbAdapter.methods.__construct.password = NULL
di.definitions.1.My\Mapper.methods.__construct.dbAdapter = My\DbAdapter
di.definitions.1.My\Repository.methods.__construct.mapper = My\Mapper
di.definitions.1.My\Repository.methods.__construct.mapper = My\Mapper

[section-c]

di.definition.runtime.xxx = zzz

[section-d]

di.definition.runtime.enabled = 0

[section-e]

di.definition.runtime.use_annotations = 1
10 changes: 10 additions & 0 deletions test/_files/sample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
return array(
'di' => array(
'definition' => array(
'compiler' => array(
__DIR__ . '/definition-array.php',
),
),
),
);

0 comments on commit 9e4c786

Please sign in to comment.