A super easy PHP Framework for web development! https://github.com/exacti/phacil-framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
2.8 KiB

6 years ago
<?php
/*
* Copyright © 2021 ExacTI Technology Solutions. All rights reserved.
* GPLv3 General License.
* https://exacti.com.br
* Phacil PHP Framework - https://github.com/exacti/phacil-framework
*/
namespace Phacil\Framework;
/** @package Phacil\Framework */
6 years ago
final class Front {
/**
*
* @var Registry
*/
6 years ago
protected $registry;
/**
*
* @var array
*/
6 years ago
protected $pre_action = array();
6 years ago
protected $error;
/**
*
* @param Registry $registry
* @return void
*/
public function __construct(Registry $registry) {
6 years ago
$this->registry = $registry;
}
/**
* @param ActionSystem $pre_action
* @return void
*/
public function addPreAction(\Phacil\Framework\ActionSystem $pre_action) {
6 years ago
$this->pre_action[] = $pre_action;
}
/**
* @param Action $action
* @param Action $error
* @return void
*/
public function dispatch(\Phacil\Framework\Action $action, \Phacil\Framework\Action $error) {
6 years ago
$this->error = $error;
foreach ($this->pre_action as $pre_action) {
$result = $this->execute($pre_action);
if ($result) {
$action = $result;
break;
}
}
while ($action) {
$action = $this->execute($action);
}
}
private function execute(object $action) {
6 years ago
$file = $action->getFile();
$class = $action->getClass();
$classAlt = $action->getClassAlt();
6 years ago
$method = $action->getMethod();
$args = $action->getArgs();
$action = '';
$c = get_declared_classes();
6 years ago
if (file_exists($file)) {
require_once($file);
try {
$controller = new $classAlt['class']($this->registry);
$action->setClass($classAlt['class']);
array_shift($classAlt['class']);
} catch (\Throwable $th) {
foreach($classAlt as $classController){
try {
$controller = new $classController($this->registry);
$action->setClass($classController);
} catch (\Throwable $th) {
//throw $th;
}
}
}
if(!$controller) {
try {
$controller = new $class($this->registry);
} catch (\Throwable $th) {
$e = array_diff(get_declared_classes(), $c);
try {
$classController = end($e);
$controller = new $classController($this->registry);
$action->setClass($classController);
} catch (\Throwable $th) {
foreach($e as $classController){
try {
$controller = new $classController($this->registry);
$action->setClass($classController);
} catch (\Throwable $th) {
//throw $th;
}
}
}
}
}
6 years ago
if (is_callable(array($controller, $method))) {
$action = call_user_func_array(array($controller, $method), $args);
} else {
$action = $this->error;
$this->error = '';
}
} else {
$action = $this->error;
$this->error = '';
}
return $action;
}
}