|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Blitz PHP framework. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2022 Dimitri Sitchet Tomkeu <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view |
|
9
|
|
|
* the LICENSE file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace BlitzPHP\Cli\Commands\Utilities; |
|
13
|
|
|
|
|
14
|
|
|
use BlitzPHP\Cli\Console\Command; |
|
15
|
|
|
use BlitzPHP\Loader\DotEnv; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Commande pour afficher l'environnement actuel, ou pour définir un nouveau dans le fichier `.env`. |
|
19
|
|
|
*/ |
|
20
|
|
|
final class Environment extends Command |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var string Groupe |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $group = 'BlitzPHP'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string Nom |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $name = 'env'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var string Description |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $description = 'Récupère l\'environnement actuel, ou en définir un nouveau.'; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Arguments de la commande |
|
39
|
|
|
* |
|
40
|
|
|
* @var array<string, string> |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $arguments = [ |
|
43
|
|
|
'[environment]' => '[Optionel] Nouveau environnement à définir. Si aucun n\'est fourni, cela imprimera l\'environnement actuel.', |
|
44
|
|
|
]; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Valeurs autorisées pour l'environnement. |
|
48
|
|
|
* Tester le travail est exclu puisque klinge ne fonctionnera pas sur elle. |
|
49
|
|
|
* |
|
50
|
|
|
* @var array<int, string> |
|
51
|
|
|
*/ |
|
52
|
|
|
private static array $knownTypes = [ |
|
53
|
|
|
'production', |
|
54
|
|
|
'development', |
|
55
|
|
|
]; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritDoc} |
|
59
|
|
|
*/ |
|
60
|
|
|
public function execute(array $params) |
|
61
|
|
|
{ |
|
62
|
|
|
if (null === $env = $this->argument('environment')) { |
|
63
|
|
|
$this->write('Votre environnement est actuellement défini comme: '); |
|
64
|
|
|
$this->ok(config('app.environment'))->eol(); |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
return; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if ($env === 'testing') { |
|
70
|
|
|
$this->fail('L\'environnement « test » est réservé aux tests PHPUnit ou Kahlan.'); |
|
71
|
|
|
$this->fail('Vous ne pourrez pas exécuter klinge sous un environnement « test ».'); |
|
72
|
|
|
$this->newLine(); |
|
73
|
|
|
|
|
74
|
|
|
return; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
if (! in_array($env, self::$knownTypes, true)) { |
|
78
|
|
|
$this->error(sprintf('Type d\'environnement non valide "%s". Attendu un des "%s".', $env, implode('" et "', self::$knownTypes))); |
|
79
|
|
|
$this->newLine(); |
|
80
|
|
|
|
|
81
|
|
|
return; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
if (! $this->writeNewEnvironmentToEnvFile($env)) { |
|
85
|
|
|
$this->error('Erreur dans l\'écriture nouvel environnement dans le fichier `.env`.'); |
|
86
|
|
|
$this->newLine(); |
|
87
|
|
|
|
|
88
|
|
|
return; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$this->success(sprintf('Environnement est modifié avec succès pour "%s".', $env)); |
|
92
|
|
|
$this->write('La constante ENVIRONNEMENT sera modifiée dans la prochaine exécution du script.'); |
|
93
|
|
|
$this->newLine(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @see https://regex101.com/r/4sSORp/1 for the regex in action |
|
98
|
|
|
*/ |
|
99
|
|
|
private function writeNewEnvironmentToEnvFile(string $newEnv): bool |
|
100
|
|
|
{ |
|
101
|
|
|
$baseEnv = ROOTPATH . '.env.example'; |
|
102
|
|
|
$envFile = ROOTPATH . '.env'; |
|
103
|
|
|
|
|
104
|
|
|
if (! is_file($envFile)) { |
|
105
|
|
|
if (! is_file($baseEnv)) { |
|
106
|
|
|
$this->writer->warn('Les deux fichiers `.env.example` et `.env` sont manquants.', true); |
|
107
|
|
|
$this->write('Il est impossible d\'écrire le nouveau type d\'environnement.'); |
|
108
|
|
|
$this->newLine(); |
|
109
|
|
|
|
|
110
|
|
|
return false; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
copy($baseEnv, $envFile); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return DotEnv::instance()->replace(['ENVIRONMENT' => $newEnv]); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|