Implemented routerLoadControllerEvent.
Now controller loading can be intercepted and changed if needed. Part of CodeIgniter compatibility module. Useful for other things.
This commit is contained in:
parent
5bc326b5e1
commit
deef1879aa
|
@ -0,0 +1,100 @@
|
|||
<?php
|
||||
/**
|
||||
* FuzeWorks.
|
||||
*
|
||||
* The FuzeWorks MVC PHP FrameWork
|
||||
*
|
||||
* Copyright (C) 2015 TechFuze
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* @author TechFuze
|
||||
* @copyright Copyright (c) 2013 - 2016, Techfuze. (http://techfuze.net)
|
||||
* @copyright Copyright (c) 1996 - 2015, Free Software Foundation, Inc. (http://www.fsf.org/)
|
||||
* @license http://opensource.org/licenses/GPL-3.0 GPLv3 License
|
||||
*
|
||||
* @link http://fuzeworks.techfuze.net
|
||||
* @since Version 0.0.1
|
||||
*
|
||||
* @version Version 0.0.1
|
||||
*/
|
||||
|
||||
namespace FuzeWorks\Event;
|
||||
|
||||
use FuzeWorks\Event;
|
||||
|
||||
/**
|
||||
* Event that gets fired when a controller is loaded.
|
||||
*
|
||||
* Use this to cancel the loading of a controller, or change the details of what is loaded.
|
||||
*
|
||||
* Currently only used by Router::defaultCallable();
|
||||
*
|
||||
* @author Abel Hoogeveen <abel@techfuze.net>
|
||||
* @copyright Copyright (c) 2013 - 2016, Techfuze. (http://techfuze.net)
|
||||
*/
|
||||
class RouterLoadControllerEvent extends Event
|
||||
{
|
||||
/**
|
||||
* The directory where the controller is located
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $directory;
|
||||
|
||||
/**
|
||||
* The file in which the controller is found
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $file;
|
||||
|
||||
/**
|
||||
* The class of the controller
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $className;
|
||||
|
||||
/**
|
||||
* The name of the controller
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $controller;
|
||||
|
||||
/**
|
||||
* The function that will be loaded in the controller
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $function;
|
||||
|
||||
/**
|
||||
* The parameters that will be provided to the function in the controller
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
public $parameters;
|
||||
|
||||
public function init($file, $directory, $className, $controller, $function, $parameters)
|
||||
{
|
||||
$this->file = $file;
|
||||
$this->directory = $directory;
|
||||
$this->className = $className;
|
||||
$this->controller = $controller;
|
||||
$this->function = $function;
|
||||
$this->parameters = $parameters;
|
||||
}
|
||||
}
|
|
@ -350,6 +350,8 @@ class Router
|
|||
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
self::$callable = $event->callable;
|
||||
}
|
||||
|
||||
// And log the input to the logger
|
||||
|
@ -386,21 +388,31 @@ class Router
|
|||
|
||||
// Construct file paths and classes
|
||||
$class = '\Application\Controller\\'.ucfirst($controller);
|
||||
$file = 'Application/Controller/controller.'.$controller.'.php';
|
||||
$directory = 'Application/Controller/';
|
||||
$file = $directory . 'controller.'.$controller.'.php';
|
||||
|
||||
Logger::log('Loading controller '.$class.' from file: '.$file);
|
||||
$event = Events::fireEvent('routerLoadControllerEvent',
|
||||
$file,
|
||||
$directory,
|
||||
$class,
|
||||
$controller,
|
||||
$function,
|
||||
$parameters
|
||||
);
|
||||
|
||||
Logger::log('Loading controller '.$event->className.' from file: '.$event->file);
|
||||
|
||||
// Check if the file exists
|
||||
if (file_exists($file)) {
|
||||
if (!class_exists($class)) {
|
||||
include $file;
|
||||
if (file_exists($event->file)) {
|
||||
if (!class_exists($event->className)) {
|
||||
include $event->file;
|
||||
}
|
||||
|
||||
// Get the path the controller should know about
|
||||
$path = substr(self::getPath(), ($pos = strpos(self::getPath(), '/')) !== false ? $pos + 1 : 0);
|
||||
|
||||
// And create the controller
|
||||
self::$callable = new $class($path);
|
||||
self::$callable = new $event->className($path);
|
||||
|
||||
// If the controller does not want a function to be loaded, provide a halt parameter.
|
||||
if (isset(self::$callable->halt)) {
|
||||
|
@ -408,17 +420,17 @@ class Router
|
|||
}
|
||||
|
||||
// Check if method exists or if there is a caller function
|
||||
if (method_exists(self::$callable, $function) || method_exists(self::$callable, '__call')) {
|
||||
if (method_exists(self::$callable, $event->function) || method_exists(self::$callable, '__call')) {
|
||||
// Execute the function on the controller
|
||||
echo self::$callable->{$function}($parameters);
|
||||
echo self::$callable->{$event->function}($event->parameters);
|
||||
} else {
|
||||
// Function could not be found
|
||||
Logger::log('Could not find function '.$function.' on controller '.$class);
|
||||
Logger::log('Could not find function '.$event->function.' on controller '.$event->className);
|
||||
Logger::http_error(404);
|
||||
}
|
||||
} else {
|
||||
// Controller could not be found
|
||||
Logger::log('Could not find controller '.$class);
|
||||
Logger::log('Could not find controller '.$event->className);
|
||||
Logger::http_error(404);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue