Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Context.php
Go to the documentation of this file.
1 <?php
7 
18 use Magento\Framework\View\LayoutInterface as PageLayoutInterface;
19 
25 class Context implements ContextInterface
26 {
30  protected $namespace;
31 
35  protected $dataProvider;
36 
42  protected $request;
43 
50 
56  protected $acceptType;
57 
61  protected $pageLayout;
62 
67 
71  protected $actionPool;
72 
78  protected $componentsDefinitions = [];
79 
85  protected $urlBuilder;
86 
90  protected $processor;
91 
96 
110  public function __construct(
111  PageLayoutInterface $pageLayout,
114  ActionPoolFactory $actionPoolFactory,
120  $namespace = null
121  ) {
122  $this->namespace = $namespace;
123  $this->request = $request;
124  $this->buttonProviderFactory = $buttonProviderFactory;
125  $this->dataProvider = $dataProvider;
126  $this->pageLayout = $pageLayout;
127  $this->actionPool = $actionPoolFactory->create(['context' => $this]);
128  $this->contentTypeFactory = $contentTypeFactory;
129  $this->urlBuilder = $urlBuilder;
130  $this->processor = $processor;
131  $this->uiComponentFactory = $uiComponentFactory;
132  $this->setAcceptType();
133  }
134 
142  public function addComponentDefinition($name, array $config)
143  {
144  if (!isset($this->componentsDefinitions[$name])) {
145  $this->componentsDefinitions[$name] = $config;
146  } else {
147  $this->componentsDefinitions[$name] = array_merge(
148  $this->componentsDefinitions[$name],
149  $config
150  );
151  }
152  }
153 
157  public function getComponentsDefinitions()
158  {
160  }
161 
165  public function getRenderEngine()
166  {
167  return $this->contentTypeFactory->get($this->getAcceptType());
168  }
169 
173  public function getNamespace()
174  {
175  return $this->namespace;
176  }
177 
181  public function getAcceptType()
182  {
183  return $this->acceptType;
184  }
185 
189  public function getRequestParams()
190  {
191  return $this->request->getParams();
192  }
193 
197  public function getRequestParam($key, $defaultValue = null)
198  {
199  return $this->request->getParam($key, $defaultValue);
200  }
201 
205  public function getFiltersParams()
206  {
207  return $this->getRequestParam(self::FILTER_VAR, []);
208  }
209 
213  public function getFilterParam($key, $defaultValue = null)
214  {
215  $filter = $this->getFiltersParams();
216  return $filter[$key] ?? $defaultValue;
217  }
218 
222  public function getDataProvider()
223  {
224  return $this->dataProvider;
225  }
226 
230  public function getDataSourceData(UiComponentInterface $component)
231  {
232  $dataSource = $component->getDataSourceData();
233  $this->prepareDataSource($dataSource, $component);
234  $dataProviderConfig = $this->getDataProvider()->getConfigData();
235  return [
236  $this->getDataProvider()->getName() => [
237  'type' => 'dataSource',
238  'name' => $this->getDataProvider()->getName(),
239  'dataScope' => $this->getNamespace(),
240  'config' => array_replace_recursive(
241  array_merge($dataSource, $dataProviderConfig),
242  [
243  'params' => [
244  'namespace' => $this->getNamespace()
245  ],
246  ]
247  )
248  ]
249  ];
250  }
251 
255  public function getPageLayout()
256  {
257  return $this->pageLayout;
258  }
259 
263  public function addButtons(array $buttons, UiComponentInterface $component)
264  {
265  if (!empty($buttons)) {
266  foreach ($buttons as $buttonId => $buttonData) {
267  if (is_array($buttonData)) {
268  $buttons[$buttonId] = $buttonData;
269  continue;
270  }
272  $button = $this->buttonProviderFactory->create($buttonData);
273  $buttonData = $button->getButtonData();
274  if (!$buttonData) {
275  unset($buttons[$buttonId]);
276  continue;
277  }
278  $buttons[$buttonId] = $buttonData;
279  }
280  uasort($buttons, [$this, 'sortButtons']);
281 
282  foreach ($buttons as $buttonId => $buttonData) {
283  if (isset($buttonData['url'])) {
284  $buttonData['url'] = $this->getUrl($buttonData['url']);
285  }
286  $this->actionPool->add($buttonId, $buttonData, $component);
287  }
288  }
289  }
290 
298  public function sortButtons(array $itemA, array $itemB)
299  {
300  $sortOrderA = isset($itemA['sort_order']) ? intval($itemA['sort_order']) : 0;
301  $sortOrderB = isset($itemB['sort_order']) ? intval($itemB['sort_order']) : 0;
302 
303  return $sortOrderA - $sortOrderB;
304  }
305 
310  public function addHtmlBlocks(array $htmlBlocks, UiComponentInterface $component)
311  {
312  if (!empty($htmlBlocks)) {
313  foreach ($htmlBlocks as $htmlBlock => $blockData) {
314  $this->actionPool->addHtmlBlock($blockData['type'], $blockData['name'], $blockData['arguments']);
315  }
316  }
317  }
318 
324  protected function setAcceptType()
325  {
326  $this->acceptType = 'html';
327 
328  $rawAcceptType = $this->request->getHeader('Accept');
329  if (strpos($rawAcceptType, 'json') !== false) {
330  $this->acceptType = 'json';
331  } elseif (strpos($rawAcceptType, 'html') !== false) {
332  $this->acceptType = 'html';
333  } elseif (strpos($rawAcceptType, 'xml') !== false) {
334  $this->acceptType = 'xml';
335  }
336  }
337 
342  {
343  $this->dataProvider = $dataProvider;
344  }
345 
349  public function getUrl($route = '', $params = [])
350  {
351  return $this->urlBuilder->getUrl($route, $params);
352  }
353 
361  protected function prepareDataSource(array & $data, UiComponentInterface $component)
362  {
363  $childComponents = $component->getChildComponents();
364  if (!empty($childComponents)) {
365  foreach ($childComponents as $child) {
366  $this->prepareDataSource($data, $child);
367  }
368  }
369  $data = $component->prepareDataSource($data);
370  }
371 
375  public function getProcessor()
376  {
377  return $this->processor;
378  }
379 
383  public function getUiComponentFactory()
384  {
386  }
387 }
__construct(PageLayoutInterface $pageLayout, RequestInterface $request, ButtonProviderFactory $buttonProviderFactory, ActionPoolFactory $actionPoolFactory, ContentTypeFactory $contentTypeFactory, UrlInterface $urlBuilder, Processor $processor, UiComponentFactory $uiComponentFactory, DataProviderInterface $dataProvider=null, $namespace=null)
Definition: Context.php:110
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
addButtons(array $buttons, UiComponentInterface $component)
$config
Definition: fraud_order.php:17
setDataProvider(DataProviderInterface $dataProvider)
Definition: Context.php:341
prepareDataSource(array & $data, UiComponentInterface $component)
Definition: Context.php:361
getDataSourceData(UiComponentInterface $component)
Definition: Context.php:230
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
addHtmlBlocks(array $htmlBlocks, UiComponentInterface $component)
Definition: Context.php:310
if(!isset($_GET['name'])) $name
Definition: log.php:14