PHP code example of cleaniquecoders / php-env-key-manager

1. Go to this page and download the library: Download cleaniquecoders/php-env-key-manager library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cleaniquecoders / php-env-key-manager example snippets


use CleaniqueCoders\PhpEnvKeyManager\EnvKeyManager;

// Path to your .env file
$envFilePath = __DIR__ . '/.env';
$envManager = new EnvKeyManager($envFilePath);

// Set a key
$envManager->setKey('APP_DEBUG', 'true');

// Disable a key
$envManager->disableKey('APP_DEBUG');

// Enable a key
$envManager->enableKey('APP_DEBUG');

   use CleaniqueCoders\PhpEnvKeyManager\EnvKeyManager;

   public function register()
   {
       $this->app->singleton(EnvKeyManager::class, function ($app) {
           return new EnvKeyManager($app->environmentFilePath());
       });
   }
   

   

   namespace App\Console\Commands;

   use CleaniqueCoders\PhpEnvKeyManager\EnvKeyManager;
   use Illuminate\Console\Command;

   class ManageEnvKeyCommand extends Command
   {
       protected $signature = 'env:manage-key {action} {key} {value?}';
       protected $description = 'Manage an environment key';

       protected $envManager;

       public function __construct(EnvKeyManager $envManager)
       {
           parent::__construct();
           $this->envManager = $envManager;
       }

       public function handle()
       {
           $action = $this->argument('action');
           $key = $this->argument('key');
           $value = $this->argument('value');

           switch ($action) {
               case 'set':
                   $this->envManager->setKey($key, $value);
                   $this->info("Key {$key} set to {$value}.");
                   break;

               case 'disable':
                   $this->envManager->disableKey($key);
                   $this->info("Key {$key} has been disabled.");
                   break;

               case 'enable':
                   $this->envManager->enableKey($key);
                   $this->info("Key {$key} has been enabled.");
                   break;

               default:
                   $this->error("Invalid action. Use 'set', 'disable', or 'enable'.");
           }
       }
   }
   

   use CleaniqueCoders\PhpEnvKeyManager\EnvKeyManager;

   $envFilePath = __DIR__ . '/../../.env'; // Adjust the path to your Symfony .env file
   $envManager = new EnvKeyManager($envFilePath);
   

   

   namespace App\Command;

   use CleaniqueCoders\PhpEnvKeyManager\EnvKeyManager;
   use Symfony\Component\Console\Command\Command;
   use Symfony\Component\Console\Input\InputArgument;
   use Symfony\Component\Console\Input\InputInterface;
   use Symfony\Component\Console\Output\OutputInterface;

   class ManageEnvKeyCommand extends Command
   {
       protected static $defaultName = 'env:manage-key';

       private $envManager;

       public function __construct(EnvKeyManager $envManager)
       {
           parent::__construct();
           $this->envManager = $envManager;
       }

       protected function configure()
       {
           $this
               ->setDescription('Manage an environment key')
               ->addArgument('action', InputArgument::REQUIRED, 'Action: set, disable, enable')
               ->addArgument('key', InputArgument::REQUIRED, 'The environment key')
               ->addArgument('value', InputArgument::OPTIONAL, 'The value for set action');
       }

       protected function execute(InputInterface $input, OutputInterface $output)
       {
           $action = $input->getArgument('action');
           $key = $input->getArgument('key');
           $value = $input->getArgument('value');

           switch ($action) {
               case 'set':
                   $this->envManager->setKey($key, $value);
                   $output->writeln("Key {$key} set to {$value}.");
                   break;

               case 'disable':
                   $this->envManager->disableKey($key);
                   $output->writeln("Key {$key} has been disabled.");
                   break;

               case 'enable':
                   $this->envManager->enableKey($key);
                   $output->writeln("Key {$key} has been enabled.");
                   break;

               default:
                   $output->writeln("Invalid action. Use 'set', 'disable', or 'enable'.");
                   return Command::FAILURE;
           }

           return Command::SUCCESS;
       }
   }
   

   use CleaniqueCoders\PhpEnvKeyManager\EnvKeyManager;

   $envFilePath = ROOTPATH . '.env'; // CodeIgniter base path to .env
   $envManager = new EnvKeyManager($envFilePath);
   

   

   namespace App\Controllers;

   use CleaniqueCoders\PhpEnvKeyManager\EnvKeyManager;

   class EnvController extends BaseController
   {
       protected $envManager;

       public function __construct()
       {
           $this->envManager = new EnvKeyManager(ROOTPATH . '.env');
       }

       public function manageKey($action, $key, $value = null)
       {
           switch ($action) {
               case 'set':
                   $this->envManager->setKey($key, $value);
                   return "Key {$key} set to {$value}.";

               case 'disable':
                   $this->envManager->disableKey($key);
                   return "Key {$key} has been disabled.";

               case 'enable':
                   $this->envManager->enableKey($key);
                   return "Key {$key} has been enabled.";

               default:
                   return "Invalid action. Use 'set', 'disable', or 'enable'.";
           }
       }
   }