Skip to content
  • P
    Projects
  • G
    Groups
  • S
    Snippets
  • Help

semour / semour_admin

  • This project
    • Loading...
  • Sign in
Go to a project
  • Project
  • Repository
  • Issues 0
  • Merge Requests 0
  • Pipelines
  • Wiki
  • Snippets
  • Settings
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Files
  • Commits
  • Branches
  • Tags
  • Contributors
  • Graph
  • Compare
  • Charts
Find file
BlameHistoryPermalink
Switch branch/tag
  • semour_admin
  • vendor
  • vlucas
  • phpdotenv
  • src
  • Repository
  • RepositoryBuilder.php
  • mushishixian's avatar
    Initial commit · 585b9d09
    mushishixian committed 2 years ago
    585b9d09
RepositoryBuilder.php 3.79 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
<?php

namespace Dotenv\Repository;

use Dotenv\Repository\Adapter\ApacheAdapter;
use Dotenv\Repository\Adapter\AvailabilityInterface;
use Dotenv\Repository\Adapter\EnvConstAdapter;
use Dotenv\Repository\Adapter\PutenvAdapter;
use Dotenv\Repository\Adapter\ServerConstAdapter;

class RepositoryBuilder
{
    /**
     * The set of readers to use.
     *
     * @var \Dotenv\Repository\Adapter\ReaderInterface[]|null
     */
    private $readers;

    /**
     * The set of writers to use.
     *
     * @var \Dotenv\Repository\Adapter\WriterInterface[]|null
     */
    private $writers;

    /**
     * Are we immutable?
     *
     * @var bool
     */
    private $immutable;

    /**
     * Create a new repository builder instance.
     *
     * @param \Dotenv\Repository\Adapter\ReaderInterface[]|null $readers
     * @param \Dotenv\Repository\Adapter\WriterInterface[]|null $writers
     * @param bool                                              $immutable
     *
     * @return void
     */
    private function __construct(array $readers = null, array $writers = null, $immutable = false)
    {
        $this->readers = $readers;
        $this->writers = $writers;
        $this->immutable = $immutable;
    }

    /**
     * Create a new repository builder instance.
     *
     * @return \Dotenv\Repository\RepositoryBuilder
     */
    public static function create()
    {
        return new self();
    }

    /**
     * Creates a repository builder with the given readers.
     *
     * @param \Dotenv\Repository\Adapter\ReaderInterface[]|null $readers
     *
     * @return \Dotenv\Repository\RepositoryBuilder
     */
    public function withReaders(array $readers = null)
    {
        $readers = $readers === null ? null : self::filterByAvailability($readers);

        return new self($readers, $this->writers, $this->immutable);
    }

    /**
     * Creates a repository builder with the given writers.
     *
     * @param \Dotenv\Repository\Adapter\WriterInterface[]|null $writers
     *
     * @return \Dotenv\Repository\RepositoryBuilder
     */
    public function withWriters(array $writers = null)
    {
        $writers = $writers === null ? null : self::filterByAvailability($writers);

        return new self($this->readers, $writers, $this->immutable);
    }

    /**
     * Creates a repository builder with mutability enabled.
     *
     * @return \Dotenv\Repository\RepositoryBuilder
     */
    public function immutable()
    {
        return new self($this->readers, $this->writers, true);
    }

    /**
     * Creates a new repository instance.
     *
     * @return \Dotenv\Repository\RepositoryInterface
     */
    public function make()
    {
        if ($this->readers === null || $this->writers === null) {
            $defaults = self::defaultAdapters();
        }

        return new AdapterRepository(
            $this->readers === null ? $defaults : $this->readers,
            $this->writers === null ? $defaults : $this->writers,
            $this->immutable
        );
    }

    /**
     * Return the array of default adapters.
     *
     * @return \Dotenv\Repository\Adapter\AvailabilityInterface[]
     */
    private static function defaultAdapters()
    {
        return self::filterByAvailability([
            new ApacheAdapter(),
            new EnvConstAdapter(),
            new ServerConstAdapter(),
            new PutenvAdapter(),
        ]);
    }

    /**
     * Filter an array of adapters to only those that are supported.
     *
     * @param \Dotenv\Repository\Adapter\AvailabilityInterface[] $adapters
     *
     * @return \Dotenv\Repository\Adapter\AvailabilityInterface[]
     */
    private static function filterByAvailability(array $adapters)
    {
        return array_filter($adapters, function (AvailabilityInterface $adapter) {
            return $adapter->isSupported();
        });
    }
}