Settings.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\TestFramework\Bootstrap;
  7. use Magento\Framework\Filesystem\Glob;
  8. /**
  9. * Convenient access to the bootstrap settings
  10. */
  11. class Settings
  12. {
  13. /**
  14. * Base directory to be used to resolve relative paths
  15. *
  16. * @var string
  17. */
  18. private $_baseDir;
  19. /**
  20. * Key-value pairs of the settings
  21. *
  22. * @var array
  23. */
  24. private $_settings = [];
  25. /**
  26. * Constructor
  27. *
  28. * @param string $baseDir
  29. * @param array $settings
  30. * @throws \InvalidArgumentException
  31. */
  32. public function __construct($baseDir, array $settings)
  33. {
  34. if (!is_dir($baseDir)) {
  35. throw new \InvalidArgumentException("Base path '{$baseDir}' has to be an existing directory.");
  36. }
  37. $this->_baseDir = realpath($baseDir);
  38. $this->_settings = $settings;
  39. }
  40. /**
  41. * Retrieve a setting value as is
  42. *
  43. * @param string $settingName
  44. * @param mixed $defaultValue
  45. * @return mixed
  46. */
  47. public function get($settingName, $defaultValue = null)
  48. {
  49. return array_key_exists($settingName, $this->_settings) ? $this->_settings[$settingName] : $defaultValue;
  50. }
  51. /**
  52. * Interpret a setting value as a switch and return TRUE when it equals to the string "enabled" or FALSE otherwise
  53. *
  54. * @param string $settingName
  55. * @return bool
  56. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  57. */
  58. public function getAsBoolean($settingName)
  59. {
  60. return $this->get($settingName) === 'enabled';
  61. }
  62. /**
  63. * Interpret a setting value as a relative file name and return absolute path to it
  64. *
  65. * @param string $settingName
  66. * @param string $defaultValue
  67. * @return string
  68. */
  69. public function getAsFile($settingName, $defaultValue = '')
  70. {
  71. $result = $this->get($settingName, $defaultValue);
  72. if ($result !== '') {
  73. $result = $this->_resolvePath($result);
  74. }
  75. return $result;
  76. }
  77. /**
  78. * Interpret a setting value as a file optionally falling back to the '.dist' file and return absolute path to it
  79. *
  80. * @param string $settingName
  81. * @return string
  82. * @throws \Magento\Framework\Exception\LocalizedException
  83. */
  84. public function getAsConfigFile($settingName)
  85. {
  86. $result = $this->getAsFile($settingName);
  87. if ($result !== '') {
  88. if (!is_file($result) && substr($result, -5) != '.dist') {
  89. $result .= '.dist';
  90. }
  91. if (is_file($result)) {
  92. return $result;
  93. }
  94. }
  95. throw new \Magento\Framework\Exception\LocalizedException(
  96. __("Setting '%1' specifies the non-existing file '%2'.", $settingName, $result)
  97. );
  98. }
  99. /**
  100. * Interpret a setting value as a semicolon-separated relative glob pattern(s) and return matched absolute paths
  101. *
  102. * @param string $settingName
  103. * @return array
  104. */
  105. public function getAsMatchingPaths($settingName)
  106. {
  107. $settingValue = $this->get($settingName, '');
  108. if ($settingValue !== '') {
  109. return $this->_resolvePathPattern($settingValue);
  110. }
  111. return [];
  112. }
  113. /**
  114. * Return an absolute path by a relative one without checking its validity
  115. *
  116. * @param string $relativePath
  117. * @return string
  118. */
  119. protected function _resolvePath($relativePath)
  120. {
  121. return $this->_baseDir . '/' . $relativePath;
  122. }
  123. /**
  124. * Resolve semicolon-separated relative glob pattern(s) to matched absolute paths
  125. *
  126. * @param string $pattern
  127. * @return array
  128. */
  129. protected function _resolvePathPattern($pattern)
  130. {
  131. $result = [];
  132. $allPatterns = preg_split('/\s*;\s*/', trim($pattern), -1, PREG_SPLIT_NO_EMPTY);
  133. foreach ($allPatterns as $onePattern) {
  134. $onePattern = $this->_resolvePath($onePattern);
  135. $files = Glob::glob($onePattern, Glob::GLOB_BRACE);
  136. $result = array_merge($result, $files);
  137. }
  138. return $result;
  139. }
  140. }