ResultFactory.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Controller;
  7. use Magento\Framework\ObjectManagerInterface;
  8. /**
  9. * Result Factory
  10. *
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class ResultFactory
  15. {
  16. /**#@+
  17. * Allowed result types
  18. */
  19. const TYPE_JSON = 'json';
  20. const TYPE_RAW = 'raw';
  21. const TYPE_REDIRECT = 'redirect';
  22. const TYPE_FORWARD = 'forward';
  23. const TYPE_LAYOUT = 'layout';
  24. const TYPE_PAGE = 'page';
  25. /**#@-*/
  26. /**#@-*/
  27. protected $typeMap = [
  28. self::TYPE_JSON => Result\Json::class,
  29. self::TYPE_RAW => Result\Raw::class,
  30. self::TYPE_REDIRECT => Result\Redirect::class,
  31. self::TYPE_FORWARD => Result\Forward::class,
  32. self::TYPE_LAYOUT => \Magento\Framework\View\Result\Layout::class,
  33. self::TYPE_PAGE => \Magento\Framework\View\Result\Page::class,
  34. ];
  35. /**
  36. * @var ObjectManagerInterface
  37. */
  38. private $objectManager;
  39. /**
  40. * Constructor
  41. *
  42. * @param ObjectManagerInterface $objectManager
  43. * @param array $typeMap
  44. */
  45. public function __construct(
  46. ObjectManagerInterface $objectManager,
  47. array $typeMap = []
  48. ) {
  49. $this->objectManager = $objectManager;
  50. $this->mergeTypes($typeMap);
  51. }
  52. /**
  53. * Add or override result types
  54. *
  55. * @param array $typeMap
  56. * @return void
  57. */
  58. protected function mergeTypes(array $typeMap)
  59. {
  60. foreach ($typeMap as $typeInfo) {
  61. if (isset($typeInfo['type']) && isset($typeInfo['class'])) {
  62. $this->typeMap[$typeInfo['type']] = $typeInfo['class'];
  63. }
  64. }
  65. }
  66. /**
  67. * Create new page regarding its type
  68. *
  69. * @param string $type
  70. * @param array $arguments
  71. * @throws \InvalidArgumentException
  72. * @return ResultInterface
  73. */
  74. public function create($type, array $arguments = [])
  75. {
  76. if (empty($this->typeMap[$type])) {
  77. throw new \InvalidArgumentException('"' . $type . ': isn\'t allowed');
  78. }
  79. $resultInstance = $this->objectManager->create($this->typeMap[$type], $arguments);
  80. if (!$resultInstance instanceof ResultInterface) {
  81. throw new \InvalidArgumentException(get_class($resultInstance) . ' isn\'t instance of ResultInterface');
  82. }
  83. /**
  84. * TODO: Temporary solution, must be removed after full refactoring to the new result rendering system
  85. *
  86. * Used for knowledge how result page was created, page was created through result factory or it's default page
  87. * in App\View created in constructor
  88. */
  89. if ($resultInstance instanceof \Magento\Framework\View\Result\Layout) {
  90. // Initialization has to be in constructor of ResultPage
  91. $resultInstance->addDefaultHandle();
  92. }
  93. return $resultInstance;
  94. }
  95. }