Complex classes like Controller often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Controller, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class Controller extends RequestHandler implements TemplateGlobalProvider { |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @var array $urlParams An array of arguments extracted from the URL |
||
| 16 | */ |
||
| 17 | protected $urlParams; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var array $requestParams Contains all GET and POST parameters |
||
| 21 | * passed to the current {@link SS_HTTPRequest}. |
||
| 22 | * @uses SS_HTTPRequest->requestVars() |
||
| 23 | */ |
||
| 24 | protected $requestParams; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string $action The URL part matched on the current controller as |
||
| 28 | * determined by the "$Action" part of the {@link $url_handlers} definition. |
||
| 29 | * Should correlate to a public method on this controller. |
||
| 30 | * Used in {@link render()} and {@link getViewer()} to determine |
||
| 31 | * action-specific templates. |
||
| 32 | */ |
||
| 33 | protected $action; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * The {@link Session} object for this controller |
||
| 37 | */ |
||
| 38 | protected $session; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Stack of current controllers. |
||
| 42 | * Controller::$controller_stack[0] is the current controller. |
||
| 43 | */ |
||
| 44 | protected static $controller_stack = array(); |
||
| 45 | |||
| 46 | protected $basicAuthEnabled = true; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var SS_HTTPResponse $response The response object that the controller returns. |
||
| 50 | * Set in {@link handleRequest()}. |
||
| 51 | */ |
||
| 52 | protected $response; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Default URL handlers - (Action)/(ID)/(OtherID) |
||
| 56 | */ |
||
| 57 | private static $url_handlers = array( |
||
| 58 | '$Action//$ID/$OtherID' => 'handleAction', |
||
| 59 | ); |
||
| 60 | |||
| 61 | private static $allowed_actions = array( |
||
| 62 | 'handleAction', |
||
| 63 | 'handleIndex', |
||
| 64 | ); |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Initialisation function that is run before any action on the controller is called. |
||
| 68 | * |
||
| 69 | * @uses BasicAuth::requireLogin() |
||
| 70 | */ |
||
| 71 | public function init() { |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Executes this controller, and return an {@link SS_HTTPResponse} object with the result. |
||
| 94 | * |
||
| 95 | * This method first does a few set-up activities: |
||
| 96 | * - Push this controller ont to the controller stack - |
||
| 97 | * see {@link Controller::curr()} for information about this. |
||
| 98 | * - Call {@link init()} |
||
| 99 | * - Defer to {@link RequestHandler->handleRequest()} to determine which action |
||
| 100 | * should be executed |
||
| 101 | * |
||
| 102 | * Note: $requestParams['executeForm'] support was removed, |
||
| 103 | * make the following change in your URLs: |
||
| 104 | * "/?executeForm=FooBar" -> "/FooBar" |
||
| 105 | * Also make sure "FooBar" is in the $allowed_actions of your controller class. |
||
| 106 | * |
||
| 107 | * Note: You should rarely need to overload run() - |
||
| 108 | * this kind of change is only really appropriate for things like nested |
||
| 109 | * controllers - {@link ModelAsController} and {@link RootURLController} |
||
| 110 | * are two examples here. If you want to make more |
||
| 111 | * orthodox functionality, it's better to overload {@link init()} or {@link index()}. |
||
| 112 | * |
||
| 113 | * Important: If you are going to overload handleRequest, |
||
| 114 | * make sure that you start the method with $this->pushCurrent() |
||
| 115 | * and end the method with $this->popCurrent(). |
||
| 116 | * Failure to do this will create weird session errors. |
||
| 117 | * |
||
| 118 | * @param $request The {@link SS_HTTPRequest} object that is responsible |
||
| 119 | * for distributing request parsing. |
||
| 120 | * @return SS_HTTPResponse The response that this controller produces, |
||
| 121 | * including HTTP headers such as redirection info |
||
| 122 | */ |
||
| 123 | public function handleRequest(SS_HTTPRequest $request, DataModel $model) { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Controller's default action handler. It will call the method named in $Action, if that method exists. |
||
| 179 | * If $Action isn't given, it will use "index" as a default. |
||
| 180 | */ |
||
| 181 | protected function handleAction($request, $action) { |
||
| 204 | |||
| 205 | public function setURLParams($urlParams) { |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @return array The parameters extracted from the URL by the {@link Director}. |
||
| 211 | */ |
||
| 212 | public function getURLParams() { |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Returns the SS_HTTPResponse object that this controller is building up. |
||
| 218 | * Can be used to set the status code and headers |
||
| 219 | */ |
||
| 220 | public function getResponse() { |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Sets the SS_HTTPResponse object that this controller is building up. |
||
| 229 | * |
||
| 230 | * @param SS_HTTPResponse $response |
||
| 231 | * @return Controller |
||
| 232 | */ |
||
| 233 | public function setResponse(SS_HTTPResponse $response) { |
||
| 237 | |||
| 238 | protected $baseInitCalled = false; |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Return the object that is going to own a form that's being processed, and handle its execution. |
||
| 242 | * Note that the result needn't be an actual controller object. |
||
| 243 | */ |
||
| 244 | public function getFormOwner() { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * This is the default action handler used if a method doesn't exist. |
||
| 261 | * It will process the controller object with the template returned by {@link getViewer()} |
||
| 262 | */ |
||
| 263 | public function defaultAction($action) { |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Returns the action that is being executed on this controller. |
||
| 269 | */ |
||
| 270 | public function getAction() { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Return an SSViewer object to process the data |
||
| 276 | * @return SSViewer The viewer identified being the default handler for this Controller/Action combination |
||
| 277 | */ |
||
| 278 | public function getViewer($action) { |
||
| 314 | |||
| 315 | public function hasAction($action) { |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Removes all the "action" part of the current URL and returns the result. |
||
| 321 | * If no action parameter is present, returns the full URL |
||
| 322 | * @static |
||
| 323 | * @return String |
||
| 324 | */ |
||
| 325 | public function removeAction($fullURL, $action = null) { |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Return the class that defines the given action, so that we know where to check allowed_actions. |
||
| 338 | * Overrides RequestHandler to also look at defined templates |
||
| 339 | */ |
||
| 340 | protected function definingClassForAction($action) { |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Returns TRUE if this controller has a template that is specifically designed to handle a specific action. |
||
| 355 | * |
||
| 356 | * @param string $action |
||
| 357 | * @return bool |
||
| 358 | */ |
||
| 359 | public function hasActionTemplate($action) { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Render the current controller with the templates determined |
||
| 375 | * by {@link getViewer()}. |
||
| 376 | * |
||
| 377 | * @param array $params Key-value array for custom template variables (Optional) |
||
| 378 | * @return string Parsed template content |
||
| 379 | */ |
||
| 380 | public function render($params = null) { |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Call this to disable site-wide basic authentication for a specific contoller. |
||
| 393 | * This must be called before Controller::init(). That is, you must call it in your controller's |
||
| 394 | * init method before it calls parent::init(). |
||
| 395 | */ |
||
| 396 | public function disableBasicAuth() { |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Returns the current controller |
||
| 402 | * @return Controller |
||
| 403 | */ |
||
| 404 | public static function curr() { |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Tests whether we have a currently active controller or not |
||
| 414 | * @return boolean True if there is at least 1 controller in the stack. |
||
| 415 | */ |
||
| 416 | public static function has_curr() { |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Returns true if the member is allowed to do the given action. |
||
| 422 | * @param perm The permission to be checked, such as 'View'. |
||
| 423 | * @param member The member whose permissions need checking. Defaults to the currently logged |
||
| 424 | * in user. |
||
| 425 | * @return boolean |
||
| 426 | */ |
||
| 427 | public function can($perm, $member = null) { |
||
| 439 | |||
| 440 | //--------------------------------------------------------------------------------------------------------------- |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Pushes this controller onto the stack of current controllers. |
||
| 444 | * This means that any redirection, session setting, or other things that rely on Controller::curr() will now |
||
| 445 | * write to this controller object. |
||
| 446 | */ |
||
| 447 | public function pushCurrent() { |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Pop this controller off the top of the stack. |
||
| 461 | */ |
||
| 462 | public function popCurrent() { |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Redirect to the given URL. |
||
| 473 | * |
||
| 474 | * @return SS_HTTPResponse |
||
| 475 | */ |
||
| 476 | public function redirect($url, $code=302) { |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Redirect back. Uses either the HTTP_REFERER or a manually set request-variable called "BackURL". |
||
| 494 | * This variable is needed in scenarios where not HTTP-Referer is sent ( |
||
| 495 | * e.g when calling a page by location.href in IE). |
||
| 496 | * If none of the two variables is available, it will redirect to the base |
||
| 497 | * URL (see {@link Director::baseURL()}). |
||
| 498 | * @uses redirect() |
||
| 499 | */ |
||
| 500 | public function redirectBack() { |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Tests whether a redirection has been requested. |
||
| 533 | * @return string If redirect() has been called, it will return the URL redirected to. Otherwise, it will |
||
| 534 | * return null; |
||
| 535 | */ |
||
| 536 | public function redirectedTo() { |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Get the Session object representing this Controller's session |
||
| 542 | * @return Session |
||
| 543 | */ |
||
| 544 | public function getSession() { |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Set the Session object. |
||
| 550 | */ |
||
| 551 | public function setSession(Session $session) { |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Joins two or more link segments together, putting a slash between them if necessary. |
||
| 557 | * Use this for building the results of {@link Link()} methods. |
||
| 558 | * If either of the links have query strings, |
||
| 559 | * then they will be combined and put at the end of the resulting url. |
||
| 560 | * |
||
| 561 | * Caution: All parameters are expected to be URI-encoded already. |
||
| 562 | * |
||
| 563 | * @param String |
||
| 564 | * @return String |
||
| 565 | */ |
||
| 566 | public static function join_links() { |
||
| 596 | |||
| 597 | public static function get_template_global_variables() { |
||
| 602 | } |
||
| 603 | |||
| 605 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: