1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Config\App\Config\Source;
- use Magento\Framework\App\Config\ConfigSourceInterface;
- use Magento\Framework\DataObject;
- use Magento\Config\Model\Placeholder\PlaceholderFactory;
- use Magento\Config\Model\Placeholder\PlaceholderInterface;
- use Magento\Framework\Stdlib\ArrayManager;
- /**
- * Class for retrieving configurations from environment variables.
- *
- * @api
- * @since 101.0.0
- */
- class EnvironmentConfigSource implements ConfigSourceInterface
- {
- /**
- * Library for working with arrays.
- *
- * @var ArrayManager
- */
- private $arrayManager;
- /**
- * Object for working with placeholders for environment variables.
- *
- * @var PlaceholderInterface
- */
- private $placeholder;
- /**
- * @param ArrayManager $arrayManager
- * @param PlaceholderFactory $placeholderFactory
- */
- public function __construct(
- ArrayManager $arrayManager,
- PlaceholderFactory $placeholderFactory
- ) {
- $this->arrayManager = $arrayManager;
- $this->placeholder = $placeholderFactory->create(PlaceholderFactory::TYPE_ENVIRONMENT);
- }
- /**
- * @inheritdoc
- * @since 101.0.0
- */
- public function get($path = '')
- {
- $data = new DataObject($this->loadConfig());
- return $data->getData($path) ?: [];
- }
- /**
- * Loads config from environment variables.
- *
- * @return array
- */
- private function loadConfig()
- {
- $config = [];
- $environmentVariables = $_ENV;
- foreach ($environmentVariables as $template => $value) {
- if (!$this->placeholder->isApplicable($template)) {
- continue;
- }
- $config = $this->arrayManager->set(
- $this->placeholder->restore($template),
- $config,
- $value
- );
- }
- return $config;
- }
- }
|