AbstractEnvironment.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App\ObjectManager\Environment;
  7. use Magento\Framework\App\EnvironmentFactory;
  8. use Magento\Framework\Interception\ObjectManager\ConfigInterface;
  9. use Magento\Framework\App\EnvironmentInterface;
  10. use Magento\Framework\ObjectManager\Profiler\FactoryDecorator;
  11. use Magento\Framework\ObjectManager\FactoryInterface;
  12. use Magento\Framework\ObjectManager\Profiler\Log;
  13. abstract class AbstractEnvironment implements EnvironmentInterface
  14. {
  15. /**
  16. * @var ConfigInterface
  17. */
  18. protected $config;
  19. /**
  20. * Mode name
  21. */
  22. protected $mode = 'developer';
  23. /**
  24. * @var string
  25. */
  26. protected $configPreference = \Magento\Framework\ObjectManager\Factory\Dynamic\Developer::class;
  27. /**
  28. * @var FactoryInterface
  29. */
  30. protected $factory;
  31. /**
  32. * @var EnvironmentFactory
  33. */
  34. protected $envFactory;
  35. /**
  36. * @param EnvironmentFactory $envFactory
  37. */
  38. public function __construct(EnvironmentFactory $envFactory)
  39. {
  40. $this->envFactory = $envFactory;
  41. }
  42. /**
  43. * Returns object manager factory
  44. *
  45. * @param array $arguments
  46. * @return FactoryInterface
  47. */
  48. public function getObjectManagerFactory($arguments)
  49. {
  50. $factoryClass = $this->getDiConfig()->getPreference($this->configPreference);
  51. $this->factory = $this->createFactory($arguments, $factoryClass);
  52. $this->decorate($arguments);
  53. return $this->factory;
  54. }
  55. /**
  56. * Return name of running mode
  57. *
  58. * @return string
  59. */
  60. public function getMode()
  61. {
  62. return $this->mode;
  63. }
  64. /**
  65. * Decorate factory
  66. *
  67. * @param array $arguments
  68. * @return void
  69. */
  70. protected function decorate($arguments)
  71. {
  72. if (isset($arguments['MAGE_PROFILER']) && $arguments['MAGE_PROFILER'] == 2) {
  73. $this->factory = new FactoryDecorator(
  74. $this->factory,
  75. Log::getInstance()
  76. );
  77. }
  78. }
  79. /**
  80. * Creates factory
  81. *
  82. * @param array $arguments
  83. * @param string $factoryClass
  84. *
  85. * @return FactoryInterface
  86. */
  87. protected function createFactory($arguments, $factoryClass)
  88. {
  89. return new $factoryClass(
  90. $this->getDiConfig(),
  91. null,
  92. $this->envFactory->getDefinitions(),
  93. $arguments
  94. );
  95. }
  96. }