Skip to content

Commit

Permalink
Supported dependency injection
Browse files Browse the repository at this point in the history
  • Loading branch information
JackieDo committed Jun 26, 2017
1 parent 4a5a93a commit 3fb83cd
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 60 deletions.
61 changes: 52 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Laravel-Dotenv-Editor
# Laravel Dotenv Editor
![laravel-dotenv-editor](https://cloud.githubusercontent.com/assets/9862115/25982836/029612b2-370a-11e7-82c5-d9146dc914a1.png)

[![Latest Stable Version](https://poser.pugx.org/jackiedo/dotenv-editor/v/stable)](https://packagist.org/packages/jackiedo/dotenv-editor)
Expand Down Expand Up @@ -28,6 +28,8 @@ Look at one of the following topics to learn more about Laravel Dotenv Editor
- [Auto backup mode](#auto-backup-mode)
- [Backup location](#backup-location)
* [Usage](#usage)
- [Working with facade](#working-with-facade)
- [Using dependency injection](#using-dependency-injection)
- [Loading file for working](#loading-file-for-working)
- [Reading file content](#reading-file-content)
- [Writing content into file](#writing-content-into-file)
Expand All @@ -36,13 +38,12 @@ Look at one of the following topics to learn more about Laravel Dotenv Editor
- [Working with Artisan CLI](#working-with-artisan-cli)
- [Exceptions](#exceptions)
* [License](#license)
* [Thanks from author](#thanks-for-use)

## Versions and compatibility

Currently, Laravel Dotenv Editor only have version 1.x that is compatible with Laravel 5.1 and later. This package is not support for Laravel 5.0 and earlier versions.

## Installation

You can install this package through [Composer](https://getcomposer.org).

- First, edit your project's `composer.json` file to require `jackiedo/dotenv-editor`:
Expand All @@ -55,12 +56,14 @@ You can install this package through [Composer](https://getcomposer.org).
},
```

- Next, update Composer from the Terminal on your project source:
- Next, run the composer update command in your command line interface:

```shell
$ composer update
```

> **Note:** Instead of performing the above two steps, you can perform faster with the command line `$ composer require jackiedo/dotenv-editor:1.*`.
- Once update operation completes, the third step is add the service provider. Open `config/app.php`, and add a new item to the providers array:

```php
Expand All @@ -78,7 +81,6 @@ $ composer update
```

## Configuration

To get started, you'll need to publish configuration file:

```shell
Expand All @@ -88,17 +90,55 @@ $ php artisan vendor:publish --provider="Jackiedo\DotenvEditor\DotenvEditorServi
This will create a `config/dotenv-editor.php` file in your app that you can modify to set your configuration. Also, make sure you check for changes to the original config file in this package between releases.

#### Auto backup mode

The option `autoBackup` is determine that your orignal file will be backed up before save or not.

#### Backup location

The option `backupPath` is where that your file is backed up to. This value is the sub path (sub-folder) from root folder of project application.

## Usage

#### Loading file for working
#### Working with facade
Laravel Dotenv Editor has a facade with name is `Jackiedo\DotenvEditor\Facades\DotenvEditor`. You can do any operation through this facade. For example:

<?php namespace Your\Namespace;

...

use Jackiedo\DotenvEditor\Facades\DotenvEditor;

class YourClass
{
public function yourMethod()
{
DotenvEditor::doSomething();
}
}

#### Using dependency injection
This package also supports dependency injection, you can easily use dependency injection to inject an instance of the `Jackiedo\DotenvEditor\DotenvEditor` class into your controller or other class. Example:

<?php namespace App\Http\Controllers;

...

use Jackiedo\DotenvEditor\DotenvEditor;

class TestDotenvEditorController extends Controller {

protected $editor;

public function __construct(DotenvEditor $editor)
{
$this->editor = $editor;
}

public function doSomething()
{
$editor = $this->editor->doSomething();
}
}

#### Loading file for working
Default, Laravel Dotenv Editor will load file `.env` in root folder of your project whenever you use the `DotenvEditor` facade. Example:

$content = DotenvEditor::getContent(); // Get raw content of file .env in root folder
Expand Down Expand Up @@ -281,4 +321,7 @@ $ php artisan dotenv:get-backups --help
#### Exceptions

## License
[MIT](LICENSE) © Jackie Do
[MIT](LICENSE) © Jackie Do

## Thanks for use
Hopefully, this package is useful to you.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
],
"require": {
"php": ">=5.5.9",
"illuminate/config": ">=5.1",
"illuminate/container": ">=5.1",
"illuminate/support": ">=5.1"
},
Expand Down
76 changes: 52 additions & 24 deletions src/Jackiedo/DotenvEditor/DotenvEditor.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php namespace Jackiedo\DotenvEditor;

use Illuminate\Container\Container;
use Jackiedo\DotenvEditor\Contracts\DotenvFormatter as DotenvFormatterContract;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Config\Repository as Config;
use Jackiedo\DotenvEditor\Exceptions\FileNotFoundException;
use Jackiedo\DotenvEditor\Exceptions\KeyNotFoundException;
use Jackiedo\DotenvEditor\Exceptions\NoBackupAvailableException;
Expand All @@ -21,6 +21,13 @@ class DotenvEditor
*/
protected $app;

/**
* Store instance of Config Repository;
*
* @var \Illuminate\Config\Repository
*/
protected $config;

/**
* The formatter instance
*
Expand Down Expand Up @@ -76,42 +83,63 @@ class DotenvEditor
/**
* Create a new DotenvEditor instance
*
* @param \Illuminate\Container\Container $app
* @param \Jackiedo\DotenvEditor\Contracts\DotenvFormatter $formatter
* @param \Illuminate\Contracts\Container\Container $app
* @param \Illuminate\Contracts\Config\Repository $config
*
* @return void
*/
public function __construct(Container $app, DotenvFormatterContract $formatter)
public function __construct(Container $app, Config $config)
{
$this->app = $app;
$this->formatter = $formatter;
$this->config = $config;
$this->formatter = new DotenvFormatter;
$this->reader = new DotenvReader($this->formatter);
$this->writer = new DotenvWriter($this->formatter);

$backupPath = $this->app['config']->get('dotenv-editor.backupPath', base_path('storage/dotenv-editor/backups/'));
$backupPath = $this->config->get('dotenv-editor.backupPath');

if (is_null($backupPath)) {
if (function_exists('base_path')) {
$backupPath = base_path('storage/dotenv-editor/backups/');
} else {
$backupPath = __DIR__.'/../../../../../../storage/dotenv-editor/backups/';
}
}

if (!is_dir($backupPath)) {
mkdir($backupPath, 0777, true);
copy(__DIR__ . '/../../stubs/gitignore.txt', $backupPath . '../.gitignore');
}

$this->backupPath = $backupPath;
$this->autoBackup = $this->app['config']->get('dotenv-editor.autoBackup', true);
$this->autoBackup = $this->config->get('dotenv-editor.autoBackup', true);

$this->load();
}

/**
* Load file for working
*
* @param string|null $filePath The file path
* @param boolean $restoreIfNotFound Restore this file from other file if it's not found
* @param string|null $restorePath The file path you want to restore from
* @param string|null $filePath The file path
* @param boolean $restoreIfNotFound Restore this file from other file if it's not found
* @param string|null $restorePath The file path you want to restore from
*
* @return DotenvEditor
*/
public function load($filePath = null, $restoreIfNotFound = false, $restorePath = null)
{
$this->resetContent();

$this->filePath = is_null($filePath) ? $this->app->environmentPath().'/'.$this->app->environmentFile() : $filePath;
if (! is_null($filePath)) {
$this->filePath = $filePath;
} else {
if (method_exists($this->app, 'environmentPath') && method_exists($this->app, 'environmentFile')) {
$this->filePath = $this->app->environmentPath().'/'.$this->app->environmentFile();
} else {
$this->filePath = __DIR__.'/../../../../../../.env';
}
}

$this->reader->load($this->filePath);

if (file_exists($this->filePath)) {
Expand All @@ -127,7 +155,7 @@ public function load($filePath = null, $restoreIfNotFound = false, $restorePath
/**
* Reset content for editor
*
* @return void;
* @return void
*/
protected function resetContent()
{
Expand Down Expand Up @@ -207,7 +235,7 @@ public function keyExists($key)
/**
* Return the value matching to a given key in the file content
*
* @param $key
* @param $key
*
* @throws \Jackiedo\DotenvEditor\Exceptions\KeyNotFoundException
*
Expand Down Expand Up @@ -273,7 +301,7 @@ public function addComment($comment)
/**
* Set many keys to buffer
*
* @param array $data
* @param array $data
*
* @return DotenvEditor
*/
Expand Down Expand Up @@ -302,10 +330,10 @@ public function setKeys($data)
/**
* Set one key to buffer
*
* @param string $key Key name of setter
* @param string|null $value Value of setter
* @param string|null $comment Comment of setter
* @param boolean $export Leading key name by "export "
* @param string $key Key name of setter
* @param string|null $value Value of setter
* @param string|null $comment Comment of setter
* @param boolean $export Leading key name by "export "
*
* @return DotenvEditor
*/
Expand Down Expand Up @@ -335,7 +363,7 @@ public function deleteKeys($keys = [])
/**
* Delete on key in buffer
*
* @param string $key
* @param string $key
*
* @return DotenvEditor
*/
Expand Down Expand Up @@ -379,7 +407,7 @@ public function save()
/**
* Switching of the auto backup mode
*
* @param boolean $on
* @param boolean $on
*
* @return DotenvEditor
*/
Expand Down Expand Up @@ -475,7 +503,7 @@ public function getLatestBackup()
/**
* Restore the loaded file from latest backup file or from special file.
*
* @param string|null $filePath
* @param string|null $filePath
*
* @return DotenvEditor
*/
Expand All @@ -502,7 +530,7 @@ public function restore($filePath = null)
/**
* Delete all or the given backup files
*
* @param array $filePaths
* @param array $filePaths
*
* @return DotenvEditor
*/
Expand All @@ -526,7 +554,7 @@ public function deleteBackups($filePaths = [])
/**
* Delete the given backup file
*
* @param string $filePath
* @param string $filePath
*
* @return DotenvEditor
*/
Expand Down
34 changes: 7 additions & 27 deletions src/Jackiedo/DotenvEditor/DotenvEditorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ public function boot()
*/
public function register()
{
$this->app->singleton('dotenv-editor', function ($app) {
$formatter = new DotenvFormatter;
return new DotenvEditor($app, $formatter);
});
$this->app->bind('dotenv-editor', DotenvEditor::class);

$this->registerCommands();
}
Expand All @@ -66,29 +63,12 @@ public function register()
*/
protected function registerCommands()
{
$this->app->singleton('command.dotenv.backup', function ($app) {
return new DotenvBackupCommand($app['dotenv-editor']);
});

$this->app->singleton('command.dotenv.deletekey', function ($app) {
return new DotenvDeleteKeyCommand($app['dotenv-editor']);
});

$this->app->singleton('command.dotenv.getbackups', function ($app) {
return new DotenvGetBackupsCommand($app['dotenv-editor']);
});

$this->app->singleton('command.dotenv.getkeys', function ($app) {
return new DotenvGetKeysCommand($app['dotenv-editor']);
});

$this->app->singleton('command.dotenv.restore', function ($app) {
return new DotenvRestoreCommand($app['dotenv-editor']);
});

$this->app->singleton('command.dotenv.setkey', function ($app) {
return new DotenvSetKeyCommand($app['dotenv-editor']);
});
$this->app->bind('command.dotenv.backup', DotenvBackupCommand::class);
$this->app->bind('command.dotenv.deletekey', DotenvDeleteKeyCommand::class);
$this->app->bind('command.dotenv.getbackups', DotenvGetBackupsCommand::class);
$this->app->bind('command.dotenv.getkeys', DotenvGetKeysCommand::class);
$this->app->bind('command.dotenv.restore', DotenvRestoreCommand::class);
$this->app->bind('command.dotenv.setkey', DotenvSetKeyCommand::class);

$this->commands('command.dotenv.backup');
$this->commands('command.dotenv.deletekey');
Expand Down

0 comments on commit 3fb83cd

Please sign in to comment.