Skip to content

Commit 8bf0e25

Browse files
committed
Add class_alias for generated proxy classes
1 parent 1f08ed9 commit 8bf0e25

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

Dumper/PhpDumper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ private function generateProxyClasses(): array
567567
$proxyClass = explode(' ', $this->inlineRequires ? substr($proxyCode, \strlen($code)) : $proxyCode, 3)[1];
568568

569569
if ($this->asFiles || $this->namespace) {
570-
$proxyCode .= "\n\\class_alias(__NAMESPACE__.'\\\\$proxyClass', '$proxyClass', false);\n";
570+
$proxyCode .= "\nif (!\\class_exists('$proxyClass', false)) {\n \\class_alias(__NAMESPACE__.'\\\\$proxyClass', '$proxyClass', false);\n}\n";
571571
}
572572

573573
$proxyClasses[$proxyClass.'.php'] = $proxyCode;
@@ -1086,7 +1086,7 @@ private function addNewInstance(Definition $definition, string $return = '', str
10861086
// If the class is a string we can optimize away
10871087
if (0 === strpos($class, "'") && false === strpos($class, '$')) {
10881088
if ("''" === $class) {
1089-
throw new RuntimeException(sprintf('Cannot dump definition: %s service is defined to be created by a factory but is missing the service reference, did you forget to define the factory service id or class?', $id ? 'The "'.$id.'"' : 'inline'));
1089+
throw new RuntimeException(sprintf('Cannot dump definition: "%s" service is defined to be created by a factory but is missing the service reference, did you forget to define the factory service id or class?', $id ? 'The "'.$id.'"' : 'inline'));
10901090
}
10911091

10921092
return $return.sprintf('%s::%s(%s)', $this->dumpLiteralClass($class), $callable[1], $arguments ? implode(', ', $arguments) : '').$tail;

Tests/Fixtures/php/services9_lazy_inlined_factories.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,9 @@ class FooClass_%s extends \Bar\FooClass implements \ProxyManager\Proxy\VirtualPr
160160
%A
161161
}
162162

163-
\class_alias(__NAMESPACE__.'\\FooClass_%s', 'FooClass_%s', false);
163+
if (!\class_exists('FooClass_%s', false)) {
164+
\class_alias(__NAMESPACE__.'\\FooClass_%s', 'FooClass_%s', false);
165+
}
164166

165167
[ProjectServiceContainer.preload.php] => <?php
166168

0 commit comments

Comments
 (0)