Ajout des vendor
This commit is contained in:
143
vendor/symfony/dotenv/Command/DebugCommand.php
vendored
Normal file
143
vendor/symfony/dotenv/Command/DebugCommand.php
vendored
Normal file
@ -0,0 +1,143 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Dotenv\Command;
|
||||
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use Symfony\Component\Dotenv\Dotenv;
|
||||
|
||||
/**
|
||||
* A console command to debug current dotenv files with variables and values.
|
||||
*
|
||||
* @author Christopher Hertel <mail@christopher-hertel.de>
|
||||
*/
|
||||
final class DebugCommand extends Command
|
||||
{
|
||||
protected static $defaultName = 'debug:dotenv';
|
||||
protected static $defaultDescription = 'Lists all dotenv files with variables and values';
|
||||
|
||||
private $kernelEnvironment;
|
||||
private $projectDirectory;
|
||||
|
||||
public function __construct(string $kernelEnvironment, string $projectDirectory)
|
||||
{
|
||||
$this->kernelEnvironment = $kernelEnvironment;
|
||||
$this->projectDirectory = $projectDirectory;
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
$io->title('Dotenv Variables & Files');
|
||||
|
||||
if (!\array_key_exists('SYMFONY_DOTENV_VARS', $_SERVER)) {
|
||||
$io->error('Dotenv component is not initialized.');
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
$envFiles = $this->getEnvFiles();
|
||||
$availableFiles = array_filter($envFiles, function (string $file) {
|
||||
return is_file($this->getFilePath($file));
|
||||
});
|
||||
|
||||
if (\in_array('.env.local.php', $availableFiles, true)) {
|
||||
$io->warning('Due to existing dump file (.env.local.php) all other dotenv files are skipped.');
|
||||
}
|
||||
|
||||
if (is_file($this->getFilePath('.env')) && is_file($this->getFilePath('.env.dist'))) {
|
||||
$io->warning('The file .env.dist gets skipped due to the existence of .env.');
|
||||
}
|
||||
|
||||
$io->section('Scanned Files (in descending priority)');
|
||||
$io->listing(array_map(static function (string $envFile) use ($availableFiles) {
|
||||
return \in_array($envFile, $availableFiles, true)
|
||||
? sprintf('<fg=green>✓</> %s', $envFile)
|
||||
: sprintf('<fg=red>⨯</> %s', $envFile);
|
||||
}, $envFiles));
|
||||
|
||||
$io->section('Variables');
|
||||
$io->table(
|
||||
array_merge(['Variable', 'Value'], $availableFiles),
|
||||
$this->getVariables($availableFiles)
|
||||
);
|
||||
|
||||
$io->comment('Note real values might be different between web and CLI.');
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private function getVariables(array $envFiles): array
|
||||
{
|
||||
$vars = explode(',', $_SERVER['SYMFONY_DOTENV_VARS'] ?? '');
|
||||
sort($vars);
|
||||
|
||||
$output = [];
|
||||
$fileValues = [];
|
||||
foreach ($vars as $var) {
|
||||
$realValue = $_SERVER[$var];
|
||||
$varDetails = [$var, $realValue];
|
||||
foreach ($envFiles as $envFile) {
|
||||
$values = $fileValues[$envFile] ?? $fileValues[$envFile] = $this->loadValues($envFile);
|
||||
|
||||
$varString = $values[$var] ?? '<fg=yellow>n/a</>';
|
||||
$shortenedVar = $this->getHelper('formatter')->truncate($varString, 30);
|
||||
$varDetails[] = $varString === $realValue ? '<fg=green>'.$shortenedVar.'</>' : $shortenedVar;
|
||||
}
|
||||
|
||||
$output[] = $varDetails;
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
private function getEnvFiles(): array
|
||||
{
|
||||
$files = [
|
||||
'.env.local.php',
|
||||
sprintf('.env.%s.local', $this->kernelEnvironment),
|
||||
sprintf('.env.%s', $this->kernelEnvironment),
|
||||
];
|
||||
|
||||
if ('test' !== $this->kernelEnvironment) {
|
||||
$files[] = '.env.local';
|
||||
}
|
||||
|
||||
if (!is_file($this->getFilePath('.env')) && is_file($this->getFilePath('.env.dist'))) {
|
||||
$files[] = '.env.dist';
|
||||
} else {
|
||||
$files[] = '.env';
|
||||
}
|
||||
|
||||
return $files;
|
||||
}
|
||||
|
||||
private function getFilePath(string $file): string
|
||||
{
|
||||
return $this->projectDirectory.\DIRECTORY_SEPARATOR.$file;
|
||||
}
|
||||
|
||||
private function loadValues(string $file): array
|
||||
{
|
||||
$filePath = $this->getFilePath($file);
|
||||
|
||||
if (str_ends_with($filePath, '.php')) {
|
||||
return include $filePath;
|
||||
}
|
||||
|
||||
return (new Dotenv())->parse(file_get_contents($filePath));
|
||||
}
|
||||
}
|
123
vendor/symfony/dotenv/Command/DotenvDumpCommand.php
vendored
Normal file
123
vendor/symfony/dotenv/Command/DotenvDumpCommand.php
vendored
Normal file
@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Dotenv\Command;
|
||||
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
|
||||
use Symfony\Component\Dotenv\Dotenv;
|
||||
|
||||
/**
|
||||
* A console command to compile .env files into a PHP-optimized file called .env.local.php.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
#[Autoconfigure(bind: ['$projectDir' => '%kernel.project_dir%', '$defaultEnv' => '%kernel.environment%'])]
|
||||
final class DotenvDumpCommand extends Command
|
||||
{
|
||||
protected static $defaultName = 'dotenv:dump';
|
||||
protected static $defaultDescription = 'Compiles .env files to .env.local.php';
|
||||
|
||||
private $projectDir;
|
||||
private $defaultEnv;
|
||||
|
||||
public function __construct(string $projectDir, string $defaultEnv = null)
|
||||
{
|
||||
$this->projectDir = $projectDir;
|
||||
$this->defaultEnv = $defaultEnv;
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function configure()
|
||||
{
|
||||
$this
|
||||
->setDefinition([
|
||||
new InputArgument('env', null === $this->defaultEnv ? InputArgument::REQUIRED : InputArgument::OPTIONAL, 'The application environment to dump .env files for - e.g. "prod".'),
|
||||
])
|
||||
->addOption('empty', null, InputOption::VALUE_NONE, 'Ignore the content of .env files')
|
||||
->setHelp(<<<'EOT'
|
||||
The <info>%command.name%</info> command compiles .env files into a PHP-optimized file called .env.local.php.
|
||||
|
||||
<info>%command.full_name%</info>
|
||||
EOT
|
||||
)
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$config = [];
|
||||
if (is_file($projectDir = $this->projectDir)) {
|
||||
$config = ['dotenv_path' => basename($projectDir)];
|
||||
$projectDir = \dirname($projectDir);
|
||||
}
|
||||
|
||||
$composerFile = $projectDir.'/composer.json';
|
||||
$config += (is_file($composerFile) ? json_decode(file_get_contents($composerFile), true) : [])['extra']['runtime'] ?? [];
|
||||
$dotenvPath = $projectDir.'/'.($config['dotenv_path'] ?? '.env');
|
||||
$env = $input->getArgument('env') ?? $this->defaultEnv;
|
||||
$envKey = $config['env_var_name'] ?? 'APP_ENV';
|
||||
|
||||
if ($input->getOption('empty')) {
|
||||
$vars = [$envKey => $env];
|
||||
} else {
|
||||
$vars = $this->loadEnv($dotenvPath, $env, $config);
|
||||
$env = $vars[$envKey];
|
||||
}
|
||||
|
||||
$vars = var_export($vars, true);
|
||||
$vars = <<<EOF
|
||||
<?php
|
||||
|
||||
// This file was generated by running "php bin/console dotenv:dump $env"
|
||||
|
||||
return $vars;
|
||||
|
||||
EOF;
|
||||
file_put_contents($dotenvPath.'.local.php', $vars, \LOCK_EX);
|
||||
|
||||
$output->writeln(sprintf('Successfully dumped .env files in <info>.env.local.php</> for the <info>%s</> environment.', $env));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private function loadEnv(string $dotenvPath, string $env, array $config): array
|
||||
{
|
||||
$dotenv = new Dotenv();
|
||||
$envKey = $config['env_var_name'] ?? 'APP_ENV';
|
||||
$testEnvs = $config['test_envs'] ?? ['test'];
|
||||
|
||||
$globalsBackup = [$_SERVER, $_ENV];
|
||||
unset($_SERVER[$envKey]);
|
||||
$_ENV = [$envKey => $env];
|
||||
$_SERVER['SYMFONY_DOTENV_VARS'] = implode(',', array_keys($_SERVER));
|
||||
|
||||
try {
|
||||
$dotenv->loadEnv($dotenvPath, null, 'dev', $testEnvs);
|
||||
unset($_ENV['SYMFONY_DOTENV_VARS']);
|
||||
|
||||
return $_ENV;
|
||||
} finally {
|
||||
[$_SERVER, $_ENV] = $globalsBackup;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user