Environment.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Config\Model\Placeholder;
  7. use Magento\Framework\App\Config\ScopeConfigInterface;
  8. use Magento\Framework\App\DeploymentConfig;
  9. /**
  10. * Class is used to work with placeholders for environment variables names based on config paths
  11. * @api
  12. * @since 100.1.2
  13. */
  14. class Environment implements PlaceholderInterface
  15. {
  16. /**
  17. * @const string Prefix for placeholder
  18. */
  19. const PREFIX = 'CONFIG__';
  20. /**
  21. * @var DeploymentConfig
  22. */
  23. private $deploymentConfig;
  24. /**
  25. * @param DeploymentConfig $deploymentConfig
  26. */
  27. public function __construct(DeploymentConfig $deploymentConfig)
  28. {
  29. $this->deploymentConfig = $deploymentConfig;
  30. }
  31. /**
  32. * Generates placeholder like CONFIG__DEFAULT__TEST__TEST_VALUE
  33. *
  34. * @inheritdoc
  35. * @since 100.1.2
  36. */
  37. public function generate($path, $scopeType = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeCode = null)
  38. {
  39. $parts = $scopeType ? [$scopeType] : [];
  40. if ($scopeType !== ScopeConfigInterface::SCOPE_TYPE_DEFAULT && $scopeCode) {
  41. $parts[] = $scopeCode;
  42. }
  43. $parts[] = $path;
  44. $template = implode('__', $parts);
  45. $template = str_replace('/', '__', $template);
  46. $template = static::PREFIX . $template;
  47. $template = strtoupper($template);
  48. return $template;
  49. }
  50. /**
  51. * @inheritdoc
  52. * @since 100.1.2
  53. */
  54. public function restore($template)
  55. {
  56. $template = preg_replace('/^' . static::PREFIX . '/', '', $template);
  57. $template = str_replace('__', '/', $template);
  58. $template = strtolower($template);
  59. return $template;
  60. }
  61. /**
  62. * @inheritdoc
  63. * @since 100.1.2
  64. */
  65. public function isApplicable($placeholder)
  66. {
  67. return 1 === preg_match('/^' . static::PREFIX . '([a-zA-Z]+)([a-zA-Z0-9_])*$/', $placeholder);
  68. }
  69. }