Compiled.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\ObjectManager\Factory;
  7. class Compiled extends AbstractFactory
  8. {
  9. /**
  10. * Object manager config
  11. *
  12. * @var \Magento\Framework\ObjectManager\ConfigInterface
  13. */
  14. protected $config;
  15. /**
  16. * Global arguments
  17. *
  18. * @var array
  19. */
  20. protected $globalArguments;
  21. /**
  22. * @var array
  23. */
  24. private $sharedInstances;
  25. /**
  26. * @param \Magento\Framework\ObjectManager\ConfigInterface $config
  27. * @param array $sharedInstances
  28. * @param array $globalArguments
  29. */
  30. public function __construct(
  31. \Magento\Framework\ObjectManager\ConfigInterface $config,
  32. &$sharedInstances = [],
  33. $globalArguments = []
  34. ) {
  35. $this->config = $config;
  36. $this->globalArguments = $globalArguments;
  37. $this->sharedInstances = &$sharedInstances;
  38. }
  39. /**
  40. * Create instance with call time arguments
  41. *
  42. * @param string $requestedType
  43. * @param array $arguments
  44. * @return object
  45. * @throws \Exception
  46. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  47. */
  48. public function create($requestedType, array $arguments = [])
  49. {
  50. $args = $this->config->getArguments($requestedType);
  51. $type = $this->config->getInstanceType($requestedType);
  52. if ($args === []) {
  53. // Case 1: no arguments required
  54. return new $type();
  55. } elseif ($args !== null) {
  56. /**
  57. * Case 2: arguments retrieved from pre-compiled DI cache
  58. *
  59. * Argument key meanings:
  60. *
  61. * _i_: shared instance of a class or interface
  62. * _ins_: non-shared instance of a class or interface
  63. * _v_: non-array literal value
  64. * _vac_: array, may be nested and contain other types of keys listed here (objects, array, nulls, etc)
  65. * _vn_: null value
  66. * _a_: value to be taken from named environment variable
  67. * _d_: default value in case environment variable specified by _a_ does not exist
  68. */
  69. foreach ($args as $key => &$argument) {
  70. if (isset($arguments[$key])) {
  71. $argument = $arguments[$key];
  72. } elseif (isset($argument['_i_'])) {
  73. $argument = $this->get($argument['_i_']);
  74. } elseif (isset($argument['_ins_'])) {
  75. $argument = $this->create($argument['_ins_']);
  76. } elseif (isset($argument['_v_'])) {
  77. $argument = $argument['_v_'];
  78. } elseif (isset($argument['_vac_'])) {
  79. $argument = $argument['_vac_'];
  80. $this->parseArray($argument);
  81. } elseif (isset($argument['_vn_'])) {
  82. $argument = null;
  83. } elseif (isset($argument['_a_'])) {
  84. if (isset($this->globalArguments[$argument['_a_']])) {
  85. $argument = $this->globalArguments[$argument['_a_']];
  86. } else {
  87. $argument = $argument['_d_'];
  88. }
  89. }
  90. }
  91. $args = array_values($args);
  92. } else {
  93. // Case 3: arguments retrieved in runtime
  94. $parameters = $this->getDefinitions()->getParameters($type) ?: [];
  95. $args = $this->resolveArgumentsInRuntime(
  96. $type,
  97. $parameters,
  98. $arguments
  99. );
  100. }
  101. return $this->createObject($type, $args);
  102. }
  103. /**
  104. * Parse array argument
  105. *
  106. * @param array $array
  107. *
  108. * @return void
  109. *
  110. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  111. */
  112. protected function parseArray(&$array)
  113. {
  114. foreach ($array as $key => &$argument) {
  115. if ($argument === (array)$argument) {
  116. if (isset($argument['_i_'])) {
  117. $argument = $this->get($argument['_i_']);
  118. } elseif (isset($argument['_ins_'])) {
  119. $argument = $this->create($argument['_ins_']);
  120. } elseif (isset($argument['_a_'])) {
  121. if (isset($this->globalArguments[$argument['_a_']])) {
  122. $argument = $this->globalArguments[$argument['_a_']];
  123. } else {
  124. $argument = $argument['_d_'];
  125. }
  126. } else {
  127. $this->parseArray($argument);
  128. }
  129. }
  130. }
  131. }
  132. /**
  133. * Retrieve cached object instance
  134. *
  135. * @param string $type
  136. * @return mixed
  137. */
  138. protected function get($type)
  139. {
  140. if (!isset($this->sharedInstances[$type])) {
  141. $this->sharedInstances[$type] = $this->create($type);
  142. }
  143. return $this->sharedInstances[$type];
  144. }
  145. }