Loader.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. declare(strict_types=1);
  3. namespace Dotenv\Loader;
  4. use Dotenv\Parser\Entry;
  5. use Dotenv\Parser\Value;
  6. use Dotenv\Repository\RepositoryInterface;
  7. final class Loader implements LoaderInterface
  8. {
  9. /**
  10. * Load the given entries into the repository.
  11. *
  12. * We'll substitute any nested variables, and send each variable to the
  13. * repository, with the effect of actually mutating the environment.
  14. *
  15. * @param \Dotenv\Repository\RepositoryInterface $repository
  16. * @param \Dotenv\Parser\Entry[] $entries
  17. *
  18. * @return array<string,string|null>
  19. */
  20. public function load(RepositoryInterface $repository, array $entries)
  21. {
  22. return \array_reduce($entries, static function (array $vars, Entry $entry) use ($repository) {
  23. $name = $entry->getName();
  24. $value = $entry->getValue()->map(static function (Value $value) use ($repository) {
  25. return Resolver::resolve($repository, $value);
  26. });
  27. if ($value->isDefined()) {
  28. $inner = $value->get();
  29. if ($repository->set($name, $inner)) {
  30. return \array_merge($vars, [$name => $inner]);
  31. }
  32. } else {
  33. if ($repository->clear($name)) {
  34. return \array_merge($vars, [$name => null]);
  35. }
  36. }
  37. return $vars;
  38. }, []);
  39. }
  40. }