Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Config.php
Go to the documentation of this file.
1 <?php
7 
9 
13 class Config
14 {
20  protected $_configFilePath;
21 
25  protected $_domFactory;
26 
30  protected $_moduleReader;
31 
35  protected $_configDomXPath = null;
36 
42  protected $_layout;
43 
49  protected $_appState;
50 
57 
61  protected $readFactory;
62 
71  public function __construct(
72  \Magento\Framework\Config\DomFactory $domFactory,
73  \Magento\Framework\Module\Dir\Reader $moduleReader,
74  \Magento\Framework\View\LayoutInterface $layout,
75  \Magento\Framework\App\State $appState,
76  \Magento\Persistent\Model\Factory $persistentFactory,
77  \Magento\Framework\Filesystem\Directory\ReadFactory $readFactory
78  ) {
79  $this->_domFactory = $domFactory;
80  $this->_moduleReader = $moduleReader;
81  $this->_layout = $layout;
82  $this->_appState = $appState;
83  $this->_persistentFactory = $persistentFactory;
84  $this->readFactory = $readFactory;
85  }
86 
94  public function setConfigFilePath($path)
95  {
96  $this->_configFilePath = $path;
97  return $this;
98  }
99 
106  protected function _getConfigDomXPath()
107  {
108  if ($this->_configDomXPath === null) {
109  $dir = explode("/", $this->_configFilePath);
110  array_pop($dir);
111  $dir = implode("/", $dir);
112  $directoryRead = $this->readFactory->create($dir);
113  $filePath = $directoryRead->getRelativePath($this->_configFilePath);
114  $isFile = $directoryRead->isFile($filePath);
115  $isReadable = $directoryRead->isReadable($filePath);
116  if (!$isFile || !$isReadable) {
117  throw new \Magento\Framework\Exception\LocalizedException(
118  __('We cannot load the configuration from file %1.', $this->_configFilePath)
119  );
120  }
121  $xml = $directoryRead->readFile($filePath);
123  $configDom = $this->_domFactory->createDom(
124  [
125  'xml' => $xml,
126  'idAttributes' => ['config/instances/blocks/reference' => 'id'],
127  'schemaFile' => $this->_moduleReader->getModuleDir(Dir::MODULE_ETC_DIR, 'Magento_Persistent')
128  . '/persistent.xsd',
129  ]
130  );
131  $this->_configDomXPath = new \DOMXPath($configDom->getDom());
132  }
133  return $this->_configDomXPath;
134  }
135 
143  public function getBlockConfigInfo($block)
144  {
145  $xPath = '//instances/blocks/*[block_type="' . $block . '"]';
146  $blocks = $this->_getConfigDomXPath()->query($xPath);
147  return $this->_convertBlocksToArray($blocks);
148  }
149 
156  public function collectInstancesToEmulate()
157  {
158  $xPath = '/config/instances/blocks/reference';
159  $blocks = $this->_getConfigDomXPath()->query($xPath);
160  $blocksArray = $this->_convertBlocksToArray($blocks);
161  return ['blocks' => $blocksArray];
162  }
163 
170  protected function _convertBlocksToArray($blocks)
171  {
172  $blocksArray = [];
173  foreach ($blocks as $reference) {
174  $referenceAttributes = $reference->attributes;
175  $id = $referenceAttributes->getNamedItem('id')->nodeValue;
176  $blocksArray[$id] = [];
178  foreach ($reference->childNodes as $referenceSubNode) {
179  switch ($referenceSubNode->nodeName) {
180  case 'name_in_layout':
181  case 'class':
182  case 'method':
183  case 'block_type':
184  $blocksArray[$id][$referenceSubNode->nodeName] = $referenceSubNode->nodeValue;
185  break;
186  default:
187  }
188  }
189  }
190  return $blocksArray;
191  }
192 
198  public function fire()
199  {
200  foreach ($this->collectInstancesToEmulate() as $type => $elements) {
201  if (!is_array($elements)) {
202  continue;
203  }
204  foreach ($elements as $info) {
205  switch ($type) {
206  case 'blocks':
207  $this->fireOne($info, $this->_layout->getBlock($info['name_in_layout']));
208  break;
209  }
210  }
211  }
212  return $this;
213  }
214 
223  public function fireOne($info, $instance = false)
224  {
225  if (!$instance || isset(
226  $info['block_type']
227  ) && !$instance instanceof $info['block_type'] || !isset(
228  $info['class']
229  ) || !isset(
230  $info['method']
231  )
232  ) {
233  return $this;
234  }
235  $object = $this->_persistentFactory->create($info['class']);
236  $method = $info['method'];
237 
238  if (method_exists($object, $method)) {
239  $object->{$method}($instance);
240  } elseif ($this->_appState->getMode() == \Magento\Framework\App\State::MODE_DEVELOPER) {
241  throw new \Magento\Framework\Exception\LocalizedException(
242  __('Method "%1" is not defined in "%2"', $method, get_class($object))
243  );
244  }
245 
246  return $this;
247  }
248 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$id
Definition: fieldset.phtml:14
__()
Definition: __.php:13
$block
Definition: block.php:8
$type
Definition: item.phtml:13
fireOne($info, $instance=false)
Definition: Config.php:223
__construct(\Magento\Framework\Config\DomFactory $domFactory, \Magento\Framework\Module\Dir\Reader $moduleReader, \Magento\Framework\View\LayoutInterface $layout, \Magento\Framework\App\State $appState, \Magento\Persistent\Model\Factory $persistentFactory, \Magento\Framework\Filesystem\Directory\ReadFactory $readFactory)
Definition: Config.php:71
$method
Definition: info.phtml:13
foreach( $_productCollection as $_product)() ?>" class $info
Definition: listing.phtml:52