From 2c21e22227f0c25712878297b2319bbf5b2f8886 Mon Sep 17 00:00:00 2001 From: Awilum Date: Tue, 11 Jun 2019 19:41:40 +0300 Subject: [PATCH] Flextype Core: General fixes and refactoring #117 --- flextype/bootstrap.php | 243 ++------------------------------------ flextype/dependencies.php | 218 ++++++++++++++++++++++++++++++++++ flextype/middlewares.php | 16 +++ flextype/routes/web.php | 13 ++ 4 files changed, 256 insertions(+), 234 deletions(-) create mode 100644 flextype/dependencies.php create mode 100644 flextype/middlewares.php create mode 100644 flextype/routes/web.php diff --git a/flextype/bootstrap.php b/flextype/bootstrap.php index 881599c5..a5e9cef1 100755 --- a/flextype/bootstrap.php +++ b/flextype/bootstrap.php @@ -15,16 +15,6 @@ namespace Flextype; use Flextype\Component\Session\Session; use Flextype\Component\Registry\Registry; use Flextype\Component\Filesystem\Filesystem; -use Thunder\Shortcode\ShortcodeFacade; -use Psr\Http\Message\ResponseInterface as Response; -use Psr\Http\Message\ServerRequestInterface as Request; -use Psr7Middlewares\Middleware; -use Psr7Middlewares\Middleware\TrailingSlash; -use Slim\Flash\Messages; -use League\Glide\ServerFactory; -use League\Glide\Responses\SlimResponseFactory; -use League\Event\Emitter; -use Cocur\Slugify\Slugify; /** * The version of Flextype @@ -66,64 +56,25 @@ $config = [ $app = new \Slim\App($config); /** - * Set Flextype Dependency Container + * Set Flextype Dependency Injection Container */ $flextype = $app->getContainer(); /** - * Add CSRF (cross-site request forgery) protection service to Flextype container + * Include Dependencies */ -$flextype['csrf'] = function ($container) { - return new \Slim\Csrf\Guard; -}; +include_once 'dependencies.php'; /** - * Add logger + * Include Middlewares */ -$flextype['logger'] = function($container) { - $logger = new \Monolog\Logger('flextype'); - $file_handler = new \Monolog\Handler\StreamHandler(PATH['site'] . '/logs/' . date('Y-m-d') . '.log'); - $logger->pushHandler($file_handler); - return $logger; -}; +include_once 'middlewares.php'; /** - * Add middleware CSRF (cross-site request forgery) protection for all routes + * Include Routes */ -$app->add($flextype->get('csrf')); +include_once 'routes/web.php'; -/** - * Add middleware TrailingSlash for all routes - */ -$app->add((new TrailingSlash(false))->redirect(301)); - -/** - * Add emitter service to Flextype container - */ -$flextype['emitter'] = function ($container) { - return new Emitter(); -}; - -/** - * Add slugify service to Flextype container - */ -$flextype['slugify'] = function ($container) { - return new Slugify(['separator' => '-', 'lowercase' => true, 'trim' => true]); -}; - -/** - * Add flash service to Flextype container - */ -$flextype['flash'] = function ($container) { - return new Messages(); -}; - -/** - * Add registry service to Flextype container - */ -$flextype['registry'] = function ($container) { - return new Registry(); -}; // Set empty settings array $flextype['registry']->set('settings', []); @@ -160,12 +111,6 @@ function_exists('mb_language') and mb_language('uni'); function_exists('mb_regex_encoding') and mb_regex_encoding($flextype['registry']->get('settings.charset')); function_exists('mb_internal_encoding') and mb_internal_encoding($flextype['registry']->get('settings.charset')); -/** - * Set error handler - * - * @access private - */ - // Display Errors if ($flextype['registry']->get('settings.errors.display')) { @@ -181,92 +126,6 @@ if ($flextype['registry']->get('settings.errors.display')) { // Set default timezone date_default_timezone_set($flextype['registry']->get('settings.timezone')); -/** - * Add cache service to Flextype container - */ -$flextype['cache'] = function ($container) use ($flextype) { - return new Cache($flextype); -}; - -/** - * Add images service to Flextype container - */ -$flextype['images'] = function ($container) { - - // Get images settings - $imagesSettings = $container->get('settings')['images']; - - // Set source filesystem - $source = new \League\Flysystem\Filesystem( - new \League\Flysystem\Adapter\Local(PATH['entries']) - ); - - // Set cache filesystem - $cache = new \League\Flysystem\Filesystem( - new \League\Flysystem\Adapter\Local(PATH['cache'] . '/glide') - ); - - // Set watermarks filesystem - $watermarks = new \League\Flysystem\Filesystem( - new \League\Flysystem\Adapter\Local(PATH['site'] . '/watermarks') - ); - - // Set image manager - $imageManager = new \Intervention\Image\ImageManager($imagesSettings); - - // Set manipulators - $manipulators = [ - new \League\Glide\Manipulators\Orientation(), - new \League\Glide\Manipulators\Crop(), - new \League\Glide\Manipulators\Size(2000*2000), - new \League\Glide\Manipulators\Brightness(), - new \League\Glide\Manipulators\Contrast(), - new \League\Glide\Manipulators\Gamma(), - new \League\Glide\Manipulators\Sharpen(), - new \League\Glide\Manipulators\Filter(), - new \League\Glide\Manipulators\Blur(), - new \League\Glide\Manipulators\Pixelate(), - new \League\Glide\Manipulators\Watermark($watermarks), - new \League\Glide\Manipulators\Background(), - new \League\Glide\Manipulators\Border(), - new \League\Glide\Manipulators\Encode(), - ]; - - // Set API - $api = new \League\Glide\Api\Api($imageManager, $manipulators); - - // Setup Glide server - $server = \League\Glide\ServerFactory::create([ - 'source' => $source, - 'cache' => $cache, - 'api' => $api, - 'response' => new SlimResponseFactory(), - ]); - - return $server; -}; - -/** - * Add fieldsets service to Flextype container - */ -$flextype['fieldsets'] = function ($container) use ($flextype) { - return new Fieldsets($flextype); -}; - -/** - * Add snippets service to Flextype container - */ -$flextype['snippets'] = function ($container) use ($flextype) { - return new Snippets($flextype); -}; - -/** - * Add shortcodes service to Flextype container - */ -$flextype['shortcodes'] = function ($container) { - return new ShortcodeFacade(); -}; - // Get Default Shortocdes List $shortcodes_list = Filesystem::listContents(ROOT_DIR . '/flextype/shortcodes'); @@ -276,93 +135,9 @@ foreach ($shortcodes_list as $shortcode) { } /** - * Add entries service to Flextype container + * Init themes */ -$flextype['entries'] = function ($container) { - return new Entries($container); -}; - -/** - * Add view service to Flextype container - */ -$flextype['view'] = function ($container) { - - // Get twig settings - $twigSettings = $container->get('settings')['twig']; - - // Create Twig View - $view = new \Slim\Views\Twig(PATH['site'], $twigSettings); - - // Instantiate - $router = $container->get('router'); - $uri = \Slim\Http\Uri::createFromEnvironment(new \Slim\Http\Environment($_SERVER)); - - // Add Twig Extension - $view->addExtension(new \Slim\Views\TwigExtension($router, $uri)); - - // Add Twig Debug Extension - $view->addExtension(new \Twig\Extension\DebugExtension()); - - // Add Entries Twig Extension - $view->addExtension(new EntriesTwigExtension($container)); - - // Add Emitter Twig Extension - $view->addExtension(new EmitterTwigExtension($container)); - - // Add Flash Twig Extension - $view->addExtension(new FlashTwigExtension($container)); - - // Add I18n Twig Extension - $view->addExtension(new I18nTwigExtension()); - - // Add JsonParser Extension - $view->addExtension(new JsonParserTwigExtension()); - - // Add Filesystem Extension - $view->addExtension(new FilesystemTwigExtension()); - - // Add Assets Twig Extension - $view->addExtension(new AssetsTwigExtension()); - - // Add Csrf Twig Extension - $view->addExtension(new CsrfTwigExtension($container->get('csrf'))); - - // Add Global Vars Twig Extension - $view->addExtension(new GlobalVarsTwigExtension($container)); - - // Add Global Shortcodes Twig Extension - $view->addExtension(new ShortcodesTwigExtension($container)); - - // Return view - return $view; -}; - -/** - * Generates and returns the image response - */ -$app->get('/image/{path:.+}', function (Request $request, Response $response, array $args) use ($flextype) { - return $flextype['images']->getImageResponse($args['path'], $_GET); -}); - -/** - * Add themes service to Flextype container - */ - $flextype['themes'] = function ($container) use ($flextype, $app) { - return new Themes($flextype, $app); - }; - - /** - * Init themes - */ - $flextype['themes']->init($flextype, $app); - - -/** - * Add plugins service to Flextype container - */ -$flextype['plugins'] = function ($container) use ($flextype, $app) { - return new Plugins($flextype, $app); -}; +$flextype['themes']->init($flextype, $app); /** * Init plugins diff --git a/flextype/dependencies.php b/flextype/dependencies.php new file mode 100644 index 00000000..33946053 --- /dev/null +++ b/flextype/dependencies.php @@ -0,0 +1,218 @@ +pushHandler($file_handler); + return $logger; +}; + +/** + * Add emitter service to Flextype container + */ +$flextype['emitter'] = function ($container) { + return new Emitter(); +}; + +/** + * Add slugify service to Flextype container + */ +$flextype['slugify'] = function ($container) { + return new Slugify(['separator' => '-', 'lowercase' => true, 'trim' => true]); +}; + +/** + * Add flash service to Flextype container + */ +$flextype['flash'] = function ($container) { + return new Messages(); +}; + +/** + * Add registry service to Flextype container + */ +$flextype['registry'] = function ($container) { + return new Registry(); +}; + +/** + * Add cache service to Flextype container + */ +$flextype['cache'] = function ($container) use ($flextype) { + return new Cache($flextype); +}; + +/** + * Add images service to Flextype container + */ +$flextype['images'] = function ($container) { + + // Get images settings + $imagesSettings = $container->get('settings')['images']; + + // Set source filesystem + $source = new \League\Flysystem\Filesystem( + new \League\Flysystem\Adapter\Local(PATH['entries']) + ); + + // Set cache filesystem + $cache = new \League\Flysystem\Filesystem( + new \League\Flysystem\Adapter\Local(PATH['cache'] . '/glide') + ); + + // Set watermarks filesystem + $watermarks = new \League\Flysystem\Filesystem( + new \League\Flysystem\Adapter\Local(PATH['site'] . '/watermarks') + ); + + // Set image manager + $imageManager = new \Intervention\Image\ImageManager($imagesSettings); + + // Set manipulators + $manipulators = [ + new \League\Glide\Manipulators\Orientation(), + new \League\Glide\Manipulators\Crop(), + new \League\Glide\Manipulators\Size(2000*2000), + new \League\Glide\Manipulators\Brightness(), + new \League\Glide\Manipulators\Contrast(), + new \League\Glide\Manipulators\Gamma(), + new \League\Glide\Manipulators\Sharpen(), + new \League\Glide\Manipulators\Filter(), + new \League\Glide\Manipulators\Blur(), + new \League\Glide\Manipulators\Pixelate(), + new \League\Glide\Manipulators\Watermark($watermarks), + new \League\Glide\Manipulators\Background(), + new \League\Glide\Manipulators\Border(), + new \League\Glide\Manipulators\Encode(), + ]; + + // Set API + $api = new \League\Glide\Api\Api($imageManager, $manipulators); + + // Setup Glide server + $server = \League\Glide\ServerFactory::create([ + 'source' => $source, + 'cache' => $cache, + 'api' => $api, + 'response' => new SlimResponseFactory(), + ]); + + return $server; +}; + +/** + * Add fieldsets service to Flextype container + */ +$flextype['fieldsets'] = function ($container) use ($flextype) { + return new Fieldsets($flextype); +}; + +/** + * Add snippets service to Flextype container + */ +$flextype['snippets'] = function ($container) use ($flextype) { + return new Snippets($flextype); +}; + +/** + * Add shortcodes service to Flextype container + */ +$flextype['shortcodes'] = function ($container) { + return new ShortcodeFacade(); +}; + +/** + * Add entries service to Flextype container + */ +$flextype['entries'] = function ($container) { + return new Entries($container); +}; + +/** + * Add view service to Flextype container + */ +$flextype['view'] = function ($container) { + + // Get twig settings + $twigSettings = $container->get('settings')['twig']; + + // Create Twig View + $view = new \Slim\Views\Twig(PATH['site'], $twigSettings); + + // Instantiate + $router = $container->get('router'); + $uri = \Slim\Http\Uri::createFromEnvironment(new \Slim\Http\Environment($_SERVER)); + + // Add Twig Extension + $view->addExtension(new \Slim\Views\TwigExtension($router, $uri)); + + // Add Twig Debug Extension + $view->addExtension(new \Twig\Extension\DebugExtension()); + + // Add Entries Twig Extension + $view->addExtension(new EntriesTwigExtension($container)); + + // Add Emitter Twig Extension + $view->addExtension(new EmitterTwigExtension($container)); + + // Add Flash Twig Extension + $view->addExtension(new FlashTwigExtension($container)); + + // Add I18n Twig Extension + $view->addExtension(new I18nTwigExtension()); + + // Add JsonParser Extension + $view->addExtension(new JsonParserTwigExtension()); + + // Add Filesystem Extension + $view->addExtension(new FilesystemTwigExtension()); + + // Add Assets Twig Extension + $view->addExtension(new AssetsTwigExtension()); + + // Add Csrf Twig Extension + $view->addExtension(new CsrfTwigExtension($container->get('csrf'))); + + // Add Global Vars Twig Extension + $view->addExtension(new GlobalVarsTwigExtension($container)); + + // Add Global Shortcodes Twig Extension + $view->addExtension(new ShortcodesTwigExtension($container)); + + // Return view + return $view; +}; + +/** + * Add themes service to Flextype container + */ +$flextype['themes'] = function ($container) use ($flextype, $app) { + return new Themes($flextype, $app); +}; + + /** + * Add plugins service to Flextype container + */ +$flextype['plugins'] = function ($container) use ($flextype, $app) { + return new Plugins($flextype, $app); +}; diff --git a/flextype/middlewares.php b/flextype/middlewares.php new file mode 100644 index 00000000..a2afc711 --- /dev/null +++ b/flextype/middlewares.php @@ -0,0 +1,16 @@ +add($flextype->get('csrf')); + +/** + * Add middleware TrailingSlash for all routes + */ +$app->add((new TrailingSlash(false))->redirect(301)); diff --git a/flextype/routes/web.php b/flextype/routes/web.php new file mode 100644 index 00000000..b8db77d9 --- /dev/null +++ b/flextype/routes/web.php @@ -0,0 +1,13 @@ +get('/image/{path:.+}', function (Request $request, Response $response, array $args) use ($flextype) { + return $flextype['images']->getImageResponse($args['path'], $_GET); +});