. */ 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\Entity\User; use Sikofitt\App\Traits\EntityManagerTrait; use Sikofitt\App\Traits\FlashTrait; use Sikofitt\Security\MySqlUserProvider; 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 EntityManagerTrait; 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(); } public function setUpRoutes(\Kernel $app) { } /** * @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', ], ]); //$j = new Doctrine\Common\Cache\FilesystemCache() $this->register(new DoctrineOrmServiceProvider(), [ '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', 'orm.em.options' => [ 'connection' => 'default', 'mappings' => [ [ 'type' => 'annotation', 'path' => $this->getBaseDir().'/src/Sikofitt/App/Entity', 'namespace' => 'Sikofitt\App\Entity', 'alias' => 'Sikofitt', 'auto_generate_proxies' => true, '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() { /*$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 );*/ $this['protected_pages'] = function () { return [ 'gallery', 'rsvp/update', ]; }; $this ->register(new CsrfServiceProvider()) ->register(new FormServiceProvider()) //->register(new SecurityServiceProvider()) ; $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; } } }