This commit is contained in:
Paolo A
2024-08-13 13:44:16 +00:00
parent 1bbb23088d
commit e796d76612
4001 changed files with 30101 additions and 40075 deletions

View File

@@ -1,9 +1,26 @@
<?php
declare(strict_types=1);
namespace Dotenv\Store\File;
class Paths
/**
* @internal
*/
final class Paths
{
/**
* This class is a singleton.
*
* @codeCoverageIgnore
*
* @return void
*/
private function __construct()
{
//
}
/**
* Returns the full paths to the files.
*
@@ -18,7 +35,7 @@ class Paths
foreach ($paths as $path) {
foreach ($names as $name) {
$files[] = rtrim($path, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$name;
$files[] = \rtrim($path, \DIRECTORY_SEPARATOR).\DIRECTORY_SEPARATOR.$name;
}
}

View File

@@ -1,11 +1,30 @@
<?php
declare(strict_types=1);
namespace Dotenv\Store\File;
use Dotenv\Exception\InvalidEncodingException;
use Dotenv\Util\Str;
use PhpOption\Option;
class Reader
/**
* @internal
*/
final class Reader
{
/**
* This class is a singleton.
*
* @codeCoverageIgnore
*
* @return void
*/
private function __construct()
{
//
}
/**
* Read the file(s), and return their raw content.
*
@@ -13,17 +32,20 @@ class Reader
* short circuit mode is enabled, then the returned array with have length
* at most one. File paths that couldn't be read are omitted entirely.
*
* @param string[] $filePaths
* @param bool $shortCircuit
* @param string[] $filePaths
* @param bool $shortCircuit
* @param string|null $fileEncoding
*
* @throws \Dotenv\Exception\InvalidEncodingException
*
* @return array<string,string>
*/
public static function read(array $filePaths, $shortCircuit = true)
public static function read(array $filePaths, bool $shortCircuit = true, ?string $fileEncoding = null)
{
$output = [];
foreach ($filePaths as $filePath) {
$content = self::readFromFile($filePath);
$content = self::readFromFile($filePath, $fileEncoding);
if ($content->isDefined()) {
$output[$filePath] = $content->get();
if ($shortCircuit) {
@@ -38,15 +60,22 @@ class Reader
/**
* Read the given file.
*
* @param string $filePath
* @param string $path
* @param string|null $encoding
*
* @throws \Dotenv\Exception\InvalidEncodingException
*
* @return \PhpOption\Option<string>
*/
private static function readFromFile($filePath)
private static function readFromFile(string $path, ?string $encoding = null)
{
$content = @file_get_contents($filePath);
/** @var Option<string> */
$content = Option::fromValue(@\file_get_contents($path), false);
/** @var \PhpOption\Option<string> */
return Option::fromValue($content, false);
return $content->flatMap(static function (string $content) use ($encoding) {
return Str::utf8($content, $encoding)->mapError(static function (string $error) {
throw new InvalidEncodingException($error);
})->success();
});
}
}