Skip to content

Allow only valid preferences during setup:di:compile #33161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
aa10200
Fix regular expressions for 'exclude tests'
fredden Jun 7, 2021
d5cb373
Revert "Fix regular expressions for 'exclude tests'"
fredden Jun 7, 2021
5fdf3d4
Allow only valid preferences during di:compile
fredden Jun 7, 2021
38c2061
Remove invalid preferences
fredden Jun 7, 2021
0677c17
Allow classes without contructors
fredden Jun 7, 2021
1188e03
Fix type error
fredden Jun 7, 2021
d3a8b3f
Allow preferences for classes in the 'setup' area
fredden Jun 7, 2021
84c45e4
Make noise on DI compile failure
fredden Jun 7, 2021
f24f574
Fix argumentresolver unit test failure
fredden Jun 9, 2021
38d7e5d
Fix test for DI config reader
fredden Jun 15, 2021
0e05a21
Merge remote-tracking branch 'upstream/2.4-develop' into di-compile-t…
fredden Jun 15, 2021
44db149
Merge remote-tracking branch 'upstream/2.4-develop' into di-compile-t…
fredden Jun 28, 2021
b2529df
Merge branch '2.4-develop' into di-compile-tests-regex
fredden Jul 21, 2021
b57132e
Merge remote-tracking branch 'upstream/2.4-develop' into di-compile-t…
fredden Sep 14, 2021
eb21222
Merge remote-tracking branch 'upstream/2.4-develop' into di-compile-t…
fredden Dec 31, 2021
6586d00
Merge branch '2.4-develop' into di-compile-tests-regex
fredden Apr 12, 2022
028d15f
Merge branch '2.4-develop' into di-compile-tests-regex
fredden Oct 21, 2022
de0e3f0
Merge branch '2.4-develop' into di-compile-tests-regex
fredden Nov 4, 2022
71eafa7
Merge branch '2.4-develop' into di-compile-tests-regex
ihor-sviziev Jan 3, 2024
9740343
Merge branch '2.4-develop' into di-compile-tests-regex
andrewbess Jan 27, 2024
d42d631
Merge branch '2.4-develop' into di-compile-tests-regex
engcom-Hotel Feb 1, 2024
f0990fc
Merge branch '2.4-develop' into di-compile-tests-regex
engcom-Hotel Feb 6, 2024
2c2039e
Merge branch '2.4-develop' into di-compile-tests-regex
engcom-Hotel Feb 15, 2024
d4a4882
Merge remote-tracking branch 'upstream/2.4-develop' into di-compile-t…
fredden Feb 22, 2024
69e3761
Adhere to coding standard
fredden Feb 22, 2024
0a193a4
Merge branch '2.4-develop' into di-compile-tests-regex
engcom-Hotel Feb 26, 2024
4bcc36b
Merge branch '2.4-develop' into di-compile-tests-regex
engcom-Hotel Mar 8, 2024
b8ca859
Merge branch '2.4-develop' into di-compile-tests-regex
engcom-Charlie Jun 3, 2024
05d467d
Merge branch '2.4-develop' into di-compile-tests-regex
fredden Jul 14, 2024
717828c
Merge branch '2.4-develop' into di-compile-tests-regex
fredden Sep 10, 2024
23583c0
Merge branch '2.4-develop' into di-compile-tests-regex
engcom-Hotel Oct 1, 2024
cb33164
33161: Fix static failure related to copyright tag
engcom-Dash Nov 8, 2024
7d2fc7d
33161: Fix static failure related to copyright tag
engcom-Dash Nov 8, 2024
59e9ddd
Merge branch '2.4-develop' into di-compile-tests-regex
engcom-Dash Nov 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions app/code/Magento/Catalog/etc/di.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2013 Adobe
* All Rights Reserved.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
Expand Down Expand Up @@ -73,7 +73,6 @@
<preference for="Magento\Catalog\Model\Indexer\Product\Price\UpdateIndexInterface" type="Magento\Catalog\Model\Indexer\Product\Price\InvalidateIndex" />
<preference for="Magento\Catalog\Model\Product\Gallery\ImagesConfigFactoryInterface" type="Magento\Catalog\Model\Product\Gallery\ImagesConfigFactory" />
<preference for="Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface" type="Magento\Catalog\Model\Product\Configuration\Item\ItemResolverComposite" />
<preference for="Magento\Catalog\Api\Data\MassActionInterface" type="Magento\Catalog\Model\MassAction" />
<preference for="Magento\Catalog\Model\ProductLink\Data\ListCriteriaInterface" type="Magento\Catalog\Model\ProductLink\Data\ListCriteria" />
<preference for="Magento\Catalog\Api\CategoryListDeleteBySkuInterface" type="Magento\Catalog\Model\CategoryLinkRepository"/>
<preference for="Magento\Theme\CustomerData\MessagesProviderInterface" type="Magento\Catalog\Model\Theme\CustomerData\MessagesProvider"/>
Expand Down
5 changes: 2 additions & 3 deletions app/code/Magento/RemoteStorage/etc/di.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2020 Adobe
* All Rights Reserved.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\RemoteStorage\Driver\Adapter\Cache\CacheInterface" type="Magento\RemoteStorage\Driver\Adapter\Cache\Generic"/>
<preference for="Magento\RemoteStorage\Driver\Adapter\CachedAdapterInterface" type="Magento\RemoteStorage\Driver\Adapter\CachedAdapter"/>
<preference for="Magento\RemoteStorage\Driver\Adapter\MetadataProviderInterface" type="Magento\RemoteStorage\Driver\Adapter\MetadataProvider"/>
<preference for="Magento\RemoteStorage\Driver\Adapter\MetadataProviderFactoryInterface" type="Magento\RemoteStorage\Driver\Adapter\MetadataProviderFactory"/>
<preference for="Magento\Framework\Filesystem\DriverPool" type="Magento\RemoteStorage\Driver\DriverPool"/>
<preference for="Magento\Framework\File\Uploader" type="Magento\RemoteStorage\Model\File\Uploader"/>
<virtualType name="remoteWriteFactory" type="Magento\Framework\Filesystem\Directory\WriteFactory">
Expand Down
5 changes: 2 additions & 3 deletions app/etc/di.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2013 Adobe
* All Rights Reserved.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
Expand Down Expand Up @@ -82,7 +82,6 @@
<preference for="Magento\Framework\Json\DecoderInterface" type="Magento\Framework\Json\Decoder" />
<preference for="Magento\Framework\Message\ManagerInterface" type="Magento\Framework\Message\Manager" />
<preference for="Magento\Framework\App\Config\ValueInterface" type="Magento\Framework\App\Config\Value" />
<preference for="Magento\Framework\Interception\ChainInterface" type="Magento\Framework\Interception\Chain\Chain" />
<preference for="Magento\Framework\Module\Output\ConfigInterface" type="Magento\Framework\Module\Output\Config" />
<preference for="Magento\Framework\View\Design\Theme\CustomizationInterface" type="Magento\Framework\View\Design\Theme\Customization" />
<preference for="Magento\Framework\View\Asset\ConfigInterface" type="Magento\Framework\View\Asset\Config" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php
/**
* Copyright 2011 Adobe
* All Rights Reserved.
*
* Obsolete classes
*
* Format: array(<class_name>[, <replacement>])
*
* @codingStandardsIgnoreFile
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
return [
['Mage_Admin_Helper_Data', 'Magento\Backend\Helper\Data'],
Expand Down Expand Up @@ -2845,7 +2845,7 @@
['Magento\Framework\Stdlib\CookieManager', 'Magento\Framework\Stdlib\CookieManagerInterface'],
['Magento\Framework\Interception\PluginList', 'Magento\Framework\Interception\PluginListInterface'],
['Magento\Framework\Interception\Config', 'Magento\Framework\Interception\ConfigInterface'],
['Magento\Framework\Interception\Chain', 'Magento\Framework\Interception\ChainInterface'],
['Magento\Framework\Interception\Chain'],
['Magento\Framework\Interception\Definition', 'Magento\Framework\Interception\DefinitionInterface'],
['Magento\Framework\ObjectManager\Factory', 'Magento\Framework\ObjectManager\FactoryInterface'],
['Magento\Framework\ObjectManager\Config', 'Magento\Framework\ObjectManager\ConfigInterface'],
Expand Down
21 changes: 15 additions & 6 deletions setup/src/Magento/Setup/Console/Command/DiCompileCommand.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2015 Adobe
* All Rights Reserved.
*/

