Skip to content

Commit 5d59740

Browse files
committed
Merge branch '1.0' of git://github.com/thecodingmachine/yaco into 1.0
2 parents 2962461 + ae9ba50 commit 5d59740

11 files changed

+93
-26
lines changed

src/Compiler.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function __construct(DefinitionConverterInterface $converter = null)
4343
/**
4444
* Adds a definition to the list of definitions managed by this compiler.
4545
*
46-
* @param string $identifier
46+
* @param string $identifier
4747
* @param DefinitionInterface $definition
4848
*/
4949
public function addDefinition($identifier, DefinitionInterface $definition)
@@ -57,7 +57,8 @@ public function addDefinition($identifier, DefinitionInterface $definition)
5757
*
5858
* @param DefinitionProviderInterface $definitionProvider
5959
*/
60-
public function register(DefinitionProviderInterface $definitionProvider) {
60+
public function register(DefinitionProviderInterface $definitionProvider)
61+
{
6162
foreach ($definitionProvider->getDefinitions() as $identifier => $definition) {
6263
$this->addDefinition($identifier, $definition);
6364
}
@@ -75,6 +76,36 @@ public function addDumpableDefinition(DumpableInterface $dumpableDefinition)
7576
unset($this->definitions[$dumpableDefinition->getIdentifier()]);
7677
}
7778

79+
/**
80+
* @param string $identifier
81+
*
82+
* @return bool
83+
*/
84+
public function has($identifier)
85+
{
86+
return isset($this->dumpableDefinitions[$identifier]) || isset($this->definitions[$identifier]);
87+
}
88+
89+
/**
90+
* Returns the dumpable definition matching the $identifier.
91+
*
92+
* @param string $identifier
93+
*
94+
* @return Definition\AliasDefinition|DumpableInterface|Definition\FactoryCallDefinition|Definition\ObjectDefinition|Definition\ParameterDefinition
95+
*
96+
* @throws CompilerException
97+
*/
98+
public function getDumpableDefinition($identifier)
99+
{
100+
if (isset($this->dumpableDefinitions[$identifier])) {
101+
return $this->dumpableDefinitions[$identifier];
102+
} elseif (isset($this->definitions[$identifier])) {
103+
return $this->converter->convert($identifier, $this->definitions[$identifier]);
104+
} else {
105+
throw new CompilerException(sprintf('Unknown identifier in compiler: "%s"', $identifier));
106+
}
107+
}
108+
78109
/**
79110
* @param string $className
80111
*

src/Definition/FactoryCallDefinition.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class FactoryCallDefinition implements DumpableInterface
4040
* Constructs an factory definition.
4141
*
4242
* @param string|null $identifier The identifier of the instance in the container. Can be null if the instance is anonymous (declared inline of other instances)
43-
* @param ReferenceInterface $factory A pointer to the service that the factory method will be called upon, or a fully qualified class name
43+
* @param ReferenceInterface $factory A pointer to the service that the factory method will be called upon, or a fully qualified class name
4444
* @param string $methodName The name of the factory method
4545
* @param array $methodArguments The parameters of the factory method
4646
*/

src/Definition/ParameterDefinition.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,6 @@ public function getValue()
6565
*/
6666
public function toPhpCode($containerVariable, array $usedVariables = array())
6767
{
68-
return new InlineEntry(var_export($this->value, true), null, $usedVariables, false);
68+
return ValueUtils::dumpValue($this->value, $containerVariable, $usedVariables);
6969
}
7070
}

src/Definition/ValueUtils.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static function dumpValue($value, $containerVariable, array $usedVariable
2424
} elseif (is_object($value) || is_resource($value)) {
2525
throw new \RuntimeException('Unable to dump a container if a parameter is an object or a resource.');
2626
} else {
27-
return new InlineEntry(var_export($value, true), null, $usedVariables);
27+
return new InlineEntry(var_export($value, true), null, $usedVariables, false);
2828
}
2929
}
3030

src/DefinitionConverter.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace TheCodingMachine\Yaco;
44

5-
use Interop\Container\Definition\AliasDefinitionInterface;
65
use Interop\Container\Definition\DefinitionInterface;
76
use Interop\Container\Definition\FactoryCallDefinitionInterface;
87
use Interop\Container\Definition\ObjectDefinitionInterface;
@@ -21,11 +20,12 @@
2120
class DefinitionConverter implements DefinitionConverterInterface
2221
{
2322
/**
24-
* @param string $identifier
25-
* @param DefinitionInterface $definition
23+
* @param string $identifier
24+
* @param DefinitionInterface|mixed $definition
25+
*
2626
* @return AliasDefinition|FactoryCallDefinition|ObjectDefinition|ParameterDefinition
2727
*/
28-
public function convert($identifier, DefinitionInterface $definition)
28+
public function convert($identifier, $definition)
2929
{
3030
if ($definition instanceof ObjectDefinitionInterface) {
3131
$yacoObjectDefinition = new ObjectDefinition($identifier,
@@ -47,13 +47,15 @@ public function convert($identifier, DefinitionInterface $definition)
4747
$definition->getMethodName(),
4848
$this->convertArguments($definition->getArguments()));
4949
} elseif ($definition instanceof ParameterDefinitionInterface) {
50-
return new ParameterDefinition($identifier, $definition->getValue());
50+
return new ParameterDefinition($identifier, $this->convertValue($definition->getValue()));
5151
} elseif ($definition instanceof ReferenceDefinitionInterface) {
5252
if ($identifier !== null) {
5353
return new AliasDefinition($identifier, $definition->getTarget());
5454
} else {
5555
return new Reference($definition->getTarget());
5656
}
57+
} elseif (is_scalar($definition) || is_array($definition)) {
58+
return new ParameterDefinition($identifier, $this->convertValue($definition));
5759
} else {
5860
throw new \RuntimeException(sprintf('Cannot convert object of type "%s"', get_class($definition)));
5961
}

src/DefinitionConverterInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ interface DefinitionConverterInterface
1414
/**
1515
* Converts a definition from container-interop's definition format to Yaco internal format.
1616
*
17-
* @param string $identifier The container entry identifier
18-
* @param DefinitionInterface $definition The container entry definition
17+
* @param string $identifier The container entry identifier
18+
* @param DefinitionInterface|mixed $definition The container entry definition
1919
*
2020
* @return DumpableInterface
2121
*/
22-
public function convert($identifier, DefinitionInterface $definition);
22+
public function convert($identifier, $definition);
2323
}

tests/CompilerTest.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ public function testException()
7575
$code = $compiler->compile('MyNamespace\\MyContainerWithParameters');
7676
}
7777

