From b52c7302217f41306224bccfe8451b1008562446 Mon Sep 17 00:00:00 2001 From: Awilum Date: Tue, 9 Feb 2021 23:32:39 +0300 Subject: [PATCH] feat(core): use flextype.php as common entry point. --- index.php | 2 +- src/flextype/bootstrap.php | 205 ------------------ .../{dependencies.php => flextype.php} | 204 ++++++++++++++++- tests/Pest.php | 2 +- 4 files changed, 197 insertions(+), 216 deletions(-) delete mode 100755 src/flextype/bootstrap.php rename src/flextype/{dependencies.php => flextype.php} (55%) mode change 100644 => 100755 diff --git a/index.php b/index.php index 8929535d..b0ac1ede 100755 --- a/index.php +++ b/index.php @@ -64,4 +64,4 @@ $flextypeLoader = require_once $flextypeAutoload; * will load up this application so that we can run it and send * the responses back to the browser and delight our users. */ -include __DIR__ . '/src/flextype/bootstrap.php'; +include __DIR__ . '/src/flextype/flextype.php'; diff --git a/src/flextype/bootstrap.php b/src/flextype/bootstrap.php deleted file mode 100755 index bee23617..00000000 --- a/src/flextype/bootstrap.php +++ /dev/null @@ -1,205 +0,0 @@ - [ - 'debug' => $registry->get('flextype.settings.errors.display'), - 'displayErrorDetails' => $registry->get('flextype.settings.display_error_details'), - 'addContentLengthHeader' => $registry->get('flextype.settings.add_content_length_header'), - 'routerCacheFile' => $registry->get('flextype.settings.router_cache_file'), - 'determineRouteBeforeAppMiddleware' => $registry->get('flextype.settings.determine_route_before_app_middleware'), - 'outputBuffering' => $registry->get('flextype.settings.output_buffering'), - 'responseChunkSize' => $registry->get('flextype.settings.response_chunk_size'), - 'httpVersion' => $registry->get('flextype.settings.http_version'), - ], -]); - -/** - * Display Errors - */ -if ($registry->get('flextype.settings.errors.display')) { - $environment = new Environment($_SERVER); - $uri = Uri::createFromEnvironment($environment); - - $prettyPageHandler = new PrettyPageHandler(); - - $prettyPageHandler->setEditor((string) $registry->get('flextype.settings.whoops.editor')); - $prettyPageHandler->setPageTitle((string) $registry->get('flextype.settings.whoops.page_title')); - - $prettyPageHandler->addDataTable('Flextype Application', [ - 'Application Class' => get_class(flextype()), - 'Script Name' => $environment->get('SCRIPT_NAME'), - 'Request URI' => $environment->get('PATH_INFO') ?: '', - ]); - - $prettyPageHandler->addDataTable('Flextype Application (Request)', [ - 'Path' => $uri->getPath(), - 'URL' => (string) $uri, - 'Query String' => $uri->getQuery() ?: '', - 'Scheme' => $uri->getScheme() ?: '', - 'Port' => $uri->getPort() ?: '', - 'Host' => $uri->getHost() ?: '', - ]); - - // Set Whoops to default exception handler - $whoops = new Run(); - $whoops->pushHandler($prettyPageHandler); - - // Enable JsonResponseHandler when request is AJAX - if (Misc::isAjaxRequest()) { - $whoops->pushHandler(new JsonResponseHandler()); - } - - $whoops->register(); - - flextype()->container()['whoops'] = $whoops; -} else { - error_reporting(0); -} - -/** - * Include Dependencies - */ -include_once ROOT_DIR . '/src/flextype/dependencies.php'; - -/** - * Set session options before you start the session - * Standard PHP session configuration options - * https://secure.php.net/manual/en/session.configuration.php - */ -flextype('session')->setOptions(flextype('registry')->get('flextype.settings.session')); - -/** - * Start the session - */ -flextype('session')->start(); - -/** - * Add CSRF (cross-site request forgery) protection service to Flextype container - */ -flextype()->container()['csrf'] = fn() => new Csrf('__csrf_token', '', 128); - -/** - * Set internal encoding - */ -function_exists('mb_language') and mb_language('uni'); -function_exists('mb_regex_encoding') and mb_regex_encoding(flextype('registry')->get('flextype.settings.charset')); -function_exists('mb_internal_encoding') and mb_internal_encoding(flextype('registry')->get('flextype.settings.charset')); - -/** - * Set default timezone - */ -if (in_array(flextype('registry')->get('flextype.settings.timezone'), DateTimeZone::listIdentifiers())) { - date_default_timezone_set(flextype('registry')->get('flextype.settings.timezone')); -} - -/** - * Init shortocodes - * - * Load Flextype Shortcodes from directory /flextype/Support/Parsers/Shortcodes/ based on flextype.settings.parsers.shortcode.shortcodes array - */ -$shortcodes = flextype('registry')->get('flextype.settings.parsers.shortcode.shortcodes'); - -foreach ($shortcodes as $shortcodeName => $shortcode) { - $shortcodeFilePath = ROOT_DIR . '/src/flextype/Support/Parsers/Shortcodes/' . str_replace('_', '', ucwords($shortcodeName, '_')) . 'Shortcode.php'; - if (! file_exists($shortcodeFilePath)) { - continue; - } - - include_once $shortcodeFilePath; -} - -/** - * Init entries fields - * - * Load Flextype Entries fields from directory /flextype/Foundation/Entries/Fields/ based on flextype.settings.entries.fields array - */ -$entryFields = flextype('registry')->get('flextype.settings.entries.fields'); - -foreach ($entryFields as $fieldName => $field) { - $entryFieldFilePath = ROOT_DIR . '/src/flextype/Foundation/Entries/Fields/' . str_replace('_', '', ucwords($fieldName, '_')) . 'Field.php'; - if (! file_exists($entryFieldFilePath)) { - continue; - } - - include_once $entryFieldFilePath; -} - -/** - * Init plugins - */ -flextype('plugins')->init(); - -/** - * Include API ENDPOINTS - */ -include_once ROOT_DIR . '/src/flextype/Endpoints/Utils/errors.php'; -include_once ROOT_DIR . '/src/flextype/Endpoints/Utils/access.php'; -include_once ROOT_DIR . '/src/flextype/Endpoints/entries.php'; -include_once ROOT_DIR . '/src/flextype/Endpoints/registry.php'; -include_once ROOT_DIR . '/src/flextype/Endpoints/media.php'; -include_once ROOT_DIR . '/src/flextype/Endpoints/images.php'; - -/** - * Enable lazy CORS - * - * CORS (Cross-origin resource sharing) allows JavaScript web apps to make HTTP requests to other domains. - * 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. - */ -flextype('cors')->init(); - -/** - * Run high priority event: onFlextypeBeforeRun before Flextype Application starts. - */ -flextype('emitter')->emit('onFlextypeBeforeRun'); - -/** - * Run application - */ -flextype()->run(); diff --git a/src/flextype/dependencies.php b/src/flextype/flextype.php old mode 100644 new mode 100755 similarity index 55% rename from src/flextype/dependencies.php rename to src/flextype/flextype.php index f79e0060..e393a0bb --- a/src/flextype/dependencies.php +++ b/src/flextype/flextype.php @@ -9,11 +9,15 @@ declare(strict_types=1); namespace Flextype; +use Atomastic\Csrf\Csrf; +use Atomastic\Registry\Registry; use Atomastic\Session\Session; use Bnf\Slim3Psr15\CallableResolver; use Cocur\Slugify\Slugify; +use DateTimeZone; use Flextype\Foundation\Cors; use Flextype\Foundation\Entries\Entries; +use Flextype\Foundation\Flextype; use Flextype\Foundation\Media\Media; use Flextype\Foundation\Plugins; use Flextype\Support\Parsers\Parsers; @@ -43,23 +47,108 @@ 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 function date; +use function date_default_timezone_set; +use function error_reporting; use function extension_loaded; +use function file_exists; use function flextype; +use function function_exists; +use function get_class; use function in_array; +use function mb_internal_encoding; +use function mb_language; +use function mb_regex_encoding; +use function str_replace; use function strings; use function sys_get_temp_dir; +use function ucwords; + +/** + * Init Registry + */ +$registry = Registry::getInstance(); + +/** + * Preflight the Flextype + */ +include_once ROOT_DIR . '/src/flextype/preflight.php'; + +/** + * Create new Flextype Application + */ +$flextype = Flextype::getInstance([ + 'settings' => [ + 'debug' => $registry->get('flextype.settings.errors.display'), + 'displayErrorDetails' => $registry->get('flextype.settings.display_error_details'), + 'addContentLengthHeader' => $registry->get('flextype.settings.add_content_length_header'), + 'routerCacheFile' => $registry->get('flextype.settings.router_cache_file'), + 'determineRouteBeforeAppMiddleware' => $registry->get('flextype.settings.determine_route_before_app_middleware'), + 'outputBuffering' => $registry->get('flextype.settings.output_buffering'), + 'responseChunkSize' => $registry->get('flextype.settings.response_chunk_size'), + 'httpVersion' => $registry->get('flextype.settings.http_version'), + ], +]); + +/** + * Display Errors + */ +if ($registry->get('flextype.settings.errors.display')) { + $environment = new Environment($_SERVER); + $uri = Uri::createFromEnvironment($environment); + + $prettyPageHandler = new PrettyPageHandler(); + + $prettyPageHandler->setEditor((string) $registry->get('flextype.settings.whoops.editor')); + $prettyPageHandler->setPageTitle((string) $registry->get('flextype.settings.whoops.page_title')); + + $prettyPageHandler->addDataTable('Flextype Application', [ + 'Application Class' => get_class(flextype()), + 'Script Name' => $environment->get('SCRIPT_NAME'), + 'Request URI' => $environment->get('PATH_INFO') ?: '', + ]); + + $prettyPageHandler->addDataTable('Flextype Application (Request)', [ + 'Path' => $uri->getPath(), + 'URL' => (string) $uri, + 'Query String' => $uri->getQuery() ?: '', + 'Scheme' => $uri->getScheme() ?: '', + 'Port' => $uri->getPort() ?: '', + 'Host' => $uri->getHost() ?: '', + ]); + + // Set Whoops to default exception handler + $whoops = new Run(); + $whoops->pushHandler($prettyPageHandler); + + // Enable JsonResponseHandler when request is AJAX + if (Misc::isAjaxRequest()) { + $whoops->pushHandler(new JsonResponseHandler()); + } + + $whoops->register(); + + flextype()->container()['whoops'] = $whoops; +} else { + error_reporting(0); +} /** * Create a standard session hanndler */ -flextype()->container()['session'] = fn() => new Session(); +flextype()->container()['session'] = static fn () => new Session(); /** * Supply a custom callable resolver, which resolves PSR-15 middlewares. */ -flextype()->container()['callableResolver'] = fn() => new CallableResolver(flextype()->container()); +flextype()->container()['callableResolver'] = static fn () => new CallableResolver(flextype()->container()); /** * Add registry service to Flextype container @@ -79,7 +168,7 @@ flextype()->container()['logger'] = static function () { /** * Add emitter service to Flextype container */ -flextype()->container()['emitter'] = fn() => new Emitter(); +flextype()->container()['emitter'] = static fn () => new Emitter(); /** * Add slugify service to Flextype container @@ -208,12 +297,12 @@ flextype()->container()['cache'] = static function () { /** * Add parsers service to Flextype container */ -flextype()->container()['parsers'] = fn() => new Parsers(); +flextype()->container()['parsers'] = static fn () => new Parsers(); /** * Add serializer service to Flextype container */ -flextype()->container()['serializers'] = fn() => new Serializers(); +flextype()->container()['serializers'] = static fn () => new Serializers(); /** * Add images service to Flextype container @@ -273,19 +362,116 @@ flextype()->container()['images'] = static function () { /** * Add entries service to Flextype container */ -flextype()->container()['entries'] = fn() => new Entries(); +flextype()->container()['entries'] = static fn () => new Entries(); /** * Add media service to Flextype container */ -flextype()->container()['media'] = fn() => new Media(); +flextype()->container()['media'] = static fn () => new Media(); /** * Add plugins service to Flextype container */ -flextype()->container()['plugins'] = fn() => new Plugins(); +flextype()->container()['plugins'] = static fn () => new Plugins(); /** * Add cors service to Flextype container */ -flextype()->container()['cors'] = fn() => new Cors(); +flextype()->container()['cors'] = static fn () => new Cors(); + +/** + * Set session options before you start the session + * Standard PHP session configuration options + * https://secure.php.net/manual/en/session.configuration.php + */ +flextype('session')->setOptions(flextype('registry')->get('flextype.settings.session')); + +/** + * Start the session + */ +flextype('session')->start(); + +/** + * Add CSRF (cross-site request forgery) protection service to Flextype container + */ +flextype()->container()['csrf'] = static fn () => new Csrf('__csrf_token', '', 128); + +/** + * Set internal encoding + */ +function_exists('mb_language') and mb_language('uni'); +function_exists('mb_regex_encoding') and mb_regex_encoding(flextype('registry')->get('flextype.settings.charset')); +function_exists('mb_internal_encoding') and mb_internal_encoding(flextype('registry')->get('flextype.settings.charset')); + +/** + * Set default timezone + */ +if (in_array(flextype('registry')->get('flextype.settings.timezone'), DateTimeZone::listIdentifiers())) { + date_default_timezone_set(flextype('registry')->get('flextype.settings.timezone')); +} + +/** + * Init shortocodes + * + * Load Flextype Shortcodes from directory /flextype/Support/Parsers/Shortcodes/ based on flextype.settings.parsers.shortcode.shortcodes array + */ +$shortcodes = flextype('registry')->get('flextype.settings.parsers.shortcode.shortcodes'); + +foreach ($shortcodes as $shortcodeName => $shortcode) { + $shortcodeFilePath = ROOT_DIR . '/src/flextype/Support/Parsers/Shortcodes/' . str_replace('_', '', ucwords($shortcodeName, '_')) . 'Shortcode.php'; + if (! file_exists($shortcodeFilePath)) { + continue; + } + + include_once $shortcodeFilePath; +} + +/** + * Init entries fields + * + * Load Flextype Entries fields from directory /flextype/Foundation/Entries/Fields/ based on flextype.settings.entries.fields array + */ +$entryFields = flextype('registry')->get('flextype.settings.entries.fields'); + +foreach ($entryFields as $fieldName => $field) { + $entryFieldFilePath = ROOT_DIR . '/src/flextype/Foundation/Entries/Fields/' . str_replace('_', '', ucwords($fieldName, '_')) . 'Field.php'; + if (! file_exists($entryFieldFilePath)) { + continue; + } + + include_once $entryFieldFilePath; +} + +/** + * Init plugins + */ +flextype('plugins')->init(); + +/** + * Include API ENDPOINTS + */ +include_once ROOT_DIR . '/src/flextype/Endpoints/Utils/errors.php'; +include_once ROOT_DIR . '/src/flextype/Endpoints/Utils/access.php'; +include_once ROOT_DIR . '/src/flextype/Endpoints/entries.php'; +include_once ROOT_DIR . '/src/flextype/Endpoints/registry.php'; +include_once ROOT_DIR . '/src/flextype/Endpoints/media.php'; +include_once ROOT_DIR . '/src/flextype/Endpoints/images.php'; + +/** + * Enable lazy CORS + * + * CORS (Cross-origin resource sharing) allows JavaScript web apps to make HTTP requests to other domains. + * 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. + */ +flextype('cors')->init(); + +/** + * Run high priority event: onFlextypeBeforeRun before Flextype Application starts. + */ +flextype('emitter')->emit('onFlextypeBeforeRun'); + +/** + * Run application + */ +flextype()->run(); diff --git a/tests/Pest.php b/tests/Pest.php index 75a0d454..b2f74136 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -15,4 +15,4 @@ define('PATH', [ ! is_file($flextype_autoload = ROOT_DIR . '/vendor/autoload.php') and exit('Please run: composer install for flextype'); $flextype_loader = require_once $flextype_autoload; -include ROOT_DIR . '/src/flextype/bootstrap.php'; +include ROOT_DIR . '/src/flextype/flextype.php';