blue-twilight/app/Providers/AppServiceProvider.php

150 lines
4.0 KiB
PHP

<?php
namespace App\Providers;
use App\Album;
use App\Configuration;
use App\Facade\Theme;
use App\Facade\UserConfig;
use App\Helpers\ConfigHelper;
use App\Helpers\ImageHelper;
use App\Helpers\MiscHelper;
use App\Helpers\ThemeHelper;
use Illuminate\Database\QueryException;
use Illuminate\Mail\Mailer;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
if ($this->checkIfInstalled())
{
$this->app->singleton('image', function ($app)
{
return new ImageHelper();
});
$this->app->singleton('theme', function ($app)
{
return new ThemeHelper();
});
$this->app->singleton('user_config', function ($app)
{
return new ConfigHelper();
});
// When running migrations or CLI tasks, don't need to add things to the view
if (php_sapi_name() != 'cli')
{
$this->addThemeInfoToView();
$this->addAlbumsToView();
}
// Set the default mail configuration as per user's requirements
$this->updateMailConfig();
}
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
private function checkIfInstalled()
{
if ($_SERVER['REQUEST_URI'] == '/install')
{
$baseDirectory = dirname(dirname(__DIR__));
// Generate an application key and store to the .env file
if (!file_exists($baseDirectory . '/.env'))
{
$key = MiscHelper::randomString(32);
file_put_contents($baseDirectory . '/.env', sprintf('APP_KEY=%s', $key) . PHP_EOL);
app('config')->set(['app' => ['key' => $key]]);
}
return false;
}
try
{
if (!Configuration::installCompleted())
{
throw new \Exception(sprintf('install_completed configuration record is not present!'));
}
return true;
}
catch (\Exception $ex)
{
header(sprintf('Location: %s', url('/install')));
die();
}
}
private function addAlbumsToView()
{
$albums = Album::all()->sortBy('name');
View::share('albums', $albums);
}
private function addThemeInfoToView()
{
$themeInfo = Theme::info();
// Add each theme info element to the view - prefixing with theme_
// e.g. $themeInfo['name'] becomes $theme_name in the view
foreach ($themeInfo as $key => $value)
{
View::share('theme_' . $key, $value);
}
// Also add a theme_url key
View::share('theme_url', sprintf('themes/%s', Theme::current()));
}
private function updateMailConfig()
{
/** @var Mailer $mailer */
$mailer = $this->app->mailer;
$swiftMailer = $mailer->getSwiftMailer();
/** @var \Swift_SmtpTransport $transport */
$transport = $swiftMailer->getTransport();
$transport->setHost(UserConfig::get('smtp_server'));
$transport->setPort(intval(UserConfig::get('smtp_port')));
$username = UserConfig::get('smtp_username');
if (!is_null($username))
{
$transport->setUsername($username);
}
$password = UserConfig::get('smtp_password');
if (!is_null($password))
{
$transport->setPassword(decrypt($password));
}
if (UserConfig::get('smtp_encryption'))
{
$transport->setEncryption('tls');
}
$mailer->alwaysFrom(UserConfig::get('sender_address'), UserConfig::get('sender_name'));
}
}