doughnut-wedding/app/Kernel.php

355 lines
9.8 KiB
PHP

<?php
/*
* doughnutwedding.com
* Copyright (C) 2017 http://doughnutwedding.com eric@doughnutwedding.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
use Bramus\Monolog\Formatter\{
ColorSchemes\TrafficLight,
ColoredLineFormatter
};
use Composer\Autoload\ClassLoader;
use Dflydev\Provider\DoctrineOrm\DoctrineOrmServiceProvider;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Monolog\{
Handler\StreamHandler,
Logger
};
use Sikofitt\App\Traits\FlashTrait;
use Silex\Application;
use Silex\Application\{
FormTrait,
MonologTrait,
SecurityTrait,
SwiftmailerTrait,
TranslationTrait,
TwigTrait,
UrlGeneratorTrait
};
use Silex\Provider\{
AssetServiceProvider,
CsrfServiceProvider,
DoctrineServiceProvider,
ExceptionHandlerServiceProvider,
FormServiceProvider,
HttpFragmentServiceProvider,
HttpKernelServiceProvider,
MonologServiceProvider,
RoutingServiceProvider,
SecurityServiceProvider,
ServiceControllerServiceProvider,
SessionServiceProvider,
SwiftmailerServiceProvider,
TwigServiceProvider,
ValidatorServiceProvider,
VarDumperServiceProvider,
WebProfilerServiceProvider
};
use Symfony\Bridge\Twig\Extension\TranslationExtension;
use Symfony\Component\Debug\ErrorHandler;
use Symfony\Component\Debug\ExceptionHandler;
use Symfony\Component\Form\FormFactory;
use Symfony\Component\Translation\Translator;
/**
* Class Kernel.
*/
class Kernel extends Application
{
use FlashTrait;
use FormTrait;
use MonologTrait;
use SecurityTrait;
use SwiftmailerTrait;
use TranslationTrait;
use TwigTrait;
use UrlGeneratorTrait;
/**
* Kernel constructor.
*
* @param ClassLoader $loader
* @param bool $debug
* @param array $values
*/
public function __construct(ClassLoader $loader, bool $debug = false, array $values = [])
{
parent::__construct($values);
AnnotationRegistry::registerLoader([$loader, 'loadClass']);
if (true === $debug) {
$this->setDebug();
}
$this->setUpProviders();
$this->setUpDatabase();
$this->setUpView();
$this->setUpLogger();
$this->setUpMailer();
}
/**
* @param array $values
*
* @throws \InvalidArgumentException
*
* @return int
*/
public function mail(array $values)
{
if (false === isset($values['from']) || false === is_array($values['from'])) {
throw new \InvalidArgumentException('Array key "from" should be an array.');
} elseif (false === isset($values['to']) || false === is_array($values['to'])) {
throw new \InvalidArgumentException('Array key "to" should be an array.');
} elseif (false === isset($values['body']) || false === is_string($values['body'])) {
throw new \InvalidArgumentException('Array key "body" should be a string.');
} elseif (false === isset($values['subject']) || false === is_string($values['subject'])) {
throw new \InvalidArgumentException('Array key "subject" should be a string.');
}
$message = \Swift_Message::newInstance();
$message
->setSubject($values['subject'])
->setFrom($values['from'])
->setTo($values['to'])
->setBody($values['body']);
/**
* @var \Swift_Transport $mailer
*/
$mailer = $this['mailer'];
return $mailer->send($message);
}
/**
* Sets the application to debug.
*/
public function setDebug()
{
$this['debug'] = true;
ErrorHandler::register();
ExceptionHandler::register();
}
/**
* @return bool
*/
public function getDebug(): bool
{
return $this['debug'];
}
/**
* @return string
*/
public function getBaseDir(): string
{
return __DIR__.'/..';
}
/**
* @return string
*/
public function getAppDir(): string
{
return $this->getBaseDir().'/app';
}
/**
* @return string
*/
public function getConfigDir(): string
{
return $this->getAppDir().'/config';
}
/**
* @return string
*/
public function getLogDir(): string
{
return $this->getAppDir().'/logs';
}
/**
* @return string
*/
public function getCacheDir(): string
{
return $this->getBaseDir().'/cache';
}
/**
* @return FormFactory
*/
public function getFormFactory()
{
return $this['form.factory'];
}
/**
* Sets up the database environment.
*/
protected function setUpDatabase()
{
$this->register(new DoctrineServiceProvider(), [
'db.options' => [
'driver' => 'pdo_mysql',
'dbname' => 'doughnut',
'host' => 'mysql',
'user' => 'doughnut',
'password' => 'doughnut',
],
]);
$this->register(new DoctrineOrmServiceProvider(), [
'orm.proxies_dir' => $this->getCacheDir().'/doctrine/proxies',
'orm.default_cache' => 'array',
'orm.em.options' => [
'connection' => 'default',
'mappings' => [
[
'type' => 'annotation',
'path' => $this->getBaseDir().'/src/Sikofitt/App/Entity',
'namespace' => 'Sikofitt\App\Entity',
'use_simple_annotation_reader' => false,
],
],
],
]);
}
/**
* Sets up the view for the application.
*/
protected function setUpView()
{
$this
->register(new HttpKernelServiceProvider())
->register(new RoutingServiceProvider())
->register(new AssetServiceProvider())
->register(new SessionServiceProvider())
->register(new HttpFragmentServiceProvider())
->register(new ServiceControllerServiceProvider())
->register(new ValidatorServiceProvider());
$this->register(new TwigServiceProvider(), [
'twig.path' => $this->getAppDir().'/views',
]);
if (true === $this['debug']) {
$this
->register(new VarDumperServiceProvider())
->register(new WebProfilerServiceProvider(),
[
'profiler.cache_dir' => $this->getCacheDir().'/profiler',
'profiler.mount_prefix' => '/_profiler',
])
->register(new ExceptionHandlerServiceProvider())
;
}
/*
* Closure supports \Twig_Environment and Silex\Application as a second
* parameter, but we never use Silex\Application so we leave it out.
*/
$r = new \Symfony\Component\HttpFoundation\RequestStack();
$this->extend('twig', function (\Twig_Environment $twig) {
$twig->addGlobal('session', $this['session']);
$twig->addExtension(new TranslationExtension(new Translator('en')));
return $twig;
});
}
/**
* Sets up the rest of the providers for the application.
*/
protected function setUpProviders()
{
$this
->register(new CsrfServiceProvider())
->register(new FormServiceProvider())
->register(new SecurityServiceProvider(), [
'security.firewalls' => [
'admin' => [
'pattern' => '^/admin',
'http' => true,
],
],
])
;
$this->extend('form.extensions', function ($extensions) {
return $extensions;
});
}
/**
* Sets up the logger for the application.
*/
protected function setUpLogger()
{
if (true === $this->getDebug()) {
$monologLevel = Logger::DEBUG;
} else {
$monologLevel = Logger::INFO;
}
$this->register(new MonologServiceProvider(), [
'monolog.logfile' => $this->getLogDir().'/'.$this->getEnvironment().'.log',
'monolog.level' => $monologLevel,
]);
$this->extend('monolog', function (Logger $monolog, Application $app) {
$streamHandler = new StreamHandler($app['monolog.logfile']);
$streamHandler->setFormatter(new ColoredLineFormatter(new TrafficLight()));
$monolog->pushHandler($streamHandler);
return $monolog;
});
}
/**
* Sets up the mailer for the application.
*/
protected function setUpMailer()
{
$this['swiftmailer.options'] = [
'host' => 'mx.bgemi.net',
'port' => '25',
'username' => null,
'password' => null,
'encryption' => null,
'auth_mode' => null,
];
$this->register(new SwiftmailerServiceProvider());
}
/**
* @return string
*/
protected function getEnvironment(): string
{
$appEnv = getenv('APP_ENV');
if (false === $appEnv) {
return 'development';
} else {
return $appEnv;
}
}
}