Developer.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\ObjectManager\Factory\Dynamic;
  7. class Developer extends \Magento\Framework\ObjectManager\Factory\AbstractFactory
  8. {
  9. /**
  10. * Resolve constructor arguments
  11. *
  12. * @param string $requestedType
  13. * @param array $parameters
  14. * @param array $arguments
  15. *
  16. * @return array
  17. *
  18. * @throws \UnexpectedValueException
  19. * @throws \BadMethodCallException
  20. */
  21. protected function _resolveArguments($requestedType, array $parameters, array $arguments = [])
  22. {
  23. // Get default arguments from config, merge with supplied arguments
  24. $defaultArguments = $this->config->getArguments($requestedType);
  25. if (is_array($defaultArguments)) {
  26. if (count($arguments)) {
  27. $arguments = array_replace($defaultArguments, $arguments);
  28. } else {
  29. $arguments = $defaultArguments;
  30. }
  31. }
  32. return $this->resolveArgumentsInRuntime($requestedType, $parameters, $arguments);
  33. }
  34. /**
  35. * Create instance with call time arguments
  36. *
  37. * @param string $requestedType
  38. * @param array $arguments
  39. * @return object
  40. * @throws \Exception
  41. */
  42. public function create($requestedType, array $arguments = [])
  43. {
  44. $type = $this->config->getInstanceType($requestedType);
  45. $parameters = $this->definitions->getParameters($type);
  46. if ($parameters == null) {
  47. return new $type();
  48. }
  49. if (isset($this->creationStack[$requestedType])) {
  50. $lastFound = end($this->creationStack);
  51. $this->creationStack = [];
  52. throw new \LogicException("Circular dependency: {$requestedType} depends on {$lastFound} and vice versa.");
  53. }
  54. $this->creationStack[$requestedType] = $requestedType;
  55. try {
  56. $args = $this->_resolveArguments($requestedType, $parameters, $arguments);
  57. unset($this->creationStack[$requestedType]);
  58. } catch (\Exception $e) {
  59. unset($this->creationStack[$requestedType]);
  60. throw $e;
  61. }
  62. return $this->createObject($type, $args);
  63. }
  64. }