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 |
||
| 21 | class Controller extends RequestHandler implements TemplateGlobalProvider |
||
| 22 | { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * An array of arguments extracted from the URL. |
||
| 26 | * |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | protected $urlParams; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Contains all GET and POST parameters passed to the current {@link HTTPRequest}. |
||
| 33 | * |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | protected $requestParams; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The URL part matched on the current controller as determined by the "$Action" part of the |
||
| 40 | * {@link $url_handlers} definition. Should correlate to a public method on this controller. |
||
| 41 | * |
||
| 42 | * Used in {@link render()} and {@link getViewer()} to determine action-specific templates. |
||
| 43 | * |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | protected $action; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Stack of current controllers. Controller::$controller_stack[0] is the current controller. |
||
| 50 | * |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | protected static $controller_stack = array(); |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Assign templates for this controller. |
||
| 57 | * Map of action => template name |
||
| 58 | * |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | protected $templates = []; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var bool |
||
| 65 | */ |
||
| 66 | protected $basicAuthEnabled = true; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * The response object that the controller returns. |
||
| 70 | * |
||
| 71 | * Set in {@link handleRequest()}. |
||
| 72 | * |
||
| 73 | * @var HTTPResponse |
||
| 74 | */ |
||
| 75 | protected $response; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Default URL handlers. |
||
| 79 | * |
||
| 80 | * @var array |
||
| 81 | */ |
||
| 82 | private static $url_handlers = array( |
||
| 83 | '$Action//$ID/$OtherID' => 'handleAction', |
||
| 84 | ); |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var array |
||
| 88 | */ |
||
| 89 | private static $allowed_actions = array( |
||
| 90 | 'handleAction', |
||
| 91 | 'handleIndex', |
||
| 92 | ); |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Initialisation function that is run before any action on the controller is called. |
||
| 96 | * |
||
| 97 | * @uses BasicAuth::requireLogin() |
||
| 98 | */ |
||
| 99 | protected function init() |
||
| 100 | { |
||
| 101 | if ($this->basicAuthEnabled) { |
||
| 102 | BasicAuth::protect_site_if_necessary(); |
||
| 103 | } |
||
| 104 | |||
| 105 | // This is used to test that subordinate controllers are actually calling parent::init() - a common bug |
||
| 106 | $this->baseInitCalled = true; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * A stand in function to protect the init function from failing to be called as well as providing before and |
||
| 111 | * after hooks for the init function itself |
||
| 112 | * |
||
| 113 | * This should be called on all controllers before handling requests |
||
| 114 | */ |
||
| 115 | public function doInit() |
||
| 116 | { |
||
| 117 | //extension hook |
||
| 118 | $this->extend('onBeforeInit'); |
||
| 119 | |||
| 120 | // Safety call |
||
| 121 | $this->baseInitCalled = false; |
||
| 122 | $this->init(); |
||
| 123 | if (!$this->baseInitCalled) { |
||
| 124 | $class = static::class; |
||
| 125 | user_error( |
||
| 126 | "init() method on class '{$class}' doesn't call Controller::init()." |
||
| 127 | . "Make sure that you have parent::init() included.", |
||
| 128 | E_USER_WARNING |
||
| 129 | ); |
||
| 130 | } |
||
| 131 | |||
| 132 | $this->extend('onAfterInit'); |
||
| 133 | } |
||
| 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 | */ |
||
| 155 | protected function beforeHandleRequest(HTTPRequest $request) |
||
| 156 | { |
||
| 157 | //Set up the internal dependencies (request, response) |
||
| 158 | $this->setRequest($request); |
||
| 159 | //Push the current controller to protect against weird session issues |
||
| 160 | $this->pushCurrent(); |
||
| 161 | $this->setResponse(new HTTPResponse()); |
||
| 162 | //kick off the init functionality |
||
| 163 | $this->doInit(); |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Cleanup for the handleRequest method |
||
| 168 | */ |
||
| 169 | protected function afterHandleRequest() |
||
| 170 | { |
||
| 171 | //Pop the current controller from the stack |
||
| 172 | $this->popCurrent(); |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Executes this controller, and return an {@link HTTPResponse} object with the result. |
||
| 177 | * |
||
| 178 | * This method defers to {@link RequestHandler->handleRequest()} to determine which action |
||
| 179 | * should be executed |
||
| 180 | * |
||
| 181 | * Note: You should rarely need to overload handleRequest() - |
||
| 182 | * this kind of change is only really appropriate for things like nested |
||
| 183 | * controllers - {@link ModelAsController} and {@link RootURLController} |
||
| 184 | * are two examples here. If you want to make more |
||
| 185 | * orthodox functionality, it's better to overload {@link init()} or {@link index()}. |
||
| 186 | * |
||
| 187 | * Important: If you are going to overload handleRequest, |
||
| 188 | * make sure that you start the method with $this->beforeHandleRequest() |
||
| 189 | * and end the method with $this->afterHandleRequest() |
||
| 190 | * |
||
| 191 | * @param HTTPRequest $request |
||
| 192 | * @return HTTPResponse |
||
| 193 | */ |
||
| 194 | public function handleRequest(HTTPRequest $request) |
||
| 195 | { |
||
| 196 | if (!$request) { |
||
| 197 | user_error("Controller::handleRequest() not passed a request!", E_USER_ERROR); |
||
| 198 | } |
||
| 199 | |||
| 200 | //set up the controller for the incoming request |
||
| 201 | $this->beforeHandleRequest($request); |
||
| 202 | |||
| 203 | //if the before handler manipulated the response in a way that we shouldn't proceed, then skip our request |
||
| 204 | // handling |
||
| 205 | if (!$this->getResponse()->isFinished()) { |
||
| 206 | //retrieve the response for the request |
||
| 207 | $response = parent::handleRequest($request); |
||
| 208 | |||
| 209 | //prepare the response (we can receive an assortment of response types (strings/objects/HTTPResponses) |
||
| 210 | $this->prepareResponse($response); |
||
| 211 | } |
||
| 212 | |||
| 213 | //after request work |
||
| 214 | $this->afterHandleRequest(); |
||
| 215 | |||
| 216 | //return the response |
||
| 217 | return $this->getResponse(); |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Prepare the response (we can receive an assortment of response types (strings/objects/HTTPResponses) and |
||
| 222 | * changes the controller response object appropriately |
||
| 223 | * |
||
| 224 | * @param HTTPResponse|Object $response |
||
| 225 | */ |
||
| 226 | protected function prepareResponse($response) |
||
|
|
|||
| 227 | { |
||
| 228 | if ($response instanceof HTTPResponse) { |
||
| 229 | if (isset($_REQUEST['debug_request'])) { |
||
| 230 | $class = static::class; |
||
| 231 | Debug::message( |
||
| 232 | "Request handler returned HTTPResponse object to {$class} controller;" |
||
| 233 | . "returning it without modification." |
||
| 234 | ); |
||
| 235 | } |
||
| 236 | $this->setResponse($response); |
||
| 237 | } else { |
||
| 238 | // Could be Controller, or ViewableData_Customised controller wrapper |
||
| 239 | if (ClassInfo::hasMethod($response, 'getViewer')) { |
||
| 240 | if (isset($_REQUEST['debug_request'])) { |
||
| 241 | $class = static::class; |
||
| 242 | $responseClass = get_class($response); |
||
| 243 | Debug::message( |
||
| 244 | "Request handler {$responseClass} object to {$class} controller;" |
||
| 245 | . "rendering with template returned by {$responseClass}::getViewer()" |
||
| 246 | ); |
||
| 247 | } |
||
| 248 | $response = $response->getViewer($this->getAction())->process($response); |
||
| 249 | } |
||
| 250 | |||
| 251 | $this->getResponse()->setBody($response); |
||
| 252 | } |
||
| 253 | |||
| 254 | //deal with content if appropriate |
||
| 255 | ContentNegotiator::process($this->getResponse()); |
||
| 256 | |||
| 257 | //add cache headers |
||
| 258 | HTTP::add_cache_headers($this->getResponse()); |
||
| 259 | } |
||
| 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) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @param string $action |
||
| 422 | * |
||
| 423 | * @return bool |
||
| 424 | */ |
||
| 425 | public function hasAction($action) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Removes all the "action" part of the current URL and returns the result. If no action parameter |
||
| 432 | * is present, returns the full URL. |
||
| 433 | * |
||
| 434 | * @param string $fullURL |
||
| 435 | * @param null|string $action |
||
| 436 | * |
||
| 437 | * @return string |
||
| 438 | */ |
||
| 439 | public function removeAction($fullURL, $action = null) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Return the class that defines the given action, so that we know where to check allowed_actions. |
||
| 455 | * Overrides RequestHandler to also look at defined templates. |
||
| 456 | * |
||
| 457 | * @param string $action |
||
| 458 | * |
||
| 459 | * @return string |
||
| 460 | */ |
||
| 461 | protected function definingClassForAction($action) |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Returns TRUE if this controller has a template that is specifically designed to handle a |
||
| 483 | * specific action. |
||
| 484 | * |
||
| 485 | * @param string $action |
||
| 486 | * |
||
| 487 | * @return bool |
||
| 488 | */ |
||
| 489 | public function hasActionTemplate($action) |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Render the current controller with the templates determined by {@link getViewer()}. |
||
| 508 | * |
||
| 509 | * @param array $params |
||
| 510 | * |
||
| 511 | * @return string |
||
| 512 | */ |
||
| 513 | public function render($params = null) |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Call this to disable site-wide basic authentication for a specific controller. This must be |
||
| 529 | * called before Controller::init(). That is, you must call it in your controller's init method |
||
| 530 | * before it calls parent::init(). |
||
| 531 | */ |
||
| 532 | public function disableBasicAuth() |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Returns the current controller. |
||
| 539 | * |
||
| 540 | * @return Controller |
||
| 541 | */ |
||
| 542 | public static function curr() |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Tests whether we have a currently active controller or not. True if there is at least 1 |
||
| 553 | * controller in the stack. |
||
| 554 | * |
||
| 555 | * @return bool |
||
| 556 | */ |
||
| 557 | public static function has_curr() |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Returns true if the member is allowed to do the given action. Defaults to the currently logged |
||
| 564 | * in user. |
||
| 565 | * |
||
| 566 | * @param string $perm |
||
| 567 | * @param null|member $member |
||
| 568 | * |
||
| 569 | * @return bool |
||
| 570 | */ |
||
| 571 | public function can($perm, $member = null) |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Pushes this controller onto the stack of current controllers. This means that any redirection, |
||
| 589 | * session setting, or other things that rely on Controller::curr() will now write to this |
||
| 590 | * controller object. |
||
| 591 | * |
||
| 592 | * Note: Ensure this controller is assigned a request with a valid session before pushing |
||
| 593 | * it to the stack. |
||
| 594 | */ |
||
| 595 | public function pushCurrent() |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Pop this controller off the top of the stack. |
||
| 604 | */ |
||
| 605 | public function popCurrent() |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Redirect to the given URL. |
||
| 620 | * |
||
| 621 | * @param string $url |
||
| 622 | * @param int $code |
||
| 623 | * @return HTTPResponse |
||
| 624 | */ |
||
| 625 | public function redirect($url, $code = 302) |
||
| 636 | |||
| 637 | /** |
||
| 638 | * Tests whether a redirection has been requested. If redirect() has been called, it will return |
||
| 639 | * the URL redirected to. Otherwise, it will return null. |
||
| 640 | * |
||
| 641 | * @return null|string |
||
| 642 | */ |
||
| 643 | public function redirectedTo() |
||
| 647 | |||
| 648 | /** |
||
| 649 | * Joins two or more link segments together, putting a slash between them if necessary. Use this |
||
| 650 | * for building the results of {@link Link()} methods. If either of the links have query strings, |
||
| 651 | * then they will be combined and put at the end of the resulting url. |
||
| 652 | * |
||
| 653 | * Caution: All parameters are expected to be URI-encoded already. |
||
| 654 | * |
||
| 655 | * @param string|array $arg,.. One or more link segments, or list of link segments as an array |
||
| 656 | * @return string |
||
| 657 | */ |
||
| 658 | public static function join_links($arg = null) |
||
| 700 | |||
| 701 | /** |
||
| 702 | * @return array |
||
| 703 | */ |
||
| 704 | public static function get_template_global_variables() |
||
| 710 | } |
||
| 711 |
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: