1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-05 04:37:43 +02:00

feat(core): use flextype.php as common entry point.

This commit is contained in:
Awilum
2021-02-09 23:32:39 +03:00
parent 7d9cfb9e56
commit b52c730221
4 changed files with 197 additions and 216 deletions

View File

@@ -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';

View File

@@ -1,205 +0,0 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype;
use Atomastic\Csrf\Csrf;
use Atomastic\Session\Session;
use Atomastic\Registry\Registry;
use Flextype\Foundation\Flextype;
use Slim\Http\Environment;
use Slim\Http\Uri;
use Whoops\Handler\JsonResponseHandler;
use Whoops\Handler\PrettyPageHandler;
use Whoops\Run;
use Whoops\Util\Misc;
use DateTimeZone;
use function date_default_timezone_set;
use function error_reporting;
use function file_exists;
use function flextype;
use function function_exists;
use function get_class;
use function mb_internal_encoding;
use function mb_language;
use function mb_regex_encoding;
use function str_replace;
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') ?: '<none>',
]);
$prettyPageHandler->addDataTable('Flextype Application (Request)', [
'Path' => $uri->getPath(),
'URL' => (string) $uri,
'Query String' => $uri->getQuery() ?: '<none>',
'Scheme' => $uri->getScheme() ?: '<none>',
'Port' => $uri->getPort() ?: '<none>',
'Host' => $uri->getHost() ?: '<none>',
]);
// 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();

View File

@@ -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') ?: '<none>',
]);
$prettyPageHandler->addDataTable('Flextype Application (Request)', [
'Path' => $uri->getPath(),
'URL' => (string) $uri,
'Query String' => $uri->getQuery() ?: '<none>',
'Scheme' => $uri->getScheme() ?: '<none>',
'Port' => $uri->getPort() ?: '<none>',
'Host' => $uri->getHost() ?: '<none>',
]);
// 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();

View File

@@ -15,4 +15,4 @@ define('PATH', [
! is_file($flextype_autoload = ROOT_DIR . '/vendor/autoload.php') and exit('Please run: <i>composer install</i> for flextype');
$flextype_loader = require_once $flextype_autoload;
include ROOT_DIR . '/src/flextype/bootstrap.php';
include ROOT_DIR . '/src/flextype/flextype.php';