Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.62 KB | None | 0 0
  1. <?php
  2. /* FILE LOCATION: /public_html/vendor/magento/module-authorizenet/Model/Directpost/Request.php */
  3. /**
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Framework\ObjectManager\Factory;
  8.  
  9. use Magento\Framework\Exception\RuntimeException;
  10. use Magento\Framework\ObjectManagerInterface;
  11. use Magento\Framework\Phrase;
  12. use Psr\Log\LoggerInterface;
  13. use Magento\Framework\App\ObjectManager;
  14.  
  15. abstract class AbstractFactory implements \Magento\Framework\ObjectManager\FactoryInterface
  16. {
  17. /**
  18. * Object manager
  19. *
  20. * @var ObjectManagerInterface
  21. */
  22. protected $objectManager;
  23.  
  24. /**
  25. * Object manager config
  26. *
  27. * @var \Magento\Framework\ObjectManager\ConfigInterface
  28. */
  29. protected $config;
  30.  
  31. /**
  32. * Definition list
  33. *
  34. * @var \Magento\Framework\ObjectManager\DefinitionInterface
  35. */
  36. protected $definitions;
  37.  
  38. /**
  39. * Global arguments
  40. *
  41. * @var array
  42. */
  43. protected $globalArguments;
  44.  
  45. /**
  46. * Object creation stack
  47. *
  48. * @var array
  49. */
  50. protected $creationStack = [];
  51.  
  52. /**
  53. * @param \Magento\Framework\ObjectManager\ConfigInterface $config
  54. * @param ObjectManagerInterface $objectManager
  55. * @param \Magento\Framework\ObjectManager\DefinitionInterface $definitions
  56. * @param array $globalArguments
  57. */
  58. public function __construct(
  59. \Magento\Framework\ObjectManager\ConfigInterface $config,
  60. ObjectManagerInterface $objectManager = null,
  61. \Magento\Framework\ObjectManager\DefinitionInterface $definitions = null,
  62. $globalArguments = []
  63. ) {
  64. $this->config = $config;
  65. $this->objectManager = $objectManager;
  66. $this->definitions = $definitions ?: $this->getDefinitions();
  67. $this->globalArguments = $globalArguments;
  68. }
  69.  
  70. /**
  71. * Set object manager
  72. *
  73. * @param ObjectManagerInterface $objectManager
  74. *
  75. * @return void
  76. */
  77. public function setObjectManager(ObjectManagerInterface $objectManager)
  78. {
  79. $this->objectManager = $objectManager;
  80. }
  81.  
  82. /**
  83. * Set global arguments
  84. *
  85. * @param array $arguments
  86. *
  87. * @return void
  88. */
  89. public function setArguments($arguments)
  90. {
  91. $this->globalArguments = $arguments;
  92. }
  93.  
  94. /**
  95. * @return \Magento\Framework\ObjectManager\DefinitionInterface
  96. */
  97. public function getDefinitions()
  98. {
  99. if ($this->definitions === null) {
  100. $this->definitions = new \Magento\Framework\ObjectManager\Definition\Runtime();
  101. }
  102. return $this->definitions;
  103. }
  104.  
  105. /**
  106. * Create object
  107. *
  108. * @param string $type
  109. * @param array $args
  110. *
  111. * @return object
  112. * @throws RuntimeException
  113. */
  114. protected function createObject($type, $args)
  115. {
  116. try {
  117. return new $type(...array_values($args));
  118. } catch (\TypeError $exception) {
  119. /** @var LoggerInterface $logger */
  120. $logger = ObjectManager::getInstance()->get(LoggerInterface::class);
  121. $logger->critical(
  122. sprintf('Type Error occurred when creating object: %s, %s', $type, $exception->getMessage())
  123. );
  124.  
  125. throw new RuntimeException(
  126. new Phrase('Type Error occurred when creating object: %type', ['type' => $type])
  127. );
  128. }
  129. }
  130.  
  131. /**
  132. * Resolve an argument
  133. *
  134. * @param array &$argument
  135. * @param string $paramType
  136. * @param mixed $paramDefault
  137. * @param string $paramName
  138. * @param string $requestedType
  139. *
  140. * @return void
  141. *
  142. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  143. */
  144. protected function resolveArgument(&$argument, $paramType, $paramDefault, $paramName, $requestedType)
  145. {
  146. if ($paramType && $argument !== $paramDefault && !is_object($argument)) {
  147. if (!isset($argument['instance']) || $argument !== (array)$argument) {
  148. throw new \UnexpectedValueException(
  149. 'Invalid parameter configuration provided for $' . $paramName . ' argument of ' . $requestedType
  150. );
  151. }
  152. $argumentType = $argument['instance'];
  153.  
  154. if (isset($argument['shared'])) {
  155. $isShared = $argument['shared'];
  156. } else {
  157. $isShared = $this->config->isShared($argumentType);
  158. }
  159.  
  160. if ($isShared) {
  161. $argument = $this->objectManager->get($argumentType);
  162. } else {
  163. $argument = $this->objectManager->create($argumentType);
  164. }
  165. } elseif ($argument === (array)$argument) {
  166. if (isset($argument['argument'])) {
  167. if (isset($this->globalArguments[$argument['argument']])) {
  168. $argument = $this->globalArguments[$argument['argument']];
  169. } else {
  170. $argument = $paramDefault;
  171. }
  172. } elseif (!empty($argument)) {
  173. $this->parseArray($argument);
  174. }
  175. }
  176. }
  177.  
  178. /**
  179. * Parse array argument
  180. *
  181. * @param array $array
  182. *
  183. * @return void
  184. */
  185. protected function parseArray(&$array)
  186. {
  187. foreach ($array as $key => $item) {
  188. if ($item === (array)$item) {
  189. if (isset($item['instance'])) {
  190. if (isset($item['shared'])) {
  191. $isShared = $item['shared'];
  192. } else {
  193. $isShared = $this->config->isShared($item['instance']);
  194. }
  195.  
  196. if ($isShared) {
  197. $array[$key] = $this->objectManager->get($item['instance']);
  198. } else {
  199. $array[$key] = $this->objectManager->create($item['instance']);
  200. }
  201. } elseif (isset($item['argument'])) {
  202. if (isset($this->globalArguments[$item['argument']])) {
  203. $array[$key] = $this->globalArguments[$item['argument']];
  204. } else {
  205. $array[$key] = null;
  206. }
  207. } else {
  208. $this->parseArray($array[$key]);
  209. }
  210. }
  211. }
  212. }
  213.  
  214. /**
  215. * Resolve constructor arguments
  216. *
  217. * @param string $requestedType
  218. * @param array $parameters
  219. * @param array $arguments
  220. *
  221. * @return array
  222. *
  223. * @throws \UnexpectedValueException
  224. * @throws \BadMethodCallException
  225. */
  226. protected function resolveArgumentsInRuntime($requestedType, array $parameters, array $arguments = [])
  227. {
  228. $resolvedArguments = [];
  229. foreach ($parameters as $parameter) {
  230. list($paramName, $paramType, $paramRequired, $paramDefault) = $parameter;
  231. $argument = null;
  232. if (!empty($arguments) && (isset($arguments[$paramName]) || array_key_exists($paramName, $arguments))) {
  233. $argument = $arguments[$paramName];
  234. } elseif ($paramRequired) {
  235. if ($paramType) {
  236. $argument = ['instance' => $paramType];
  237. } else {
  238. $this->creationStack = [];
  239. throw new \BadMethodCallException(
  240. 'Missing required argument $' . $paramName . ' of ' . $requestedType . '.'
  241. );
  242. }
  243. } else {
  244. $argument = $paramDefault;
  245. }
  246.  
  247. $this->resolveArgument($argument, $paramType, $paramDefault, $paramName, $requestedType);
  248.  
  249. $resolvedArguments[] = $argument;
  250. }
  251. return $resolvedArguments;
  252. }
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement