Compiled.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\ObjectManager\Config;
  7. use Magento\Framework\ObjectManager\ConfigInterface;
  8. use Magento\Framework\ObjectManager\ConfigCacheInterface;
  9. use Magento\Framework\ObjectManager\RelationsInterface;
  10. /**
  11. * Provides object manager configuration when in compiled mode
  12. */
  13. class Compiled implements ConfigInterface
  14. {
  15. /**
  16. * @var array
  17. */
  18. private $arguments;
  19. /**
  20. * @var array
  21. */
  22. private $virtualTypes;
  23. /**
  24. * @var array
  25. */
  26. private $preferences;
  27. /**
  28. * @param array $data
  29. */
  30. public function __construct($data)
  31. {
  32. $this->arguments = isset($data['arguments']) && is_array($data['arguments'])
  33. ? $data['arguments'] : [];
  34. $this->virtualTypes = isset($data['instanceTypes']) && is_array($data['instanceTypes'])
  35. ? $data['instanceTypes'] : [];
  36. $this->preferences = isset($data['preferences']) && is_array($data['preferences'])
  37. ? $data['preferences'] : [];
  38. }
  39. /**
  40. * Set class relations
  41. *
  42. * @param RelationsInterface $relations
  43. *
  44. * @return void
  45. *
  46. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  47. */
  48. public function setRelations(RelationsInterface $relations)
  49. {
  50. }
  51. /**
  52. * Set configuration cache instance
  53. *
  54. * @param ConfigCacheInterface $cache
  55. *
  56. * @return void
  57. *
  58. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  59. */
  60. public function setCache(ConfigCacheInterface $cache)
  61. {
  62. }
  63. /**
  64. * Retrieve list of arguments per type
  65. *
  66. * @param string $type
  67. * @return array
  68. */
  69. public function getArguments($type)
  70. {
  71. if (array_key_exists($type, $this->arguments)) {
  72. if ($this->arguments[$type] === null) {
  73. $this->arguments[$type] = [];
  74. }
  75. return $this->arguments[$type];
  76. } else {
  77. return null;
  78. }
  79. }
  80. /**
  81. * Check whether type is shared
  82. *
  83. * @param string $type
  84. * @return bool
  85. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  86. */
  87. public function isShared($type)
  88. {
  89. return true;
  90. }
  91. /**
  92. * Retrieve instance type
  93. *
  94. * @param string $instanceName
  95. * @return mixed
  96. */
  97. public function getInstanceType($instanceName)
  98. {
  99. if (isset($this->virtualTypes[$instanceName])) {
  100. return $this->virtualTypes[$instanceName];
  101. }
  102. return $instanceName;
  103. }
  104. /**
  105. * Retrieve preference for type
  106. *
  107. * @param string $type
  108. * @return string
  109. * @throws \LogicException
  110. */
  111. public function getPreference($type)
  112. {
  113. $type = ltrim($type, '\\');
  114. if (isset($this->preferences[$type])) {
  115. return $this->preferences[$type];
  116. }
  117. return $type;
  118. }
  119. /**
  120. * Extend configuration
  121. *
  122. * @param array $configuration
  123. * @return void
  124. */
  125. public function extend(array $configuration)
  126. {
  127. $this->arguments = isset($configuration['arguments']) && is_array($configuration['arguments'])
  128. ? array_replace($this->arguments, $configuration['arguments'])
  129. : $this->arguments;
  130. $this->virtualTypes = isset($configuration['instanceTypes']) && is_array($configuration['instanceTypes'])
  131. ? array_replace($this->virtualTypes, $configuration['instanceTypes'])
  132. : $this->virtualTypes;
  133. $this->preferences = isset($configuration['preferences']) && is_array($configuration['preferences'])
  134. ? array_replace($this->preferences, $configuration['preferences'])
  135. : $this->preferences;
  136. }
  137. /**
  138. * Retrieve all virtual types
  139. *
  140. * @return string
  141. */
  142. public function getVirtualTypes()
  143. {
  144. return $this->virtualTypes;
  145. }
  146. /**
  147. * Returns list on preferences
  148. *
  149. * @return array
  150. */
  151. public function getPreferences()
  152. {
  153. return $this->preferences;
  154. }
  155. }