From ae6138b40b103e4db398b72ea99ef29843393168 Mon Sep 17 00:00:00 2001 From: Awilum Date: Thu, 29 Jul 2021 16:56:42 +0300 Subject: [PATCH] feat(flextype): code standard updates #199 --- src/flextype/flextype.php | 95 ++++++++++++++++++++------------------- 1 file changed, 50 insertions(+), 45 deletions(-) diff --git a/src/flextype/flextype.php b/src/flextype/flextype.php index 65ed9e23c..a4bcedc9e 100644 --- a/src/flextype/flextype.php +++ b/src/flextype/flextype.php @@ -9,31 +9,11 @@ namespace Flextype\Foundation; -use Exception; -use DI\Bridge\Slim\Bridge; -use DI\Container; -use Middlewares\Whoops; -use Slim\App; -use Slim\Middleware\ContentLengthMiddleware; -use Slim\Middleware\OutputBufferingMiddleware; -use Slim\Middleware\RoutingMiddleware; -use Slim\Middleware\ErrorMiddleware; -use Slim\Psr7\Factory\StreamFactory; use Atomastic\Csrf\Csrf; -use Atomastic\Registry\Registry; use Atomastic\Session\Session; use Cocur\Slugify\Slugify; use DateTimeZone; -use Flextype\Foundation\Actions; -use Psr\Http\Message\ResponseInterface as Response; -use Psr\Http\Message\ServerRequestInterface as Request; -use Psr\Http\Server\RequestHandlerInterface; -use Psr\Container\ContainerInterface; -use Bnf\Slim3Psr15\CallableResolver; -use Flextype\Foundation\Cors; use Flextype\Foundation\Entries\Entries; -use Flextype\Foundation\Flextype; -use Flextype\Foundation\Plugins; use Flextype\Support\Parsers\Parsers; use Flextype\Support\Serializers\Serializers; use Intervention\Image\ImageManager; @@ -55,20 +35,49 @@ use League\Glide\Manipulators\Sharpen; use League\Glide\Manipulators\Size; use League\Glide\Manipulators\Watermark; -use League\Glide\Responses\SlimResponseFactory; +use League\Glide\Responses\PsrResponseFactory; use League\Glide\ServerFactory; use Monolog\Handler\StreamHandler; use Monolog\Logger; use Phpfastcache\Drivers\Apcu\Config; use Phpfastcache\Helper\Psr16Adapter as Cache; -use Slim\Http\Environment; -use Slim\Http\Uri; -use Whoops\Handler\JsonResponseHandler; -use Whoops\Handler\PrettyPageHandler; -use Whoops\Run; -use Whoops\Util\Misc; +use Psr\Http\Message\ResponseInterface as Response; +use Psr\Http\Message\ServerRequestInterface as Request; +use Psr\Http\Server\RequestHandlerInterface; +use Slim\Middleware\ContentLengthMiddleware; +use Slim\Middleware\OutputBufferingMiddleware; +use Slim\Middleware\RoutingMiddleware; +use Slim\Psr7\Factory\StreamFactory; +use Slim\Psr7\Stream; use Symfony\Component\Yaml\Yaml as SymfonyYaml; +use function app; +use function array_replace_recursive; +use function container; +use function count; +use function date; +use function date_default_timezone_set; +use function emitter; +use function entries; +use function extension_loaded; +use function file_exists; +use function filemtime; +use function filesystem; +use function flextype; +use function function_exists; +use function implode; +use function in_array; +use function mb_internal_encoding; +use function mb_language; +use function mb_regex_encoding; +use function md5; +use function registry; +use function session; +use function strings; +use function sys_get_temp_dir; +use function trim; +use function var_export; + // Init Flextype flextype(); @@ -92,7 +101,7 @@ $cacheID = md5($flextypeManifestFilePath . $defaultFlextypeSettingsFilePath . $customFlextypeSettingsFilePath . $f1 . $f2 . $f3); if (filesystem()->file($preflightFlextypePath . '/' . $cacheID . '.php')->exists()) { - $flextypeData = include($preflightFlextypePath . '/' . $cacheID . '.php'); + $flextypeData = include $preflightFlextypePath . '/' . $cacheID . '.php'; } else { // Set settings if Flextype Default settings config files exist if (! filesystem()->file($defaultFlextypeSettingsFilePath)->exists()) { @@ -135,7 +144,7 @@ // Merge flextype default settings with custom project settings. $flextypeData = array_replace_recursive($defaultFlextypeSettings, $customFlextypeSettings, $flextypeManifest); - filesystem()->file($preflightFlextypePath . $cacheID . '.php')->put("file($preflightFlextypePath . $cacheID . '.php')->put("add(new OutputBufferingMiddleware(new StreamFactory(), OutputBufferingMiddleware::PREPEND)); break; - + case 'append': default: app()->add(new OutputBufferingMiddleware(new StreamFactory(), OutputBufferingMiddleware::APPEND)); @@ -198,7 +207,7 @@ ])); // Add Cache service -container()->set('cache', static function () { +container()->set('cache', function () { $driverName = registry()->get('flextype.settings.cache.driver'); $config = []; @@ -262,7 +271,7 @@ function getDriverConfig(string $driverName): array break; case 'phparray': $config = new \Phpfastcache\Drivers\Phparray\Config(getDriverConfig($driverName)); - + break; case 'leveldb': $config = new \Phpfastcache\Drivers\Leveldb\Config(getDriverConfig($driverName)); @@ -318,8 +327,7 @@ function getDriverConfig(string $driverName): array container()->set('serializers', new Serializers()); // Add Images service -container()->set('images', static function () { - +container()->set('images', function () { // Get images settings $imagesSettings = ['driver' => registry()->get('flextype.settings.media.image.driver')]; @@ -366,15 +374,14 @@ function getDriverConfig(string $driverName): array $server = ServerFactory::create([ 'source' => $source, 'cache' => $cache, - 'api' => $api + 'api' => $api, ]); - + $server->setResponseFactory( - new \League\Glide\Responses\PsrResponseFactory( + new PsrResponseFactory( new \Slim\Psr7\Response(), - function ($stream) - { - return new \Slim\Psr7\Stream($stream); + function ($stream) { + return new Stream($stream); } ) ); @@ -417,7 +424,6 @@ function_exists('mb_internal_encoding') and mb_internal_encoding(registry()->get // This is important for third party web apps using Flextype, as without CORS, a JavaScript app hosted on example.com // couldn't access our APIs because they're hosted on another.com which is a different domain. if (registry()->get('flextype.settings.cors.enabled')) { - // Allow preflight requests for all routes. app()->options('/{routes:.+}', function (ServerRequestInterface $request, ResponseInterface $response) { return $response; @@ -425,7 +431,6 @@ function_exists('mb_internal_encoding') and mb_internal_encoding(registry()->get // Add headers app()->add(function (Request $request, RequestHandlerInterface $handler): Response { - $response = $handler->handle($request); // Set variables @@ -441,17 +446,17 @@ function_exists('mb_internal_encoding') and mb_internal_encoding(registry()->get ->withHeader('Access-Control-Allow-Methods', $methods) ->withHeader('Access-Control-Allow-Expose', $expose) ->withHeader('Access-Control-Allow-Credentials', $credentials); - }); } // Define app routes app()->get('/hello/{name}', function ($name, Request $request, Response $response) { $response->getBody()->write("Hello, $name"); + return $response; }); -$entries = entries()->fetch('', ["collection" => true]); +$entries = entries()->fetch('', ['collection' => true]); // Add Routing Middleware app()->addRoutingMiddleware(); @@ -460,4 +465,4 @@ function_exists('mb_internal_encoding') and mb_internal_encoding(registry()->get emitter()->emit('onFlextypeBeforeRun'); // Run Flextpype Application -app()->run(); \ No newline at end of file +app()->run();