| 1 | <?php |
||
| 2 | |||
| 3 | namespace Turahe\LaravelInstaller\Helpers; |
||
| 4 | |||
| 5 | use Exception; |
||
| 6 | use Illuminate\Support\Str; |
||
| 7 | use Illuminate\Http\Request; |
||
| 8 | |||
| 9 | /** |
||
| 10 | * Class EnvironmentManager. |
||
| 11 | */ |
||
| 12 | class EnvironmentManager |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var string |
||
| 16 | */ |
||
| 17 | private $envPath; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | private $envExamplePath; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Set the .env and .env.example paths. |
||
| 26 | */ |
||
| 27 | public function __construct() |
||
| 28 | { |
||
| 29 | $this->envPath = base_path('.env'); |
||
| 30 | $this->envExamplePath = base_path('.env.example'); |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Get the content of the .env file. |
||
| 35 | * |
||
| 36 | * @return string |
||
| 37 | */ |
||
| 38 | public function getEnvContent() |
||
| 39 | { |
||
| 40 | if (! file_exists($this->envPath)) { |
||
| 41 | if (file_exists($this->envExamplePath)) { |
||
| 42 | copy($this->envExamplePath, $this->envPath); |
||
| 43 | } else { |
||
| 44 | touch($this->envPath); |
||
| 45 | } |
||
| 46 | } |
||
| 47 | |||
| 48 | return file_get_contents($this->envPath); |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Get the the .env file path. |
||
| 53 | * |
||
| 54 | * @return string |
||
| 55 | */ |
||
| 56 | public function getEnvPath() |
||
| 57 | { |
||
| 58 | return $this->envPath; |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Get the the .env.example file path. |
||
| 63 | * |
||
| 64 | * @return string |
||
| 65 | */ |
||
| 66 | public function getEnvExamplePath() |
||
| 67 | { |
||
| 68 | return $this->envExamplePath; |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Save the edited content to the .env file. |
||
| 73 | * |
||
| 74 | * @param Request $input |
||
| 75 | * @return string |
||
| 76 | */ |
||
| 77 | public function saveFileClassic(Request $input) |
||
| 78 | { |
||
| 79 | $message = trans('installer_messages.environment.success'); |
||
| 80 | |||
| 81 | try { |
||
| 82 | file_put_contents($this->envPath, $input->get('envConfig')); |
||
| 83 | } catch (Exception $e) { |
||
| 84 | $message = trans('installer_messages.environment.errors'); |
||
| 85 | } |
||
| 86 | |||
| 87 | return $message; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Save the form content to the .env file. |
||
| 92 | * |
||
| 93 | * @param Request $request |
||
| 94 | * @return string |
||
| 95 | */ |
||
| 96 | public function saveFileWizard(Request $request) |
||
| 97 | { |
||
| 98 | $results = trans('installer_messages.environment.success'); |
||
| 99 | |||
| 100 | $envFileData = |
||
| 101 | 'APP_NAME=\''.$request->app_name."'\n". |
||
| 102 | 'APP_ENV='.$request->environment."\n". |
||
| 103 | 'APP_KEY='.'base64:'.base64_encode(Str::random(32))."\n". |
||
| 104 | 'APP_DEBUG='.$request->app_debug."\n". |
||
| 105 | 'APP_LOG_LEVEL='.$request->app_log_level."\n". |
||
| 106 | 'APP_URL='.$request->app_url."\n\n". |
||
| 107 | 'DB_CONNECTION='.$request->database_connection."\n". |
||
| 108 | 'DB_HOST='.$request->database_hostname."\n". |
||
| 109 | 'DB_PORT='.$request->database_port."\n". |
||
| 110 | 'DB_DATABASE='.$request->database_name."\n". |
||
| 111 | 'DB_USERNAME='.$request->database_username."\n". |
||
| 112 | 'DB_PASSWORD='.$request->database_password."\n\n". |
||
| 113 | 'BROADCAST_DRIVER='.$request->broadcast_driver."\n". |
||
| 114 | 'CACHE_DRIVER='.$request->cache_driver."\n". |
||
| 115 | 'SESSION_DRIVER='.$request->session_driver."\n". |
||
| 116 | 'QUEUE_DRIVER='.$request->queue_driver."\n\n". |
||
| 117 | 'REDIS_HOST='.$request->redis_hostname."\n". |
||
| 118 | 'REDIS_PASSWORD='.$request->redis_password."\n". |
||
| 119 | 'REDIS_PORT='.$request->redis_port."\n\n". |
||
| 120 | 'MAIL_DRIVER='.$request->mail_driver."\n". |
||
| 121 | 'MAIL_HOST='.$request->mail_host."\n". |
||
| 122 | 'MAIL_PORT='.$request->mail_port."\n". |
||
| 123 | 'MAIL_USERNAME='.$request->mail_username."\n". |
||
| 124 | 'MAIL_PASSWORD='.$request->mail_password."\n". |
||
| 125 | 'MAIL_ENCRYPTION='.$request->mail_encryption."\n\n". |
||
| 126 | 'PUSHER_APP_ID='.$request->pusher_app_id."\n". |
||
| 127 | 'PUSHER_APP_KEY='.$request->pusher_app_key."\n". |
||
| 128 | 'PUSHER_APP_SECRET='.$request->pusher_app_secret; |
||
| 129 | |||
| 130 | try { |
||
| 131 | file_put_contents($this->envPath, $envFileData); |
||
| 132 | } catch (Exception $e) { |
||
| 133 | $results = trans('installer_messages.environment.errors'); |
||
| 134 | } |
||
| 135 | |||
| 136 | return $results; |
||
|
0 ignored issues
–
show
|
|||
| 137 | } |
||
| 138 | } |
||
| 139 |