Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ResultFactory.php
Go to the documentation of this file.
1 <?php
8 
10 
18 {
22  const TYPE_JSON = 'json';
23  const TYPE_RAW = 'raw';
24  const TYPE_REDIRECT = 'redirect';
25  const TYPE_FORWARD = 'forward';
26  const TYPE_LAYOUT = 'layout';
27  const TYPE_PAGE = 'page';
31  protected $typeMap = [
32  self::TYPE_JSON => Result\Json::class,
33  self::TYPE_RAW => Result\Raw::class,
34  self::TYPE_REDIRECT => Result\Redirect::class,
35  self::TYPE_FORWARD => Result\Forward::class,
36  self::TYPE_LAYOUT => \Magento\Framework\View\Result\Layout::class,
37  self::TYPE_PAGE => \Magento\Framework\View\Result\Page::class,
38  ];
39 
43  private $objectManager;
44 
51  public function __construct(
52  ObjectManagerInterface $objectManager,
53  array $typeMap = []
54  ) {
55  $this->objectManager = $objectManager;
56  $this->mergeTypes($typeMap);
57  }
58 
65  protected function mergeTypes(array $typeMap)
66  {
67  foreach ($typeMap as $typeInfo) {
68  if (isset($typeInfo['type']) && isset($typeInfo['class'])) {
69  $this->typeMap[$typeInfo['type']] = $typeInfo['class'];
70  }
71  }
72  }
73 
82  public function create($type, array $arguments = [])
83  {
84  if (empty($this->typeMap[$type])) {
85  throw new \InvalidArgumentException('"' . $type . ': isn\'t allowed');
86  }
87 
88  $resultInstance = $this->objectManager->create($this->typeMap[$type], $arguments);
89  if (!$resultInstance instanceof ResultInterface) {
90  throw new \InvalidArgumentException(get_class($resultInstance) . ' isn\'t instance of ResultInterface');
91  }
92 
99  if ($resultInstance instanceof \Magento\Framework\View\Result\Layout) {
100  // Initialization has to be in constructor of ResultPage
101  $resultInstance->addDefaultHandle();
102  }
103 
104  return $resultInstance;
105  }
106 }
$objectManager
Definition: bootstrap.php:17
create($type, array $arguments=[])
$type
Definition: item.phtml:13
$arguments
__construct(ObjectManagerInterface $objectManager, array $typeMap=[])