Skip to content

Commit

Permalink
Merge pull request #2 from neelkanthk/v1.0.0
Browse files Browse the repository at this point in the history
V1.0.0
  • Loading branch information
neelkanthk committed Oct 4, 2020
2 parents d241d5b + 62f9e65 commit b44d805
Show file tree
Hide file tree
Showing 12 changed files with 595 additions and 2 deletions.
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Neelkanth Kaushik <me.neelkanth@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
88 changes: 88 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# EasyEnv

A Laravel package to easily manage and switch between multiple environment files using artisan cli.

## Installation
```bash
composer require neelkanthk/laravel-easyenv
```
## Publish the config file.

```bash
php artisan vendor:publish --provider="Neelkanth\Laravel\EasyEnv\Providers\EasyEnvServiceProvider" --tag="config"
```

## Usage

Add the following lines just before the ```php return $app; ``` in the ``` bootstrap/app.php```

```php
use Neelkanth\Laravel\EasyEnv\EasyEnv;
$app->useEnvironmentPath(EasyEnv::path())->loadEnvironmentFrom(EasyEnv::file());
```

## Managing the Envrionments through CLI

The package provides following ```artisan``` commands to add/remove and enable/disable environments.

The commands have following signature.
```bash
php artisan easyenv:[action] {env} {path} {file}
```

```bash
[action] : Following action are available list|add|remove|enable|disable
````

```bash
{env}: The name of your choice for the environment.
{path}: The absolute path of the location of the environment file
{file}: The name of the environment file residing on the filesystem.
```

#### 1. Add a new environment.

```bash
php artisan easyenv:add staging /var/www/env/ .staging
```
#### 2. Enable an environment.
_Only 1 environment can be enabled at a time._
```bash
php artisan easyenv:enable staging
```

#### 3. Listing all available environments.

```bash
php artisan easyenv:list
```
| Environment | Status | Path | File |
|-------------|---------|---------------|----------|
| staging | Enabled | /var/www/env/ | .staging |


#### 4. Disable currently enabled environment.

```bash
php artisan easyenv:disable
```

#### 5. Remove an environment

```bash
php artisan easyenv:remove staging
```

## Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

## Security
If you discover any security-related issues, please email me.neelkanth@gmail.com instead of using the issue tracker.

## Credits

- [Neelkanth Kaushik](https://github.com/username)
- [All Contributors](../../contributors)

## License
[MIT](https://choosealicense.com/licenses/mit/)
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "neelkanthk/laravel-easyenv",
"description": "Easily manage and switch between multiple environment files using commands.",
"keywords": ["laravel", "env", "environment"],
"description": "Easily manage and switch between multiple environment files using cli.",
"keywords": ["laravel", "env", "environment-configuration"],
"type": "package",
"license": "MIT",
"authors": [
Expand Down
9 changes: 9 additions & 0 deletions config/easyenv.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

return array(
'environments' =>
array(

),
'enabled' => '',
);
62 changes: 62 additions & 0 deletions src/Console/Commands/AddEnv.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Neelkanth\Laravel\EasyEnv\Console\Commands;

use Neelkanth\Laravel\EasyEnv\Console\Commands\EasyEnvCommand;

class AddEnv extends EasyEnvCommand
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'easyenv:add {env} {path} {file}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Add a new environment';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->setEnv();
$this->setPath();
$this->setFile();

$existingConfig = config("easyenv");
if (is_null($existingConfig)) {
$this->showConfigFileMissingError();
} else if (!empty($this->getEnv()) && !empty($this->getPath())) {
$env = $this->getEnv();
$path = $this->getPath();
$file = $this->getFile();
if ((!in_array($env, array_keys($existingConfig["environments"])) && empty($existingConfig["environments"][$env]["path"]) && empty($existingConfig["environments"][$env]["file"]))) {
$existingConfig["environments"][$env]["path"] = $path;
$existingConfig["environments"][$env]["file"] = $file;
$newConfig = var_export($existingConfig, true);
$this->updateConfigFile($newConfig);
$this->info("Done");
} else {
$this->info("\nThe environment: $env already exists having path: " . $existingConfig['environments'][$env]["path"]);
}
}
}
}
54 changes: 54 additions & 0 deletions src/Console/Commands/DisableEnv.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Neelkanth\Laravel\EasyEnv\Console\Commands;

use Neelkanth\Laravel\EasyEnv\Console\Commands\EasyEnvCommand;

class DisableEnv extends EasyEnvCommand
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'easyenv:disable';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Disable currently enabled environment.';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
if (is_null(config("easyenv"))) {
$this->showConfigFileMissingError();
} else {
$this->warn("\nAttention: This will cause the application to look for .env file at: " . base_path('.env'));
$userInput = $this->confirm("If you want to proceed type 'y' or 'yes' and press Enter.");
if ($userInput) {
$existingConfig = config("easyenv");
$existingConfig["enabled"] = "";
$newConfig = var_export($existingConfig, true);
$this->updateConfigFile($newConfig);
$this->info("Done");
}
}
}
}
77 changes: 77 additions & 0 deletions src/Console/Commands/EasyEnvCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Neelkanth\Laravel\EasyEnv\Console\Commands;

use Illuminate\Console\Command;

class EasyEnvCommand extends Command
{
private $env;
private $path;
private $file;
private $config;

public function __construct()
{
parent::__construct();
}

public function getEnv()
{
return $this->env;
}

public function getPath()
{
return $this->path;
}

public function getFile()
{
return $this->file;
}

public function getConfig()
{
return $this->config;
}

public function setEnv()
{
$this->env = $this->argument("env");
}

public function setPath()
{
$this->path = $this->argument("path");
}

public function setFile()
{
$this->file = $this->argument("file");
}

/**
* Error message to show when config file is not found.
*
* @return void
*/
public function showConfigFileMissingError()
{
$this->error("\nCould not load the config/easyenv.php file.");
$this->line("\nPlease make sure that you have published the package's config file using the command mentioned below.");
$this->info('php artisan vendor:publish --provider="Neelkanth\Laravel\EasyEnv\Providers\EasyEnvServiceProvider" --tag="config" --force');
$this->line("\nStill facing the issue? Kindly report it at https://github.com/neelkanthk/laravel-easyenv/issues.");
}

/**
* Write updated configuration.
*
* @return void
*/
public function updateConfigFile($newConfig)
{
$newConfigFileContent = '<?php return ' . $newConfig . ';';
file_put_contents(config_path('easyenv.php'), $newConfigFileContent);
}
}
58 changes: 58 additions & 0 deletions src/Console/Commands/EnableEnv.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Neelkanth\Laravel\EasyEnv\Console\Commands;

use Neelkanth\Laravel\EasyEnv\Console\Commands\EasyEnvCommand;

class EnableEnv extends EasyEnvCommand
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'easyenv:enable {env}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Enabled an environment.';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->setEnv();
if (is_null(config("easyenv"))) {
$this->showConfigFileMissingError();
} else {
$env = $this->getEnv();
$existingConfig = config("easyenv");
$environments = $existingConfig["environments"];
if ($env == "default" || (in_array($env, array_keys($environments)) && !empty($environments[$env]))) {
$existingConfig["enabled"] = $env;
$newConfig = var_export($existingConfig, true);
$this->updateConfigFile($newConfig);
$this->info("Done");
} else {
$this->warn("\nThe provided environment is not added in the list.");
$this->info("\nLet's add one using 'php artisan easyenv:add {env} {path}'");
}
}
}
}
Loading

0 comments on commit b44d805

Please sign in to comment.