Skip to content

Commit 5ed9e88

Browse files
[DI] Fix initialization of legacy containers by delaying include_once
1 parent 4ceae5e commit 5ed9e88

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

Container.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,13 @@ public function setParameter($name, $value)
167167
*/
168168
public function set($id, $service)
169169
{
170+
// Runs the internal initializer; used by the dumped container to include always-needed files
171+
if (isset($this->privates['service_container']) && $this->privates['service_container'] instanceof \Closure) {
172+
$initialize = $this->privates['service_container'];
173+
unset($this->privates['service_container']);
174+
$initialize();
175+
}
176+
170177
$id = $this->normalizeId($id);
171178

172179
if ('service_container' === $id) {

Dumper/PhpDumper.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,16 +1243,16 @@ private function addInlineRequires()
12431243
}
12441244
}
12451245

1246-
$code = "\n";
1246+
$code = '';
12471247

12481248
foreach ($lineage as $file) {
12491249
if (!isset($this->inlinedRequires[$file])) {
12501250
$this->inlinedRequires[$file] = true;
1251-
$code .= sprintf(" include_once %s;\n", $file);
1251+
$code .= sprintf("\n include_once %s;", $file);
12521252
}
12531253
}
12541254

1255-
return "\n" === $code ? '' : $code;
1255+
return $code ? sprintf("\n \$this->privates['service_container'] = function () {%s\n };\n", $code) : '';
12561256
}
12571257

12581258
/**

Tests/Fixtures/php/services_inline_requires.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@ public function __construct()
4646

4747
$this->aliases = array();
4848

49-
include_once $this->targetDirs[1].'/includes/HotPath/I1.php';
50-
include_once $this->targetDirs[1].'/includes/HotPath/P1.php';
51-
include_once $this->targetDirs[1].'/includes/HotPath/T1.php';
52-
include_once $this->targetDirs[1].'/includes/HotPath/C1.php';
49+
$this->privates['service_container'] = function () {
50+
include_once $this->targetDirs[1].'/includes/HotPath/I1.php';
51+
include_once $this->targetDirs[1].'/includes/HotPath/P1.php';
52+
include_once $this->targetDirs[1].'/includes/HotPath/T1.php';
53+
include_once $this->targetDirs[1].'/includes/HotPath/C1.php';
54+
};
5355
}
5456

5557
public function getRemovedIds()

0 commit comments

Comments
 (0)