Comment.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Config\Model\Config\Parser;
  7. use Magento\Config\Model\Placeholder\Environment;
  8. use Magento\Config\Model\Placeholder\PlaceholderInterface;
  9. use Magento\Framework\App\Config\CommentParserInterface;
  10. use Magento\Framework\App\Config\ScopeConfigInterface;
  11. use Magento\Framework\App\Filesystem\DirectoryList;
  12. use Magento\Framework\Exception\FileSystemException;
  13. use Magento\Framework\Filesystem;
  14. /**
  15. * Config file parser
  16. *
  17. * It is used to parse config paths from
  18. * comment section in provided configuration file.
  19. * @api
  20. * @since 101.0.0
  21. */
  22. class Comment implements CommentParserInterface
  23. {
  24. /**
  25. * @var Filesystem
  26. */
  27. private $filesystem;
  28. /**
  29. * @var PlaceholderInterface
  30. */
  31. private $placeholder;
  32. /**
  33. * @param Filesystem $filesystem
  34. * @param PlaceholderInterface $placeholder
  35. */
  36. public function __construct(
  37. Filesystem $filesystem,
  38. PlaceholderInterface $placeholder
  39. ) {
  40. $this->filesystem = $filesystem;
  41. $this->placeholder = $placeholder;
  42. }
  43. /**
  44. * Retrieves config array paths from comment section of the config file
  45. *
  46. * Example:
  47. * some.file.config.php file is located in the directory /<root_app_dir>/app/etc/
  48. * File Content some.file.config.php
  49. * ```php
  50. * return [
  51. * 'scopes' => [
  52. * //...
  53. * ],
  54. * // ...
  55. * // Sensitive data can be stored in the following environment variables:
  56. * // CONFIG__DEFAULT__SOME__CONF__PATH_ONE for some/conf/path_one
  57. * 'system' => [],
  58. * // ...
  59. * // CONFIG__DEFAULT__SOME__CONF__PATH_TWO for some/conf/path_two
  60. * // ...
  61. * ];
  62. * ```
  63. * Usage:
  64. * ```php
  65. * // ...
  66. * // $commentParser variable contains an object of type \Magento\Config\Model\Config\Parser\Comment
  67. * $fileName = 'some.file.config.php';
  68. * $result = $commentParser->execute($fileName);
  69. * // ...
  70. * ```
  71. * The variable $result will be set to
  72. * ```php
  73. * array(
  74. * 'CONFIG__DEFAULT__SOME__CONF__PATH_ONE' => 'some/conf/path_one',
  75. * 'CONFIG__DEFAULT__SOME__CONF__PATH_TWO' => 'some/conf/path_two'
  76. * );
  77. * ```
  78. *
  79. * @param string $fileName the basename of file
  80. * @return array
  81. * @throws FileSystemException
  82. * @since 101.0.0
  83. */
  84. public function execute($fileName)
  85. {
  86. $fileContent = $this->filesystem
  87. ->getDirectoryRead(DirectoryList::CONFIG)
  88. ->readFile($fileName);
  89. $pattern = sprintf('/\s+\*\s+(?P<placeholder>%s.*?)\s/', preg_quote(Environment::PREFIX));
  90. preg_match_all($pattern, $fileContent, $matches);
  91. if (!isset($matches['placeholder'])) {
  92. return [];
  93. }
  94. $configs = [];
  95. foreach ($matches['placeholder'] as $placeholder) {
  96. $path = $this->placeholder->restore($placeholder);
  97. $path = preg_replace('/^' . ScopeConfigInterface::SCOPE_TYPE_DEFAULT . '\//', '', $path);
  98. $configs[$placeholder] = $path;
  99. }
  100. return $configs;
  101. }
  102. }