6use Ubiquity\controllers\traits\StartupConfigTrait;
27 protected static function parseUrl(&$url): array {
29 return self::$urlParts = [$url =
'_default'];
31 return self::$urlParts = \explode(
'/', \rtrim($url,
'/'));
35 if (\class_exists($controllerName, true)) {
36 $controller =
new $controllerName ();
38 if (isset (self::$config [
'di']) && \is_array(self::$config [
'di'])) {
39 self::injectDependencies($controller);
48 $templateEngine = $config [
'templateEngine'];
49 $engineOptions = $config [
'templateEngineOptions'] ?? [
'cache' =>
false];
50 $engine =
new $templateEngine ($engineOptions);
52 self::$templateEngine = $engine;
54 }
catch (\Exception $e) {
55 echo $e->getTraceAsString();
60 foreach ($mainParams as $k => $v) {
61 if (\method_exists($controller, $k)) {
74 public static function run(array &$config): void {
76 self::forward($_GET [
'c']);
84 public static function init(array &$config): void {
85 self::$config = $config;
86 if (isset ($config [
'templateEngine'])) {
87 self::startTemplateEngine($config);
89 if (isset ($config [
'sessionName'])) {
90 USession::start($config [
'sessionName']);
92 self::$ctrlNS = self::getNS();
102 public static function forward(
string $url,
bool $initialize =
true,
bool $finalize =
true): void {
103 $u = self::parseUrl($url);
104 if (\is_array(Router::getRoutes()) && ($ru = Router::getRoute($url,
true, self::$config [
'debug'] ??
false)) !==
false) {
105 if (\is_array($ru)) {
106 if (isset ($ru [
'controller'])) {
107 static::runAction($ru, $initialize, $finalize);
109 self::runCallable($ru);
115 $ru = [
'controller' => self::$ctrlNS . $u [0],
'action' => $u [1] ??
'index',
'params' => \array_slice($u, 2)];
116 static::runAction($ru, $initialize, $finalize);
126 $config = self::$config;
127 if (isset ($config [
'templateEngine'])) {
128 $templateEngine = $config [
'templateEngine'];
129 return new $templateEngine ([]);
140 if (self::$templateEngine === null || !self::$templateEngine instanceof \
Ubiquity\views\engine\twig\Twig) {
141 $config = self::$config;
142 $config[
'templateEngine'] = \Ubiquity\views\engine\twig\Twig::class;
143 self::startTemplateEngine($config);
145 return self::$templateEngine;
155 public static function runAction(array &$u,
bool $initialize =
true,
bool $finalize =
true): void {
156 self::$controller = $ctrl = $u [
'controller'];
157 self::$action = $action = $u [
'action'] ??
'index';
158 self::$actionParams = $u[
'params'] ?? [];
161 if (
null !== $controller = self::_getControllerInstance($ctrl)) {
162 if ($mainParams = $u[
'mainParams'] ??
false) {
163 static::setMainParams($controller, $mainParams);
165 if (!$controller->isValid($action)) {
166 $controller->onInvalidControl();
169 $controller->initialize();
172 $controller->$action (...(self::$actionParams));
173 }
catch (\Error $e) {
174 if (!\method_exists($controller, $action)) {
175 static::onError(404,
"This action does not exist on the controller " . $ctrl, $controller);
177 static::logError($e->getCode(), $e->getMessage());
178 if (self::$config [
'debug']) {
181 static::onError(500, $e->getMessage(), $controller);
186 $controller->finalize();
190 Logger::warn(
'Startup',
"The controller `$ctrl` doesn't exist! <br/>",
'runAction');
191 static::onError(404,
"The controller `$ctrl` doesn't exist! <br/>");
193 }
catch (\Error $eC) {
194 static::logError($eC->getCode(), $eC->getMessage());
195 if (self::$config [
'debug']) {
198 static::onError(500, $eC->getMessage());
209 self::$actionParams = $u[
'params'] ?? [];
210 if (isset (self::$config [
'di'])) {
211 $di = self::$config [
'di'];
212 if (\is_array($di)) {
213 self::$actionParams += \array_values($di);
216 $func = $u [
'callback'];
217 $func (...(self::$actionParams));
228 foreach ($di as $k => $v) {
229 $setter =
'set' . \ucfirst($k);
230 if (\method_exists($controller, $setter)) {
231 $controller->$setter ($v ($controller));
233 $controller->$k = $v ($controller);
238 $di = self::$config [
'di'] ?? [];
239 if (isset ($di [
'@exec'])) {
240 foreach ($di [
'@exec'] as $k => $v) {
241 $controller->$k = $v ($controller);
254 public static function runAsString(array &$u,
bool $initialize =
true,
bool $finalize =
true): string {
256 self::runAction($u, $initialize, $finalize);
257 return \ob_get_clean();
260 public static function onError(
int $code, ?
string $message =
null, $controllerInstance =
null) {
261 $onError = self::$config [
'onError'] ?? (
function ($code, $message =
null, $controllerInstance =
null) {
264 self::getHttpInstance()->header(
'HTTP/1.0 404 Not Found',
'',
true, 404);
265 echo $message ??
"The page you are looking for doesn't exist!";
269 echo $message ??
"A server error occurred!";
273 $onError ($code, $message, $controllerInstance);
276 public static function logError(
int $code,
string $message) {
277 if ($code <= E_ERROR) {
278 Logger::critical(
'Startup', $message,
'runAction');
279 } elseif ($code <= E_WARNING) {
280 Logger::error(
'Startup', $message,
'runAction');
282 Logger::warn(
'Startup', $message,
'runAction');
292 return self::$controller;
301 return (new \ReflectionClass (self::$controller))->getShortName();
310 return self::$config [
'templateEngineOptions'][
'fileExt'] ??
'html';
319 return self::$action;
328 return self::$actionParams;
337 return \dirname(__FILE__);
346 return \dirname(\ROOT);
355 return \basename(\dirname(\ROOT));
static getController()
Returns the active controller name.
static runCallable(array &$u)
Runs a callback.
static getControllerSimpleName()
Returns the class simple name of the active controller.
static forward(string $url, bool $initialize=true, bool $finalize=true)
Forwards to url.
static startTemplateEngine(array &$config)
static runAction(array &$u, bool $initialize=true, bool $finalize=true)
Runs an action on a controller.
static logError(int $code, string $message)
static run(array &$config)
Handles the request.
static init(array &$config)
Initialize the app with $config array.
static injectDependencies($controller)
Injects the dependencies from the di config key in a controller.
static setMainParams($controller, $mainParams)
static getTemplateEngineInstance()
Returns the template engine instance.
static _getControllerInstance(string $controllerName)
static getApplicationName()
Returns the application name.
static startDefaultTemplateEngine()
Starts the default Template engine (Twig for Webtools).
static getApplicationDir()
Returns the application directory (app directory)
static runAsString(array &$u, bool $initialize=true, bool $finalize=true)
Runs an action on a controller and returns a string.
static onError(int $code, ?string $message=null, $controllerInstance=null)
static getViewNameFileExtension()
Returns the extension for view files.
static getActionParams()
Returns the active parameters.
static getFrameworkDir()
Returns the framework directory.
static getAction()
Returns tha active action.
Manage dependency injection.
Abstract class for logging Ubiquity\log$Logger This class is part of Ubiquity.
Http Session utilities This class is part of Ubiquity.
Abstract template engine.
Class Configuration \config.