Skip to content

Commit c3ce543

Browse files
laudecotonivdv
authored andcommitted
use the zend config into the Symfony container (#8)
With this commit it's now possible to use the ZF config into the Symfony service container. When the system creates the Symfony container it injects the ZF config. Since ZF is using the same naming convention with a different meaning as Symfony we need to translate them before the import.
1 parent 70433ee commit c3ce543

File tree

4 files changed

+189
-1
lines changed

4 files changed

+189
-1
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,36 @@ the Zend Service Manager.
101101

102102
$container = $this->getServiceLocator()->get('zf_symfony_container');
103103
```
104+
105+
#### Use the Zend config as Symfony parameters
106+
The module takes care of transforming all zend configurations into [Symfony parameters](https://symfony.com/doc/current/service_container/parameters.html) allowing you to immediately use the parameters in the service definition file:
107+
```php
108+
<?php
109+
// global.php
110+
return [
111+
'myconfig' => 'hello world'
112+
];
113+
```
114+
```yaml
115+
services:
116+
Some\Service:
117+
arguments: ['%myconfig%']
118+
```
119+
When the Zend configuration contains deep level keys, the "keys" will be concatenated with a dot.
120+
```php
121+
<?php
122+
// global.php
123+
return [
124+
'deep' => [
125+
'level' => [
126+
'config' => 'hello world'
127+
]
128+
]
129+
];
130+
```
131+
```yaml
132+
services:
133+
Some\Service:
134+
arguments: ['%deep.level.config%']
135+
```
136+
_Note: zend configurations using callables will be ignored and cannot be used!_
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* This file is part of the Adlogix package.
4+
*
5+
* (c) Allan Segebarth <[email protected]>
6+
* (c) Jean-Jacques Courtens <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Adlogix\ZfSymfonyContainer\ParameterBag;
13+
14+
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
15+
16+
/**
17+
* Translate the ZF config into Symfony parameters.
18+
*
19+
* @author Laurent De Coninck <[email protected]>
20+
*/
21+
class ZendFrameworkParameterBag extends ParameterBag
22+
{
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public function add(array $parameters)
27+
{
28+
foreach ($parameters as $key => $value) {
29+
if (is_array($value)) {
30+
$this->addChildren($key, $value);
31+
continue;
32+
}
33+
34+
if (is_callable($value)) {
35+
//not supported by Symfony
36+
continue;
37+
}
38+
39+
$this->set($key, str_replace('%', '%%', $value));
40+
}
41+
}
42+
43+
/**
44+
* @param string $previousKey
45+
* @param array $children
46+
*/
47+
private function addChildren($previousKey, array $children)
48+
{
49+
$formattedChildren = [];
50+
51+
foreach ($children as $key => $value) {
52+
$formattedChildren[$previousKey . '.' . $key] = $value;
53+
}
54+
55+
$this->add($formattedChildren);
56+
}
57+
}

src/Service/Factory/SymfonyContainerFactory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Adlogix\ZfSymfonyContainer\Service\Factory;
1313

1414
use Adlogix\ZfSymfonyContainer\Options\Configuration;
15+
use Adlogix\ZfSymfonyContainer\ParameterBag\ZendFrameworkParameterBag;
1516
use Interop\Container\ContainerInterface;
1617
use Symfony\Component\Config\ConfigCache;
1718
use Symfony\Component\Config\FileLocator;
@@ -47,7 +48,8 @@ public function __invoke(ContainerInterface $zfContainer, $requestedName, array
4748
$containerConfigCache = new ConfigCache($cachedFilePath, $configuration->isDebug());
4849

4950
if (!$containerConfigCache->isFresh()) {
50-
$containerBuilder = new ContainerBuilder();
51+
$zfConfig = $zfContainer->get('config');
52+
$containerBuilder = new ContainerBuilder(new ZendFrameworkParameterBag($zfConfig));
5153
$loader = new YamlFileLoader($containerBuilder, new FileLocator([$configuration->getConfigDir()]));
5254
$loader->load('services.yaml');
5355

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
/**
3+
* This file is part of the Adlogix package.
4+
*
5+
* (c) Allan Segebarth <[email protected]>
6+
* (c) Jean-Jacques Courtens <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Adlogix\ZfSymfonyContainer\Test\ParameterBag;
13+
14+
use Adlogix\ZfSymfonyContainer\ParameterBag\ZendFrameworkParameterBag;
15+
16+
/**
17+
* @author Laurent De Coninck <[email protected]>
18+
*/
19+
class ZendFrameworkParameterBagTest extends \PHPUnit_Framework_TestCase
20+
{
21+
22+
/**
23+
* @param array $parameters
24+
*
25+
* @return ZendFrameworkParameterBag
26+
*/
27+
private function createParameterBag(array $parameters)
28+
{
29+
return new ZendFrameworkParameterBag($parameters);
30+
}
31+
32+
/**
33+
* @test
34+
*/
35+
public function add_WithValues_Success()
36+
{
37+
$data = [
38+
'key' => 'value1',
39+
'anotherKey' => '%value1%',
40+
];
41+
42+
$parameterBag = $this->createParameterBag($data);
43+
44+
$this->assertEquals('value1', $parameterBag->get('key'));
45+
$this->assertEquals('%%value1%%', $parameterBag->get('anotherKey'));
46+
}
47+
48+
/**
49+
* @test
50+
*/
51+
public function add_WithTables_Success()
52+
{
53+
$data = [
54+
'key' => 'value1',
55+
'associative' => [
56+
'a' => 1,
57+
'c' => 2,
58+
'd' => 3,
59+
],
60+
'index' => [
61+
'A',
62+
'B',
63+
'C',
64+
],
65+
'arrayOfArray' => [
66+
'a' => [
67+
'A',
68+
'B',
69+
'C',
70+
],
71+
],
72+
];
73+
74+
$parameterBag = $this->createParameterBag($data);
75+
$this->assertEquals('value1', $parameterBag->get('key'));
76+
$this->assertEquals(2, $parameterBag->get('associative.c'));
77+
$this->assertEquals('B', $parameterBag->get('index.1'));
78+
$this->assertEquals('B', $parameterBag->get('arrayOfArray.a.1'));
79+
}
80+
81+
/**
82+
* @test
83+
*/
84+
public function add_WithCallable_Skipped()
85+
{
86+
$data = [
87+
'callback' => function () {
88+
return true;
89+
},
90+
];
91+
92+
$parameterBag = $this->createParameterBag($data);
93+
94+
$this->assertFalse($parameterBag->has('callback'));
95+
}
96+
}

0 commit comments

Comments
 (0)