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 |
||
| 13 | class Controller extends RequestHandler implements TemplateGlobalProvider { |
||
| 14 | |||
| 15 | /** |
||
| 16 | * An array of arguments extracted from the URL. |
||
| 17 | * |
||
| 18 | * @var array |
||
| 19 | */ |
||
| 20 | protected $urlParams; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Contains all GET and POST parameters passed to the current {@link SS_HTTPRequest}. |
||
| 24 | * |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | protected $requestParams; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The URL part matched on the current controller as determined by the "$Action" part of the |
||
| 31 | * {@link $url_handlers} definition. Should correlate to a public method on this controller. |
||
| 32 | * |
||
| 33 | * Used in {@link render()} and {@link getViewer()} to determine action-specific templates. |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $action; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The {@link Session} object for this controller. |
||
| 41 | * |
||
| 42 | * @var Session |
||
| 43 | */ |
||
| 44 | protected $session; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Stack of current controllers. Controller::$controller_stack[0] is the current controller. |
||
| 48 | * |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | protected static $controller_stack = array(); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var bool |
||
| 55 | */ |
||
| 56 | protected $basicAuthEnabled = true; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * The response object that the controller returns. |
||
| 60 | * |
||
| 61 | * Set in {@link handleRequest()}. |
||
| 62 | * |
||
| 63 | * @var SS_HTTPResponse |
||
| 64 | */ |
||
| 65 | protected $response; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Default URL handlers. |
||
| 69 | * |
||
| 70 | * @var array |
||
| 71 | */ |
||
| 72 | private static $url_handlers = array( |
||
| 73 | '$Action//$ID/$OtherID' => 'handleAction', |
||
| 74 | ); |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var array |
||
| 78 | */ |
||
| 79 | private static $allowed_actions = array( |
||
| 80 | 'handleAction', |
||
| 81 | 'handleIndex', |
||
| 82 | ); |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Initialisation function that is run before any action on the controller is called. |
||
| 86 | * |
||
| 87 | * @uses BasicAuth::requireLogin() |
||
| 88 | */ |
||
| 89 | public function init() { |
||
| 90 | if($this->basicAuthEnabled) BasicAuth::protect_site_if_necessary(); |
||
| 91 | |||
| 92 | // This is used to test that subordinate controllers are actually calling parent::init() - a common bug |
||
| 93 | $this->baseInitCalled = true; |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Returns a link to this controller. Overload with your own Link rules if they exist. |
||
| 98 | * |
||
| 99 | * @return string |
||
| 100 | */ |
||
| 101 | public function Link() { |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Executes this controller, and return an {@link SS_HTTPResponse} object with the result. |
||
| 107 | * |
||
| 108 | * This method first does a few set-up activities: |
||
| 109 | * - Push this controller ont to the controller stack - see {@link Controller::curr()} for |
||
| 110 | * information about this. |
||
| 111 | * - Call {@link init()} |
||
| 112 | * - Defer to {@link RequestHandler->handleRequest()} to determine which action should be executed. |
||
| 113 | * |
||
| 114 | * Note: $requestParams['executeForm'] support was removed, make the following change in your URLs: |
||
| 115 | * "/?executeForm=FooBar" -> "/FooBar". |
||
| 116 | * |
||
| 117 | * Also make sure "FooBar" is in the $allowed_actions of your controller class. |
||
| 118 | * |
||
| 119 | * Note: You should rarely need to overload run() - this kind of change is only really appropriate |
||
| 120 | * for things like nested controllers - {@link ModelAsController} and {@link RootURLController} |
||
| 121 | * are two examples here. If you want to make more orthodox functionality, it's better to overload |
||
| 122 | * {@link init()} or {@link index()}. |
||
| 123 | * |
||
| 124 | * Important: If you are going to overload handleRequest, make sure that you start the method with |
||
| 125 | * $this->pushCurrent() and end the method with $this->popCurrent(). Failure to do this will create |
||
| 126 | * weird session errors. |
||
| 127 | * |
||
| 128 | * @param SS_HTTPRequest $request |
||
| 129 | * @param DataModel $model |
||
| 130 | * |
||
| 131 | * @return SS_HTTPResponse |
||
| 132 | */ |
||
| 133 | public function handleRequest(SS_HTTPRequest $request, DataModel $model) { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Controller's default action handler. It will call the method named in "$Action", if that method |
||
| 192 | * exists. If "$Action" isn't given, it will use "index" as a default. |
||
| 193 | * |
||
| 194 | * @param SS_HTTPRequest $request |
||
| 195 | * @param string $action |
||
| 196 | * |
||
| 197 | * @return HTMLText|SS_HTTPResponse |
||
| 198 | */ |
||
| 199 | protected function handleAction($request, $action) { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @param array $urlParams |
||
| 236 | */ |
||
| 237 | public function setURLParams($urlParams) { |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Returns the parameters extracted from the URL by the {@link Director}. |
||
| 243 | * |
||
| 244 | * @return array |
||
| 245 | */ |
||
| 246 | public function getURLParams() { |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Returns the SS_HTTPResponse object that this controller is building up. Can be used to set the |
||
| 252 | * status code and headers. |
||
| 253 | * |
||
| 254 | * @return SS_HTTPResponse |
||
| 255 | */ |
||
| 256 | public function getResponse() { |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Sets the SS_HTTPResponse object that this controller is building up. |
||
| 265 | * |
||
| 266 | * @param SS_HTTPResponse $response |
||
| 267 | * |
||
| 268 | * @return $this |
||
| 269 | */ |
||
| 270 | public function setResponse(SS_HTTPResponse $response) { |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @var bool |
||
| 277 | */ |
||
| 278 | protected $baseInitCalled = false; |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Return the object that is going to own a form that's being processed, and handle its execution. |
||
| 282 | * Note that the result need not be an actual controller object. |
||
| 283 | * |
||
| 284 | * @return mixed |
||
| 285 | */ |
||
| 286 | public function getFormOwner() { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * This is the default action handler used if a method doesn't exist. It will process the |
||
| 303 | * controller object with the template returned by {@link getViewer()}. |
||
| 304 | * |
||
| 305 | * @param string $action |
||
| 306 | * |
||
| 307 | * @return HTMLText |
||
| 308 | */ |
||
| 309 | public function defaultAction($action) { |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Returns the action that is being executed on this controller. |
||
| 315 | * |
||
| 316 | * @return string |
||
| 317 | */ |
||
| 318 | public function getAction() { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Return the viewer identified being the default handler for this Controller/Action combination. |
||
| 324 | * |
||
| 325 | * @param string $action |
||
| 326 | * |
||
| 327 | * @return SSViewer |
||
| 328 | */ |
||
| 329 | public function getViewer($action) { |
||
| 365 | |||
| 366 | /** |
||
| 367 | * @param string $action |
||
| 368 | * |
||
| 369 | * @return bool |
||
| 370 | */ |
||
| 371 | public function hasAction($action) { |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Removes all the "action" part of the current URL and returns the result. If no action parameter |
||
| 377 | * is present, returns the full URL. |
||
| 378 | * |
||
| 379 | * @param string $fullURL |
||
| 380 | * @param null|string $action |
||
| 381 | * |
||
| 382 | * @return string |
||
| 383 | */ |
||
| 384 | public function removeAction($fullURL, $action = null) { |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Return the class that defines the given action, so that we know where to check allowed_actions. |
||
| 397 | * Overrides RequestHandler to also look at defined templates. |
||
| 398 | * |
||
| 399 | * @param string $action |
||
| 400 | * |
||
| 401 | * @return string |
||
| 402 | */ |
||
| 403 | protected function definingClassForAction($action) { |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Returns TRUE if this controller has a template that is specifically designed to handle a |
||
| 418 | * specific action. |
||
| 419 | * |
||
| 420 | * @param string $action |
||
| 421 | * |
||
| 422 | * @return bool |
||
| 423 | */ |
||
| 424 | public function hasActionTemplate($action) { |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Render the current controller with the templates determined by {@link getViewer()}. |
||
| 440 | * |
||
| 441 | * @param array $params |
||
| 442 | * |
||
| 443 | * @return string |
||
| 444 | */ |
||
| 445 | public function render($params = null) { |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Call this to disable site-wide basic authentication for a specific controller. This must be |
||
| 458 | * called before Controller::init(). That is, you must call it in your controller's init method |
||
| 459 | * before it calls parent::init(). |
||
| 460 | */ |
||
| 461 | public function disableBasicAuth() { |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Returns the current controller. |
||
| 467 | * |
||
| 468 | * @return Controller |
||
| 469 | */ |
||
| 470 | public static function curr() { |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Tests whether we have a currently active controller or not. True if there is at least 1 |
||
| 480 | * controller in the stack. |
||
| 481 | * |
||
| 482 | * @return bool |
||
| 483 | */ |
||
| 484 | public static function has_curr() { |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Returns true if the member is allowed to do the given action. Defaults to the currently logged |
||
| 490 | * in user. |
||
| 491 | * |
||
| 492 | * @param string $perm |
||
| 493 | * @param null|member $member |
||
| 494 | * |
||
| 495 | * @return bool |
||
| 496 | */ |
||
| 497 | public function can($perm, $member = null) { |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Pushes this controller onto the stack of current controllers. This means that any redirection, |
||
| 512 | * session setting, or other things that rely on Controller::curr() will now write to this |
||
| 513 | * controller object. |
||
| 514 | */ |
||
| 515 | public function pushCurrent() { |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Pop this controller off the top of the stack. |
||
| 529 | */ |
||
| 530 | public function popCurrent() { |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Redirect to the given URL. |
||
| 541 | * |
||
| 542 | * @param string $url |
||
| 543 | * @param int $code |
||
| 544 | * |
||
| 545 | * @return SS_HTTPResponse |
||
| 546 | */ |
||
| 547 | public function redirect($url, $code=302) { |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Redirect back. Uses either the HTTP-Referer or a manually set request-variable called "BackURL". |
||
| 565 | * This variable is needed in scenarios where HTTP-Referer is not sent (e.g when calling a page by |
||
| 566 | * location.href in IE). If none of the two variables is available, it will redirect to the base |
||
| 567 | * URL (see {@link Director::baseURL()}). |
||
| 568 | * |
||
| 569 | * @uses redirect() |
||
| 570 | * |
||
| 571 | * @return bool|SS_HTTPResponse |
||
| 572 | */ |
||
| 573 | public function redirectBack() { |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Tests whether a redirection has been requested. If redirect() has been called, it will return |
||
| 606 | * the URL redirected to. Otherwise, it will return null. |
||
| 607 | * |
||
| 608 | * @return null|string |
||
| 609 | */ |
||
| 610 | public function redirectedTo() { |
||
| 613 | |||
| 614 | /** |
||
| 615 | * Get the Session object representing this Controller's session. |
||
| 616 | * |
||
| 617 | * @return Session |
||
| 618 | */ |
||
| 619 | public function getSession() { |
||
| 622 | |||
| 623 | /** |
||
| 624 | * Set the Session object. |
||
| 625 | * |
||
| 626 | * @param Session $session |
||
| 627 | */ |
||
| 628 | public function setSession(Session $session) { |
||
| 631 | |||
| 632 | /** |
||
| 633 | * Joins two or more link segments together, putting a slash between them if necessary. Use this |
||
| 634 | * for building the results of {@link Link()} methods. If either of the links have query strings, |
||
| 635 | * then they will be combined and put at the end of the resulting url. |
||
| 636 | * |
||
| 637 | * Caution: All parameters are expected to be URI-encoded already. |
||
| 638 | * |
||
| 639 | * @param string |
||
| 640 | * |
||
| 641 | * @return string |
||
| 642 | */ |
||
| 643 | public static function join_links() { |
||
| 673 | |||
| 674 | /** |
||
| 675 | * @return array |
||
| 676 | */ |
||
| 677 | public static function get_template_global_variables() { |
||
| 682 | } |
||
| 683 | |||
| 685 |
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: