-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathAdminRootController.php
142 lines (124 loc) · 4.49 KB
/
AdminRootController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
namespace SilverStripe\Admin;
use SilverStripe\Control\Controller;
use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\View\TemplateGlobalProvider;
class AdminRootController extends Controller implements TemplateGlobalProvider
{
/**
* Fallback admin URL in case this cannot be infered from Director.rules
*
* @var string
* @config
*/
private static $url_base = 'admin';
/**
* Convenience function to return the admin route config.
* Looks for the {@link Director::$rules} for the current admin Controller.
*
* @return string
*/
public static function get_admin_route()
{
$rules = Director::config()->get('rules');
$adminRoute = array_search(__CLASS__, $rules ?? []);
return $adminRoute ?: static::config()->get('url_base');
}
/**
* Returns the root admin URL for the site with trailing slash
*
* @return string
*/
public static function admin_url()
{
return self::get_admin_route() . '/';
}
/**
* @var string
* @config
* The LeftAndMain child that will be used as the initial panel to display if none is selected (i.e. if you
* visit /admin)
*/
private static $default_panel = SecurityAdmin::class;
/**
* @var array
* @internal
*
* Holds an array of url_pattern => controller k/v pairs, the same as Director::rules. However this is built
* dynamically from introspecting on all the classes that derive from LeftAndMain.
*
* Don't access this directly - always access via the rules() accessor below, which will build this array
* the first time it's accessed
*/
private static $adminRules = null;
/**
* Gets a list of url_pattern => controller k/v pairs for each LeftAndMain derived controller
*/
public static function rules()
{
if (self::$adminRules === null) {
self::$adminRules = [];
// Map over the array calling add_rule_for_controller on each
$classes = CMSMenu::get_cms_classes(null, true, CMSMenu::URL_PRIORITY);
array_map([__CLASS__, 'add_rule_for_controller'], $classes ?? []);
}
return self::$adminRules;
}
/**
* Add the appropriate k/v pair to self::$rules for the given controller.
*
* @param string $controllerClass Name of class
*/
protected static function add_rule_for_controller($controllerClass)
{
$config = Config::forClass($controllerClass);
$urlSegment = $config->get('url_segment');
$urlRule = $config->get('url_rule');
if ($urlSegment) {
// Make director rule
if ($urlRule[0] == '/') {
$urlRule = substr($urlRule ?? '', 1);
}
$rule = $urlSegment . '//' . $urlRule;
// ensure that the first call to add_rule_for_controller for a rule takes precedence
if (!isset(self::$adminRules[$rule])) {
self::$adminRules[$rule] = $controllerClass;
}
}
}
public function handleRequest(HTTPRequest $request)
{
// If this is the final portion of the request (i.e. the URL is just /admin), direct to the default panel
if ($request->allParsed()) {
$segment = Config::forClass($this->config()->get('default_panel'))
->get('url_segment');
$this->redirect(Controller::join_links(self::admin_url(), $segment, '/'));
return $this->getResponse();
}
// Otherwise
$rules = self::rules();
foreach ($rules as $pattern => $controller) {
if (($arguments = $request->match($pattern, true)) !== false) {
/** @var LeftAndMain $controllerObj */
$controllerObj = Injector::inst()->create($controller);
return $controllerObj->handleRequest($request);
}
}
// Fall back to methods defined on LeftAndMain
$controllerObj = Injector::inst()->create(LeftAndMain::class);
return $controllerObj->handleRequest($request);
}
/**
* @return array Returns an array of strings of the method names of methods on the call that should be exposed
* as global variables in the templates.
*/
public static function get_template_global_variables()
{
return [
'adminURL' => 'admin_url'
];
}
}