App class

Framework\MVC\App

Class App.

Defined in libraries/mvc/src/App.php

Properties #

$services
Array with keys with names of services and their values have arrays where the keys are the names of the instances and the values are the objects.
$isRunning
Tells if the App is running.
$config
The Config instance.
$isCli
Tells if the request is by command line. Updating directly makes it possible to run tests simulating HTTP or CLI.
$debugCollector
The App collector instance that is set when in debug mode.
$defaultServerVars
Variables set in the $_SERVER super-global in command-line requests.

$services

protected static array $services = array ( )

Array with keys with names of services and their values have arrays where the keys are the names of the instances and the values are the objects.

$isRunning

protected static bool $isRunning = false

Tells if the App is running.

$config

protected static ?\Framework\Config\Config $config

The Config instance.

$isCli

protected static ?bool $isCli = NULL

Tells if the request is by command line. Updating directly makes it possible to run tests simulating HTTP or CLI.

$debugCollector

protected static \Framework\MVC\Debug\AppCollector $debugCollector

The App collector instance that is set when in debug mode.

$defaultServerVars

protected static array $defaultServerVars = array ( 'REMOTE_ADDR' => '127.0.0.1', 'REQUEST_METHOD' => 'GET', 'REQUEST_URI' => '/', 'SERVER_PROTOCOL' => 'HTTP/1.1', 'HTTP_HOST' => 'localhost', )

Variables set in the $_SERVER super-global in command-line requests.

Methods #

__construct()
Initialize the App.
addDebugData()
Add services data to the debug collector.
antiCsrf()
Get a antiCsrf service.
autoloader()
Get a autoloader service.
cache()
Get a cache service.
config()
Get the Config instance.
console()
Get a console service.
database()
Get a database service.
debugger()
Get a debugger service.
exceptionHandler()
Get a exceptionHandler service.
getService()
Get a service.
isCli()
Tell if it is a command-line request.
isDebugging()
Tell if the App is in debug mode.
language()
Get a language service.
locator()
Get a locator service.
logger()
Get a logger service.
mailer()
Get a mailer service.
migrator()
Get a migrator service.
removeService()
Remove services.
request()
Get a request service.
response()
Get a response service.
router()
Get a router service.
run()
Detects if the request is via command-line and runs as a CLI request, otherwise runs as HTTP.
runCli()
Run the App on CLI requests.
runHttp()
Run the App on HTTP requests.
session()
Get a session service.
setIsCli()
Set if it is a CLI request. Used for testing.
setService()
Set a service.
validation()
Get a validation service.
view()
Get a view service.
addCommand()
Detects if the file has a command and adds it to the console.
debugEnd()
Ends the debugging of the App and prints the debugbar if there is no download file, if the request is not via AJAX and the Content-Type is text/html.
debugStart()
Start debugging the App.
loadAutoloader()
Make sure to load the autoloader service if its default config is set.
loadConfigs()
Load service configs catching exceptions.
loadExceptionHandler()
Make sure to load the exceptionHandler service if its default config is set.
negotiateLanguage()
Negotiates the language either via the command line or over HTTP.
prepareToRun()
Prepare the App to run via CLI or HTTP.
requireRouterFiles()
Load files that set the routes.
setAntiCsrf()
Set a antiCsrf service.
setAutoloader()
Set a autoloader service.
setCache()
Set a cache service.
setConsole()
Set a console service.
setDatabase()
Set a database service.
setDebugger()
Set a debugger service.
setExceptionHandler()
Set a exceptionHandler service.
setLanguage()
Set a language service.
setLocator()
Set a locator service.
setLogger()
Set a logger service.
setMailer()
Set a mailer service.
setMigrator()
Set a migrator service.
setRequest()
Set a request service.
setResponse()
Set a response service.
setRouter()
Set a router service.
setServerVars()
Overrides variables to be set in the $_SERVER super-global when the request is made via the command line.
setSession()
Set a session service.
setValidation()
Set a validation service.
setView()
Set a view service.

__construct()

public function __construct(\Framework\Config\Config|array|string|null $config = NULL, bool $debug = false)

Initialize the App.

Parameters
  • \Framework\Config\Config|array|string|null $config - The config
  • bool $debug - Set true to enable debug mode. False to disable.

addDebugData()

public static function addDebugData(string $service, string $instance, float $start, float $end): void

Add services data to the debug collector.

Parameters
  • string $service - Service name
  • string $instance - Service instance name
  • float $start - Microtime right before setting up the service
  • float $end - Microtime right after setting up the service
Returns void

antiCsrf()

public static function antiCsrf(string $instance = 'default'): \Framework\HTTP\AntiCSRF

Get a antiCsrf service.

Parameters
  • string $instance - The antiCsrf instance name
Returns \Framework\HTTP\AntiCSRF

autoloader()

public static function autoloader(string $instance = 'default'): \Framework\Autoload\Autoloader

Get a autoloader service.

Parameters
  • string $instance - The autoloader instance name
Returns \Framework\Autoload\Autoloader

cache()

public static function cache(string $instance = 'default'): \Framework\Cache\Cache

