Skip to content

Commit 5fdf3d4

Browse files
committed
Allow only valid preferences during di:compile
1 parent d5cb373 commit 5fdf3d4

File tree

1 file changed

+33
-0
lines changed
  • setup/src/Magento/Setup/Module/Di/Compiler/Config

1 file changed

+33
-0
lines changed

setup/src/Magento/Setup/Module/Di/Compiler/Config/Reader.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
namespace Magento\Setup\Module\Di\Compiler\Config;
88

99
use Magento\Framework\App;
10+
use Magento\Framework\Exception\LocalizedException;
1011
use Magento\Framework\ObjectManager\ConfigInterface;
12+
use Magento\Framework\Phrase;
1113
use Magento\Setup\Module\Di\Code\Reader\ClassReaderDecorator;
1214
use Magento\Setup\Module\Di\Code\Reader\Type;
1315
use Magento\Setup\Module\Di\Compiler\ArgumentsResolverFactory;
@@ -92,6 +94,37 @@ public function generateCachePerScope(
9294
foreach ($definitionsCollection->getInstancesNamesList() as $instanceName) {
9395
$preference = $areaConfig->getPreference($instanceName);
9496
if ($instanceName !== $preference) {
97+
if (array_key_exists($preference, $areaConfig->getVirtualTypes())) {
98+
// Special handling is required for virtual types.
99+
$config['preferences'][$instanceName] = $preference;
100+
continue;
101+
}
102+
103+
if (!class_exists($preference)) {
104+
throw new LocalizedException(new Phrase(
105+
'Preference declared for "%instanceName" as "%preference", but the latter does not exist.',
106+
[
107+
'instanceName' => $instanceName,
108+
'preference' => $preference,
109+
]
110+
));
111+
}
112+
113+
// Classes defined by PHP extensions are allowed.
114+
$reflection = new \ReflectionClass($preference);
115+
if ($reflection->getExtension()) {
116+
$config['preferences'][$instanceName] = $preference;
117+
continue;
118+
}
119+
120+
if (!$definitionsCollection->hasInstance($preference)) {
121+
// Removing this preference, because its class has been excluded intentionally.
122+
// See 'excludePatterns' in Magento\Setup\Module\Di\Code\Reader\ClassesScanner,
123+
// populated via Magento\Setup\Console\Command\DiCompileCommand
124+
// TODO: add logging here so developers get useful messaging.
125+
continue;
126+
}
127+
95128
$config['preferences'][$instanceName] = $preference;
96129
}
97130
}

0 commit comments

Comments
 (0)