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 |
||
| 23 | class Controller extends RequestHandler implements TemplateGlobalProvider |
||
| 24 | { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * An array of arguments extracted from the URL. |
||
| 28 | * |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | protected $urlParams; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Contains all GET and POST parameters passed to the current {@link HTTPRequest}. |
||
| 35 | * |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | protected $requestParams; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The URL part matched on the current controller as determined by the "$Action" part of the |
||
| 42 | * {@link $url_handlers} definition. Should correlate to a public method on this controller. |
||
| 43 | * |
||
| 44 | * Used in {@link render()} and {@link getViewer()} to determine action-specific templates. |
||
| 45 | * |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | protected $action; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * The {@link Session} object for this controller. |
||
| 52 | * |
||
| 53 | * @var Session |
||
| 54 | */ |
||
| 55 | protected $session; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Stack of current controllers. Controller::$controller_stack[0] is the current controller. |
||
| 59 | * |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | protected static $controller_stack = array(); |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var bool |
||
| 66 | */ |
||
| 67 | protected $basicAuthEnabled = true; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * The response object that the controller returns. |
||
| 71 | * |
||
| 72 | * Set in {@link handleRequest()}. |
||
| 73 | * |
||
| 74 | * @var HTTPResponse |
||
| 75 | */ |
||
| 76 | protected $response; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Default URL handlers. |
||
| 80 | * |
||
| 81 | * @var array |
||
| 82 | */ |
||
| 83 | private static $url_handlers = array( |
||
| 84 | '$Action//$ID/$OtherID' => 'handleAction', |
||
| 85 | ); |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var array |
||
| 89 | */ |
||
| 90 | private static $allowed_actions = array( |
||
| 91 | 'handleAction', |
||
| 92 | 'handleIndex', |
||
| 93 | ); |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Initialisation function that is run before any action on the controller is called. |
||
| 97 | * |
||
| 98 | * @uses BasicAuth::requireLogin() |
||
| 99 | */ |
||
| 100 | protected function init() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * A stand in function to protect the init function from failing to be called as well as providing before and |
||
| 112 | * after hooks for the init function itself |
||
| 113 | * |
||
| 114 | * This should be called on all controllers before handling requests |
||
| 115 | */ |
||
| 116 | public function doInit() |
||
| 134 | |||
| 135 | /** |
||
| 136 | * {@inheritdoc} |
||
| 137 | * |
||
| 138 | * Also set the URLParams |
||
| 139 | */ |
||
| 140 | public function setRequest($request) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * A bootstrap for the handleRequest method |
||
| 150 | * |
||
| 151 | * @todo setDataModel and setRequest are redundantly called in parent::handleRequest() - sort this out |
||
| 152 | * |
||
| 153 | * @param HTTPRequest $request |
||
| 154 | * @param DataModel $model |
||
| 155 | */ |
||
| 156 | protected function beforeHandleRequest(HTTPRequest $request, DataModel $model) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Cleanup for the handleRequest method |
||
| 170 | */ |
||
| 171 | protected function afterHandleRequest() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Executes this controller, and return an {@link HTTPResponse} object with the result. |
||
| 179 | * |
||
| 180 | * This method defers to {@link RequestHandler->handleRequest()} to determine which action |
||
| 181 | * should be executed |
||
| 182 | * |
||
| 183 | * Note: You should rarely need to overload handleRequest() - |
||
| 184 | * this kind of change is only really appropriate for things like nested |
||
| 185 | * controllers - {@link ModelAsController} and {@link RootURLController} |
||
| 186 | * are two examples here. If you want to make more |
||
| 187 | * orthodox functionality, it's better to overload {@link init()} or {@link index()}. |
||
| 188 | * |
||
| 189 | * Important: If you are going to overload handleRequest, |
||
| 190 | * make sure that you start the method with $this->beforeHandleRequest() |
||
| 191 | * and end the method with $this->afterHandleRequest() |
||
| 192 | * |
||
| 193 | * @param HTTPRequest $request |
||
| 194 | * @param DataModel $model |
||
| 195 | * |
||
| 196 | * @return HTTPResponse |
||
| 197 | */ |
||
| 198 | public function handleRequest(HTTPRequest $request, DataModel $model) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Prepare the response (we can receive an assortment of response types (strings/objects/HTTPResponses) and |
||
| 226 | * changes the controller response object appropriately |
||
| 227 | * |
||
| 228 | * @param HTTPResponse|Object $response |
||
| 229 | */ |
||
| 230 | protected function prepareResponse($response) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Controller's default action handler. It will call the method named in "$Action", if that method |
||
| 263 | * exists. If "$Action" isn't given, it will use "index" as a default. |
||
| 264 | * |
||
| 265 | * @param HTTPRequest $request |
||
| 266 | * @param string $action |
||
| 267 | * |
||
| 268 | * @return DBHTMLText|HTTPResponse |
||
| 269 | */ |
||
| 270 | protected function handleAction($request, $action) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @param array $urlParams |
||
| 310 | * @return $this |
||
| 311 | */ |
||
| 312 | public function setURLParams($urlParams) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Returns the parameters extracted from the URL by the {@link Director}. |
||
| 320 | * |
||
| 321 | * @return array |
||
| 322 | */ |
||
| 323 | public function getURLParams() |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Returns the HTTPResponse object that this controller is building up. Can be used to set the |
||
| 330 | * status code and headers. |
||
| 331 | * |
||
| 332 | * @return HTTPResponse |
||
| 333 | */ |
||
| 334 | public function getResponse() |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Sets the HTTPResponse object that this controller is building up. |
||
| 344 | * |
||
| 345 | * @param HTTPResponse $response |
||
| 346 | * |
||
| 347 | * @return $this |
||
| 348 | */ |
||
| 349 | public function setResponse(HTTPResponse $response) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @var bool |
||
| 357 | */ |
||
| 358 | protected $baseInitCalled = false; |
||
| 359 | |||
| 360 | /** |
||
| 361 | * This is the default action handler used if a method doesn't exist. It will process the |
||
| 362 | * controller object with the template returned by {@link getViewer()}. |
||
| 363 | * |
||
| 364 | * @param string $action |
||
| 365 | * @return DBHTMLText |
||
| 366 | */ |
||
| 367 | public function defaultAction($action) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Returns the action that is being executed on this controller. |
||
| 374 | * |
||
| 375 | * @return string |
||
| 376 | */ |
||
| 377 | public function getAction() |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Return the viewer identified being the default handler for this Controller/Action combination. |
||
| 384 | * |
||
| 385 | * @param string $action |
||
| 386 | * |
||
| 387 | * @return SSViewer |
||
| 388 | */ |
||
| 389 | public function getViewer($action) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * @param string $action |
||
| 426 | * |
||
| 427 | * @return bool |
||
| 428 | */ |
||
| 429 | public function hasAction($action) |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Removes all the "action" part of the current URL and returns the result. If no action parameter |
||
| 436 | * is present, returns the full URL. |
||
| 437 | * |
||
| 438 | * @param string $fullURL |
||
| 439 | * @param null|string $action |
||
| 440 | * |
||
| 441 | * @return string |
||
| 442 | */ |
||
| 443 | public function removeAction($fullURL, $action = null) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Return the class that defines the given action, so that we know where to check allowed_actions. |
||
| 459 | * Overrides RequestHandler to also look at defined templates. |
||
| 460 | * |
||
| 461 | * @param string $action |
||
| 462 | * |
||
| 463 | * @return string |
||
| 464 | */ |
||
| 465 | protected function definingClassForAction($action) |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Returns TRUE if this controller has a template that is specifically designed to handle a |
||
| 487 | * specific action. |
||
| 488 | * |
||
| 489 | * @param string $action |
||
| 490 | * |
||
| 491 | * @return bool |
||
| 492 | */ |
||
| 493 | public function hasActionTemplate($action) |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Render the current controller with the templates determined by {@link getViewer()}. |
||
| 512 | * |
||
| 513 | * @param array $params |
||
| 514 | * |
||
| 515 | * @return string |
||
| 516 | */ |
||
| 517 | public function render($params = null) |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Call this to disable site-wide basic authentication for a specific controller. This must be |
||
| 533 | * called before Controller::init(). That is, you must call it in your controller's init method |
||
| 534 | * before it calls parent::init(). |
||
| 535 | */ |
||
| 536 | public function disableBasicAuth() |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Returns the current controller. |
||
| 543 | * |
||
| 544 | * @return Controller |
||
| 545 | */ |
||
| 546 | public static function curr() |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Tests whether we have a currently active controller or not. True if there is at least 1 |
||
| 557 | * controller in the stack. |
||
| 558 | * |
||
| 559 | * @return bool |
||
| 560 | */ |
||
| 561 | public static function has_curr() |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Returns true if the member is allowed to do the given action. Defaults to the currently logged |
||
| 568 | * in user. |
||
| 569 | * |
||
| 570 | * @param string $perm |
||
| 571 | * @param null|member $member |
||
| 572 | * |
||
| 573 | * @return bool |
||
| 574 | */ |
||
| 575 | public function can($perm, $member = null) |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Pushes this controller onto the stack of current controllers. This means that any redirection, |
||
| 593 | * session setting, or other things that rely on Controller::curr() will now write to this |
||
| 594 | * controller object. |
||
| 595 | */ |
||
| 596 | public function pushCurrent() |
||
| 608 | |||
| 609 | /** |
||
| 610 | * Pop this controller off the top of the stack. |
||
| 611 | */ |
||
| 612 | public function popCurrent() |
||
| 623 | |||
| 624 | /** |
||
| 625 | * Redirect to the given URL. |
||
| 626 | * |
||
| 627 | * @param string $url |
||
| 628 | * @param int $code |
||
| 629 | * @return HTTPResponse |
||
| 630 | */ |
||
| 631 | public function redirect($url, $code = 302) |
||
| 642 | |||
| 643 | /** |
||
| 644 | * Tests whether a redirection has been requested. If redirect() has been called, it will return |
||
| 645 | * the URL redirected to. Otherwise, it will return null. |
||
| 646 | * |
||
| 647 | * @return null|string |
||
| 648 | */ |
||
| 649 | public function redirectedTo() |
||
| 653 | |||
| 654 | /** |
||
| 655 | * Get the Session object representing this Controller's session. |
||
| 656 | * |
||
| 657 | * @return Session |
||
| 658 | */ |
||
| 659 | public function getSession() |
||
| 663 | |||
| 664 | /** |
||
| 665 | * Set the Session object. |
||
| 666 | * |
||
| 667 | * @param Session $session |
||
| 668 | */ |
||
| 669 | public function setSession(Session $session) |
||
| 673 | |||
| 674 | /** |
||
| 675 | * Joins two or more link segments together, putting a slash between them if necessary. Use this |
||
| 676 | * for building the results of {@link Link()} methods. If either of the links have query strings, |
||
| 677 | * then they will be combined and put at the end of the resulting url. |
||
| 678 | * |
||
| 679 | * Caution: All parameters are expected to be URI-encoded already. |
||
| 680 | * |
||
| 681 | * @param string|array $arg,.. One or more link segments, or list of link segments as an array |
||
| 682 | * @return string |
||
| 683 | */ |
||
| 684 | public static function join_links($arg = null) |
||
| 726 | |||
| 727 | /** |
||
| 728 | * @return array |
||
| 729 | */ |
||
| 730 | public static function get_template_global_variables() |
||
| 736 | } |
||
| 737 |
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: