From 1a037bad04c604a8df94ed3403342e0c0824a7cd Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Tue, 21 Apr 2015 20:07:34 +1000 Subject: [PATCH] Add the ability to include middleware in CMS / Backend controllers Add init.php script to plugins Fixes https://github.com/octobercms/library/issues/106 --- modules/backend/classes/BackendController.php | 24 +++++++++ modules/cms/classes/CmsController.php | 49 +++++++++++++++++++ modules/cms/classes/Controller.php | 48 +----------------- modules/cms/routes.php | 2 +- modules/system/ServiceProvider.php | 5 +- modules/system/classes/PluginManager.php | 8 +++ 6 files changed, 86 insertions(+), 50 deletions(-) create mode 100644 modules/cms/classes/CmsController.php diff --git a/modules/backend/classes/BackendController.php b/modules/backend/classes/BackendController.php index ff4bc43e8..6fb4ef53d 100644 --- a/modules/backend/classes/BackendController.php +++ b/modules/backend/classes/BackendController.php @@ -6,6 +6,7 @@ use File; use Config; use Illuminate\Routing\Controller as ControllerBase; use October\Rain\Router\Helper as RouterHelper; +use Closure; /** * The Backend controller class. @@ -16,6 +17,13 @@ use October\Rain\Router\Helper as RouterHelper; */ class BackendController extends ControllerBase { + use \October\Rain\Extension\ExtendableTrait; + + /** + * @var array Behaviors implemented by this controller. + */ + public $implement; + /** * @var string Allows early access to page action. */ @@ -26,6 +34,22 @@ class BackendController extends ControllerBase */ public static $params; + /** + * Instantiate a new BackendController instance. + */ + public function __construct() + { + $this->extendableConstruct(); + } + + /** + * Extend this object properties upon construction. + */ + public static function extend(Closure $callback) + { + self::extendableExtendCallback($callback); + } + /** * Finds and serves the requested backend controller. * If the controller cannot be found, returns the Cms page with the URL /404. diff --git a/modules/cms/classes/CmsController.php b/modules/cms/classes/CmsController.php new file mode 100644 index 000000000..ab884406f --- /dev/null +++ b/modules/cms/classes/CmsController.php @@ -0,0 +1,49 @@ +extendableConstruct(); + } + + /** + * Extend this object properties upon construction. + */ + public static function extend(Closure $callback) + { + self::extendableExtendCallback($callback); + } + + /** + * Finds and serves the request using the primary controller. + * @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) + { + return App::make('Cms\Classes\Controller')->run($url); + } +} diff --git a/modules/cms/classes/Controller.php b/modules/cms/classes/Controller.php index 60600a29f..6aba24e50 100644 --- a/modules/cms/classes/Controller.php +++ b/modules/cms/classes/Controller.php @@ -1052,7 +1052,7 @@ class Controller $url = substr($url, 1); } - $routeAction = 'Cms\Classes\Controller@run'; + $routeAction = 'Cms\Classes\CmsController@run'; $actionExists = Route::getRoutes()->getByAction($routeAction) !== null; if ($actionExists) { @@ -1264,50 +1264,4 @@ class Controller } } } - - // - // Keep Laravel Happy - // - - /** - * Get the middleware assigned to the controller. - * - * @return array - */ - public function getMiddleware() - { - return []; - } - - /** - * Get the registered "before" filters. - * - * @return array - */ - public function getBeforeFilters() - { - return []; - } - - /** - * Get the registered "after" filters. - * - * @return array - */ - public function getAfterFilters() - { - return []; - } - - /** - * Execute an action on the controller. - * - * @param string $method - * @param array $parameters - * @return \Symfony\Component\HttpFoundation\Response - */ - public function callAction($method, $parameters) - { - return call_user_func_array(array($this, $method), $parameters); - } } diff --git a/modules/cms/routes.php b/modules/cms/routes.php index 5e84b32a6..f99bd1150 100644 --- a/modules/cms/routes.php +++ b/modules/cms/routes.php @@ -8,5 +8,5 @@ App::before(function ($request) { * The CMS module intercepts all URLs that were not * handled by the back-end modules. */ - Route::any('{slug}', 'Cms\Classes\Controller@run')->where('slug', '(.*)?'); + Route::any('{slug}', 'Cms\Classes\CmsController@run')->where('slug', '(.*)?'); }); diff --git a/modules/system/ServiceProvider.php b/modules/system/ServiceProvider.php index 08029fb1d..0cfda6edb 100644 --- a/modules/system/ServiceProvider.php +++ b/modules/system/ServiceProvider.php @@ -345,16 +345,17 @@ class ServiceProvider extends ModuleServiceProvider */ protected function registerPrivilegedActions() { - $requests = ['/combine', '@/system/updates', '@/backend/auth']; + $requests = ['/combine', '@/system/updates', '@/system/install', '@/backend/auth']; $commands = ['october:up', 'october:update']; /* * Requests */ $path = RouterHelper::normalizeUrl(Request::path()); + $backendUri = RouterHelper::normalizeUrl(Config::get('cms.backendUri')); foreach ($requests as $request) { if (substr($request, 0, 1) == '@') { - $request = Config::get('cms.backendUri') . substr($request, 1); + $request = $backendUri . substr($request, 1); } if (stripos($path, $request) === 0) { diff --git a/modules/system/classes/PluginManager.php b/modules/system/classes/PluginManager.php index fa06eac42..d4cf86418 100644 --- a/modules/system/classes/PluginManager.php +++ b/modules/system/classes/PluginManager.php @@ -210,6 +210,14 @@ class PluginManager View::addNamespace($pluginNamespace, $viewsPath); } + /* + * Add init, if available + */ + $initFile = $pluginPath . '/init.php'; + if (!self::$noInit && File::exists($initFile)) { + require $initFile; + } + /* * Add routes, if available */