ObjectManager.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\TestFramework;
  7. /**
  8. * ObjectManager for integration test framework.
  9. */
  10. class ObjectManager extends \Magento\Framework\App\ObjectManager
  11. {
  12. /**
  13. * Classes with xml properties to explicitly call __destruct() due to https://bugs.php.net/bug.php?id=62468
  14. *
  15. * @var array
  16. */
  17. protected $_classesToDestruct = [
  18. \Magento\Framework\View\Layout::class,
  19. \Magento\Framework\Registry::class
  20. ];
  21. /**
  22. * @var array
  23. */
  24. protected $persistedInstances = [
  25. \Magento\Framework\App\ResourceConnection::class,
  26. \Magento\Framework\Config\Scope::class,
  27. \Magento\Framework\ObjectManager\RelationsInterface::class,
  28. \Magento\Framework\ObjectManager\ConfigInterface::class,
  29. \Magento\Framework\Interception\DefinitionInterface::class,
  30. \Magento\Framework\ObjectManager\DefinitionInterface::class,
  31. \Magento\Framework\Session\Config::class,
  32. \Magento\Framework\ObjectManager\Config\Mapper\Dom::class,
  33. ];
  34. /**
  35. * Clear InstanceManager cache.
  36. *
  37. * @return \Magento\TestFramework\ObjectManager
  38. */
  39. public function clearCache()
  40. {
  41. foreach ($this->_classesToDestruct as $className) {
  42. if (isset($this->_sharedInstances[$className])) {
  43. $this->_sharedInstances[$className] = null;
  44. }
  45. }
  46. \Magento\Framework\App\Config\Base::destroy();
  47. $sharedInstances = [
  48. \Magento\Framework\ObjectManagerInterface::class => $this,
  49. \Magento\Framework\App\ObjectManager::class => $this,
  50. ];
  51. foreach ($this->persistedInstances as $persistedClass) {
  52. if (isset($this->_sharedInstances[$persistedClass])) {
  53. $sharedInstances[$persistedClass] = $this->_sharedInstances[$persistedClass];
  54. }
  55. }
  56. $this->_sharedInstances = $sharedInstances;
  57. $this->_config->clean();
  58. $this->clearMappedTableNames();
  59. return $this;
  60. }
  61. /**
  62. * Clear mapped table names list.
  63. *
  64. * @return void
  65. */
  66. private function clearMappedTableNames()
  67. {
  68. $resourceConnection = $this->get(\Magento\Framework\App\ResourceConnection::class);
  69. if ($resourceConnection) {
  70. $reflection = new \ReflectionClass($resourceConnection);
  71. $dataProperty = $reflection->getProperty('mappedTableNames');
  72. $dataProperty->setAccessible(true);
  73. $dataProperty->setValue($resourceConnection, null);
  74. }
  75. }
  76. /**
  77. * Add shared instance.
  78. *
  79. * @param mixed $instance
  80. * @param string $className
  81. * @return void
  82. */
  83. public function addSharedInstance($instance, $className)
  84. {
  85. $this->_sharedInstances[$className] = $instance;
  86. }
  87. /**
  88. * Remove shared instance.
  89. *
  90. * @param string $className
  91. * @return void
  92. */
  93. public function removeSharedInstance($className)
  94. {
  95. unset($this->_sharedInstances[$className]);
  96. }
  97. /**
  98. * Set objectManager.
  99. *
  100. * @param \Magento\Framework\ObjectManagerInterface $objectManager
  101. * @return \Magento\Framework\ObjectManagerInterface
  102. */
  103. public static function setInstance(\Magento\Framework\ObjectManagerInterface $objectManager)
  104. {
  105. return self::$_instance = $objectManager;
  106. }
  107. /**
  108. * @return \Magento\Framework\ObjectManager\FactoryInterface|\Magento\Framework\ObjectManager\Factory\Factory
  109. */
  110. public function getFactory()
  111. {
  112. return $this->_factory;
  113. }
  114. }