Skip to content

Commit

Permalink
a bit linting
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoSot committed Jan 16, 2020
1 parent dbf50b6 commit 1ac2a14
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 102 deletions.
73 changes: 40 additions & 33 deletions src/Controllers/EnvController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,50 @@

namespace GeoSot\EnvEditor\Controllers;

use App\Http\Controllers\Controller;
use GeoSot\EnvEditor\Facades\EnvEditor;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\View\View;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class EnvController extends Controller
class EnvController extends BaseController
{
protected $package = 'env-editor';


/**
* Display main view with the Collection of current .env values
*
* @return \Illuminate\Http\Response
* @return Response|View
*/
public function index()
{
$envValues = EnvEditor::getEnvFileContent();
if (request()->wantsJson()) {
return $this->returnGenericResponse(true, ['items' => $envValues]);
}
return view($this->package . '::index', compact('envValues'));
return view($this->package.'::index', compact('envValues'));
}

/**
* Add a new key on current .env file
* @param Request $request
* @param Request $request
*
* @return \Illuminate\Http\Response
* @return Response
*/
public function addKey(Request $request)
{
$result = EnvEditor::addKey($request->input('key'), $request->input('value'), $request->except(['key', 'value']));
$result = EnvEditor::addKey($request->input('key'), $request->input('value'),
$request->except(['key', 'value']));
return $this->returnGenericResponse($result, [], 'keyWasAdded', $request->input('key'));
}

/**
* Edit a key of current .env file
* @param Request $request
* @param Request $request
*
* @return \Illuminate\Http\Response
* @return Response
*/
public function editKey(Request $request)
{
Expand All @@ -51,9 +55,9 @@ public function editKey(Request $request)

/**
* Delete a key from current .env file
* @param Request $request
* @param Request $request
*
* @return \Illuminate\Http\Response
* @return Response
*/
public function deleteKey(Request $request)
{
Expand All @@ -65,21 +69,21 @@ public function deleteKey(Request $request)
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
* @return View|Response
*/
public function getBackupFiles()
{
$backUpFiles = EnvEditor::getAllBackUps();
if (request()->wantsJson()) {
return $this->returnGenericResponse(true, ['items' => $backUpFiles]);
}
return view($this->package . '::index', compact('backUpFiles'));
return view($this->package.'::index', compact('backUpFiles'));
}

/**
* Create BackUp of .env File
*
* @return \Illuminate\Http\Response
* @return Response
*/
public function createBackup()
{
Expand All @@ -91,8 +95,8 @@ public function createBackup()
/**
* Restore Backup file
*
* @param string $filename
* @return \Illuminate\Http\Response
* @param string $filename
* @return Response
*/
public function restoreBackup(string $filename)
{
Expand All @@ -104,8 +108,8 @@ public function restoreBackup(string $filename)
/**
* Delete Backup file
*
* @param string $filename
* @return \Illuminate\Http\Response
* @param string $filename
* @return Response
*/
public function destroyBackup(string $filename)
{
Expand All @@ -117,8 +121,8 @@ public function destroyBackup(string $filename)
/**
* Get Files As Download
*
* @param string $filename
* @return \Illuminate\Http\Response
* @param string $filename
* @return BinaryFileResponse
*/
public function download(string $filename = '')
{
Expand All @@ -130,8 +134,8 @@ public function download(string $filename = '')
/**
* Upload File As BackUp or replace Current .Env
*
* @param Request $request
* @return \Illuminate\Http\Response
* @param Request $request
* @return Response
*/
public function upload(Request $request)
{
Expand All @@ -148,24 +152,27 @@ public function upload(Request $request)
/**
* Generic ajax response
*
* @param bool $success
* @param bool $success
* @param array $data
* @param string $translationWord
* @param string $keyName
* @param string $translationWord
* @param string $keyName
*
* @return \Illuminate\Http\Response
* @return Response
*/
protected function returnGenericResponse(bool $success, array $data = [], string $translationWord = '', string $keyName = '')
{
protected function returnGenericResponse(
bool $success,
array $data = [],
string $translationWord = '',
string $keyName = ''
) {
if (!empty($translationWord) and $success) {
$data = array_merge($data, [
'message' => __($this->package . "::env-editor.controllerMessages.$translationWord", ['name' => $keyName])
'message' => __($this->package."::env-editor.controllerMessages.$translationWord", ['name' => $keyName])
]);
}
return response()
->json(array_merge($data, [
'success' => $success,
]));
return response()->json(array_merge($data, [
'success' => $success,
]));
}

}
2 changes: 1 addition & 1 deletion src/Exceptions/EnvException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function __construct($message, $code, Exception $previous = null)

public function __toString()
{
return __CLASS__ . ":[{$this->code}]: {$this->message}\n";
return __CLASS__.":[{$this->code}]: {$this->message}\n";
}

}
38 changes: 29 additions & 9 deletions src/Facades/EnvEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,38 @@

namespace GeoSot\EnvEditor\Facades;

use Illuminate\Http\UploadedFile;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Facade;
use Symfony\Component\HttpFoundation\File\File;

/**
* @method static Collection getEnvFileContent(string $fileName = '')
* @method static bool keyExists(string $key)
* @method static mixed getKey(string $key, $default = null)
* @method static bool addKey(string $key, $value, array $options = [])
* @method static bool editKey(string $keyToChange, $newValue)
* @method static bool deleteKey(string $key)
* @method static Collection getAllBackUps()
* @method static File upload(UploadedFile $uploadedFile, bool $replaceCurrentEnv)
* @method static bool backUpCurrent()
* @method static string getFilePath(string $fileName = '')
* @method static bool deleteBackup(string $fileName)
* @method static bool restoreBackUp(string $fileName)
* @method static mixed config(string $key, $default = null)
*
* @see \GeoSot\EnvEditor\EnvEditor
*/
class EnvEditor extends Facade
{

/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return \GeoSot\EnvEditor\EnvEditor::class;
}
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return \GeoSot\EnvEditor\EnvEditor::class;
}
}
25 changes: 12 additions & 13 deletions src/Helpers/EnvFileContentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class EnvFileContentManager
/**
* Constructor
*
* @param EnvEditor $envEditor
* @param EnvEditor $envEditor
*/
public function __construct(EnvEditor $envEditor)
{
Expand All @@ -31,7 +31,7 @@ public function __construct(EnvEditor $envEditor)
/**
* Parse the .env Contents
*
* @param string $fileName
* @param string $fileName
*
* @return Collection
* @throws EnvException
Expand All @@ -51,10 +51,10 @@ public function getParsedFileContent(string $fileName = '')
}
$entry = explode("=", $line, 2);
$groupArray = [
'key' => Arr::get($entry, 0),
'value' => Arr::get($entry, 1),
'group' => $groupIndex,
'index' => $index,
'key' => Arr::get($entry, 0),
'value' => Arr::get($entry, 1),
'group' => $groupIndex,
'index' => $index,
'separator' => false
];
$collection->push($groupArray);
Expand All @@ -71,7 +71,7 @@ public function getParsedFileContent(string $fileName = '')
/**
* Get The File Contents
*
* @param string $file
* @param string $file
*
* @return mixed
* @throws EnvException
Expand All @@ -81,21 +81,21 @@ protected function getFileContents(string $file = '')
$envFile = $this->envEditor->getFilesManager()->getFilePath($file);

if (!$this->filesystem->exists($envFile)) {
throw new EnvException(__($this->package . '::exceptions.fileNotExists', ['name' => $envFile]), 0);
throw new EnvException(__($this->package.'::exceptions.fileNotExists', ['name' => $envFile]), 0);
}
try {
return $this->filesystem->get($envFile);
} catch (\Exception $e) {
throw new EnvException(__($this->package . '::exceptions.fileNotExists', ['name' => $envFile]), 2);
throw new EnvException(__($this->package.'::exceptions.fileNotExists', ['name' => $envFile]), 2);
}

}

/**
* Save the new collection on .env file
*
* @param Collection $envValues
* @param string $fileName
* @param Collection $envValues
* @param string $fileName
*
* @return bool
* @throws EnvException
Expand All @@ -105,9 +105,8 @@ public function save(Collection $envValues, string $fileName = '')
$env = $envValues->sortBy(['index'])->map(function ($item) {
if ($item['key'] == '') {
return '';
} else {
return $item['key'] . '=' . $item['value'];
}
return $item['key'].'='.$item['value'];
});

$content = implode("\n", $env->toArray());
Expand Down
Loading

0 comments on commit 1ac2a14

Please sign in to comment.