doughnut-wedding/app/Kernel.php

407 lines
12 KiB
PHP
Raw Normal View History

2017-02-10 21:34:31 -08:00
<?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
};
2017-02-13 09:56:18 -08:00
use Sikofitt\App\Entity\User;
use Sikofitt\App\Traits\EntityManagerTrait;
2017-02-10 21:34:31 -08:00
use Sikofitt\App\Traits\FlashTrait;
2017-02-13 09:56:18 -08:00
use Sikofitt\Security\MySqlUserProvider;
2017-02-10 21:34:31 -08:00
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
{
2017-02-13 09:56:18 -08:00
use EntityManagerTrait;
2017-02-10 21:34:31 -08:00
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();
}
2017-02-13 09:56:18 -08:00
2017-02-10 21:34:31 -08:00
$this->setUpProviders();
$this->setUpDatabase();
$this->setUpView();
$this->setUpLogger();
$this->setUpMailer();
}
2017-03-17 10:30:04 -07:00
2017-02-13 09:56:18 -08:00
public function setUpRoutes(\Kernel $app)
{
}
2017-03-17 10:30:04 -07:00
2017-02-10 21:34:31 -08:00
/**
* @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',
],
]);
2017-03-17 10:30:04 -07:00
//$j = new Doctrine\Common\Cache\FilesystemCache()
2017-02-10 21:34:31 -08:00
$this->register(new DoctrineOrmServiceProvider(), [
2017-03-17 10:30:04 -07:00
'orm.proxies_dir' => $this->getCacheDir().'/doctrine/proxies/Doughnut/Wedding',
'orm.proxies_namespace' => 'Doughnut\Wedding',
'orm.default_cache' => [
'driver' => 'filesystem',
'path' => $this->getCacheDir().'/doctrine/cache',
],
'orm.auto_generate_proxies' => true,
'orm.strategy' => 'naming',
2017-02-10 21:34:31 -08:00
'orm.em.options' => [
'connection' => 'default',
'mappings' => [
[
'type' => 'annotation',
'path' => $this->getBaseDir().'/src/Sikofitt/App/Entity',
'namespace' => 'Sikofitt\App\Entity',
2017-03-17 10:30:04 -07:00
'alias' => 'Sikofitt',
'auto_generate_proxies' => true,
2017-02-10 21:34:31 -08:00
'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.
*/
$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()
{
2017-02-13 09:56:18 -08:00
/*$this['app.mysql_authenticator'] = function($app) {
return new Sikofitt\Security\MysqlAuthenticator($app['security.encoder_factory'], $app->getEntityManager());
};
$this['security.firewalls'] = array(
'login' => [
'pattern' => '^/login$',
'anonymous' => true,
],
'secured' => [
'pattern' => '^/rsvp$',
'guard' => [
'authenticators' => [
'app.mysql_authenticator',
],
'form' => [
'login_path' => '/login',
'check_path' => '/login',
]
],
'users' => $this['users'] = function() {
return new MySqlUserProvider($this['orm.em']);
},
],
// configure where your users come from. Hardcode them, or load them from somewhere
// http://silex.sensiolabs.org/doc/providers/security.html#defining-a-custom-user-provider
// 'anonymous' => true
);*/
2017-03-17 10:30:04 -07:00
$this['protected_pages'] = function () {
2017-02-13 09:56:18 -08:00
return [
'gallery',
2017-03-17 10:30:04 -07:00
'rsvp/update',
2017-02-13 09:56:18 -08:00
];
};
2017-02-10 21:34:31 -08:00
$this
->register(new CsrfServiceProvider())
->register(new FormServiceProvider())
2017-02-13 09:56:18 -08:00
//->register(new SecurityServiceProvider())
2017-02-10 21:34:31 -08:00
;
2017-02-13 09:56:18 -08:00
2017-02-10 21:34:31 -08:00
$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;
}
}
}