ObjectManagerFactory.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\TestFramework;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. use Magento\Framework\Exception\LocalizedException;
  9. use Magento\Framework\Filesystem\DriverPool;
  10. /**
  11. * Class ObjectManagerFactory
  12. *
  13. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  14. */
  15. class ObjectManagerFactory extends \Magento\Framework\App\ObjectManagerFactory
  16. {
  17. /**
  18. * Locator class name
  19. *
  20. * @var string
  21. */
  22. protected $_locatorClassName = \Magento\TestFramework\ObjectManager::class;
  23. /**
  24. * Config class name
  25. *
  26. * @var string
  27. */
  28. protected $_configClassName = \Magento\TestFramework\ObjectManager\Config::class;
  29. /**
  30. * @var string
  31. */
  32. protected $envFactoryClassName = \Magento\TestFramework\App\EnvironmentFactory::class;
  33. /**
  34. * @var array
  35. */
  36. protected $_primaryConfigData = null;
  37. /**
  38. * Restore locator instance
  39. *
  40. * @param ObjectManager $objectManager
  41. * @param DirectoryList $directoryList
  42. * @param array $arguments
  43. * @return ObjectManager
  44. */
  45. public function restore(ObjectManager $objectManager, $directoryList, array $arguments)
  46. {
  47. \Magento\TestFramework\ObjectManager::setInstance($objectManager);
  48. $this->directoryList = $directoryList;
  49. $objectManager->configure($this->_primaryConfigData);
  50. $objectManager->addSharedInstance($this->directoryList, \Magento\Framework\App\Filesystem\DirectoryList::class);
  51. $objectManager->addSharedInstance($this->directoryList, \Magento\Framework\Filesystem\DirectoryList::class);
  52. $deploymentConfig = $this->createDeploymentConfig($directoryList, $this->configFilePool, $arguments);
  53. $this->factory->setArguments($arguments);
  54. $objectManager->addSharedInstance($deploymentConfig, \Magento\Framework\App\DeploymentConfig::class);
  55. $objectManager->addSharedInstance(
  56. $objectManager->get(\Magento\Framework\App\ObjectManager\ConfigLoader::class),
  57. \Magento\Framework\ObjectManager\ConfigLoaderInterface::class
  58. );
  59. $objectManager->get(\Magento\Framework\Interception\PluginListInterface::class)->reset();
  60. $objectManager->configure(
  61. $objectManager->get(\Magento\Framework\App\ObjectManager\ConfigLoader::class)->load('global')
  62. );
  63. return $objectManager;
  64. }
  65. /**
  66. * Load primary config
  67. *
  68. * @param \Magento\Framework\App\Filesystem\DirectoryList $directoryList
  69. * @param DriverPool $driverPool
  70. * @param mixed $argumentMapper
  71. * @param string $appMode
  72. * @return array
  73. */
  74. protected function _loadPrimaryConfig(DirectoryList $directoryList, $driverPool, $argumentMapper, $appMode)
  75. {
  76. if (null === $this->_primaryConfigData) {
  77. $this->_primaryConfigData = array_replace(
  78. parent::_loadPrimaryConfig($directoryList, $driverPool, $argumentMapper, $appMode),
  79. [
  80. 'default_setup' => ['type' => \Magento\TestFramework\Db\ConnectionAdapter::class]
  81. ]
  82. );
  83. $diPreferences = [];
  84. $diPreferencesPath = __DIR__ . '/../../../etc/di/preferences/';
  85. $preferenceFiles = glob($diPreferencesPath . '*.php');
  86. foreach ($preferenceFiles as $file) {
  87. if (!is_readable($file)) {
  88. throw new LocalizedException(__("'%1' is not readable file.", $file));
  89. }
  90. $diPreferences = array_replace($diPreferences, include $file);
  91. }
  92. $this->_primaryConfigData['preferences'] = array_replace(
  93. $this->_primaryConfigData['preferences'],
  94. $diPreferences
  95. );
  96. }
  97. return $this->_primaryConfigData;
  98. }
  99. }