Context.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Element\UiComponent;
  7. use Magento\Framework\App\ObjectManager;
  8. use Magento\Framework\App\RequestInterface;
  9. use Magento\Framework\AuthorizationInterface;
  10. use Magento\Framework\UrlInterface;
  11. use Magento\Framework\View\Element\UiComponent\ContentType\ContentTypeFactory;
  12. use Magento\Framework\View\Element\UiComponent\Control\ActionPoolFactory;
  13. use Magento\Framework\View\Element\UiComponent\Control\ActionPoolInterface;
  14. use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderFactory;
  15. use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;
  16. use Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface;
  17. use Magento\Framework\View\Element\UiComponentFactory;
  18. use Magento\Framework\View\Element\UiComponentInterface;
  19. use Magento\Framework\View\LayoutInterface as PageLayoutInterface;
  20. /**
  21. * Class Context
  22. *
  23. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  24. */
  25. class Context implements ContextInterface
  26. {
  27. /**
  28. * @var string
  29. */
  30. protected $namespace;
  31. /**
  32. * @var DataProviderInterface
  33. */
  34. protected $dataProvider;
  35. /**
  36. * Application request
  37. *
  38. * @var RequestInterface
  39. */
  40. protected $request;
  41. /**
  42. * Factory renderer for a content type
  43. *
  44. * @var ContentTypeFactory
  45. */
  46. protected $contentTypeFactory;
  47. /**
  48. * Accept type
  49. *
  50. * @var string
  51. */
  52. protected $acceptType;
  53. /**
  54. * @var PageLayoutInterface
  55. */
  56. protected $pageLayout;
  57. /**
  58. * @var ButtonProviderFactory
  59. */
  60. protected $buttonProviderFactory;
  61. /**
  62. * @var ActionPoolInterface
  63. */
  64. protected $actionPool;
  65. /**
  66. * Registry components
  67. *
  68. * @var array
  69. */
  70. protected $componentsDefinitions = [];
  71. /**
  72. * Url Builder
  73. *
  74. * @var UrlInterface
  75. */
  76. protected $urlBuilder;
  77. /**
  78. * @var Processor
  79. */
  80. protected $processor;
  81. /**
  82. * @var UiComponentFactory
  83. */
  84. protected $uiComponentFactory;
  85. /**
  86. * @var AuthorizationInterface
  87. */
  88. private $authorization;
  89. /**
  90. * @param PageLayoutInterface $pageLayout
  91. * @param RequestInterface $request
  92. * @param ButtonProviderFactory $buttonProviderFactory
  93. * @param ActionPoolFactory $actionPoolFactory
  94. * @param ContentTypeFactory $contentTypeFactory
  95. * @param UrlInterface $urlBuilder
  96. * @param Processor $processor
  97. * @param UiComponentFactory $uiComponentFactory
  98. * @param DataProviderInterface|null $dataProvider
  99. * @param string $namespace
  100. * @param AuthorizationInterface|null $authorization
  101. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  102. */
  103. public function __construct(
  104. PageLayoutInterface $pageLayout,
  105. RequestInterface $request,
  106. ButtonProviderFactory $buttonProviderFactory,
  107. ActionPoolFactory $actionPoolFactory,
  108. ContentTypeFactory $contentTypeFactory,
  109. UrlInterface $urlBuilder,
  110. Processor $processor,
  111. UiComponentFactory $uiComponentFactory,
  112. DataProviderInterface $dataProvider = null,
  113. $namespace = null,
  114. AuthorizationInterface $authorization = null
  115. ) {
  116. $this->namespace = $namespace;
  117. $this->request = $request;
  118. $this->buttonProviderFactory = $buttonProviderFactory;
  119. $this->dataProvider = $dataProvider;
  120. $this->pageLayout = $pageLayout;
  121. $this->actionPool = $actionPoolFactory->create(['context' => $this]);
  122. $this->contentTypeFactory = $contentTypeFactory;
  123. $this->urlBuilder = $urlBuilder;
  124. $this->processor = $processor;
  125. $this->uiComponentFactory = $uiComponentFactory;
  126. $this->authorization = $authorization ?: ObjectManager::getInstance()->get(
  127. AuthorizationInterface::class
  128. );
  129. $this->setAcceptType();
  130. }
  131. /**
  132. * Add component into registry
  133. *
  134. * @param string $name
  135. * @param array $config
  136. * @return void
  137. */
  138. public function addComponentDefinition($name, array $config)
  139. {
  140. if (!isset($this->componentsDefinitions[$name])) {
  141. $this->componentsDefinitions[$name] = $config;
  142. } else {
  143. $this->componentsDefinitions[$name] = array_merge(
  144. $this->componentsDefinitions[$name],
  145. $config
  146. );
  147. }
  148. }
  149. /**
  150. * @inheritdoc
  151. */
  152. public function getComponentsDefinitions()
  153. {
  154. return $this->componentsDefinitions;
  155. }
  156. /**
  157. * @inheritdoc
  158. */
  159. public function getRenderEngine()
  160. {
  161. return $this->contentTypeFactory->get($this->getAcceptType());
  162. }
  163. /**
  164. * @inheritdoc
  165. */
  166. public function getNamespace()
  167. {
  168. return $this->namespace;
  169. }
  170. /**
  171. * @inheritdoc
  172. */
  173. public function getAcceptType()
  174. {
  175. return $this->acceptType;
  176. }
  177. /**
  178. * @inheritdoc
  179. */
  180. public function getRequestParams()
  181. {
  182. return $this->request->getParams();
  183. }
  184. /**
  185. * @inheritdoc
  186. */
  187. public function getRequestParam($key, $defaultValue = null)
  188. {
  189. return $this->request->getParam($key, $defaultValue);
  190. }
  191. /**
  192. * @inheritdoc
  193. */
  194. public function getFiltersParams()
  195. {
  196. return $this->getRequestParam(self::FILTER_VAR, []);
  197. }
  198. /**
  199. * @inheritdoc
  200. */
  201. public function getFilterParam($key, $defaultValue = null)
  202. {
  203. $filter = $this->getFiltersParams();
  204. return $filter[$key] ?? $defaultValue;
  205. }
  206. /**
  207. * @inheritdoc
  208. */
  209. public function getDataProvider()
  210. {
  211. return $this->dataProvider;
  212. }
  213. /**
  214. * @inheritdoc
  215. */
  216. public function getDataSourceData(UiComponentInterface $component)
  217. {
  218. $dataSource = $component->getDataSourceData();
  219. $this->prepareDataSource($dataSource, $component);
  220. $dataProviderConfig = $this->getDataProvider()->getConfigData();
  221. return [
  222. $this->getDataProvider()->getName() => [
  223. 'type' => 'dataSource',
  224. 'name' => $this->getDataProvider()->getName(),
  225. 'dataScope' => $this->getNamespace(),
  226. 'config' => array_replace_recursive(
  227. array_merge($dataSource, $dataProviderConfig),
  228. [
  229. 'params' => [
  230. 'namespace' => $this->getNamespace()
  231. ],
  232. ]
  233. )
  234. ]
  235. ];
  236. }
  237. /**
  238. * @inheritdoc
  239. */
  240. public function getPageLayout()
  241. {
  242. return $this->pageLayout;
  243. }
  244. /**
  245. * @inheritdoc
  246. */
  247. public function addButtons(array $buttons, UiComponentInterface $component)
  248. {
  249. if (!empty($buttons)) {
  250. foreach ($buttons as $buttonId => $buttonData) {
  251. if (is_array($buttonData)) {
  252. $buttons[$buttonId] = $buttonData;
  253. continue;
  254. }
  255. /** @var ButtonProviderInterface $button */
  256. $button = $this->buttonProviderFactory->create($buttonData);
  257. $buttonData = $button->getButtonData();
  258. if (!$buttonData) {
  259. unset($buttons[$buttonId]);
  260. continue;
  261. }
  262. $buttons[$buttonId] = $buttonData;
  263. }
  264. uasort($buttons, [$this, 'sortButtons']);
  265. foreach ($buttons as $buttonId => $buttonData) {
  266. if (isset($buttonData['aclResource']) && !$this->authorization->isAllowed($buttonData['aclResource'])) {
  267. continue;
  268. }
  269. if (isset($buttonData['url'])) {
  270. $buttonData['url'] = $this->getUrl($buttonData['url']);
  271. }
  272. $this->actionPool->add($buttonId, $buttonData, $component);
  273. }
  274. }
  275. }
  276. /**
  277. * Sort buttons by sort order
  278. *
  279. * @param array $itemA
  280. * @param array $itemB
  281. * @return int
  282. */
  283. public function sortButtons(array $itemA, array $itemB)
  284. {
  285. $sortOrderA = isset($itemA['sort_order']) ? (int)$itemA['sort_order'] : 0;
  286. $sortOrderB = isset($itemB['sort_order']) ? (int)$itemB['sort_order'] : 0;
  287. return $sortOrderA - $sortOrderB;
  288. }
  289. /**
  290. * @inheritdoc
  291. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  292. */
  293. public function addHtmlBlocks(array $htmlBlocks, UiComponentInterface $component)
  294. {
  295. if (!empty($htmlBlocks)) {
  296. foreach ($htmlBlocks as $htmlBlock => $blockData) {
  297. $this->actionPool->addHtmlBlock($blockData['type'], $blockData['name'], $blockData['arguments']);
  298. }
  299. }
  300. }
  301. /**
  302. * Getting requested accept type
  303. *
  304. * @return void
  305. */
  306. protected function setAcceptType()
  307. {
  308. $this->acceptType = 'html';
  309. $rawAcceptType = $this->request->getHeader('Accept');
  310. if (strpos($rawAcceptType, 'json') !== false) {
  311. $this->acceptType = 'json';
  312. } elseif (strpos($rawAcceptType, 'html') !== false) {
  313. $this->acceptType = 'html';
  314. } elseif (strpos($rawAcceptType, 'xml') !== false) {
  315. $this->acceptType = 'xml';
  316. }
  317. }
  318. /**
  319. * @inheritdoc
  320. */
  321. public function setDataProvider(DataProviderInterface $dataProvider)
  322. {
  323. $this->dataProvider = $dataProvider;
  324. }
  325. /**
  326. * @inheritdoc
  327. */
  328. public function getUrl($route = '', $params = [])
  329. {
  330. return $this->urlBuilder->getUrl($route, $params);
  331. }
  332. /**
  333. * Call `prepareData` method of all the components
  334. *
  335. * @param array $data
  336. * @param UiComponentInterface $component
  337. * @return void
  338. */
  339. protected function prepareDataSource(array & $data, UiComponentInterface $component)
  340. {
  341. $childComponents = $component->getChildComponents();
  342. if (!empty($childComponents)) {
  343. foreach ($childComponents as $child) {
  344. $this->prepareDataSource($data, $child);
  345. }
  346. }
  347. $data = $component->prepareDataSource($data);
  348. }
  349. /**
  350. * @inheritdoc
  351. */
  352. public function getProcessor()
  353. {
  354. return $this->processor;
  355. }
  356. /**
  357. * @inheritdoc
  358. */
  359. public function getUiComponentFactory()
  360. {
  361. return $this->uiComponentFactory;
  362. }
  363. }