Dependencies.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * System Configuration Dependencies Mapper
  8. */
  9. namespace Magento\Config\Model\Config\Structure\Mapper;
  10. /**
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class Dependencies extends \Magento\Config\Model\Config\Structure\AbstractMapper
  15. {
  16. /**
  17. * Class that can convert relative paths from "depends" node to absolute
  18. *
  19. * @var \Magento\Config\Model\Config\Structure\Mapper\Helper\RelativePathConverter
  20. */
  21. protected $_pathConverter;
  22. /**
  23. * @param \Magento\Config\Model\Config\Structure\Mapper\Helper\RelativePathConverter $pathConverted
  24. */
  25. public function __construct(
  26. \Magento\Config\Model\Config\Structure\Mapper\Helper\RelativePathConverter $pathConverted
  27. ) {
  28. $this->_pathConverter = $pathConverted;
  29. }
  30. /**
  31. * Apply map
  32. *
  33. * @param array $data
  34. * @return array
  35. */
  36. public function map(array $data)
  37. {
  38. if ($this->_hasValue('config/system/sections', $data)) {
  39. foreach ($data['config']['system']['sections'] as &$sectionConfig) {
  40. $sectionConfig = $this->_processConfig($sectionConfig);
  41. }
  42. }
  43. return $data;
  44. }
  45. /**
  46. * Process configuration
  47. *
  48. * @param array $config
  49. * @return array
  50. */
  51. protected function _processConfig($config)
  52. {
  53. $config = $this->_processDepends($config);
  54. if ($this->_hasValue('children', $config)) {
  55. foreach ($config['children'] as &$subConfig) {
  56. $subConfig = $this->_processConfig($subConfig);
  57. }
  58. }
  59. return $config;
  60. }
  61. /**
  62. * Process dependencies configuration
  63. *
  64. * @param array $config
  65. * @return array
  66. */
  67. protected function _processDepends($config)
  68. {
  69. if ($this->_hasValue('depends/fields', $config)) {
  70. foreach ($config['depends']['fields'] as &$field) {
  71. $dependPath = $this->_getDependPath($field, $config);
  72. $field['dependPath'] = $dependPath;
  73. $field['id'] = implode('/', $dependPath);
  74. }
  75. }
  76. return $config;
  77. }
  78. /**
  79. * Get depend path
  80. *
  81. * @param array $field
  82. * @param array $config
  83. * @return string[]
  84. * @throws \InvalidArgumentException
  85. */
  86. protected function _getDependPath($field, $config)
  87. {
  88. $dependPath = $field['id'];
  89. $elementPath = $config['path'] . '/' . $config['id'];
  90. return explode('/', $this->_pathConverter->convert($elementPath, $dependPath));
  91. }
  92. }