2015-03-17 02:30:56 +01:00
|
|
|
<?php namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use Artisan;
|
|
|
|
use Cache;
|
|
|
|
use Config;
|
|
|
|
use DB;
|
|
|
|
use Exception;
|
|
|
|
use Input;
|
|
|
|
use Utils;
|
|
|
|
use View;
|
2015-03-24 09:21:12 +01:00
|
|
|
use App\Ninja\Mailers\Mailer;
|
|
|
|
use App\Ninja\Repositories\AccountRepository;
|
2015-03-23 07:52:01 +01:00
|
|
|
use Redirect;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
class AppController extends BaseController
|
|
|
|
{
|
|
|
|
protected $accountRepo;
|
|
|
|
protected $mailer;
|
|
|
|
|
|
|
|
public function __construct(AccountRepository $accountRepo, Mailer $mailer)
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->accountRepo = $accountRepo;
|
|
|
|
$this->mailer = $mailer;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function showSetup()
|
|
|
|
{
|
|
|
|
if (Utils::isNinja() || Utils::isDatabaseSetup()) {
|
|
|
|
return Redirect::to('/');
|
|
|
|
}
|
|
|
|
|
|
|
|
return View::make('setup');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function doSetup()
|
|
|
|
{
|
2015-03-24 09:21:12 +01:00
|
|
|
// if (Utils::isNinja() || Utils::isDatabaseSetup()) {
|
|
|
|
// return Redirect::to('/');
|
|
|
|
// }
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
$valid = false;
|
|
|
|
$test = Input::get('test');
|
|
|
|
|
|
|
|
$app = Input::get('app');
|
|
|
|
$app['key'] = str_random(RANDOM_KEY_LENGTH);
|
2015-03-24 09:21:12 +01:00
|
|
|
$app['debug'] = 'false';
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
$database = Input::get('database');
|
2015-03-24 09:21:12 +01:00
|
|
|
$dbType = $database['driver'];
|
|
|
|
|
|
|
|
$test_database = $database;
|
|
|
|
$test_database['connections'] = [$dbType => $test_database];
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
$mail = Input::get('mail');
|
|
|
|
$email = $mail['username'];
|
|
|
|
$mail['from']['address'] = $email;
|
|
|
|
|
|
|
|
if ($test == 'mail') {
|
|
|
|
return self::testMail($mail);
|
|
|
|
}
|
|
|
|
|
2015-03-24 09:21:12 +01:00
|
|
|
$valid = self::testDatabase($test_database);
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
if ($test == 'db') {
|
|
|
|
return $valid === true ? 'Success' : $valid;
|
|
|
|
} elseif (!$valid) {
|
|
|
|
return Redirect::to('/setup')->withInput();
|
|
|
|
}
|
|
|
|
|
2015-03-24 09:21:12 +01:00
|
|
|
/*$content = "<?php return 'production';";
|
2015-03-16 22:45:25 +01:00
|
|
|
$fp = fopen(base_path()."/bootstrap/environment.php", 'w');
|
|
|
|
fwrite($fp, $content);
|
2015-03-24 09:21:12 +01:00
|
|
|
fclose($fp);*/
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-03-24 00:54:49 +01:00
|
|
|
/*$configDir = base_path().'/config/production';
|
2015-03-16 22:45:25 +01:00
|
|
|
if (!file_exists($configDir)) {
|
|
|
|
mkdir($configDir);
|
2015-03-24 00:54:49 +01:00
|
|
|
}*/
|
|
|
|
|
2015-03-24 09:21:12 +01:00
|
|
|
// TODO: GET THIS WORKING PROPERLY!!!!
|
2015-03-24 00:54:49 +01:00
|
|
|
|
|
|
|
// == ENV Settings (Production) == //
|
|
|
|
|
|
|
|
$env_config = '';
|
|
|
|
$settings = ['app' => $app, 'database' => $database, 'mail' => $mail];
|
|
|
|
|
|
|
|
// Save each config area to $env varible
|
|
|
|
foreach ($settings as $key => $config) {
|
|
|
|
|
|
|
|
// Write a nice Comment to lay out each config area
|
|
|
|
$env_config .= "# " . $key . " Settings \n";
|
2015-03-24 09:21:12 +01:00
|
|
|
|
|
|
|
|
2015-03-24 00:54:49 +01:00
|
|
|
// For Each config varible : Write to env
|
|
|
|
foreach ($config as $name => $value) {
|
2015-03-24 09:21:12 +01:00
|
|
|
if(is_array($value)){
|
|
|
|
continue; // BREAKS ON THE MAIL ARRAY
|
|
|
|
dd($value);
|
|
|
|
}
|
2015-03-24 00:54:49 +01:00
|
|
|
$env_config .= strtoupper($name) . '=' . $value . "\n";
|
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2015-03-24 00:54:49 +01:00
|
|
|
// Check Config Settings !empty
|
|
|
|
if(empty($env_config)){
|
|
|
|
dd('ERROR: No Config settings saved to content var');
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
2015-03-24 00:54:49 +01:00
|
|
|
|
|
|
|
// Write Config Settings
|
2015-03-24 09:21:12 +01:00
|
|
|
// $fp = fopen(base_path()."/.env", 'w');
|
|
|
|
// fwrite($fp, $env_config);
|
|
|
|
// fclose($fp);
|
2015-03-24 00:54:49 +01:00
|
|
|
|
|
|
|
// == END ENV Settings == //
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-03-24 00:54:49 +01:00
|
|
|
|
|
|
|
// == DB Migrate & Seed == //
|
|
|
|
|
|
|
|
/* Laravel 5 thows an error when performing these calls
|
|
|
|
* See: https://laracasts.com/discuss/channels/general-discussion/l5-artisancall-issue
|
|
|
|
|
|
|
|
Artisan::call('migrate');
|
|
|
|
Artisan::call('db:seed');
|
|
|
|
|
|
|
|
*/
|
2015-03-17 06:22:44 +01:00
|
|
|
|
|
|
|
// I Really don't want to do it this way but its the best I've found so far.
|
|
|
|
$process = new \Symfony\Component\Process\Process('cd ' . base_path() . ' && php artisan migrate --seed');
|
|
|
|
$process->run();
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-03-24 00:54:49 +01:00
|
|
|
// == END DB Migrate & Seed == //
|
|
|
|
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
$account = $this->accountRepo->create();
|
|
|
|
$user = $account->users()->first();
|
|
|
|
|
|
|
|
$user->first_name = trim(Input::get('first_name'));
|
|
|
|
$user->last_name = trim(Input::get('last_name'));
|
|
|
|
$user->email = trim(strtolower(Input::get('email')));
|
|
|
|
$user->username = $user->email;
|
|
|
|
$user->password = trim(Input::get('password'));
|
|
|
|
$user->password_confirmation = trim(Input::get('password'));
|
|
|
|
$user->registered = true;
|
2015-03-24 09:21:12 +01:00
|
|
|
$user->save();
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
//Auth::login($user, true);
|
|
|
|
$this->accountRepo->registerUser($user);
|
|
|
|
|
|
|
|
return Redirect::to('/invoices/create');
|
|
|
|
}
|
|
|
|
|
|
|
|
private function testDatabase($database)
|
|
|
|
{
|
2015-03-24 09:21:12 +01:00
|
|
|
// dd($database);
|
|
|
|
$dbType = $database['driver'];
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
Config::set('database.default', $dbType);
|
|
|
|
|
|
|
|
foreach ($database['connections'][$dbType] as $key => $val) {
|
|
|
|
Config::set("database.connections.{$dbType}.{$key}", $val);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$valid = DB::connection()->getDatabaseName() ? true : false;
|
|
|
|
} catch (Exception $e) {
|
|
|
|
return $e->getMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function testMail($mail)
|
|
|
|
{
|
|
|
|
$email = $mail['username'];
|
|
|
|
$fromName = $mail['from']['name'];
|
|
|
|
|
|
|
|
foreach ($mail as $key => $val) {
|
|
|
|
Config::set("mail.{$key}", $val);
|
|
|
|
}
|
|
|
|
|
|
|
|
Config::set('mail.from.address', $email);
|
|
|
|
Config::set('mail.from.name', $fromName);
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'text' => 'Test email',
|
|
|
|
];
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->mailer->sendTo($email, $email, $fromName, 'Test email', 'contact', $data);
|
|
|
|
|
|
|
|
return 'Sent';
|
|
|
|
} catch (Exception $e) {
|
|
|
|
return $e->getMessage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-24 00:54:49 +01:00
|
|
|
// QUSTION: Does this actually get used???
|
2015-03-16 22:45:25 +01:00
|
|
|
public function install()
|
|
|
|
{
|
|
|
|
if (!Utils::isNinja() && !Utils::isDatabaseSetup()) {
|
|
|
|
try {
|
2015-03-24 00:54:49 +01:00
|
|
|
// DB Migrate & Seed
|
|
|
|
// I Really don't want to do it this way but its the best I've found so far. See Above.
|
|
|
|
$process = new \Symfony\Component\Process\Process('cd ' . base_path() . ' && php artisan migrate --seed');
|
|
|
|
$process->run();
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
} catch (Exception $e) {
|
|
|
|
Response::make($e->getMessage(), 500);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Redirect::to('/');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function update()
|
|
|
|
{
|
|
|
|
if (!Utils::isNinja()) {
|
|
|
|
try {
|
2015-03-24 00:54:49 +01:00
|
|
|
// DB Migrate & Seed
|
|
|
|
// I Really don't want to do it this way but its the best I've found so far. See Above.
|
|
|
|
$process = new \Symfony\Component\Process\Process('cd ' . base_path() . ' && php artisan migrate');
|
|
|
|
$process->run();
|
|
|
|
|
|
|
|
// Artisan::call('migrate');
|
2015-03-16 22:45:25 +01:00
|
|
|
Cache::flush();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
Response::make($e->getMessage(), 500);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Redirect::to('/');
|
|
|
|
}
|
|
|
|
}
|