### RepositoryBuilder createWithDefaultAdapters Example Source: https://github.com/vlucas/phpdotenv/blob/master/UPGRADING.md Shows how to configure the RepositoryBuilder with default adapters and then add a specific writer like ReplacingWriter for Apache environment variables. This example handles potential unavailability of Apache functions. ```php $builder = Dotenv\Repository\RepositoryBuilder::createWithDefaultAdapters(); Dotenv\Repository\Adapter\ApacheAdapter::create()->map(function ($adapter) { return new Dotenv\Repository\Adapter\ReplacingWriter($adapter, $adapter); })->map([$builder, 'addWriter'])->getOrElse($builder); $repository = $builder->make(); ``` -------------------------------- ### RepositoryBuilder createWithNoAdapters Example Source: https://github.com/vlucas/phpdotenv/blob/master/UPGRADING.md Demonstrates how to build a repository using the RepositoryBuilder, explicitly adding adapters and writers. This replaces older methods that implicitly called getenv/putenv. ```php $repository = Dotenv\Repository\RepositoryBuilder::createWithNoAdapters() ->addAdapter(Dotenv\Repository\Adapter\EnvConstAdapter::class) ->addWriter(Dotenv\Repository\Adapter\PutenvAdapter::class) ->make(); ``` -------------------------------- ### PHP dotenv v4 Load and Interpolate Example Source: https://github.com/vlucas/phpdotenv/blob/master/UPGRADING.md Shows how to load and process variable interpolations from raw content without immediately loading them into the environment in v4. This example uses an ArrayAdapter and Loader to manage the process. ```php withReaders($adapters) ->withWriters($adapters) ->make(); $variables = (new Loader())->load($repository, $content); ``` -------------------------------- ### Example .env.example File Content Source: https://github.com/vlucas/phpdotenv/blob/master/README.md Shows the structure of an example .env file used for version control. It includes required variable names but uses dummy or blank values for sensitive information. ```shell S3_BUCKET="devbucket" SECRET_KEY="abc123" ``` -------------------------------- ### PHP dotenv v4 Immutable Loading Example Source: https://github.com/vlucas/phpdotenv/blob/master/UPGRADING.md Demonstrates initializing the Dotenv class for immutable loading in v4. It utilizes RepositoryBuilder with specific adapters and the `immutable()` method before creating the Dotenv instance and loading variables. ```php withReaders($adapters) ->withWriters($adapters) ->immutable() ->make(); Dotenv::create($repository, $path, null)->load(); ``` -------------------------------- ### Custom Repository Configuration (No Adapters, Putenv Writer) Source: https://github.com/vlucas/phpdotenv/blob/master/README.md Configures a custom repository using RepositoryBuilder. This example starts with no adapters, adds EnvConstAdapter and PutenvAdapter, marks the repository as immutable, and then creates the repository instance. ```php $repository = Dotenv\Repository\RepositoryBuilder::createWithNoAdapters() ->addAdapter(Dotenv\Repository\Adapter\EnvConstAdapter::class) ->addWriter(Dotenv\Repository\Adapter\PutenvAdapter::class) ->immutable() ->make(); $dotenv = Dotenv\Dotenv::create($repository, __DIR__); $dotenv->load(); ``` -------------------------------- ### Variable Whitelisting to Allow Listing Source: https://github.com/vlucas/phpdotenv/blob/master/UPGRADING.md Details the shift from variable whitelisting to allow listing, with the responsibility moving from the loader to the new GuardedWriter adapter. ```APIDOC Dotenv\Repository\Adapter\GuardedWriter - Description: An adapter that enforces an allow-list for variables to be written. Replaces the previous whitelisting mechanism. ``` -------------------------------- ### Example .env File Content Source: https://github.com/vlucas/phpdotenv/blob/master/README.md Demonstrates the typical structure and content of a .env file, including sensitive and non-sensitive variables. This file should be kept out of version control. ```shell S3_BUCKET="dotenv" SECRET_KEY="souper_seekret_key" ``` -------------------------------- ### DotenvDotenv Constructor API (PHP) Source: https://github.com/vlucas/phpdotenv/blob/master/UPGRADING.md Details the constructor signature for the DotenvDotenv class, including parameter types, deprecation notices, and expected interfaces. This entry covers changes introduced in v4.1. ```APIDOC Dotenv\Dotenv Constructor: __construct(array|Dotenv\Store\StoreInterface $storeOrPaths, string $file = '.env', array $overrides = [], bool $immutable = false) - Constructor for the Dotenv\Dotenv class. - Parameters: - $storeOrPaths: Either an array of file paths (deprecated in V4.1, will be removed in V5) or an instance of Dotenv\Store\StoreInterface. - $file: The name of the environment file to load (defaults to '.env'). - $overrides: An array of environment variables to override existing ones. - $immutable: If true, prevents environment variables from being changed after loading. - Deprecation: - Passing an array of file paths as the first argument is deprecated in V4.1 and will be removed in V5. Use an instance of Dotenv\Store\StoreInterface instead. ``` -------------------------------- ### DotenvDotenv createImmutable/createMutable Changes Source: https://github.com/vlucas/phpdotenv/blob/master/UPGRADING.md Explains that createImmutable and createMutable no longer implicitly call getenv and putenv. For functionality requiring these, use createUnsafeImmutable and createUnsafeMutable instead. ```APIDOC Dotenv\Dotenv::createImmutable(string $dir, string $file = '.env', bool $use_putenv = false): DotenvDotenv - Description: Creates an immutable Dotenv instance. Does not call getenv/putenv by default. Dotenv\Dotenv::createMutable(string $dir, string $file = '.env'): DotenvDotenv - Description: Creates a mutable Dotenv instance. Does not call getenv/putenv by default. Dotenv\Dotenv::createUnsafeImmutable(string $dir, string $file = '.env', bool $use_putenv = false): DotenvDotenv - Description: Creates an immutable Dotenv instance that may call getenv/putenv. Dotenv\Dotenv::createUnsafeMutable(string $dir, string $file = '.env'): DotenvDotenv - Description: Creates a mutable Dotenv instance that may call getenv/putenv. ``` -------------------------------- ### Thread-Safe phpdotenv Initialization Source: https://github.com/vlucas/phpdotenv/blob/master/UPGRADING.md Demonstrates how to initialize phpdotenv in a threaded environment by providing a custom `DotenvFactory` with thread-safe adapters, preventing potential issues with non-thread-safe functions. ```PHP load(); ``` -------------------------------- ### Install PHP dotenv via Composer Source: https://github.com/vlucas/phpdotenv/blob/master/README.md This command installs the phpdotenv package using Composer, the dependency manager for PHP. It adds the library to your project's dependencies, making it available for use in your PHP application. ```Bash $ composer require vlucas/phpdotenv ``` -------------------------------- ### DotenvDotenv Constructor Changes Source: https://github.com/vlucas/phpdotenv/blob/master/UPGRADING.md Details the modification of the Dotenv constructor to expect exactly 4 parameters: a store, a parser, a loader, and a repository. This change is less impactful for users employing the static create methods. ```APIDOC Dotenv\Dotenv __construct(StoreInterface $store, ParserInterface $parser, LoaderInterface $loader, RepositoryInterface $repository) - Parameters: - store: An instance of StoreInterface. - parser: An instance of ParserInterface. - loader: An instance of LoaderInterface. - repository: An instance of RepositoryInterface. - Note: It is more common to use the public static create methods, which remain unchanged. ``` -------------------------------- ### phpdotenv V3 Constructor Update Source: https://github.com/vlucas/phpdotenv/blob/master/UPGRADING.md Version 3 of phpdotenv requires replacing `new Dotenv(...)` with `Dotenv::create(...)` as the constructor now accepts a `Loader` instance for greater customization. ```PHP Dotenv::create($path, null, $factory)->load(); ``` -------------------------------- ### Parser and Lines Namespace Changes Source: https://github.com/vlucas/phpdotenv/blob/master/UPGRADING.md Notes the relocation of the parser and Lines class to their own namespace. The parser now processes entire files, a change expected to have minimal user impact. ```APIDOC Dotenv\Parser\ParserInterface - Description: Interface for parsing dotenv files. Dotenv\Parser\Lines - Description: Utility class for handling lines from dotenv files. ``` -------------------------------- ### Loader Return Value and Input Changes Source: https://github.com/vlucas/phpdotenv/blob/master/UPGRADING.md Explains that the loader now returns only the variables actually loaded into the repository, not all variables from the file. It also expects input from the new parser, rather than raw file content. ```APIDOC Dotenv\Loader\LoaderInterface::load(array $entries): array - Description: Loads variables into the repository and returns only the variables that were successfully loaded. - Parameters: - entries: An array of entries parsed from a dotenv file. ``` -------------------------------- ### Comment Syntax in .env Files Source: https://github.com/vlucas/phpdotenv/blob/master/README.md Provides examples of how to add comments within `.env` files using the '#' character. Comments can appear on their own line or at the end of a line containing a variable assignment. ```shell # this is a comment VAR="value" # comment VAR=value # comment ``` -------------------------------- ### Custom Repository Configuration (Allow List) Source: https://github.com/vlucas/phpdotenv/blob/master/README.md Configures a custom repository using RepositoryBuilder with default adapters and an allow list. Only variables specified in the allow list will be loaded. ```php $repository = Dotenv\Repository\RepositoryBuilder::createWithDefaultAdapters() ->allowList(['FOO', 'BAR']) ->make(); $dotenv = Dotenv\Dotenv::create($repository, __DIR__); $dotenv->load(); ``` -------------------------------- ### Load Environment Variables (Basic) Source: https://github.com/vlucas/phpdotenv/blob/master/README.md Loads environment variables from a .env file located in the specified directory. This is the most common way to initialize phpdotenv. ```php $dotenv = Dotenv\Dotenv::createImmutable(__DIR__); $dotenv->load(); ``` -------------------------------- ### Load .env from Directory Source: https://github.com/vlucas/phpdotenv/blob/master/README.md Shows the standard method for loading environment variables from a `.env` file located in a specified directory. This is equivalent to parsing the file content directly but uses a file path. ```php Dotenv\Dotenv::createArrayBacked(__DIR__)->load(); ``` -------------------------------- ### Using getenv and putenv Source: https://github.com/vlucas/phpdotenv/blob/master/README.md Instructs phpdotenv to use getenv() and putenv() for managing environment variables by using createUnsafeImmutable. This is generally discouraged due to thread-safety issues. ```php $s3_bucket = getenv('S3_BUCKET'); $s3_bucket = $_ENV['S3_BUCKET']; $s3_bucket = $_SERVER['S3_BUCKET']; ``` -------------------------------- ### Nesting Environment Variables Source: https://github.com/vlucas/phpdotenv/blob/master/README.md Shows how to nest environment variables within each other using the ${VAR_NAME} syntax. This helps reduce repetition and maintain consistency. ```shell BASE_DIR="/var/webroot/project-root" CACHE_DIR="${BASE_DIR}/cache" TMP_DIR="${BASE_DIR}/tmp" ``` -------------------------------- ### Require Specific Environment Variables (Single) Source: https://github.com/vlucas/phpdotenv/blob/master/README.md Demonstrates how to enforce the presence of a single required environment variable using the `required` method. If the variable is missing, a RuntimeException is thrown. ```php $dotenv->required('DATABASE_DSN'); ``` -------------------------------- ### Require Specific Environment Variables (Array) Source: https://github.com/vlucas/phpdotenv/blob/master/README.md Shows how to enforce the presence of multiple required environment variables by passing an array of variable names to the `required` method. Missing variables will trigger a RuntimeException. ```php $dotenv->required(['DB_HOST', 'DB_NAME', 'DB_USER', 'DB_PASS']); ``` -------------------------------- ### Parse .env Content Directly Source: https://github.com/vlucas/phpdotenv/blob/master/README.md Demonstrates parsing `.env` file content directly from a string without needing a file path. This method is useful for testing or when the `.env` content is dynamically generated. It resolves nested environment variables. ```php // ['FOO' => 'Bar', 'BAZ' => 'Hello Bar'] Dotenv\Dotenv::parse("FOO=Bar\nBAZ=\"Hello \${FOO}\""); ``` -------------------------------- ### Accessing Environment Variables Source: https://github.com/vlucas/phpdotenv/blob/master/README.md Demonstrates how to access loaded environment variables using the $_ENV and $_SERVER super-globals after calling the load() method. ```php $s3_bucket = $_ENV['S3_BUCKET']; $s3_bucket = $_SERVER['S3_BUCKET']; ``` -------------------------------- ### Enforce Non-Empty Environment Variables Source: https://github.com/vlucas/phpdotenv/blob/master/README.md Illustrates how to ensure an environment variable is not only set but also contains a non-empty value. An exception is thrown if the variable is empty. ```php $dotenv->required('DATABASE_DSN')->notEmpty(); ``` -------------------------------- ### Load Environment Variables (Custom Filename) Source: https://github.com/vlucas/phpdotenv/blob/master/README.md Loads environment variables from a custom-named file instead of the default '.env'. The filename is passed as the second argument to createImmutable. ```php $dotenv = Dotenv\Dotenv::createImmutable(__DIR__, 'myconfig'); $dotenv->load(); ``` -------------------------------- ### Enforce Integer Environment Variables Source: https://github.com/vlucas/phpdotenv/blob/master/README.md Demonstrates how to validate that an environment variable is an integer. The `isInteger` method checks the value, and an exception is raised if it's not an integer. ```php $dotenv->required('FOO')->isInteger(); ``` -------------------------------- ### Conditionally Enforce Integer Environment Variables Source: https://github.com/vlucas/phpdotenv/blob/master/README.md Shows how to apply validation rules only if an environment variable is present. The `ifPresent` method ensures `isInteger` is only checked when the variable exists. ```php $dotenv->ifPresent('FOO')->isInteger(); ``` -------------------------------- ### Mutable Environment Variable Loading Source: https://github.com/vlucas/phpdotenv/blob/master/README.md Loads environment variables using createMutable, which allows Dotenv to overwrite existing environment variables. By default, the repository allows overwriting. ```php $dotenv = Dotenv\Dotenv::createMutable(__DIR__); $dotenv->load(); ``` -------------------------------- ### Conditionally Enforce Boolean Environment Variables Source: https://github.com/vlucas/phpdotenv/blob/master/README.md Demonstrates applying boolean validation only when an environment variable is present. The `ifPresent` method ensures `isBoolean` is only checked if the variable exists. ```php $dotenv->ifPresent('FOO')->isBoolean(); ``` -------------------------------- ### Enforce Allowed Regex Values for Environment Variables Source: https://github.com/vlucas/phpdotenv/blob/master/README.md Illustrates how to validate an environment variable against a regular expression. The `allowedRegexValues` method checks if the variable's value matches the provided regex pattern. ```php $dotenv->required('FOO')->allowedRegexValues('([[:lower:]]{3})'); ``` -------------------------------- ### Load Environment Variables (Safe) Source: https://github.com/vlucas/phpdotenv/blob/master/README.md Loads environment variables from a .env file, suppressing exceptions if the .env file is not found. This is useful for development environments where the .env file might be optional. ```php $dotenv = Dotenv\Dotenv::createImmutable(__DIR__); $dotenv->safeLoad(); ``` -------------------------------- ### Enforce Allowed Values for Environment Variables Source: https://github.com/vlucas/phpdotenv/blob/master/README.md Shows how to restrict an environment variable to a specific set of allowed string values. If the variable's value is not in the provided array, an exception is thrown. ```php $dotenv->required('SESSION_STORE')->allowedValues(['Filesystem', 'Memcached']); ``` -------------------------------- ### Enforce Boolean Environment Variables Source: https://github.com/vlucas/phpdotenv/blob/master/README.md Illustrates how to validate an environment variable as a boolean, accepting common string representations like 'true', 'false', 'On', 'Off', '1', '0', 'Yes', 'No'. An exception is thrown if the value is not recognized as a boolean. ```php $dotenv->required('FOO')->isBoolean(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.