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.

156 lines
3.7 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;
use Phacil\Framework\Interfaces\Front as frontInterface;
/**
* @package Phacil\Framework
* @since 1.0.0
**/
final class Front implements frontInterface {
/**
*
* @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;
}
/**
3 years ago
* @param \Phacil\Framework\Interfaces\Action $pre_action
* @return void
*/
3 years ago
public function addPreAction(\Phacil\Framework\Interfaces\Action $pre_action) {
6 years ago
$this->pre_action[] = $pre_action;
}
/**
* @param Action $action
* @param string $error
* @return void
*/
3 years ago
public function dispatch(\Phacil\Framework\Interfaces\Action $action, $error) {
6 years ago
$this->error = $error;
$this->registry->set('route', $action->getRoute());
6 years ago
foreach ($this->pre_action as $pre_action) {
$result = $this->execute($pre_action);
if ($result) {
$action = $result;
break;
}
}
while ($action) {
$action = $this->execute($action);
}
}
/**
* @param object $action
* @return \Phacil\Framework\Interfaces\Action|\Phacil\Framework\Interfaces\Controller|null
* @throws Exception
*/
private function execute(\Phacil\Framework\Interfaces\Action $action) {
6 years ago
$file = $action->getFile();
$class = $action->getClass();
$classAlt = $action->getClassAlt();
6 years ago
$method = $action->getMethod();
$args = $action->getArgs();
unset($action);
if ($file && file_exists($file)) {
6 years ago
require_once($file);
foreach($classAlt as $classController){
try {
if(class_exists($classController)){
$action = $this->callController($this->injectionClass($classController), $method, $args);
/* if(!is_subclass_of($controller, 'Phacil\Framework\Controller')){
throw new Exception('PHACIL ERROR: Controller '. get_class($controller) . ' doesn\'t have Phacil\Framework\Controller implemented');
} */
break;
}
} catch (\Phacil\Framework\Exception\Throwable $th) {
throw $th;
}
}
6 years ago
} elseif(!$file && isset($classAlt['class'])) {
try {
$this->injectionClass($classController);
$action = $this->callController(new $classAlt['class']($this->registry), $method, $args);
} catch (\Phacil\Framework\Exception\Throwable $th) {
throw ($th);
}
}else {
$action = new Action($this->error);
6 years ago
$this->error = '';
}
return $action;
}
/**
*
* @param \Phacil\Framework\Interfaces\Controller $controller
* @return \Phacil\Framework\Interfaces\Controller|\Phacil\Framework\Interfaces\Action|null
* @throws \Phacil\Framework\Exception
*/
protected function callController(\Phacil\Framework\Interfaces\Controller $controller, $method, $args = array()) {
try {
//code...
if (is_callable(array($controller, $method))) {
$action = call_user_func_array(array($controller, $method), $args);
} else {
$action = new Action($this->error);
$this->error = '';
//throw new Exception("The controller can't be loaded", 1);
new Exception("PHACIL ERROR: Controller class " . get_class($controller) . "->" . $method . "() is not a callable function");
}
return $action;
} catch (\Exception $th) {
throw new Exception($th->getMessage(), $th->getCode(), $th);
//throw $th;
}
}
protected function injectionClass($class){
return $this->registry->injectionClass($class);
}
6 years ago
}