Engine
Class
BlackFox\Engine
performs tasks of
management of the connection of the necessary entities,
content generation and wrapper generation for content.
All requests for php files and folders should be redirected to a file
The engine works in two stages:
Constructor
- Loads configuration
- Initializes a session
- Registers method for autoload classes
- Registers method for handling exceptions
- Loads the
BlackFox core - Initializes a global instance of database connection
- Initializes a global instance of cache
Work Method
- Loads information about the requested section
- Initializes the global instance of the current user
- Checks for access to the section
- Generates page content (or content that displays an error that occurred during the generation of page content)
- If required, generates a page wrapper and wraps content in it
Content Generation
The content of the requested page is generated in a pair of
The
The
- Looks for the requested file or the
index.php file in the requested folder - Looks for the
.router.php file up the directory hierarchy (sorting through all the virtual roots) - Searches for a redirect or content page in the database
In case of fail, it throws
Useful properties and methods of the engine
Since the engine is a common repository of all dynamically generated data, it is subject to an exception in terms of allowing modifications to the internal state of the global instance.
Access to the following properties and methods of the engine is carried out:
- in the context of the page - via
$this-> - in the context of a unit - via
$this->ENGINE-> - in other contexts - via
\BlackFox\Engine::I()->
Properties
Title | Type | Description |
---|---|---|
$TITLE | string | Page Title |
$KEYWORDS | string | Page Keywords |
$DESCRIPTION | string | Page Description |
$HEADERS * | structure | Headers to be placed in the html head block |
$TEMPLATE * | string | Template Code |
$WRAPPER | string | Template wrapper code |
$TEMPLATE_PATH * | string | The path to the template folder, relative to the server root, is used to connect the template files |
$SECTION * | dictionary | Data loaded from .section.php file |
$BREADCRUMBS * | dictionary | Breadcrumbs |
$Database | object | Global instance of database |
$Cache | object | Global instance of cache |
$User | object | Global instance of the current user |
Methods
Title | Parameters | Description |
---|---|---|
SetTemplate | $template | Installs a new template ($TEMPLATE), updates the relative path to it ($TEMPLATE_PATH) |
AddHeaderStyle | $path | Adds a style to the headers ($HEADERS) |
AddHeaderScript | $path | Adds a script to headers ($HEADERS) |
AddHeaderString | $string | Adds a custom string to the headers ($HEADERS) |
AddDelayedCall | $callable, $params = [] | Creates a deferred call, returning a unique string, which will later be replaced by the result of the call |
GetHeader | Calls MakeHeader in deferred mode, which allows you to manipulate headers anywhere in the application | |
MakeHeader | Combines the headers ($HEADERS) into a single line, which is printed in the html head block | |
ShowAuthForm | $message | Displays the login form |
ShowErrors | $errors | Displays errors by including the errors.php file in the root of the active template |
Show404 | Displays 404 error | |
Show403 | Displays 403 error | |
SearchAncestorFile | $uri, $filename | Searches for a file up the hierarchy of virtual root directories |
GetRelativePath | $absolute_path, $root_path = null | Converts an absolute path to relative |
GetAbsolutePath | $relative_path | Converts an relative path to absolute |
AddBreadcrumb | $name, $link = null | Adds breadcrumbs to the chain ($BREADCRUMBS) |
GetLanguage | Returns the character code of the current language | |
SetLanguage | $language | Sets the character code of the current language |
Own engine
In order to override behavior of engine methods, you must:
- inherit it to your own core and namespace
- set key 'BlackFox\Engine' in the configuration in the section 'overrides'
- include it in the file
/includes.php (because engine can't find itself until loaded) - launch it in the file
/index.php (optional)
<?php
namespace Site;
class Engine extends \BlackFox\Engine {
public function Show403() {
require($_SERVER['DOCUMENT_ROOT'] . $this->TEMPLATE_PATH . '/403.php');
}
public function Show404() {
require($_SERVER['DOCUMENT_ROOT'] . $this->TEMPLATE_PATH . '/404.php');
}
public function ShowAuthForm($message = null) {
UnitLogin::Run([
'LINK_SUCCESS' => '/profile/',
'LINK_FORGOT' => '/auth/forgot.php',
'LINK_REGISTER' => '/auth/registration.php',
'MESSAGE' => $message,
]);
}
}
<?php
return [
// ...
'overrides' => [
'BlackFox\Engine' => 'Site\Engine',
// ...
],
// ...
];
/includes.php
<?php
require_once('Site/classes/Engine.php');
// ...
/index.php
<?php
require_once("BlackFox/includes.php");
\Site\Engine::I()->Work();