diff --git a/app/.env.sample b/app/.env.sample index 0de096a..3e1971f 100644 --- a/app/.env.sample +++ b/app/.env.sample @@ -1,3 +1,4 @@ +DEPLOYMENT_ENV="development" DATABASE_HOST="localhost" DATABASE_USER="slovocast" DATABASE_PASSWORD="Password01" diff --git a/app/src/Bootstrap.php b/app/src/Bootstrap.php index a998d68..6ed4df5 100644 --- a/app/src/Bootstrap.php +++ b/app/src/Bootstrap.php @@ -2,6 +2,7 @@ namespace Slovocast; +use Exception; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; use Slim\App; @@ -14,11 +15,8 @@ use Psr\Http\Message\ResponseFactoryInterface; use DI\Container; use DI\ContainerBuilder; -use Slim\Views\Twig; -use Slim\Views\TwigMiddleware; use Slim\Psr7\Factory\ResponseFactory; -use Slovocast\Routes; use Slovocast\Configuration\SiteInformationSchema; use Slovocast\Configuration\DatabaseConnectionSchema; use Slovocast\Configuration\SessionSchema; @@ -90,11 +88,10 @@ class Bootstrap } /** - * Set up the Container with all of its definitions, as well as - * initialization of configuration. + * Set up the Container with all of its definitions, as well as initialization of configuration. * * @return Container - * @throws \Exception + * @throws Exception */ protected static function initContainer(): Container { @@ -105,9 +102,7 @@ class Bootstrap 'config' => $config, LoggerInterface::class => function() { $logger = new Logger(); - $logger->pushHandler( - new StreamHandler(APP_LOG_DIR, Level::Warning) - ); + $logger->pushHandler(new StreamHandler(APP_LOGS_DIR, Level::Warning)); return $logger; }, @@ -146,7 +141,7 @@ class Bootstrap ); return new MysqlClient($connectionString); }, - + /** * Utility classes */ @@ -177,14 +172,12 @@ class Bootstrap */ protected static function establishRoutes(App $app): void { - Routes::init($app); + Routes::setup($app); } /** - * Taking an instantiated application, sets up all the middleware required - * for the application to run. This appends Middleware in a Global sense, - * not per-route. Per-route middleware will be set in the `establishRoutes` - * method. + * Taking an instantiated application, sets up all the middleware required for the application to run. This appends + * Middleware in a Global sense not per-route. Per-route middleware will be set in the `establishRoutes` method. * * @param App $app * @throws ContainerExceptionInterface @@ -193,30 +186,7 @@ class Bootstrap */ protected static function establishMiddleware(App $app): void { - /** @var ContainerInterface $container */ - $container = $app->getContainer(); - - /** - * @var Configuration $config - */ - $config = $container->get('config'); - - // Twig - $templateCache = false; - $twig = Twig::create(APP_TEMPLATES_DIR, [ 'cache' => $templateCache ]); - - // Add the global variables - $twig->getEnvironment() - ->addGlobal('site_name', $config->get('site.name')); - $twig->getEnvironment() - ->addGlobal('site_description', $config->get('site.description')); - $flash = $container->get(SessionInterface::class)->getFlash(); - $twig->getEnvironment() - ->addGlobal('flash', $flash); - $app->add(TwigMiddleware::create($app, $twig)); - - // Add the error handling middleware - $app->addErrorMiddleware(true, true, true); + Middlewares::setup($app); } /** @@ -226,7 +196,7 @@ class Bootstrap * @throws ContainerExceptionInterface * @throws LoaderError * @throws NotFoundExceptionInterface - * @throws \Exception + * @throws Exception */ public static function init(): App { diff --git a/app/src/Domain/Repository/User/UserRepository.php b/app/src/Domain/Repository/User/UserRepository.php index c988910..37bab0b 100644 --- a/app/src/Domain/Repository/User/UserRepository.php +++ b/app/src/Domain/Repository/User/UserRepository.php @@ -34,9 +34,6 @@ class UserRepository implements UserRepositoryInterface /** * Get a single instance of the User Entity. - * - * @param int $id - * @return User */ public function get(int $id): User { @@ -63,10 +60,6 @@ class UserRepository implements UserRepositoryInterface return $this->userFromQueryResults($results->resultRows[0]); } - /** - * @param User $user - * @return bool True if the User is saved. - */ public function create(User $user): bool { $query = "INSERT INTO users (email, password, name) @@ -99,11 +92,6 @@ class UserRepository implements UserRepositoryInterface return (bool) $results->affectedRows; } - /** - * @param string $email - * @param string $password - * @return bool - */ public function verifyPassword(string $email, string $password): bool { try { diff --git a/app/src/Infrastructure/Middleware.php b/app/src/Infrastructure/Middleware.php deleted file mode 100644 index 8447b69..0000000 --- a/app/src/Infrastructure/Middleware.php +++ /dev/null @@ -1,38 +0,0 @@ -invoke($request, $response, $next); - } - - /** - * To write Middleware, we need to implement this concrete method, and set - * the child class as the Middleware in the Application bootstrapping. - * - * @param Request $request - * @param Response $response - * @param callable $next - * @return Response - */ - abstract public function invoke( - Request $request, - Response $response, - callable $next - ): Response; -} diff --git a/app/src/Middlewares.php b/app/src/Middlewares.php new file mode 100644 index 0000000..0cc8804 --- /dev/null +++ b/app/src/Middlewares.php @@ -0,0 +1,46 @@ +getContainer(); + + /** + * @var Configuration $config + */ + $config = $container->get('config'); + + // Twig + $templateCache = false; + $twig = Twig::create(APP_TEMPLATES_DIR, [ 'cache' => $templateCache ]); + + // Add the global variables + $twig->getEnvironment()->addGlobal('site_name', $config->get('site.name')); + $twig->getEnvironment()->addGlobal('site_description', $config->get('site.description')); + $flash = $container->get(SessionInterface::class)->getFlash(); + $twig->getEnvironment()->addGlobal('flash', $flash); + $app->add(TwigMiddleware::create($app, $twig)); + + // Add the error handling middleware + $app->addErrorMiddleware(true, true, true); + } +} diff --git a/app/src/Routes.php b/app/src/Routes.php index 99e4755..80b4d48 100644 --- a/app/src/Routes.php +++ b/app/src/Routes.php @@ -19,7 +19,7 @@ class Routes * @param App $app Instantiated Application * @return void */ - public static function init(App $app): void + public static function setup(App $app): void { $app->get('/', HomePage::class); $app->get('/healthcheck', HealthCheck::class); diff --git a/app/templates/layouts/components/flash.twig b/app/templates/layouts/components/flash.twig new file mode 100644 index 0000000..1593c13 --- /dev/null +++ b/app/templates/layouts/components/flash.twig @@ -0,0 +1,11 @@ +{% for message in flash.get('notice') %} +
+ {{ message }} +
+{% endfor %} + +{% for message in flash.get('error') %} +
+ {{ message }} +
+{% endfor %} diff --git a/app/templates/layouts/skeleton.twig b/app/templates/layouts/skeleton.twig index 4bbb17f..3d8c0e4 100644 --- a/app/templates/layouts/skeleton.twig +++ b/app/templates/layouts/skeleton.twig @@ -2,7 +2,10 @@ {% block page_title %}{% endblock %}{{ site_name }} - + {% block meta_tags %} + + + {% endblock %}
@@ -11,11 +14,9 @@
- {% for message in flash.get('error') %} -
- {{ message }} -
- {% endfor %} + {% include 'components/flash.twig' %} + + {% block content %}{% endblock %}