Skip to content

Commit 8b20596

Browse files
committed
Multi container XML paths
1 parent 96ee630 commit 8b20596

File tree

4 files changed

+102
-16
lines changed

4 files changed

+102
-16
lines changed

README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ includes:
5656

5757
# Configuration
5858

59-
You have to provide a path to `srcDevDebugProjectContainer.xml` or similar XML file describing your container.
59+
You have to provide a path to XML container file generated by Symfony.
6060

61-
```yaml
61+
```neon
6262
parameters:
6363
symfony:
6464
containerXmlPath: var/cache/dev/srcDevDebugProjectContainer.xml
@@ -71,6 +71,24 @@ parameters:
7171
- var/cache/dev/Symfony/Config
7272
```
7373

74+
There is a multi environment solution. Container locator will check any location provided in `containerXmlPaths` parameter:
75+
76+
```neon
77+
parameters:
78+
symfony:
79+
containerXmlPaths:
80+
- var/cache/dev/srcDevDebugProjectContainer.xml
81+
- var/cache/test/srcTestDebugProjectContainer.xml
82+
# or with Symfony 4.2+
83+
containerXmlPaths:
84+
- var/cache/dev/srcApp_KernelDevDebugContainer.xml
85+
- var/cache/test/srcApp_KernelTestDebugContainer.xml
86+
# or with Symfony 5+
87+
containerXmlPaths:
88+
- var/cache/dev/App_KernelDevDebugContainer.xml
89+
- var/cache/test/App_KernelTestDebugContainer.xml
90+
```
91+
7492
## Constant hassers
7593

7694
Sometimes, when you are dealing with optional dependencies, the `::has()` methods can cause problems. For example, the following construct would complain that the condition is always either on or off, depending on whether you have the dependency for `service` installed:

src/Symfony/Configuration.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ public function getContainerXmlPath(): ?string
2121
return $this->parameters['containerXmlPath'] ?? $this->parameters['container_xml_path'] ?? null;
2222
}
2323

24+
/**
25+
* @return array<int, string>
26+
*/
27+
public function getContainerXmlPaths(): array
28+
{
29+
return $this->parameters['containerXmlPaths'] ?? [];
30+
}
31+
2432
public function hasConstantHassers(): bool
2533
{
2634
return $this->parameters['constantHassers'] ?? $this->parameters['constant_hassers'] ?? true;

src/Symfony/XmlParameterMapFactory.php

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,58 @@
77
final class XmlParameterMapFactory implements ParameterMapFactory
88
{
99

10-
/** @var string|null */
11-
private $containerXml;
10+
/** @var array<int, string> */
11+
private $containerXmls;
1212

1313
public function __construct(Configuration $configuration)
1414
{
15-
$this->containerXml = $configuration->getContainerXmlPath();
15+
$this->containerXmls = $configuration->getContainerXmlPaths();
16+
17+
$containerXml = $configuration->getContainerXmlPath();
18+
if ($containerXml === null) {
19+
return;
20+
}
21+
22+
$this->containerXmls = array_merge(
23+
[$containerXml],
24+
$this->containerXmls
25+
);
1626
}
1727

1828
public function create(): ParameterMap
1929
{
20-
if ($this->containerXml === null) {
30+
if (count($this->containerXmls) === 0) {
2131
return new FakeParameterMap();
2232
}
2333

24-
$fileContents = file_get_contents($this->containerXml);
34+
foreach ($this->containerXmls as $containerXml) {
35+
try {
36+
return $this->loadXml($containerXml);
37+
} catch (XmlContainerNotExistsException $e) {
38+
continue;
39+
}
40+
}
41+
42+
throw new XmlContainerNotExistsException(
43+
'Container not found. Attempted to load:' . PHP_EOL .
44+
implode(PHP_EOL, $this->containerXmls)
45+
);
46+
}
47+
48+
private function loadXml(string $containerXml): DefaultParameterMap
49+
{
50+
if (file_exists($containerXml) === false) {
51+
throw new XmlContainerNotExistsException(sprintf('Container %s does not exist', $containerXml));
52+
}
53+
54+
$fileContents = file_get_contents($containerXml);
2555
if ($fileContents === false) {
26-
throw new XmlContainerNotExistsException(sprintf('Container %s does not exist', $this->containerXml));
56+
throw new XmlContainerNotExistsException(sprintf('Container %s could not load the content', $containerXml));
2757
}
2858

2959
$xml = @simplexml_load_string($fileContents);
3060
if ($xml === false) {
31-
throw new XmlContainerNotExistsException(sprintf('Container %s cannot be parsed', $this->containerXml));
61+
throw new XmlContainerNotExistsException(sprintf('Container %s cannot be parsed', $containerXml));
3262
}
3363

3464
/** @var \PHPStan\Symfony\Parameter[] $parameters */

src/Symfony/XmlServiceMapFactory.php

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,58 @@
1010
final class XmlServiceMapFactory implements ServiceMapFactory
1111
{
1212

13-
/** @var string|null */
14-
private $containerXml;
13+
/** @var array<int, string> */
14+
private $containerXmls;
1515

1616
public function __construct(Configuration $configuration)
1717
{
18-
$this->containerXml = $configuration->getContainerXmlPath();
18+
$this->containerXmls = $configuration->getContainerXmlPaths();
19+
20+
$containerXml = $configuration->getContainerXmlPath();
21+
if ($containerXml === null) {
22+
return;
23+
}
24+
25+
$this->containerXmls = array_merge(
26+
[$containerXml],
27+
$this->containerXmls
28+
);
1929
}
2030

2131
public function create(): ServiceMap
2232
{
23-
if ($this->containerXml === null) {
33+
if (count($this->containerXmls) === 0) {
2434
return new FakeServiceMap();
2535
}
2636

27-
$fileContents = file_get_contents($this->containerXml);
37+
foreach ($this->containerXmls as $containerXml) {
38+
try {
39+
return $this->loadXml($containerXml);
40+
} catch (XmlContainerNotExistsException $e) {
41+
continue;
42+
}
43+
}
44+
45+
throw new XmlContainerNotExistsException(
46+
'Container not found. Attempted to load:' . PHP_EOL .
47+
implode(PHP_EOL, $this->containerXmls)
48+
);
49+
}
50+
51+
private function loadXml(string $containerXml): DefaultServiceMap
52+
{
53+
if (file_exists($containerXml) === false) {
54+
throw new XmlContainerNotExistsException(sprintf('Container %s does not exist', $containerXml));
55+
}
56+
57+
$fileContents = file_get_contents($containerXml);
2858
if ($fileContents === false) {
29-
throw new XmlContainerNotExistsException(sprintf('Container %s does not exist', $this->containerXml));
59+
throw new XmlContainerNotExistsException(sprintf('Container %s could not load the content', $containerXml));
3060
}
3161

3262
$xml = @simplexml_load_string($fileContents);
3363
if ($xml === false) {
34-
throw new XmlContainerNotExistsException(sprintf('Container %s cannot be parsed', $this->containerXml));
64+
throw new XmlContainerNotExistsException(sprintf('Container %s cannot be parsed', $containerXml));
3565
}
3666

3767
/** @var \PHPStan\Symfony\Service[] $services */

0 commit comments

Comments
 (0)