123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Ui\Component;
- use Magento\Framework\DataObject;
- use Magento\Framework\Exception\LocalizedException;
- use Magento\Framework\View\Element\UiComponentFactory;
- use Magento\Framework\View\Element\UiComponentInterface;
- use Magento\Framework\View\Element\UiComponent\ContextInterface;
- use Magento\Framework\View\Element\UiComponent\DataSourceInterface;
- use Magento\Framework\View\Element\UiComponent\ObserverInterface;
- use Magento\Framework\Data\ValueSourceInterface;
- /**
- * Abstract class AbstractComponent
- *
- * @api
- * @SuppressWarnings(PHPMD.NumberOfChildren)
- * @since 100.0.2
- */
- abstract class AbstractComponent extends DataObject implements UiComponentInterface
- {
- /**
- * Render context
- *
- * @var ContextInterface
- */
- protected $context;
- /**
- * @var UiComponentInterface[]
- */
- protected $components;
- /**
- * @var array
- */
- protected $componentData = [];
- /**
- * @var DataSourceInterface[]
- */
- protected $dataSources = [];
- /**
- * Constructor
- *
- * @param ContextInterface $context
- * @param UiComponentInterface[] $components
- * @param array $data
- */
- public function __construct(
- ContextInterface $context,
- array $components = [],
- array $data = []
- ) {
- $this->context = $context;
- $this->components = $components;
- $this->_data = array_replace_recursive($this->_data, $data);
- $this->initObservers($this->_data);
- }
- /**
- * Get component context
- *
- * @return ContextInterface
- */
- public function getContext()
- {
- return $this->context;
- }
- /**
- * Get component name
- *
- * @return string
- */
- public function getName()
- {
- return $this->getData('name');
- }
- /**
- * Prepare component configuration
- *
- * @return void
- */
- public function prepare()
- {
- $config = $this->getData('config');
- if (isset($config['value']) && $config['value'] instanceof ValueSourceInterface) {
- $config['value'] = $config['value']->getValue($this->getName());
- }
- $this->setData('config', (array)$config);
- $jsConfig = $this->getJsConfig($this);
- if (isset($jsConfig['provider'])) {
- unset($jsConfig['extends']);
- $this->getContext()->addComponentDefinition($this->getName(), $jsConfig);
- } else {
- $this->getContext()->addComponentDefinition($this->getComponentName(), $jsConfig);
- }
- if ($this->hasData('actions')) {
- $this->getContext()->addActions($this->getData('actions'), $this);
- }
- if ($this->hasData('html_blocks')) {
- $this->getContext()->addHtmlBlocks($this->getData('html_blocks'), $this);
- }
- if ($this->hasData('buttons')) {
- $this->getContext()->addButtons($this->getData('buttons'), $this);
- }
- $this->context->getProcessor()->register($this);
- $this->getContext()->getProcessor()->notify($this->getComponentName());
- }
- /**
- * Call prepare method in the component UI
- *
- * @param UiComponentInterface $component
- * @return $this
- * @since 100.1.0
- */
- protected function prepareChildComponent(UiComponentInterface $component)
- {
- $childComponents = $component->getChildComponents();
- if (!empty($childComponents)) {
- foreach ($childComponents as $child) {
- $this->prepareChildComponent($child);
- }
- }
- $component->prepare();
- return $this;
- }
- /**
- * Produce and return block's html output
- *
- * @return string
- */
- public function toHtml()
- {
- $this->render();
- }
- /**
- * Render component
- *
- * @return string
- */
- public function render()
- {
- $result = $this->getContext()->getRenderEngine()->render($this, $this->getTemplate());
- return $result;
- }
- /**
- * Add component
- *
- * @param string $name
- * @param UiComponentInterface $component
- * @return void
- */
- public function addComponent($name, UiComponentInterface $component)
- {
- $this->components[$name] = $component;
- }
- /**
- * @param string $name
- * @return UiComponentInterface
- */
- public function getComponent($name)
- {
- return isset($this->components[$name]) ? $this->components[$name] : null;
- }
- /**
- * Get components
- *
- * @return UiComponentInterface[]
- */
- public function getChildComponents()
- {
- return $this->components;
- }
- /**
- * Render child component
- *
- * @param string $name
- * @return string
- */
- public function renderChildComponent($name)
- {
- $result = null;
- if (isset($this->components[$name])) {
- $result = $this->components[$name]->render();
- }
- return $result;
- }
- /**
- * Get template
- *
- * @return string
- */
- public function getTemplate()
- {
- return $this->getData('template') . '.xhtml';
- }
- /**
- * Get component configuration
- *
- * @return array
- */
- public function getConfiguration()
- {
- return (array)$this->getData('config');
- }
- /**
- * Get configuration of related JavaScript Component
- * (force extending the root component if component does not extend other component)
- *
- * @param UiComponentInterface $component
- * @return array
- */
- public function getJsConfig(UiComponentInterface $component)
- {
- $jsConfig = (array)$component->getData('js_config');
- if (!isset($jsConfig['extends'])) {
- $jsConfig['extends'] = $component->getContext()->getNamespace();
- }
- return $jsConfig;
- }
- /**
- * Component data setter
- *
- * @param string|array $key
- * @param mixed $value
- * @return void
- */
- public function setData($key, $value = null)
- {
- parent::setData($key, $value);
- }
- /**
- * Component data getter
- *
- * @param string $key
- * @param string|int $index
- * @return mixed
- */
- public function getData($key = '', $index = null)
- {
- return parent::getData($key, $index);
- }
- /**
- * Prepare Data Source
- *
- * @param array $dataSource
- * @return array
- */
- public function prepareDataSource(array $dataSource)
- {
- return $dataSource;
- }
- /**
- * {@inheritdoc}
- */
- public function getDataSourceData()
- {
- return [];
- }
- /**
- * Initiate observers
- *
- * @param array $data
- * @return void
- */
- protected function initObservers(array & $data = [])
- {
- if (isset($data['observers']) && is_array($data['observers'])) {
- foreach ($data['observers'] as $observerType => $observer) {
- if (!is_object($observer)) {
- $observer = $this;
- }
- if ($observer instanceof ObserverInterface) {
- $this->getContext()->getProcessor()->attach($observerType, $observer);
- }
- unset($data['observers']);
- }
- }
- }
- }
|