定制 cleaniquecoders/php-env-key-manager 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

cleaniquecoders/php-env-key-manager

最新稳定版本:1.1.0

Composer 安装命令:

composer require cleaniquecoders/php-env-key-manager

包简介

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

README 文档

README

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.

统计信息

  • 总下载量: 855
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-11-11

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固