Skip to content

Commit bdc04e3

Browse files
committed
Update README on Usage
1 parent 56c0a89 commit bdc04e3

File tree

1 file changed

+128
-65
lines changed

1 file changed

+128
-65
lines changed

README.md

Lines changed: 128 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,35 @@ composer require cleaniquecoders/php-env-key-manager
1616

1717
### Basic Usage
1818

19-
**Initialize `EnvKeyManager`**: Provide the path to your `.env` file when creating the instance.
19+
Following are the basic usage examples for the package:
2020

21-
```php
22-
use CleaniqueCoders\PhpEnvKeyManager\EnvKeyManager;
21+
```php
22+
use CleaniqueCoders\PhpEnvKeyManager\EnvKeyManager;
2323

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);
2827

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');
3030

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');
3433

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+
```
4137

4238
---
4339

4440
### Framework-Specific Examples
4541

4642
#### Laravel
4743

44+
To use `EnvKeyManager` in a Laravel application, register it as a singleton in the `AppServiceProvider` to allow easy access across your application.
45+
4846
<details>
49-
<summary>Usage in Laravel</summary>
47+
<summary>Laravel Usage</summary>
5048

5149
1. **Register as a Singleton**
5250

@@ -65,52 +63,68 @@ composer require cleaniquecoders/php-env-key-manager
6563

6664
2. **Usage in a Command**
6765

66+
Create a Laravel Artisan command to set, disable, or enable environment keys:
67+
6868
```php
6969
<?php
7070

7171
namespace App\Console\Commands;
7272

7373
use CleaniqueCoders\PhpEnvKeyManager\EnvKeyManager;
7474
use Illuminate\Console\Command;
75-
use Illuminate\Support\Str;
7675

77-
class GenerateLoginKeyCommand extends Command
76+
class ManageEnvKeyCommand extends Command
7877
{
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';
8180

82-
protected $envKeyManager;
81+
protected $envManager;
8382

84-
public function __construct(EnvKeyManager $envKeyManager)
83+
public function __construct(EnvKeyManager $envManager)
8584
{
8685
parent::__construct();
87-
$this->envKeyManager = $envKeyManager;
86+
$this->envManager = $envManager;
8887
}
8988

9089
public function handle()
9190
{
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'.");
96113
}
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.');
104114
}
105115
}
106116
```
107117

108118
</details>
109119

120+
---
121+
110122
#### Symfony
111123

124+
To use `EnvKeyManager` in Symfony, initialize it with the `.env` path, and use it in Symfony commands or services.
125+
112126
<details>
113-
<summary>Usage in Symfony</summary>
127+
<summary>Symfony Usage</summary>
114128

115129
1. **Initialize `EnvKeyManager`** with Symfony’s `.env` path.
116130

@@ -123,7 +137,7 @@ composer require cleaniquecoders/php-env-key-manager
123137

124138
2. **Use in a Symfony Command**
125139

126-
Create a Symfony console command and inject `EnvKeyManager`:
140+
Create a Symfony console command to manage environment keys:
127141

128142
```php
129143
<?php
@@ -132,43 +146,75 @@ composer require cleaniquecoders/php-env-key-manager
132146

133147
use CleaniqueCoders\PhpEnvKeyManager\EnvKeyManager;
134148
use Symfony\Component\Console\Command\Command;
149+
use Symfony\Component\Console\Input\InputArgument;
135150
use Symfony\Component\Console\Input\InputInterface;
136151
use Symfony\Component\Console\Output\OutputInterface;
137152

138-
class GenerateLoginKeyCommand extends Command
153+
class ManageEnvKeyCommand extends Command
139154
{
140-
protected static $defaultName = 'app:generate-login-key';
141-
private $envKeyManager;
155+
protected static $defaultName = 'env:manage-key';
156+
157+
private $envManager;
142158

143-
public function __construct(EnvKeyManager $envKeyManager)
159+
public function __construct(EnvKeyManager $envManager)
144160
{
145161
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');
147172
}
148173

149174
protected function execute(InputInterface $input, OutputInterface $output)
150175
{
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;
159199
}
200+
201+
return Command::SUCCESS;
160202
}
161203
}
162204
```
163205

164206
</details>
165207

208+
---
209+
166210
#### CodeIgniter
167211

212+
To use `EnvKeyManager` in CodeIgniter, initialize it with the `.env` path and use it within controllers or custom classes.
213+
168214
<details>
169-
<summary>Usage in CodeIgniter</summary>
215+
<summary>CodeIgniter Usage</summary>
170216

171-
1. **Set Up**: Define `.env` path and create `EnvKeyManager` instance.
217+
1. **Initialize `EnvKeyManager`** with the CodeIgniter `.env` path.
172218

173219
```php
174220
use CleaniqueCoders\PhpEnvKeyManager\EnvKeyManager;
@@ -177,9 +223,9 @@ composer require cleaniquecoders/php-env-key-manager
177223
$envManager = new EnvKeyManager($envFilePath);
178224
```
179225

180-
2. **Usage in CodeIgniter Controller**
226+
2. **Use in a CodeIgniter Controller**
181227

182-
In a controller or any other class:
228+
Create a CodeIgniter controller method to manage environment keys:
183229

184230
```php
185231
<?php
@@ -190,24 +236,41 @@ composer require cleaniquecoders/php-env-key-manager
190236

191237
class EnvController extends BaseController
192238
{
193-
public function updateEnv()
239+
protected $envManager;
240+
241+
public function __construct()
194242
{
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'.";
204263
}
205264
}
206265
}
207266
```
208267

209268
</details>
210269

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+
211274
## Testing
212275

213276
```bash

0 commit comments

Comments
 (0)