EnvironmentPlaceholder.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Config\Model\Config\Processor;
  7. use Magento\Config\Model\Placeholder\PlaceholderFactory;
  8. use Magento\Config\Model\Placeholder\PlaceholderInterface;
  9. use Magento\Framework\App\Config\Spi\PreProcessorInterface;
  10. use Magento\Framework\Stdlib\ArrayManager;
  11. /**
  12. * Allows to extract configurations from environment variables.
  13. * @api
  14. * @since 100.1.2
  15. */
  16. class EnvironmentPlaceholder implements PreProcessorInterface
  17. {
  18. /**
  19. * @var PlaceholderFactory
  20. */
  21. private $placeholderFactory;
  22. /**
  23. * @var ArrayManager
  24. */
  25. private $arrayManager;
  26. /**
  27. * @var PlaceholderInterface
  28. */
  29. private $placeholder;
  30. /**
  31. * @param PlaceholderFactory $placeholderFactory
  32. * @param ArrayManager $arrayManager
  33. */
  34. public function __construct(
  35. PlaceholderFactory $placeholderFactory,
  36. ArrayManager $arrayManager
  37. ) {
  38. $this->placeholderFactory = $placeholderFactory;
  39. $this->arrayManager = $arrayManager;
  40. $this->placeholder = $placeholderFactory->create(PlaceholderFactory::TYPE_ENVIRONMENT);
  41. }
  42. /**
  43. * Method extracts environment variables.
  44. * If environment variable is matching the desired rule - it's being used as value.
  45. *
  46. * {@inheritdoc}
  47. * @since 100.1.2
  48. */
  49. public function process(array $config)
  50. {
  51. $environmentVariables = $_ENV;
  52. foreach ($environmentVariables as $template => $value) {
  53. if (!$this->placeholder->isApplicable($template)) {
  54. continue;
  55. }
  56. $config = $this->arrayManager->set(
  57. $this->placeholder->restore($template),
  58. $config,
  59. $value
  60. );
  61. }
  62. return $config;
  63. }
  64. }