Get a cache service.

Parameters
  • string $instance - The cache instance name
Returns \Framework\Cache\Cache

config()

public static function config(): \Framework\Config\Config

Get the Config instance.

Returns \Framework\Config\Config

console()

public static function console(string $instance = 'default'): \Framework\CLI\Console

Get a console service.

Parameters
  • string $instance - The console instance name
Returns \Framework\CLI\Console
throws
ReflectionException

database()

public static function database(string $instance = 'default'): \Framework\Database\Database

Get a database service.

Parameters
  • string $instance - The database instance name
Returns \Framework\Database\Database

debugger()

public static function debugger(string $instance = 'default'): \Framework\Debug\Debugger

Get a debugger service.

Parameters
  • string $instance - The debugger instance name
Returns \Framework\Debug\Debugger

exceptionHandler()

public static function exceptionHandler(string $instance = 'default'): \Framework\Debug\ExceptionHandler

Get a exceptionHandler service.

Parameters
  • string $instance - The exceptionHandler instance name
Returns \Framework\Debug\ExceptionHandler

getService()

public static function getService(string $name, string $instance = 'default'): ?object

Get a service.

Parameters
  • string $name - Service name
  • string $instance - Service instance name
Returns ?object - The service object or null

isCli()

public static function isCli(): bool

Tell if it is a command-line request.

Returns bool

isDebugging()

public static function isDebugging(): bool

Tell if the App is in debug mode.

Returns bool

language()

public static function language(string $instance = 'default'): \Framework\Language\Language

Get a language service.

Parameters
  • string $instance - The language instance name
Returns \Framework\Language\Language

locator()

public static function locator(string $instance = 'default'): \Framework\Autoload\Locator

Get a locator service.

Parameters
  • string $instance - The locator instance name
Returns \Framework\Autoload\Locator

logger()

public static function logger(string $instance = 'default'): \Framework\Log\Logger

Get a logger service.

Parameters
  • string $instance - The logger instance name
Returns \Framework\Log\Logger

mailer()

public static function mailer(string $instance = 'default'): \Framework\Email\Mailer

Get a mailer service.

Parameters
  • string $instance - The mailer instance name
Returns \Framework\Email\Mailer

migrator()

public static function migrator(string $instance = 'default'): \Framework\Database\Extra\Migrator

Get a migrator service.

Parameters
  • string $instance - The migrator instance name
Returns \Framework\Database\Extra\Migrator

removeService()

public static function removeService(string $name, ?string $instance = 'default'): void

Remove services.

Parameters
  • string $name - Service name
  • ?string $instance - Service instance name or null to remove all instances
Returns void

request()

public static function request(string $instance = 'default'): \Framework\HTTP\Request

Get a request service.

Parameters
  • string $instance - The request instance name
Returns \Framework\HTTP\Request

response()

public static function response(string $instance = 'default'): \Framework\HTTP\Response

Get a response service.

Parameters
  • string $instance - The response instance name
Returns \Framework\HTTP\Response

router()

public static function router(string $instance = 'default'): \Framework\Routing\Router

Get a router service.

Parameters
  • string $instance - The router instance name
Returns \Framework\Routing\Router

run()

public function run(): void

Detects if the request is via command-line and runs as a CLI request, otherwise runs as HTTP.

Returns void

runCli()

public function runCli(): void

Run the App on CLI requests.

Returns void

runHttp()

public function runHttp(): void

Run the App on HTTP requests.

Returns void

session()

public static function session(string $instance = 'default'): \Framework\Session\Session

Get a session service.

Parameters
  • string $instance - The session instance name
Returns \Framework\Session\Session

setIsCli()

public static function setIsCli(bool $is): void

Set if it is a CLI request. Used for testing.

Parameters
  • bool $is
Returns void

setService()

public static function setService(string $name, object $service, string $instance = 'default'): object

Set a service.

Parameters
  • string $name - Service name
  • object $service - Service object
  • string $instance - Service instance name
Returns object - The service object

validation()

public static function validation(string $instance = 'default'): \Framework\Validation\Validation

Get a validation service.

Parameters
  • string $instance - The validation instance name
Returns \Framework\Validation\Validation

view()

public static function view(string $instance = 'default'): \Framework\MVC\View

Get a view service.

Parameters
  • string $instance - The view instance name
Returns \Framework\MVC\View

addCommand()

protected static function addCommand(string $file, \Framework\CLI\Console $console, \Framework\Autoload\Locator $locator): bool

Detects if the file has a command and adds it to the console.

Parameters
  • string $file - The file to get the command class
  • \Framework\CLI\Console $console - The console to add the class
  • \Framework\Autoload\Locator $locator - The locator to get the class name in the file
Returns bool - True if the command was added. If not, it's false.
throws
ReflectionException

debugEnd()

protected function debugEnd(): void

Ends the debugging of the App and prints the debugbar if there is no download file, if the request is not via AJAX and the Content-Type is text/html.

Returns void

debugStart()

protected function debugStart(): void

Start debugging the App.

Returns void

loadAutoloader()

