Config.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\PageLayout;
  7. /**
  8. * Page layouts configuration
  9. */
  10. class Config extends \Magento\Framework\Config\AbstractXml
  11. {
  12. /**
  13. * @var \Magento\Framework\Config\Dom\UrnResolver
  14. */
  15. protected $urnResolver;
  16. /**
  17. * Instantiate with the list of files to merge
  18. *
  19. * @param array $configFiles
  20. * @param \Magento\Framework\Config\DomFactory $domFactory
  21. * @param \Magento\Framework\Config\Dom\UrnResolver $urnResolver
  22. * @throws \InvalidArgumentException
  23. */
  24. public function __construct(
  25. $configFiles,
  26. \Magento\Framework\Config\DomFactory $domFactory,
  27. \Magento\Framework\Config\Dom\UrnResolver $urnResolver
  28. ) {
  29. $this->urnResolver = $urnResolver;
  30. parent::__construct($configFiles, $domFactory);
  31. }
  32. /**
  33. * Get absolute path to the XML-schema file
  34. *
  35. * @return string
  36. */
  37. public function getSchemaFile()
  38. {
  39. return $this->urnResolver->getRealPath('urn:magento:framework:View/PageLayout/etc/layouts.xsd');
  40. }
  41. /**
  42. * Get page layout that contains declared in system
  43. *
  44. * @return string[][]
  45. */
  46. public function getPageLayouts()
  47. {
  48. return $this->_data;
  49. }
  50. /**
  51. * Checks that the page layout declared in configuration
  52. *
  53. * @param string $pageLayout
  54. * @return bool
  55. */
  56. public function hasPageLayout($pageLayout)
  57. {
  58. return isset($this->_data[$pageLayout]);
  59. }
  60. /**
  61. * Retrieve page layout options
  62. *
  63. * @return array
  64. */
  65. public function getOptions()
  66. {
  67. return $this->getPageLayouts();
  68. }
  69. /**
  70. * @param bool $withEmpty
  71. * @return array
  72. */
  73. public function toOptionArray($withEmpty = false)
  74. {
  75. $options = [];
  76. foreach ($this->getPageLayouts() as $value => $label) {
  77. $options[] = ['label' => $label, 'value' => $value];
  78. }
  79. if ($withEmpty) {
  80. array_unshift($options, [
  81. 'value' => '',
  82. 'label' => (string)new \Magento\Framework\Phrase('-- Please Select --')
  83. ]);
  84. }
  85. return $options;
  86. }
  87. /**
  88. * Extract configuration data from the DOM structure
  89. *
  90. * @param \DOMDocument $dom
  91. * @return array
  92. */
  93. protected function _extractData(\DOMDocument $dom)
  94. {
  95. $result = [];
  96. /** @var \DOMElement $layout */
  97. foreach ($dom->getElementsByTagName('layout') as $layout) {
  98. $result[$layout->getAttribute('id')] = trim($layout->nodeValue);
  99. }
  100. return $result;
  101. }
  102. /**
  103. * Get XML-contents, initial for merging
  104. *
  105. * @return string
  106. */
  107. protected function _getInitialXml()
  108. {
  109. return '<?xml version="1.0" encoding="UTF-8"?>'
  110. . '<page_layouts xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"></page_layouts>';
  111. }
  112. /**
  113. * Get list of paths to identifiable nodes
  114. *
  115. * @return array
  116. */
  117. protected function _getIdAttributes()
  118. {
  119. return [
  120. '/page_layouts/layout' => 'id'
  121. ];
  122. }
  123. }