namespace Magento\Setup\Console\Command;

use Magento\Framework\App\ObjectManager;
Expand Down Expand Up @@ -30,8 +31,7 @@
*/
class DiCompileCommand extends Command
{
/** Command name */
const NAME = 'setup:di:compile';
public const NAME = 'setup:di:compile';

/**
* @var \Magento\Framework\App\DeploymentConfig
Expand Down Expand Up @@ -79,8 +79,6 @@ class DiCompileCommand extends Command
private $file;

/**
* Constructor
*
* @param DeploymentConfig $deploymentConfig
* @param DirectoryList $directoryList
* @param Manager $taskManager
Expand All @@ -89,6 +87,7 @@ class DiCompileCommand extends Command
* @param DriverInterface $fileDriver
* @param \Magento\Framework\Component\ComponentRegistrar $componentRegistrar
* @param File|null $file
*
* @throws \Magento\Setup\Exception
*/
public function __construct(
Expand Down Expand Up @@ -151,6 +150,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
foreach ($errors as $line) {
$output->writeln($line);
}

// we must have an exit code higher than zero to indicate something was wrong
return Cli::RETURN_FAILURE;
}
Expand Down Expand Up @@ -224,13 +224,15 @@ function (OperationInterface $operation) use ($progressBar) {
// we must have an exit code higher than zero to indicate something was wrong
return Cli::RETURN_FAILURE;
}

return Cli::RETURN_SUCCESS;
}

