|
| 1 | +<?php |
| 2 | + |
| 3 | +use Illuminate\Console\Command; |
| 4 | +use Symfony\Component\Console\Input\InputOption; |
| 5 | +use Symfony\Component\Console\Input\InputArgument; |
| 6 | +use Symfony\Component\Console\Output\StreamOutput; |
| 7 | + |
| 8 | +class ConfigureCommand extends Command { |
| 9 | + |
| 10 | + /** |
| 11 | + * The console command name. |
| 12 | + * |
| 13 | + * @var string |
| 14 | + */ |
| 15 | + protected $name = 'syntax:configure'; |
| 16 | + |
| 17 | + /** |
| 18 | + * The console command description. |
| 19 | + * |
| 20 | + * @var string |
| 21 | + */ |
| 22 | + protected $description = 'Set up Laravel configs.'; |
| 23 | + |
| 24 | + /** |
| 25 | + * An object containing the remote config details |
| 26 | + * |
| 27 | + * @var string[] |
| 28 | + */ |
| 29 | + protected $remoteDetails; |
| 30 | + |
| 31 | + /** |
| 32 | + * An object containing the database config details |
| 33 | + * |
| 34 | + * @var string[] |
| 35 | + */ |
| 36 | + protected $databaseDetails; |
| 37 | + |
| 38 | + /** |
| 39 | + * The URL for the this site |
| 40 | + * |
| 41 | + * @var string |
| 42 | + */ |
| 43 | + protected $siteUrl; |
| 44 | + |
| 45 | + /** |
| 46 | + * The output stream for any artisan commands |
| 47 | + * |
| 48 | + * @var string |
| 49 | + */ |
| 50 | + protected $stream; |
| 51 | + |
| 52 | + /** |
| 53 | + * Create a new command instance. |
| 54 | + * |
| 55 | + * @return void |
| 56 | + */ |
| 57 | + public function __construct() |
| 58 | + { |
| 59 | + parent::__construct(); |
| 60 | + |
| 61 | + $this->remoteDetails = new stdClass(); |
| 62 | + $this->databaseDetails = new stdClass(); |
| 63 | + $this->stream = fopen('php://output', 'w'); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Execute the console command. |
| 68 | + * |
| 69 | + * @return mixed |
| 70 | + */ |
| 71 | + public function fire() |
| 72 | + { |
| 73 | + $this->comment('Starting Laravel configurations...'); |
| 74 | + |
| 75 | + // Set up the configs |
| 76 | + $this->setUpRemote(); |
| 77 | + $this->setUpDatabase(); |
| 78 | + $this->setUpApp(); |
| 79 | + |
| 80 | + $this->comment('Laravel configurations complete!'); |
| 81 | + } |
| 82 | + |
| 83 | + /******************************************************************** |
| 84 | + * Unique Methods |
| 85 | + *******************************************************************/ |
| 86 | + protected function confirmConfig($type, $configDetails = null) |
| 87 | + { |
| 88 | + $this->line('Your '. $type .' configuration will be set to the following.'); |
| 89 | + |
| 90 | + switch ($type) { |
| 91 | + case 'remote': |
| 92 | + $this->line(print_r($this->remoteDetails, 1)); |
| 93 | + if (!$this->confirm('Do you want to keep this configuration? [yes|no]')) { |
| 94 | + return $this->setUpRemote(); |
| 95 | + } else { |
| 96 | + return $this->configureRemote(); |
| 97 | + } |
| 98 | + break; |
| 99 | + case 'database': |
| 100 | + $this->line(print_r($this->databaseDetails, 1)); |
| 101 | + if (!$this->confirm('Do you want to keep this configuration? [yes|no]')) { |
| 102 | + return $this->setUpDatabase(); |
| 103 | + } else { |
| 104 | + return $this->configureDatabase(); |
| 105 | + } |
| 106 | + break; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + /******************************************************************** |
| 111 | + * Set Up Methods |
| 112 | + *******************************************************************/ |
| 113 | + protected function setUpRemote() |
| 114 | + { |
| 115 | + // Set up our remote config |
| 116 | + $this->comment('Setting up remote config options...'); |
| 117 | + $this->remoteDetails->host = $this->ask('What is your remote host? (for ports add :<PORT> to the end)'); |
| 118 | + $this->remoteDetails->username = $this->ask('What is your remote username?'); |
| 119 | + $this->remoteDetails->password = $this->secret('What is your remote password?'); |
| 120 | + $this->remoteDetails->key = $this->ask('Where is your remote rsa key?'); |
| 121 | + $this->remoteDetails->keyphrase = $this->ask('What is the passphrase?'); |
| 122 | + $this->remoteDetails->root = $this->ask('Where is your remote root?'); |
| 123 | + |
| 124 | + $this->confirmConfig('remote'); |
| 125 | + } |
| 126 | + |
| 127 | + protected function setUpDatabase() |
| 128 | + { |
| 129 | + // Set up our database config |
| 130 | + $this->comment('Setting up datatabase details...'); |
| 131 | + $this->databaseDetails->driver = $this->ask('What is your database driver? [Hit enter to use mysql driver]', 'mysql'); |
| 132 | + $this->databaseDetails->host = $this->ask('What is your database host?'); |
| 133 | + $this->databaseDetails->database = $this->ask('What is the database name?'); |
| 134 | + $this->databaseDetails->username = $this->ask('What is your database username?'); |
| 135 | + $this->databaseDetails->password = $this->secret('What is your database password?'); |
| 136 | + $this->databaseDetails->charset = $this->ask('What is your database charset? [Hit enter to use utf8]', 'utf8'); |
| 137 | + $this->databaseDetails->collation = $this->ask('What is your database collation? [Hit enter to use utf8_unicode_ci]', 'utf8_unicode_ci'); |
| 138 | + $this->databaseDetails->prefix = $this->ask('What is your database prefix?'); |
| 139 | + |
| 140 | + $this->confirmConfig('database'); |
| 141 | + } |
| 142 | + |
| 143 | + protected function setUpApp() |
| 144 | + { |
| 145 | + // Set up our app config |
| 146 | + $this->comment('Setting up app details...'); |
| 147 | + $this->siteUrl = $this->ask('What is this site\'s url?'); |
| 148 | + |
| 149 | + list($path, $contents) = $this->getConfig('app'); |
| 150 | + |
| 151 | + $contents = str_replace($this->laravel['config']['app.url'], $this->siteUrl, $contents); |
| 152 | + |
| 153 | + File::put($path, $contents); |
| 154 | + } |
| 155 | + |
| 156 | + /******************************************************************** |
| 157 | + * Configuration Methods |
| 158 | + *******************************************************************/ |
| 159 | + protected function configureRemote() |
| 160 | + { |
| 161 | + list($path, $contents) = $this->getConfig('remote'); |
| 162 | + |
| 163 | + foreach ($this->remoteDetails as $key => $value) { |
| 164 | + $contents = str_replace($this->laravel['config']['remote.connections.default.'. $key], $value, $contents); |
| 165 | + } |
| 166 | + |
| 167 | + File::put($path, $contents); |
| 168 | + } |
| 169 | + |
| 170 | + protected function configureDatabase() |
| 171 | + { |
| 172 | + list($path, $contents) = $this->getConfig('database'); |
| 173 | + |
| 174 | + foreach ($this->databaseDetails as $key => $value) { |
| 175 | + $contents = str_replace($this->laravel['config']['database.connections.mysql.'. $key], $value, $contents); |
| 176 | + } |
| 177 | + |
| 178 | + File::put($path, $contents); |
| 179 | + } |
| 180 | + |
| 181 | + /******************************************************************** |
| 182 | + * Extra Methods |
| 183 | + *******************************************************************/ |
| 184 | + protected function getConfig($file) |
| 185 | + { |
| 186 | + $path = $this->laravel['path'].'/config/'. $file .'.php'; |
| 187 | + |
| 188 | + $contents = File::get($path); |
| 189 | + |
| 190 | + return array($path, $contents); |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * Get the console command arguments. |
| 195 | + * |
| 196 | + * @return array |
| 197 | + */ |
| 198 | + protected function getArguments() |
| 199 | + { |
| 200 | + return array( |
| 201 | + // array('example', InputArgument::REQUIRED, 'An example argument.'), |
| 202 | + ); |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Get the console command options. |
| 207 | + * |
| 208 | + * @return array |
| 209 | + */ |
| 210 | + protected function getOptions() |
| 211 | + { |
| 212 | + return array( |
| 213 | + // array('example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null), |
| 214 | + ); |
| 215 | + } |
| 216 | + |
| 217 | +} |
0 commit comments