ORIENT/modules/backend/classes/BackendController.php

127 lines
4.0 KiB
PHP
Raw Normal View History

2014-05-14 13:24:20 +00:00
<?php namespace Backend\Classes;
use Str;
use App;
use File;
use Config;
2015-02-04 08:31:41 +00:00
use Illuminate\Routing\Controller as ControllerBase;
2014-05-14 13:24:20 +00:00
use October\Rain\Router\Helper as RouterHelper;
/**
* The Backend controller class.
* The base controller services back end pages.
*
* @package october\backend
* @author Alexey Bobkov, Samuel Georges
*/
class BackendController extends ControllerBase
{
/**
* @var string Allows early access to page action.
*/
public static $action;
/**
* @var array Allows early access to page parameters.
*/
public static $params;
/**
* Finds and serves the requested backend controller.
* If the controller cannot be found, returns the Cms page with the URL /404.
* If the /404 page doesn't exist, returns the system 404 page.
* @param string $url Specifies the requested page URL.
* If the parameter is omitted, the current URL used.
* @return string Returns the processed page content.
*/
public function run($url = null)
{
$params = RouterHelper::segmentizeUrl($url);
/*
* Look for a Module controller
*/
$module = isset($params[0]) ? $params[0] : 'backend';
$controller = isset($params[1]) ? $params[1] : 'index';
2014-08-08 12:02:46 +00:00
self::$action = $action = isset($params[2]) ? $this->parseAction($params[2]) : 'index';
2014-05-14 13:24:20 +00:00
self::$params = $controllerParams = array_slice($params, 3);
$controllerClass = '\\'.$module.'\Controllers\\'.$controller;
2014-10-10 21:12:50 +00:00
if ($controllerObj = $this->findController($controllerClass, $action, '/modules')) {
2014-05-14 13:24:20 +00:00
return $controllerObj->run($action, $controllerParams);
2014-10-10 21:12:50 +00:00
}
2014-05-14 13:24:20 +00:00
/*
* Look for a Plugin controller
*/
if (count($params) >= 2) {
list($author, $plugin) = $params;
$controller = isset($params[2]) ? $params[2] : 'index';
2014-08-08 12:02:46 +00:00
self::$action = $action = isset($params[3]) ? $this->parseAction($params[3]) : 'index';
2014-05-14 13:24:20 +00:00
self::$params = $controllerParams = array_slice($params, 4);
$controllerClass = '\\'.$author.'\\'.$plugin.'\Controllers\\'.$controller;
2014-10-10 21:12:50 +00:00
if ($controllerObj = $this->findController(
$controllerClass,
$action,
2015-02-07 04:37:07 +00:00
plugins_path()
2014-10-10 21:12:50 +00:00
)) {
2014-05-14 13:24:20 +00:00
return $controllerObj->run($action, $controllerParams);
2014-10-10 21:12:50 +00:00
}
2014-05-14 13:24:20 +00:00
}
2014-05-14 13:24:20 +00:00
/*
* Fall back on Cms controller
*/
return App::make('Cms\Classes\Controller')->run($url);
}
/**
* This method is used internally.
* Finds a backend controller with a callable action method.
* @param string $controller Specifies a method name to execute.
* @param string $action Specifies a method name to execute.
* @return ControllerBase Returns the backend controller object
*/
2015-02-07 04:37:07 +00:00
protected function findController($controller, $action, $inPath = null)
2014-05-14 13:24:20 +00:00
{
/*
* Workaround: Composer does not support case insensitivity.
*/
if (!class_exists($controller)) {
2015-02-07 04:37:07 +00:00
if (!$inPath) {
$inPath = base_path();
}
2014-05-14 13:24:20 +00:00
$controller = Str::normalizeClassName($controller);
2015-02-07 04:37:07 +00:00
$controllerFile = $inPath.strtolower(str_replace('\\', '/', $controller)) . '.php';
2014-10-10 21:12:50 +00:00
if ($controllerFile = File::existsInsensitive($controllerFile)) {
2014-05-14 13:24:20 +00:00
include_once($controllerFile);
2014-10-10 21:12:50 +00:00
}
2014-05-14 13:24:20 +00:00
}
2014-10-10 21:12:50 +00:00
if (!class_exists($controller)) {
2014-05-14 13:24:20 +00:00
return false;
2014-10-10 21:12:50 +00:00
}
2014-05-14 13:24:20 +00:00
$controllerObj = App::make($controller);
2014-10-10 21:12:50 +00:00
if ($controllerObj->actionExists($action)) {
2014-05-14 13:24:20 +00:00
return $controllerObj;
2014-10-10 21:12:50 +00:00
}
2014-05-14 13:24:20 +00:00
return false;
}
2014-08-08 12:02:46 +00:00
/**
* Process the action name, since dashes are not supported in PHP methods.
* @param string $actionName
* @return string
*/
protected function parseAction($actionName)
{
2014-10-10 21:12:50 +00:00
if (strpos($actionName, '-') !== false) {
2014-08-08 12:02:46 +00:00
return camel_case($actionName);
2014-10-10 21:12:50 +00:00
}
2014-08-08 12:02:46 +00:00
return $actionName;
}
2014-10-10 21:12:50 +00:00
}