mirror of
https://github.com/flextype/flextype.git
synced 2025-08-07 13:46:42 +02:00
Flextype Core: General fixes and refactoring #117
This commit is contained in:
@@ -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
|
||||
|
218
flextype/dependencies.php
Normal file
218
flextype/dependencies.php
Normal file
@@ -0,0 +1,218 @@
|
||||
<?php
|
||||
|
||||
namespace Flextype;
|
||||
|
||||
use Flextype\Component\Registry\Registry;
|
||||
use Thunder\Shortcode\ShortcodeFacade;
|
||||
use Slim\Flash\Messages;
|
||||
use League\Glide\ServerFactory;
|
||||
use League\Glide\Responses\SlimResponseFactory;
|
||||
use League\Event\Emitter;
|
||||
use Cocur\Slugify\Slugify;
|
||||
|
||||
/**
|
||||
* Add CSRF (cross-site request forgery) protection service to Flextype container
|
||||
*/
|
||||
$flextype['csrf'] = function ($container) {
|
||||
return new \Slim\Csrf\Guard;
|
||||
};
|
||||
|
||||
/**
|
||||
* Add logger
|
||||
*/
|
||||
$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;
|
||||
};
|
||||
|
||||
/**
|
||||
* 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);
|
||||
};
|
16
flextype/middlewares.php
Normal file
16
flextype/middlewares.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace Flextype;
|
||||
|
||||
use Psr7Middlewares\Middleware;
|
||||
use Psr7Middlewares\Middleware\TrailingSlash;
|
||||
|
||||
/**
|
||||
* Add middleware CSRF (cross-site request forgery) protection for all routes
|
||||
*/
|
||||
$app->add($flextype->get('csrf'));
|
||||
|
||||
/**
|
||||
* Add middleware TrailingSlash for all routes
|
||||
*/
|
||||
$app->add((new TrailingSlash(false))->redirect(301));
|
13
flextype/routes/web.php
Normal file
13
flextype/routes/web.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace Flextype;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
|
||||
/**
|
||||
* 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);
|
||||
});
|
Reference in New Issue
Block a user