123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\View\Element\UiComponent;
- use Magento\Framework\App\ObjectManager;
- use Magento\Framework\App\RequestInterface;
- use Magento\Framework\AuthorizationInterface;
- use Magento\Framework\UrlInterface;
- use Magento\Framework\View\Element\UiComponent\ContentType\ContentTypeFactory;
- use Magento\Framework\View\Element\UiComponent\Control\ActionPoolFactory;
- use Magento\Framework\View\Element\UiComponent\Control\ActionPoolInterface;
- use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderFactory;
- use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;
- use Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface;
- use Magento\Framework\View\Element\UiComponentFactory;
- use Magento\Framework\View\Element\UiComponentInterface;
- use Magento\Framework\View\LayoutInterface as PageLayoutInterface;
- /**
- * Class Context
- *
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class Context implements ContextInterface
- {
- /**
- * @var string
- */
- protected $namespace;
- /**
- * @var DataProviderInterface
- */
- protected $dataProvider;
- /**
- * Application request
- *
- * @var RequestInterface
- */
- protected $request;
- /**
- * Factory renderer for a content type
- *
- * @var ContentTypeFactory
- */
- protected $contentTypeFactory;
- /**
- * Accept type
- *
- * @var string
- */
- protected $acceptType;
- /**
- * @var PageLayoutInterface
- */
- protected $pageLayout;
- /**
- * @var ButtonProviderFactory
- */
- protected $buttonProviderFactory;
- /**
- * @var ActionPoolInterface
- */
- protected $actionPool;
- /**
- * Registry components
- *
- * @var array
- */
- protected $componentsDefinitions = [];
- /**
- * Url Builder
- *
- * @var UrlInterface
- */
- protected $urlBuilder;
- /**
- * @var Processor
- */
- protected $processor;
- /**
- * @var UiComponentFactory
- */
- protected $uiComponentFactory;
- /**
- * @var AuthorizationInterface
- */
- private $authorization;
- /**
- * @param PageLayoutInterface $pageLayout
- * @param RequestInterface $request
- * @param ButtonProviderFactory $buttonProviderFactory
- * @param ActionPoolFactory $actionPoolFactory
- * @param ContentTypeFactory $contentTypeFactory
- * @param UrlInterface $urlBuilder
- * @param Processor $processor
- * @param UiComponentFactory $uiComponentFactory
- * @param DataProviderInterface|null $dataProvider
- * @param string $namespace
- * @param AuthorizationInterface|null $authorization
- * @SuppressWarnings(PHPMD.ExcessiveParameterList)
- */
- public function __construct(
- PageLayoutInterface $pageLayout,
- RequestInterface $request,
- ButtonProviderFactory $buttonProviderFactory,
- ActionPoolFactory $actionPoolFactory,
- ContentTypeFactory $contentTypeFactory,
- UrlInterface $urlBuilder,
- Processor $processor,
- UiComponentFactory $uiComponentFactory,
- DataProviderInterface $dataProvider = null,
- $namespace = null,
- AuthorizationInterface $authorization = null
- ) {
- $this->namespace = $namespace;
- $this->request = $request;
- $this->buttonProviderFactory = $buttonProviderFactory;
- $this->dataProvider = $dataProvider;
- $this->pageLayout = $pageLayout;
- $this->actionPool = $actionPoolFactory->create(['context' => $this]);
- $this->contentTypeFactory = $contentTypeFactory;
- $this->urlBuilder = $urlBuilder;
- $this->processor = $processor;
- $this->uiComponentFactory = $uiComponentFactory;
- $this->authorization = $authorization ?: ObjectManager::getInstance()->get(
- AuthorizationInterface::class
- );
- $this->setAcceptType();
- }
- /**
- * Add component into registry
- *
- * @param string $name
- * @param array $config
- * @return void
- */
- public function addComponentDefinition($name, array $config)
- {
- if (!isset($this->componentsDefinitions[$name])) {
- $this->componentsDefinitions[$name] = $config;
- } else {
- $this->componentsDefinitions[$name] = array_merge(
- $this->componentsDefinitions[$name],
- $config
- );
- }
- }
- /**
- * @inheritdoc
- */
- public function getComponentsDefinitions()
- {
- return $this->componentsDefinitions;
- }
- /**
- * @inheritdoc
- */
- public function getRenderEngine()
- {
- return $this->contentTypeFactory->get($this->getAcceptType());
- }
- /**
- * @inheritdoc
- */
- public function getNamespace()
- {
- return $this->namespace;
- }
- /**
- * @inheritdoc
- */
- public function getAcceptType()
- {
- return $this->acceptType;
- }
- /**
- * @inheritdoc
- */
- public function getRequestParams()
- {
- return $this->request->getParams();
- }
- /**
- * @inheritdoc
- */
- public function getRequestParam($key, $defaultValue = null)
- {
- return $this->request->getParam($key, $defaultValue);
- }
- /**
- * @inheritdoc
- */
- public function getFiltersParams()
- {
- return $this->getRequestParam(self::FILTER_VAR, []);
- }
- /**
- * @inheritdoc
- */
- public function getFilterParam($key, $defaultValue = null)
- {
- $filter = $this->getFiltersParams();
- return $filter[$key] ?? $defaultValue;
- }
- /**
- * @inheritdoc
- */
- public function getDataProvider()
- {
- return $this->dataProvider;
- }
- /**
- * @inheritdoc
- */
- public function getDataSourceData(UiComponentInterface $component)
- {
- $dataSource = $component->getDataSourceData();
- $this->prepareDataSource($dataSource, $component);
- $dataProviderConfig = $this->getDataProvider()->getConfigData();
- return [
- $this->getDataProvider()->getName() => [
- 'type' => 'dataSource',
- 'name' => $this->getDataProvider()->getName(),
- 'dataScope' => $this->getNamespace(),
- 'config' => array_replace_recursive(
- array_merge($dataSource, $dataProviderConfig),
- [
- 'params' => [
- 'namespace' => $this->getNamespace()
- ],
- ]
- )
- ]
- ];
- }
- /**
- * @inheritdoc
- */
- public function getPageLayout()
- {
- return $this->pageLayout;
- }
- /**
- * @inheritdoc
- */
- public function addButtons(array $buttons, UiComponentInterface $component)
- {
- if (!empty($buttons)) {
- foreach ($buttons as $buttonId => $buttonData) {
- if (is_array($buttonData)) {
- $buttons[$buttonId] = $buttonData;
- continue;
- }
- /** @var ButtonProviderInterface $button */
- $button = $this->buttonProviderFactory->create($buttonData);
- $buttonData = $button->getButtonData();
- if (!$buttonData) {
- unset($buttons[$buttonId]);
- continue;
- }
- $buttons[$buttonId] = $buttonData;
- }
- uasort($buttons, [$this, 'sortButtons']);
- foreach ($buttons as $buttonId => $buttonData) {
- if (isset($buttonData['aclResource']) && !$this->authorization->isAllowed($buttonData['aclResource'])) {
- continue;
- }
- if (isset($buttonData['url'])) {
- $buttonData['url'] = $this->getUrl($buttonData['url']);
- }
- $this->actionPool->add($buttonId, $buttonData, $component);
- }
- }
- }
- /**
- * Sort buttons by sort order
- *
- * @param array $itemA
- * @param array $itemB
- * @return int
- */
- public function sortButtons(array $itemA, array $itemB)
- {
- $sortOrderA = isset($itemA['sort_order']) ? (int)$itemA['sort_order'] : 0;
- $sortOrderB = isset($itemB['sort_order']) ? (int)$itemB['sort_order'] : 0;
- return $sortOrderA - $sortOrderB;
- }
- /**
- * @inheritdoc
- * @SuppressWarnings(PHPMD.UnusedLocalVariable)
- */
- public function addHtmlBlocks(array $htmlBlocks, UiComponentInterface $component)
- {
- if (!empty($htmlBlocks)) {
- foreach ($htmlBlocks as $htmlBlock => $blockData) {
- $this->actionPool->addHtmlBlock($blockData['type'], $blockData['name'], $blockData['arguments']);
- }
- }
- }
- /**
- * Getting requested accept type
- *
- * @return void
- */
- protected function setAcceptType()
- {
- $this->acceptType = 'html';
- $rawAcceptType = $this->request->getHeader('Accept');
- if (strpos($rawAcceptType, 'json') !== false) {
- $this->acceptType = 'json';
- } elseif (strpos($rawAcceptType, 'html') !== false) {
- $this->acceptType = 'html';
- } elseif (strpos($rawAcceptType, 'xml') !== false) {
- $this->acceptType = 'xml';
- }
- }
- /**
- * @inheritdoc
- */
- public function setDataProvider(DataProviderInterface $dataProvider)
- {
- $this->dataProvider = $dataProvider;
- }
- /**
- * @inheritdoc
- */
- public function getUrl($route = '', $params = [])
- {
- return $this->urlBuilder->getUrl($route, $params);
- }
- /**
- * Call `prepareData` method of all the components
- *
- * @param array $data
- * @param UiComponentInterface $component
- * @return void
- */
- protected function prepareDataSource(array & $data, UiComponentInterface $component)
- {
- $childComponents = $component->getChildComponents();
- if (!empty($childComponents)) {
- foreach ($childComponents as $child) {
- $this->prepareDataSource($data, $child);
- }
- }
- $data = $component->prepareDataSource($data);
- }
- /**
- * @inheritdoc
- */
- public function getProcessor()
- {
- return $this->processor;
- }
- /**
- * @inheritdoc
- */
- public function getUiComponentFactory()
- {
- return $this->uiComponentFactory;
- }
- }
|