@@ -16,37 +16,35 @@ composer require cleaniquecoders/php-env-key-manager
16
16
17
17
### Basic Usage
18
18
19
- ** Initialize ` EnvKeyManager ` ** : Provide the path to your ` .env ` file when creating the instance.
19
+ Following are the basic usage examples for the package:
20
20
21
- ``` php
22
- use CleaniqueCoders\PhpEnvKeyManager\EnvKeyManager;
21
+ ``` php
22
+ use CleaniqueCoders\PhpEnvKeyManager\EnvKeyManager;
23
23
24
- // Path to your .env file
25
- $envFilePath = __DIR__ . '/.env';
26
- $envManager = new EnvKeyManager($envFilePath);
27
- ```
24
+ // Path to your .env file
25
+ $envFilePath = __DIR__ . '/.env';
26
+ $envManager = new EnvKeyManager($envFilePath);
28
27
29
- ** Set an Environment Key** : Use ` setKey ` to add or update a key-value pair.
28
+ // Set a key
29
+ $envManager->setKey('APP_DEBUG', 'true');
30
30
31
- ``` php
32
- $key = 'APP_LOGIN_KEY';
33
- $value = bin2hex(random_bytes(16)); // Example 32-character random key
31
+ // Disable a key
32
+ $envManager->disableKey('APP_DEBUG');
34
33
35
- if ($envManager->setKey($key, $value)) {
36
- echo "Successfully set {$key} in .env file.";
37
- } else {
38
- echo "Failed to set {$key}. Please ensure the key exists in the .env file.";
39
- }
40
- ```
34
+ // Enable a key
35
+ $envManager->enableKey('APP_DEBUG');
36
+ ```
41
37
42
38
---
43
39
44
40
### Framework-Specific Examples
45
41
46
42
#### Laravel
47
43
44
+ To use ` EnvKeyManager ` in a Laravel application, register it as a singleton in the ` AppServiceProvider ` to allow easy access across your application.
45
+
48
46
<details >
49
- <summary >Usage in Laravel</summary >
47
+ <summary >Laravel Usage </summary >
50
48
51
49
1 . ** Register as a Singleton**
52
50
@@ -65,52 +63,68 @@ composer require cleaniquecoders/php-env-key-manager
65
63
66
64
2 . ** Usage in a Command**
67
65
66
+ Create a Laravel Artisan command to set, disable, or enable environment keys:
67
+
68
68
``` php
69
69
<?php
70
70
71
71
namespace App\Console\Commands;
72
72
73
73
use CleaniqueCoders\PhpEnvKeyManager\EnvKeyManager;
74
74
use Illuminate\Console\Command;
75
- use Illuminate\Support\Str;
76
75
77
- class GenerateLoginKeyCommand extends Command
76
+ class ManageEnvKeyCommand extends Command
78
77
{
79
- protected $signature = 'generate:login -key {--show : Display the key instead of modifying files }';
80
- protected $description = 'Generate and set a login key';
78
+ protected $signature = 'env:manage -key {action} { key} {value? }';
79
+ protected $description = 'Manage an environment key';
81
80
82
- protected $envKeyManager ;
81
+ protected $envManager ;
83
82
84
- public function __construct(EnvKeyManager $envKeyManager )
83
+ public function __construct(EnvKeyManager $envManager )
85
84
{
86
85
parent::__construct();
87
- $this->envKeyManager = $envKeyManager ;
86
+ $this->envManager = $envManager ;
88
87
}
89
88
90
89
public function handle()
91
90
{
92
- $key = Str::random(32);
93
-
94
- if ($this->option('show')) {
95
- return $this->line('<comment >'.$key.'</comment >');
91
+ $action = $this->argument('action');
92
+ $key = $this->argument('key');
93
+ $value = $this->argument('value');
94
+
95
+ switch ($action) {
96
+ case 'set':
97
+ $this->envManager->setKey($key, $value);
98
+ $this->info("Key {$key} set to {$value}.");
99
+ break;
100
+
101
+ case 'disable':
102
+ $this->envManager->disableKey($key);
103
+ $this->info("Key {$key} has been disabled.");
104
+ break;
105
+
106
+ case 'enable':
107
+ $this->envManager->enableKey($key);
108
+ $this->info("Key {$key} has been enabled.");
109
+ break;
110
+
111
+ default:
112
+ $this->error("Invalid action. Use 'set', 'disable', or 'enable'.");
96
113
}
97
-
98
- if (!$this->envKeyManager->setKey('APP_LOGIN_KEY', $key)) {
99
- $this->error('Failed to set APP_LOGIN_KEY in .env');
100
- return;
101
- }
102
-
103
- $this->info('Login key set successfully.');
104
114
}
105
115
}
106
116
```
107
117
108
118
</details >
109
119
120
+ ---
121
+
110
122
#### Symfony
111
123
124
+ To use ` EnvKeyManager ` in Symfony, initialize it with the ` .env ` path, and use it in Symfony commands or services.
125
+
112
126
<details >
113
- <summary >Usage in Symfony</summary >
127
+ <summary >Symfony Usage </summary >
114
128
115
129
1 . ** Initialize ` EnvKeyManager ` ** with Symfony’s ` .env ` path.
116
130
@@ -123,7 +137,7 @@ composer require cleaniquecoders/php-env-key-manager
123
137
124
138
2 . ** Use in a Symfony Command**
125
139
126
- Create a Symfony console command and inject ` EnvKeyManager ` :
140
+ Create a Symfony console command to manage environment keys :
127
141
128
142
``` php
129
143
<?php
@@ -132,43 +146,75 @@ composer require cleaniquecoders/php-env-key-manager
132
146
133
147
use CleaniqueCoders\PhpEnvKeyManager\EnvKeyManager;
134
148
use Symfony\Component\Console\Command\Command;
149
+ use Symfony\Component\Console\Input\InputArgument;
135
150
use Symfony\Component\Console\Input\InputInterface;
136
151
use Symfony\Component\Console\Output\OutputInterface;
137
152
138
- class GenerateLoginKeyCommand extends Command
153
+ class ManageEnvKeyCommand extends Command
139
154
{
140
- protected static $defaultName = 'app:generate-login-key';
141
- private $envKeyManager;
155
+ protected static $defaultName = 'env:manage-key';
156
+
157
+ private $envManager;
142
158
143
- public function __construct(EnvKeyManager $envKeyManager )
159
+ public function __construct(EnvKeyManager $envManager )
144
160
{
145
161
parent::__construct();
146
- $this->envKeyManager = $envKeyManager;
162
+ $this->envManager = $envManager;
163
+ }
164
+
165
+ protected function configure()
166
+ {
167
+ $this
168
+ ->setDescription('Manage an environment key')
169
+ ->addArgument('action', InputArgument::REQUIRED, 'Action: set, disable, enable')
170
+ ->addArgument('key', InputArgument::REQUIRED, 'The environment key')
171
+ ->addArgument('value', InputArgument::OPTIONAL, 'The value for set action');
147
172
}
148
173
149
174
protected function execute(InputInterface $input, OutputInterface $output)
150
175
{
151
- $key = bin2hex(random_bytes(16));
152
-
153
- if ($this->envKeyManager->setKey('APP_LOGIN_KEY', $key)) {
154
- $output->writeln("Login key set successfully: {$key}");
155
- return Command::SUCCESS;
156
- } else {
157
- $output->writeln("<error >Failed to set APP_LOGIN_KEY in .env</error >");
158
- return Command::FAILURE;
176
+ $action = $input->getArgument('action');
177
+ $key = $input->getArgument('key');
178
+ $value = $input->getArgument('value');
179
+
180
+ switch ($action) {
181
+ case 'set':
182
+ $this->envManager->setKey($key, $value);
183
+ $output->writeln("Key {$key} set to {$value}.");
184
+ break;
185
+
186
+ case 'disable':
187
+ $this->envManager->disableKey($key);
188
+ $output->writeln("Key {$key} has been disabled.");
189
+ break;
190
+
191
+ case 'enable':
192
+ $this->envManager->enableKey($key);
193
+ $output->writeln("Key {$key} has been enabled.");
194
+ break;
195
+
196
+ default:
197
+ $output->writeln("Invalid action. Use 'set', 'disable', or 'enable'.");
198
+ return Command::FAILURE;
159
199
}
200
+
201
+ return Command::SUCCESS;
160
202
}
161
203
}
162
204
```
163
205
164
206
</details >
165
207
208
+ ---
209
+
166
210
#### CodeIgniter
167
211
212
+ To use ` EnvKeyManager ` in CodeIgniter, initialize it with the ` .env ` path and use it within controllers or custom classes.
213
+
168
214
<details >
169
- <summary >Usage in CodeIgniter</summary >
215
+ <summary >CodeIgniter Usage </summary >
170
216
171
- 1 . ** Set Up ** : Define ` .env ` path and create ` EnvKeyManager ` instance .
217
+ 1 . ** Initialize ` EnvKeyManager ` ** with the CodeIgniter ` .env ` path.
172
218
173
219
``` php
174
220
use CleaniqueCoders\PhpEnvKeyManager\EnvKeyManager;
@@ -177,9 +223,9 @@ composer require cleaniquecoders/php-env-key-manager
177
223
$envManager = new EnvKeyManager($envFilePath);
178
224
```
179
225
180
- 2 . ** Usage in CodeIgniter Controller**
226
+ 2 . ** Use in a CodeIgniter Controller**
181
227
182
- In a controller or any other class :
228
+ Create a CodeIgniter controller method to manage environment keys :
183
229
184
230
``` php
185
231
<?php
@@ -190,24 +236,41 @@ composer require cleaniquecoders/php-env-key-manager
190
236
191
237
class EnvController extends BaseController
192
238
{
193
- public function updateEnv()
239
+ protected $envManager;
240
+
241
+ public function __construct()
194
242
{
195
- $envFilePath = ROOTPATH . '.env';
196
- $envManager = new EnvKeyManager($envFilePath);
197
- $key = 'CI_LOGIN_KEY';
198
- $value = bin2hex(random_bytes(16));
199
-
200
- if ($envManager->setKey($key, $value)) {
201
- echo "Successfully set {$key} in .env file.";
202
- } else {
203
- echo "Failed to set {$key}. Please ensure the key exists in the .env file.";
243
+ $this->envManager = new EnvKeyManager(ROOTPATH . '.env');
244
+ }
245
+
246
+ public function manageKey($action, $key, $value = null)
247
+ {
248
+ switch ($action) {
249
+ case 'set':
250
+ $this->envManager->setKey($key, $value);
251
+ return "Key {$key} set to {$value}.";
252
+
253
+ case 'disable':
254
+ $this->envManager->disableKey($key);
255
+ return "Key {$key} has been disabled.";
256
+
257
+ case 'enable':
258
+ $this->envManager->enableKey($key);
259
+ return "Key {$key} has been enabled.";
260
+
261
+ default:
262
+ return "Invalid action. Use 'set', 'disable', or 'enable'.";
204
263
}
205
264
}
206
265
}
207
266
```
208
267
209
268
</details >
210
269
270
+ ---
271
+
272
+ These framework-specific examples demonstrate how to integrate ` EnvKeyManager ` seamlessly in Laravel, Symfony, and CodeIgniter, making it easy to manage environment keys within each framework.
273
+
211
274
## Testing
212
275
213
276
``` bash
0 commit comments