├── .gitignore ├── LICENSE ├── README.md ├── composer.json ├── routes └── web.php └── src ├── Env.php ├── EnvManager.php ├── EnvManagerServiceProvider.php └── Http └── Controllers └── EnvManagerController.php /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | phpunit.phar 3 | /vendor 4 | composer.phar 5 | composer.lock 6 | *.project 7 | .idea/ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2018 Jxlwqq(金小龙) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Env Manager extension for laravel-admin 2 | 3 | 4 | ## Screenshot 5 | 6 | ![screenshot](https://user-images.githubusercontent.com/2421068/47560963-7ea0a800-d94c-11e8-95d9-57c6fee1d9bb.png) 7 | 8 | ## Requirements 9 | 10 | * laravel-admin >= 1.6 11 | 12 | ## Installation 13 | 14 | ```bash 15 | composer require jxlwqq/env-manager 16 | 17 | # If you want to add a link entry in the left menu, use the following command to import 18 | php artisan admin:import env-manager 19 | ``` 20 | 21 | ## Configurations 22 | 23 | Add `extensions` option in your `config/admin.php` configuration file: 24 | 25 | ```php 26 | 'extensions' => [ 27 | 'env-manager' => [ 28 | // If the value is set to false, this extension will be disabled 29 | 'enable' => true 30 | ] 31 | ] 32 | ``` 33 | 34 | ## Usage 35 | 36 | Open http://your-host/admin/env-manager 37 | 38 | And you can find the `.env` variables. 39 | 40 | 41 | ## More resources 42 | 43 | [Awesome Laravel-admin](https://github.com/jxlwqq/awesome-laravel-admin) 44 | 45 | 46 | ## License 47 | 48 | Licensed under [The MIT License (MIT)](LICENSE). 49 | 50 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jxlwqq/env-manager", 3 | "description": "Env Manager for Laravel-admin", 4 | "type": "library", 5 | "keywords": ["laravel-admin", "extension"], 6 | "homepage": "https://github.com/jxlwqq/env-manager", 7 | "license": "MIT", 8 | "authors": [ 9 | { 10 | "name": "jxlwqq", 11 | "email": "jxlwqq@gmail.com" 12 | } 13 | ], 14 | "require": { 15 | "php": ">=7.0.0", 16 | "encore/laravel-admin": "~1.6" 17 | }, 18 | "require-dev": { 19 | "phpunit/phpunit": "~6.0" 20 | }, 21 | "autoload": { 22 | "psr-4": { 23 | "Jxlwqq\\EnvManager\\": "src/" 24 | } 25 | }, 26 | "extra": { 27 | "laravel": { 28 | "providers": [ 29 | "Jxlwqq\\EnvManager\\EnvManagerServiceProvider" 30 | ] 31 | 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /routes/web.php: -------------------------------------------------------------------------------- 1 | env = config('admin.extensions.env-manager.env-file-path', base_path().'/.env'); 18 | parent::__construct($attributes); 19 | } 20 | 21 | 22 | public function paginate() 23 | { 24 | $perPage = Request::get('per_page', 20); 25 | $page = Request::get('page', 1); 26 | $start = ($page - 1) * $perPage; 27 | $data = $this->getEnv(); 28 | $list = array_slice($data, $start, $perPage); 29 | $list = static::hydrate($list); 30 | $paginator = new LengthAwarePaginator($list, count($data), $perPage); 31 | $paginator->setPath(url()->current()); 32 | return $paginator; 33 | } 34 | 35 | public static function with($relations) 36 | { 37 | return new static; 38 | } 39 | 40 | public function findOrFail($id) 41 | { 42 | $item = $this->getEnv($id); 43 | return static::newFromBuilder($item); 44 | } 45 | 46 | 47 | public function save(array $options = []) 48 | { 49 | $data = $this->getAttributes(); 50 | 51 | return $this->setEnv($data['key'], $data['value']); 52 | } 53 | 54 | /** 55 | * @param $id 56 | * @return bool|null|void 57 | */ 58 | public function deleteEnv($id) 59 | { 60 | $ids = explode(',', $id); 61 | $data = $this->getEnv(); 62 | foreach ($ids as $val) { 63 | $index = array_search($val, array_column($data, 'id')); 64 | unset($data[$index]); 65 | } 66 | return $this->saveEnv($data); 67 | } 68 | 69 | /** 70 | * Get .env variable. 71 | * @param null $id 72 | * @return array|mixed 73 | */ 74 | private function getEnv($id = null) 75 | { 76 | $string = file_get_contents($this->env); 77 | $string = preg_split('/\n+/', $string); 78 | $array = []; 79 | foreach ($string as $k => $one) { 80 | if (preg_match('/^(#\s)/', $one) === 1 || preg_match('/^([\\n\\r]+)/', $one)) { 81 | continue; 82 | } 83 | $entry = explode("=", $one, 2); 84 | if (!empty($entry[0])) { 85 | $array[] = ['id' => $k + 1, 'key' => $entry[0], 'value' => isset($entry[1]) ? $entry[1] : null]; 86 | } 87 | } 88 | if (empty($id)) { 89 | return $array; 90 | } 91 | $index = array_search($id, array_column($array, 'id')); 92 | 93 | return $array[$index]; 94 | } 95 | 96 | /** 97 | * Update or create .env variable. 98 | * @param $key 99 | * @param $value 100 | * @return bool 101 | */ 102 | private function setEnv($key, $value) 103 | { 104 | $array = $this->getEnv(); 105 | $index = array_search($key, array_column($array, 'key')); 106 | if ($index !== false) { 107 | $array[$index]['value'] = $value; // 更新 108 | } else { 109 | array_push($array, ['key' => $key, 'value' => $value]); // 新增 110 | } 111 | return $this->saveEnv($array); 112 | } 113 | 114 | /** 115 | * Save .env variable. 116 | * @param $array 117 | * @return bool 118 | */ 119 | private function saveEnv($array) 120 | { 121 | if (is_array($array)) { 122 | $newArray = []; 123 | $i = 0; 124 | foreach ($array as $env) { 125 | 126 | if (preg_match('/\s/', $env['value']) > 0 && (strpos($env['value'], '"') > 0 && strpos($env['value'], '"', -0) > 0)) { 127 | $env['value'] = '"'.$env['value'].'"'; 128 | } 129 | $newArray[$i] = $env['key']."=".$env['value']; 130 | $i++; 131 | } 132 | $newArray = implode("\n", $newArray); 133 | file_put_contents($this->env, $newArray); 134 | return true; 135 | } 136 | return false; 137 | } 138 | 139 | } -------------------------------------------------------------------------------- /src/EnvManager.php: -------------------------------------------------------------------------------- 1 | 'EnvManager', 13 | 'path' => 'env-manager', 14 | 'icon' => 'fa-gears', 15 | ]; 16 | } -------------------------------------------------------------------------------- /src/EnvManagerServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->booted(function () { 19 | EnvManager::routes(__DIR__.'/../routes/web.php'); 20 | }); 21 | } 22 | } -------------------------------------------------------------------------------- /src/Http/Controllers/EnvManagerController.php: -------------------------------------------------------------------------------- 1 | model = new Env(); 24 | } 25 | 26 | public function index(Content $content) 27 | { 28 | return $content 29 | ->header('Title') 30 | ->description('Description') 31 | ->body($this->grid()); 32 | } 33 | 34 | 35 | /** 36 | * Show interface. 37 | * 38 | * @param mixed $key 39 | * @param Content $content 40 | * @return Content 41 | */ 42 | public function show($key, Content $content) 43 | { 44 | return $content 45 | ->header('Detail') 46 | ->description('description') 47 | ->body($this->detail($key)); 48 | } 49 | 50 | 51 | /** 52 | * Edit interface. 53 | * 54 | * @param $key 55 | * @return Content 56 | */ 57 | public function edit($key, Content $content) 58 | { 59 | $content->header('Title'); 60 | $content->description('Description'); 61 | $content->body($this->form()->edit($key)); 62 | return $content; 63 | } 64 | 65 | 66 | /** 67 | * Create interface. 68 | * 69 | * @param Content $content 70 | * @return Content 71 | */ 72 | public function create(Content $content) 73 | { 74 | return $content 75 | ->header('Create') 76 | ->description('description') 77 | ->body($this->form()); 78 | } 79 | 80 | 81 | /** 82 | * Make a grid builder. 83 | * 84 | * @return Grid 85 | */ 86 | protected function grid() 87 | { 88 | $grid = new Grid($this->model); 89 | $grid->key(); 90 | $grid->value(); 91 | return $grid; 92 | } 93 | 94 | /** 95 | * Make a show builder. 96 | * 97 | * @param mixed $key 98 | * @return Show 99 | */ 100 | protected function detail($key) 101 | { 102 | $show = new Show($this->model->findOrFail($key)); 103 | 104 | $show->key('Key'); 105 | $show->value('Value'); 106 | 107 | return $show; 108 | } 109 | 110 | protected function form() 111 | { 112 | $form = new Form($this->model); 113 | $form->text('key', 'Key'); 114 | $form->text('value', 'Value'); 115 | return $form; 116 | } 117 | 118 | 119 | /** 120 | * Destroy data 121 | * 122 | * @param $id 123 | * 124 | * @return mixed 125 | */ 126 | public function destroy($id) 127 | { 128 | if ($this->model->deleteEnv($id)) { 129 | $data = [ 130 | 'status' => true, 131 | 'message' => trans('admin.delete_succeeded'), 132 | ]; 133 | } else { 134 | 135 | $data = [ 136 | 'status' => false, 137 | 'message' => trans('admin.delete_failed'), 138 | ]; 139 | } 140 | 141 | return response()->json($data); 142 | } 143 | 144 | } --------------------------------------------------------------------------------