Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4ebbb9f

Browse files
committed
Multi container XML paths
1 parent 96ee630 commit 4ebbb9f

File tree

4 files changed

+116
-10
lines changed

4 files changed

+116
-10
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: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,51 @@ final class XmlParameterMapFactory implements ParameterMapFactory
1010
/** @var string|null */
1111
private $containerXml;
1212

13+
/** @var array<int, string> */
14+
private $paths;
15+
1316
public function __construct(Configuration $configuration)
1417
{
1518
$this->containerXml = $configuration->getContainerXmlPath();
19+
$this->paths = $configuration->getContainerXmlPaths();
1620
}
1721

1822
public function create(): ParameterMap
1923
{
20-
if ($this->containerXml === null) {
24+
$this->addSingleContainerToPaths();
25+
26+
if (count($this->paths) === 0) {
2127
return new FakeParameterMap();
2228
}
2329

24-
$fileContents = file_get_contents($this->containerXml);
30+
foreach ($this->paths as $path) {
31+
try {
32+
return $this->loadContainer($path);
33+
} catch (XmlContainerNotExistsException $e) {
34+
continue;
35+
}
36+
}
37+
38+
throw new XmlContainerNotExistsException(
39+
'Container not found. Attempted to load:' . PHP_EOL .
40+
implode(PHP_EOL, $this->paths)
41+
);
42+
}
43+
44+
private function loadContainer(string $path): DefaultParameterMap
45+
{
46+
if (file_exists($path) === false) {
47+
throw new XmlContainerNotExistsException(sprintf('Container %s does not exist', $path));
48+
}
49+
50+
$fileContents = file_get_contents($path);
2551
if ($fileContents === false) {
26-
throw new XmlContainerNotExistsException(sprintf('Container %s does not exist', $this->containerXml));
52+
throw new XmlContainerNotExistsException(sprintf('Container %s could not load the content', $path));
2753
}
2854

2955
$xml = @simplexml_load_string($fileContents);
3056
if ($xml === false) {
31-
throw new XmlContainerNotExistsException(sprintf('Container %s cannot be parsed', $this->containerXml));
57+
throw new XmlContainerNotExistsException(sprintf('Container %s cannot be parsed', $path));
3258
}
3359

3460
/** @var \PHPStan\Symfony\Parameter[] $parameters */
@@ -103,4 +129,18 @@ private function getNodeValue(\SimpleXMLElement $def)
103129
return $value;
104130
}
105131

132+
private function addSingleContainerToPaths(): void
133+
{
134+
$containerXml = $this->containerXml;
135+
136+
if ($containerXml === null) {
137+
return;
138+
}
139+
140+
$this->paths = array_merge(
141+
[$containerXml],
142+
$this->paths
143+
);
144+
}
145+
106146
}

‎src/Symfony/XmlServiceMapFactory.php‎

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,51 @@ final class XmlServiceMapFactory implements ServiceMapFactory
1313
/** @var string|null */
1414
private $containerXml;
1515

16+
/** @var array<int, string> */
17+
private $paths;
18+
1619
public function __construct(Configuration $configuration)
1720
{
1821
$this->containerXml = $configuration->getContainerXmlPath();
22+
$this->paths = $configuration->getContainerXmlPaths();
1923
}
2024

2125
public function create(): ServiceMap
2226
{
23-
if ($this->containerXml === null) {
27+
$this->addSingleContainerToPaths();
28+
29+
if (count($this->paths) === 0) {
2430
return new FakeServiceMap();
2531
}
2632

27-
$fileContents = file_get_contents($this->containerXml);
33+
foreach ($this->paths as $path) {
34+
try {
35+
return $this->loadContainer($path);
36+
} catch (XmlContainerNotExistsException $e) {
37+
continue;
38+
}
39+
}
40+
41+
throw new XmlContainerNotExistsException(
42+
'Container not found. Attempted to load:' . PHP_EOL .
43+
implode(PHP_EOL, $this->paths)
44+
);
45+
}
46+
47+
private function loadContainer(string $path): DefaultServiceMap
48+
{
49+
if (file_exists($path) === false) {
50+
throw new XmlContainerNotExistsException(sprintf('Container %s does not exist', $path));
51+
}
52+
53+
$fileContents = file_get_contents($path);
2854
if ($fileContents === false) {
29-
throw new XmlContainerNotExistsException(sprintf('Container %s does not exist', $this->containerXml));
55+
throw new XmlContainerNotExistsException(sprintf('Container %s could not load the content', $path));
3056
}
3157

3258
$xml = @simplexml_load_string($fileContents);
3359
if ($xml === false) {
34-
throw new XmlContainerNotExistsException(sprintf('Container %s cannot be parsed', $this->containerXml));
60+
throw new XmlContainerNotExistsException(sprintf('Container %s cannot be parsed', $path));
3561
}
3662

3763
/** @var \PHPStan\Symfony\Service[] $services */
@@ -77,4 +103,18 @@ public function create(): ServiceMap
77103
return new DefaultServiceMap($services);
78104
}
79105

106+
private function addSingleContainerToPaths(): void
107+
{
108+
$containerXml = $this->containerXml;
109+
110+
if ($containerXml === null) {
111+
return;
112+
}
113+
114+
$this->paths = array_merge(
115+
[$containerXml],
116+
$this->paths
117+
);
118+
}
119+
80120
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /