Skip to content

A framework-agnostic PHP package for easy .env file key management. Seamlessly update, add, or modify environment variables across projects with minimal configuration.

License

Notifications You must be signed in to change notification settings

cleaniquecoders/php-env-key-manager

Repository files navigation

PHP Env Key Manager

Latest Version on Packagist Tests Total Downloads

A framework-agnostic PHP package for easy .env file key management. Seamlessly update, add, or modify environment variables across projects with minimal configuration.

Installation

You can install the package via composer:

composer require cleaniquecoders/php-env-key-manager

Usage

Basic Usage

Following are the basic usage examples for the package:

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

Framework-Specific Examples

Laravel

To use EnvKeyManager in a Laravel application, register it as a singleton in the AppServiceProvider to allow easy access across your application.

Laravel Usage
  1. Register as a Singleton

    In App\Providers\AppServiceProvider:

    use CleaniqueCoders\PhpEnvKeyManager\EnvKeyManager;
    
    public function register()
    {
        $this->app->singleton(EnvKeyManager::class, function ($app) {
            return new EnvKeyManager($app->environmentFilePath());
        });
    }
  2. Usage in a Command

    Create a Laravel Artisan command to set, disable, or enable environment keys:

    <?php
    
    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'.");
            }
        }
    }

Symfony

To use EnvKeyManager in Symfony, initialize it with the .env path, and use it in Symfony commands or services.

Symfony Usage
  1. Initialize EnvKeyManager with Symfony’s .env path.

    use CleaniqueCoders\PhpEnvKeyManager\EnvKeyManager;
    
    $envFilePath = __DIR__ . '/../../.env'; // Adjust the path to your Symfony .env file
    $envManager = new EnvKeyManager($envFilePath);
  2. Use in a Symfony Command

    Create a Symfony console command to manage environment keys:

    <?php
    
    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;
        }
    }

CodeIgniter

To use EnvKeyManager in CodeIgniter, initialize it with the .env path and use it within controllers or custom classes.

CodeIgniter Usage
  1. Initialize EnvKeyManager with the CodeIgniter .env path.

    use CleaniqueCoders\PhpEnvKeyManager\EnvKeyManager;
    
    $envFilePath = ROOTPATH . '.env'; // CodeIgniter base path to .env
    $envManager = new EnvKeyManager($envFilePath);
  2. Use in a CodeIgniter Controller

    Create a CodeIgniter controller method to manage environment keys:

    <?php
    
    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'.";
            }
        }
    }

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.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

About

A framework-agnostic PHP package for easy .env file key management. Seamlessly update, add, or modify environment variables across projects with minimal configuration.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Languages