/**
* Build list of module path regexps which should be excluded from compilation
*
* @param string[] $modulePaths
*
* @return string[]
*/
private function getExcludedModulePaths(array $modulePaths)
Expand All @@ -251,6 +253,7 @@ private function getExcludedModulePaths(array $modulePaths)
$vendorPathsRegExps[] = $vendorDir
. '/(?:' . join('|', $vendorModules) . ')';
}

$basePathsRegExps[] = preg_quote($basePath, '#')
. '/(?:' . join('|', $vendorPathsRegExps) . ')';
}
Expand All @@ -266,6 +269,7 @@ private function getExcludedModulePaths(array $modulePaths)
* Build list of library path regexps which should be excluded from compilation
*
* @param string[] $libraryPaths
*
* @return string[]
*/
private function getExcludedLibraryPaths(array $libraryPaths)
Expand All @@ -288,6 +292,7 @@ function ($libraryPath) {
* Get excluded setup application paths
*
* @param string $setupPath
*
* @return string[]
*/
private function getExcludedSetupPaths($setupPath)
Expand All @@ -301,6 +306,7 @@ private function getExcludedSetupPaths($setupPath)
* Delete directories by their code from "var" directory
*
* @param array $directoryCodeList
*
* @return void
*/
private function cleanupFilesystem($directoryCodeList)
Expand All @@ -314,6 +320,7 @@ private function cleanupFilesystem($directoryCodeList)
* Configure Object Manager
*
* @param OutputInterface $output
*
* @return void
*/
private function configureObjectManager(OutputInterface $output)
Expand Down Expand Up @@ -365,6 +372,7 @@ private function configureObjectManager(OutputInterface $output)
* Returns operations configuration
*
* @param array $compiledPathsList
*
* @return array
*/
private function getOperationsConfiguration(
Expand Down Expand Up @@ -399,6 +407,7 @@ private function getOperationsConfiguration(
OperationFactory::AREA_CONFIG_GENERATOR => [
$compiledPathsList['application'],
$compiledPathsList['library'],
$compiledPathsList['setup'],
$compiledPathsList['generated_helpers'],
],
OperationFactory::INTERCEPTION_CACHE => [
Expand Down
12 changes: 4 additions & 8 deletions setup/src/Magento/Setup/Module/Di/Code/Reader/Decorator/Area.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2015 Adobe
* All Rights Reserved.
*/

namespace Magento\Setup\Module\Di\Code\Reader\Decorator;

use Magento\Setup\Module\Di\Code\Reader\ClassesScanner;
use Magento\Setup\Module\Di\Code\Reader\ClassReaderDecorator;
use Magento\Framework\Exception\FileSystemException;

/**
* Class Area
*
* @package Magento\Setup\Module\Di\Code\Reader\Decorator
*/
class Area implements \Magento\Setup\Module\Di\Code\Reader\ClassesScannerInterface
{
/**
Expand Down Expand Up @@ -50,7 +46,7 @@ public function getList($path)
{
$classes = [];
foreach ($this->classesScanner->getList($path) as $className) {
$classes[$className] = $this->classReaderDecorator->getConstructor($className);
$classes[$className] = (array) $this->classReaderDecorator->getConstructor($className);
}

return $classes;
Expand Down
11 changes: 7 additions & 4 deletions setup/src/Magento/Setup/Module/Di/Compiler/ArgumentsResolver.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2015 Adobe
* All Rights Reserved.
*/

namespace Magento\Setup\Module\Di\Compiler;
Expand Down Expand Up @@ -193,7 +192,11 @@ private function getConfiguredArguments($instanceType)
$configuredArguments = $this->diContainerConfig->getArguments($instanceType);
return array_map(
function ($type) {
if (isset($type['instance'])) {
if (is_object($type)) {
return;
}

if (is_array($type) && isset($type['instance'])) {
$type['instance'] = ltrim($type['instance'], '\\');
}

Expand Down
42 changes: 40 additions & 2 deletions setup/src/Magento/Setup/Module/Di/Compiler/Config/Reader.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2015 Adobe
* All Rights Reserved.
*/

namespace Magento\Setup\Module\Di\Compiler\Config;

use Magento\Framework\App;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\ObjectManager\ConfigInterface;
use Magento\Framework\Phrase;
use Magento\Setup\Module\Di\Code\Reader\ClassReaderDecorator;
use Magento\Setup\Module\Di\Code\Reader\Type;
use Magento\Setup\Module\Di\Compiler\ArgumentsResolverFactory;
Expand Down Expand Up @@ -91,6 +93,42 @@ public function generateCachePerScope(
foreach ($definitionsCollection->getInstancesNamesList() as $instanceName) {
$preference = $areaConfig->getPreference($instanceName);
if ($instanceName !== $preference) {
if (array_key_exists($preference, $areaConfig->getVirtualTypes())) {
// Special handling is required for virtual types.
$config['preferences'][$instanceName] = $preference;
continue;
}

if (!class_exists($preference)) {
throw new LocalizedException(new Phrase(
'Preference declared for "%instanceName" as "%preference", but the latter does not exist.',
[
'instanceName' => $instanceName,
'preference' => $preference,
]
));
}

// Classes defined by PHP extensions are allowed.
$reflection = new \ReflectionClass($preference);
if ($reflection->getExtension()) {
$config['preferences'][$instanceName] = $preference;
continue;
}

if (!$definitionsCollection->hasInstance($preference)) {
// See 'excludePatterns' in Magento\Setup\Module\Di\Code\Reader\ClassesScanner,
// populated via Magento\Setup\Console\Command\DiCompileCommand
throw new LocalizedException(new Phrase(
'Preference declared for "%instanceName" as "%preference", but the latter'
. ' has not been included in dependency injection compilation.',
[
'instanceName' => $instanceName,
'preference' => $preference,
]
));
}

$config['preferences'][$instanceName] = $preference;
}
}
Expand Down
6 changes: 3 additions & 3 deletions setup/src/Magento/Setup/Module/Di/Definition/Collection.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2015 Adobe
* All Rights Reserved.
*/

namespace Magento\Setup\Module\Di\Definition;
Expand Down Expand Up @@ -91,6 +91,6 @@ public function getInstancesNamesList()
*/
public function hasInstance($instanceName)
{
return isset($this->definitions[$instanceName]);
return array_key_exists($instanceName, $this->definitions);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2015 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);

Expand Down Expand Up @@ -174,8 +174,8 @@ private function getVirtualTypes()
private function getPreferences()
{
return [
'Interface1' => 'ConcreteType1',
'ThirdPartyInterface' => 'ConcreteType2'
'Interface1' => 'DateTime',
'ThirdPartyInterface' => 'DateTimeZone',
];
}

Expand All @@ -187,8 +187,8 @@ private function getPreferencesMap()
return [
['ConcreteType1', 'ConcreteType1'],
['ConcreteType2', 'ConcreteType2'],
['Interface1', 'ConcreteType1'],
['ThirdPartyInterface', 'ConcreteType2']
['Interface1', 'DateTime'],
['ThirdPartyInterface', 'DateTimeZone'],
];
}

Expand Down