Skip to content
Permalink
Browse files

Merge eba6f05 into 3b04ed9

  • Loading branch information...
mnapoli committed Apr 2, 2019
2 parents 3b04ed9 + eba6f05 commit 4de062f27f8489e09792c3993a7fdb6fc46c6d9d
Showing with 4 additions and 96 deletions.
  1. +4 −22 src/Compiler/Compiler.php
  2. +0 −74 tests/IntegrationTest/CompiledContainerTest.php
@@ -157,33 +157,15 @@ public function compile(
return $fileName;
}
/**
* Use a hash to ensure that the used method names in the CompiledContainer are both unique and idempotent.
*/
private function getHashedValue(string $prefix, string $value) : string
{
return $prefix . md5($value);
}
/**
* @throws DependencyException
* @throws InvalidDefinition
* @return string The method name
*/
private function compileDefinition(string $entryName, Definition $definition) : string
{
$methodName = $this->getHashedValue('get', $entryName);
//In case an Entry is already added, the used method should be equal
if (isset($this->entryToMethodMapping[$entryName]) && $this->entryToMethodMapping[$entryName] !== $methodName) {
throw new InvalidDefinition(sprintf(
'Entry "%s" cannot be compiled. An Entry with the same name already exists pointing to method %s(), while this one points to method %s().',
$entryName,
$this->entryToMethodMapping[$entryName],
$methodName
));
}
// Generate a unique method name
$methodName = str_replace('.', '', uniqid('get', true));
$this->entryToMethodMapping[$entryName] = $methodName;
switch (true) {
@@ -294,8 +276,8 @@ public function compileValue($value) : string
}
if ($value instanceof Definition) {
$subEntryName = $this->getHashedValue('SubEntry', $value->getName() . $value);
// Give it an arbitrary unique name
$subEntryName = uniqid('SubEntry');
// Compile the sub-definition in another method
$methodName = $this->compileDefinition($subEntryName, $value);
// The value is now a method call to that method (which returns the value)
@@ -56,54 +56,6 @@ public function the_container_is_compiled_once_and_never_recompiled_after()
self::assertEquals('bar', $container->get('foo'));
}
/** @test */
public function the_compiled_container_is_idempotent()
{
$compiledContainerClass1 = self::generateCompiledClassName();
$compiledContainerClass2 = self::generateCompiledClassName();
$definitions = [
'foo' => 'barFromFoo',
'fooReference' => \DI\get('foo'),
'factory' => function () {
return 'barFromFactory';
},
'factoryReference' => \DI\get('factory'),
'array' => [
1,
2,
3,
'fooBar',
],
'arrayValue' => \DI\value('array'),
CompiledContainerTest\AllKindsOfInjections::class => create()
->constructor(create('stdClass'))
->property('property', autowire(CompiledContainerTest\Autowireable::class))
->method('method', \DI\factory(
function () {
return new \stdClass;
}
)
),
CompiledContainerTest\Autowireable::class => \DI\autowire(),
];
// Create a compiled container in a specific file
$builder1 = new ContainerBuilder;
$builder1->addDefinitions($definitions);
$builder1->enableCompilation(self::COMPILATION_DIR, $compiledContainerClass1);
$builder1->build();
// Create a second compiled container with the same configuration but in a different file
$builder2 = new ContainerBuilder;
$builder2->addDefinitions($definitions);
$builder2->enableCompilation(self::COMPILATION_DIR, $compiledContainerClass2);
$builder2->build();
// The method mapping of the resulting CompiledContainers should be equal
self::assertEquals($compiledContainerClass1::METHOD_MAPPING, $compiledContainerClass2::METHOD_MAPPING);
}
/**
* @test
* @expectedException \DI\Definition\Exception\InvalidDefinition
@@ -320,29 +272,3 @@ public function __construct(AbstractClass $param)
abstract class AbstractClass
{
}
class AllKindsOfInjections
{
public $property;
public $constructorParameter;
public $methodParameter;
public function __construct($constructorParameter)
{
$this->constructorParameter = $constructorParameter;
}
public function method($methodParameter)
{
$this->methodParameter = $methodParameter;
}
}
class Autowireable
{
private $dependency;
public function __construct(AutowireableDependency $dependency)
{
$this->dependency = $dependency;
}
}
class AutowireableDependency
{
}

0 comments on commit 4de062f

Please sign in to comment.
You can’t perform that action at this time.