protected function loadAutoloader(): void

Make sure to load the autoloader service if its default config is set.

Returns void

loadConfigs()

protected function loadConfigs(string $name): ?array

Load service configs catching exceptions.

Parameters
  • string $name - The service name
Returns ?array - The service configs or null

loadExceptionHandler()

protected function loadExceptionHandler(): void

Make sure to load the exceptionHandler service if its default config is set.

Returns void

negotiateLanguage()

protected static function negotiateLanguage(\Framework\Language\Language $language, string $requestInstance = 'default'): string

Negotiates the language either via the command line or over HTTP.

Parameters
  • \Framework\Language\Language $language - The current Language instance
  • string $requestInstance - The name of the Request instance to be used
Returns string - The negotiated language

prepareToRun()

protected function prepareToRun(): void

Prepare the App to run via CLI or HTTP.

Returns void

requireRouterFiles()

protected static function requireRouterFiles(array $files, \Framework\Routing\Router $router): void

Load files that set the routes.

Parameters
  • array $files - The path of the router files
  • \Framework\Routing\Router $router
Returns void

setAntiCsrf()

protected static function setAntiCsrf(string $instance): \Framework\HTTP\AntiCSRF

Set a antiCsrf service.

Parameters
  • string $instance - The antiCsrf instance name
Returns \Framework\HTTP\AntiCSRF

setAutoloader()

protected static function setAutoloader(string $instance): \Framework\Autoload\Autoloader

Set a autoloader service.

Parameters
  • string $instance - The autoloader instance name
Returns \Framework\Autoload\Autoloader

setCache()

protected static function setCache(string $instance): \Framework\Cache\Cache

Set a cache service.

Parameters
  • string $instance - The cache instance name
Returns \Framework\Cache\Cache

setConsole()

protected static function setConsole(string $instance): \Framework\CLI\Console

Set a console service.

Parameters
  • string $instance - The console instance name
Returns \Framework\CLI\Console
throws
ReflectionException

setDatabase()

protected static function setDatabase(string $instance, ?\Framework\Database\Debug\DatabaseCollector $collector = NULL): \Framework\Database\Database

Set a database service.

Parameters
  • string $instance - The database instance name
  • ?\Framework\Database\Debug\DatabaseCollector $collector
Returns \Framework\Database\Database

setDebugger()

protected static function setDebugger(string $instance): \Framework\Debug\Debugger

Set a debugger service.

Parameters
  • string $instance - The debugger instance name
Returns \Framework\Debug\Debugger

setExceptionHandler()

protected static function setExceptionHandler(string $instance): \Framework\Debug\ExceptionHandler

Set a exceptionHandler service.

Parameters
  • string $instance - The exceptionHandler instance name
Returns \Framework\Debug\ExceptionHandler

setLanguage()

protected static function setLanguage(string $instance): \Framework\Language\Language

Set a language service.

Parameters
  • string $instance - The language instance name
Returns \Framework\Language\Language

setLocator()

protected static function setLocator(string $instance): \Framework\Autoload\Locator

Set a locator service.

Parameters
  • string $instance - The locator instance name
Returns \Framework\Autoload\Locator

setLogger()

protected static function setLogger(string $instance): \Framework\Log\Logger

Set a logger service.

Parameters
  • string $instance - The logger instance name
Returns \Framework\Log\Logger

setMailer()

protected static function setMailer(string $instance): \Framework\Email\Mailer

Set a mailer service.

Parameters
  • string $instance - The mailer instance name
Returns \Framework\Email\Mailer

setMigrator()

protected static function setMigrator(string $instance): \Framework\Database\Extra\Migrator

Set a migrator service.

Parameters
  • string $instance - The migrator instance name
Returns \Framework\Database\Extra\Migrator

setRequest()

protected static function setRequest(string $instance): \Framework\HTTP\Request

Set a request service.

Parameters
  • string $instance - The request instance name
Returns \Framework\HTTP\Request

setResponse()

protected static function setResponse(string $instance): \Framework\HTTP\Response

Set a response service.

Parameters
  • string $instance - The response instance name
Returns \Framework\HTTP\Response

setRouter()

protected static function setRouter(string $instance, ?array $config = NULL): \Framework\Routing\Router

Set a router service.

Parameters
  • string $instance - The router instance name
  • ?array $config - The router instance configs or null
Returns \Framework\Routing\Router

setServerVars()

protected static function setServerVars(array $vars = array ( )): void

Overrides variables to be set in the $_SERVER super-global when the request is made via the command line.

Parameters
  • array $vars
Returns void

setSession()

protected static function setSession(string $instance): \Framework\Session\Session

Set a session service.

Parameters
  • string $instance - The session instance name
Returns \Framework\Session\Session

setValidation()

protected static function setValidation(string $instance): \Framework\Validation\Validation

Set a validation service.

Parameters
  • string $instance - The validation instance name
Returns \Framework\Validation\Validation

setView()

protected static function setView(string $instance): \Framework\MVC\View

Set a view service.

Parameters
  • string $instance - The view instance name
Returns \Framework\MVC\View