78-
public function testRegister() {
78+
public function testRegister()
79+
{
7980
$compiler = new Compiler();
8081
$compiler->register(new TestDefinitionProvider());
8182

@@ -87,4 +88,43 @@ public function testRegister() {
8788
$result = $myContainer->get('test');
8889
$this->assertInstanceOf('\\stdClass', $result);
8990
}
91+
92+
public function testHas()
93+
{
94+
$compiler = new Compiler();
95+
$compiler->register(new TestDefinitionProvider());
96+
97+
$this->assertTrue($compiler->has('test'));
98+
$this->assertFalse($compiler->has('not_found'));
99+
}
100+
101+
public function testGetDumpableDefinitionFromDefinitionProvider()
102+
{
103+
$compiler = new Compiler();
104+
$compiler->register(new TestDefinitionProvider());
105+
106+
$this->assertInstanceOf('TheCodingMachine\\Yaco\\Definition\\ObjectDefinition', $compiler->getDumpableDefinition('test'));
107+
}
108+
109+
public function testGetDumpableDefinition()
110+
{
111+
$instanceDefinition = new ObjectDefinition('test', '\\stdClass');
112+
113+
$compiler = new Compiler();
114+
$compiler->addDumpableDefinition($instanceDefinition);
115+
116+
$this->assertInstanceOf('TheCodingMachine\\Yaco\\Definition\\ObjectDefinition', $compiler->getDumpableDefinition('test'));
117+
}
118+
119+
/**
120+
* @expectedException \TheCodingMachine\Yaco\CompilerException
121+
*
122+
* @throws CompilerException
123+
*/
124+
public function testGetDumpableDefinitionException()
125+
{
126+
$compiler = new Compiler();
127+
128+
$compiler->getDumpableDefinition('test');
129+
}
90130
}

tests/Definition/FactoryCallDefinitionTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,11 @@ public function testStaticFactory()
4040
$instanceDefinition = new FactoryCallDefinition('test', 'TheCodingMachine\Yaco\Definition\Fixtures\TestFactory', 'getStaticTest', [42]);
4141

4242
$container = $this->getContainer([
43-
'test' => $instanceDefinition
43+
'test' => $instanceDefinition,
4444
]);
4545
$result = $container->get('test');
4646

4747
$this->assertInstanceOf('TheCodingMachine\\Yaco\\Definition\\Fixtures\\Test', $result);
4848
$this->assertEquals(42, $result->cArg1);
4949
}
50-
5150
}
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
<?php
22

3-
43
namespace TheCodingMachine\Yaco\Definition\Fixtures;
54

6-
75
use Assembly\ObjectDefinition;
86
use Interop\Container\Definition\DefinitionInterface;
97
use Interop\Container\Definition\DefinitionProviderInterface;
108

119
class TestDefinitionProvider implements DefinitionProviderInterface
1210
{
13-
1411
/**
1512
* Returns the definition to register in the container.
1613
*
@@ -19,7 +16,7 @@ class TestDefinitionProvider implements DefinitionProviderInterface
1916
public function getDefinitions()
2017
{
2118
return [
22-
'test' => new ObjectDefinition('\\stdClass')
19+
'test' => new ObjectDefinition('\\stdClass'),
2320
];
2421
}
25-
}
22+
}

tests/Definition/Fixtures/TestFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ public function getTest($arg2 = null)
1616
return new Test($this->arg, $arg2);
1717
}
1818

19-
public static function getStaticTest($arg1) {
19+
public static function getStaticTest($arg1)
20+
{
2021
return new Test($arg1);
2122
}
2223
}

tests/DefinitionInteropCompatibilityTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@
22

33
namespace TheCodingMachine\Yaco;
44

5-
use Assembly\ObjectInitializer\MethodCall;
6-
use Assembly\ObjectInitializer\PropertyAssignment;
7-
use Assembly\Reference;
85
use Interop\Container\ContainerInterface;
96
use Interop\Container\Definition\DefinitionProviderInterface;
107
use Interop\Container\Definition\Test\AbstractDefinitionCompatibilityTest;
118
use Mouf\Picotainer\Picotainer;
12-
use TheCodingMachine\Yaco\Definition\AbstractDefinitionTest;
139

1410
class DefinitionInteropCompatibilityTest extends AbstractDefinitionCompatibilityTest
1511
{
@@ -28,6 +24,7 @@ public function setUp()
2824
* Takes a definition provider in parameter and returns a container containing the entries.
2925
*
3026
* @param DefinitionProviderInterface $definitionProvider
27+
*
3128
* @return ContainerInterface
3229
*/
3330
protected function getContainer(DefinitionProviderInterface $definitionProvider)

0 commit comments

Comments
 (0)