Complex classes like Director 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 Director, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class Director implements TemplateGlobalProvider |
||
| 30 | { |
||
| 31 | use Configurable; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Specifies this url is relative to the base. |
||
| 35 | * |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | const BASE = 'BASE'; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Specifies this url is relative to the site root. |
||
| 42 | * |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | const ROOT = 'ROOT'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * specifies this url is relative to the current request. |
||
| 49 | * |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | const REQUEST = 'REQUEST'; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @config |
||
| 56 | * @var array |
||
| 57 | */ |
||
| 58 | private static $rules = array(); |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Set current page |
||
| 62 | * |
||
| 63 | * @internal |
||
| 64 | * @var SiteTree |
||
| 65 | */ |
||
| 66 | private static $current_page; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @config |
||
| 70 | * @var string |
||
| 71 | */ |
||
| 72 | private static $alternate_base_folder; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Force the base_url to a specific value. |
||
| 76 | * If assigned, default_base_url and the value in the $_SERVER |
||
| 77 | * global is ignored. |
||
| 78 | * Supports back-ticked vars; E.g. '`SS_BASE_URL`' |
||
| 79 | * |
||
| 80 | * @config |
||
| 81 | * @var string |
||
| 82 | */ |
||
| 83 | private static $alternate_base_url; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Base url to populate if cannot be determined otherwise. |
||
| 87 | * Supports back-ticked vars; E.g. '`SS_BASE_URL`' |
||
| 88 | * |
||
| 89 | * @config |
||
| 90 | * @var string |
||
| 91 | */ |
||
| 92 | private static $default_base_url = '`SS_BASE_URL`'; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Assigned environment type |
||
| 96 | * |
||
| 97 | * @internal |
||
| 98 | * @var string |
||
| 99 | */ |
||
| 100 | protected static $environment_type; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Process the given URL, creating the appropriate controller and executing it. |
||
| 104 | * |
||
| 105 | * Request processing is handled as follows: |
||
| 106 | * - Director::direct() creates a new HTTPResponse object and passes this to |
||
| 107 | * Director::handleRequest(). |
||
| 108 | * - Director::handleRequest($request) checks each of the Director rules and identifies a controller |
||
| 109 | * to handle this request. |
||
| 110 | * - Controller::handleRequest($request) is then called. This will find a rule to handle the URL, |
||
| 111 | * and call the rule handling method. |
||
| 112 | * - RequestHandler::handleRequest($request) is recursively called whenever a rule handling method |
||
| 113 | * returns a RequestHandler object. |
||
| 114 | * |
||
| 115 | * In addition to request processing, Director will manage the session, and perform the output of |
||
| 116 | * the actual response to the browser. |
||
| 117 | * |
||
| 118 | * @uses handleRequest() rule-lookup logic is handled by this. |
||
| 119 | * @uses TestController::handleRequest() This handles the page logic for a Director::direct() call. |
||
| 120 | * @param HTTPRequest $request |
||
| 121 | * @return HTTPResponse |
||
| 122 | * @throws HTTPResponse_Exception |
||
| 123 | */ |
||
| 124 | public static function direct(HTTPRequest $request) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Test a URL request, returning a response object. This method is the counterpart of |
||
| 158 | * Director::direct() that is used in functional testing. It will execute the URL given, and |
||
| 159 | * return the result as an HTTPResponse object. |
||
| 160 | * |
||
| 161 | * @uses TestController::handleRequest() Handles the page logic for a Director::direct() call. |
||
| 162 | * |
||
| 163 | * @param string $url The URL to visit. |
||
| 164 | * @param array $postVars The $_POST & $_FILES variables. |
||
| 165 | * @param array|Session $session The {@link Session} object representing the current session. |
||
| 166 | * By passing the same object to multiple calls of Director::test(), you can simulate a persisted |
||
| 167 | * session. |
||
| 168 | * @param string $httpMethod The HTTP method, such as GET or POST. It will default to POST if |
||
| 169 | * postVars is set, GET otherwise. Overwritten by $postVars['_method'] if present. |
||
| 170 | * @param string $body The HTTP body. |
||
| 171 | * @param array $headers HTTP headers with key-value pairs. |
||
| 172 | * @param array|Cookie_Backend $cookies to populate $_COOKIE. |
||
| 173 | * @param HTTPRequest $request The {@see SS_HTTP_Request} object generated as a part of this request. |
||
| 174 | * |
||
| 175 | * @return HTTPResponse |
||
| 176 | * |
||
| 177 | * @throws HTTPResponse_Exception |
||
| 178 | */ |
||
| 179 | public static function test( |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Mock a request, passing this to the given callback, before resetting. |
||
| 206 | * |
||
| 207 | * @param callable $callback Action to pass the HTTPRequst object |
||
| 208 | * @param string $url The URL to build |
||
| 209 | * @param array $postVars The $_POST & $_FILES variables. |
||
| 210 | * @param array|Session $session The {@link Session} object representing the current session. |
||
| 211 | * By passing the same object to multiple calls of Director::test(), you can simulate a persisted |
||
| 212 | * session. |
||
| 213 | * @param string $httpMethod The HTTP method, such as GET or POST. It will default to POST if |
||
| 214 | * postVars is set, GET otherwise. Overwritten by $postVars['_method'] if present. |
||
| 215 | * @param string $body The HTTP body. |
||
| 216 | * @param array $headers HTTP headers with key-value pairs. |
||
| 217 | * @param array|Cookie_Backend $cookies to populate $_COOKIE. |
||
| 218 | * @param HTTPRequest $request The {@see SS_HTTP_Request} object generated as a part of this request. |
||
| 219 | * @return mixed Result of callback |
||
| 220 | */ |
||
| 221 | public static function mockRequest( |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Handle an HTTP request, defined with a HTTPRequest object. |
||
| 345 | * |
||
| 346 | * @skipUpgrade |
||
| 347 | * @param HTTPRequest $request |
||
| 348 | * @return HTTPResponse |
||
| 349 | */ |
||
| 350 | protected static function handleRequest(HTTPRequest $request) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Return the {@link SiteTree} object that is currently being viewed. If there is no SiteTree |
||
| 402 | * object to return, then this will return the current controller. |
||
| 403 | * |
||
| 404 | * @return SiteTree|Controller |
||
| 405 | */ |
||
| 406 | public static function get_current_page() |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Set the currently active {@link SiteTree} object that is being used to respond to the request. |
||
| 413 | * |
||
| 414 | * @param SiteTree $page |
||
| 415 | */ |
||
| 416 | public static function set_current_page($page) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Turns the given URL into an absolute URL. By default non-site root relative urls will be |
||
| 423 | * evaluated relative to the current base_url. |
||
| 424 | * |
||
| 425 | * @param string $url URL To transform to absolute. |
||
| 426 | * @param string $relativeParent Method to use for evaluating relative urls. |
||
| 427 | * Either one of BASE (baseurl), ROOT (site root), or REQUEST (requested page). |
||
| 428 | * Defaults to BASE, which is the same behaviour as template url resolution. |
||
| 429 | * Ignored if the url is absolute or site root. |
||
| 430 | * |
||
| 431 | * @return string |
||
| 432 | */ |
||
| 433 | public static function absoluteURL($url, $relativeParent = self::BASE) |
||
| 473 | |||
| 474 | /** |
||
| 475 | * A helper to determine the current hostname used to access the site. |
||
| 476 | * The following are used to determine the host (in order) |
||
| 477 | * - Director.alternate_base_url (if it contains a domain name) |
||
| 478 | * - Trusted proxy headers |
||
| 479 | * - HTTP Host header |
||
| 480 | * - SS_BASE_URL env var |
||
| 481 | * - SERVER_NAME |
||
| 482 | * - gethostname() |
||
| 483 | * |
||
| 484 | * @return string |
||
| 485 | */ |
||
| 486 | public static function host() |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Returns the domain part of the URL 'http://www.mysite.com'. Returns FALSE is this environment |
||
| 531 | * variable isn't set. |
||
| 532 | * |
||
| 533 | * @return bool|string |
||
| 534 | */ |
||
| 535 | public static function protocolAndHost() |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Return the current protocol that the site is running under. |
||
| 542 | * |
||
| 543 | * @return string |
||
| 544 | */ |
||
| 545 | public static function protocol() |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Return whether the site is running as under HTTPS. |
||
| 552 | * |
||
| 553 | * @return bool |
||
| 554 | */ |
||
| 555 | public static function is_https() |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Return the root-relative url for the baseurl |
||
| 602 | * |
||
| 603 | * @return string Root-relative url with trailing slash. |
||
| 604 | */ |
||
| 605 | public static function baseURL() |
||
| 625 | |||
| 626 | /** |
||
| 627 | * Returns the root filesystem folder for the site. It will be automatically calculated unless |
||
| 628 | * it is overridden with {@link setBaseFolder()}. |
||
| 629 | * |
||
| 630 | * @return string |
||
| 631 | */ |
||
| 632 | public static function baseFolder() |
||
| 637 | |||
| 638 | /** |
||
| 639 | * Turns an absolute URL or folder into one that's relative to the root of the site. This is useful |
||
| 640 | * when turning a URL into a filesystem reference, or vice versa. |
||
| 641 | * |
||
| 642 | * @param string $url Accepts both a URL or a filesystem path. |
||
| 643 | * |
||
| 644 | * @return string |
||
| 645 | */ |
||
| 646 | public static function makeRelative($url) |
||
| 694 | |||
| 695 | /** |
||
| 696 | * Returns true if a given path is absolute. Works under both *nix and windows systems. |
||
| 697 | * |
||
| 698 | * @param string $path |
||
| 699 | * |
||
| 700 | * @return bool |
||
| 701 | */ |
||
| 702 | public static function is_absolute($path) |
||
| 712 | |||
| 713 | /** |
||
| 714 | * Determine if the url is root relative (i.e. starts with /, but not with //) SilverStripe |
||
| 715 | * considers root relative urls as a subset of relative urls. |
||
| 716 | * |
||
| 717 | * @param string $url |
||
| 718 | * |
||
| 719 | * @return bool |
||
| 720 | */ |
||
| 721 | public static function is_root_relative_url($url) |
||
| 725 | |||
| 726 | /** |
||
| 727 | * Checks if a given URL is absolute (e.g. starts with 'http://' etc.). URLs beginning with "//" |
||
| 728 | * are treated as absolute, as browsers take this to mean the same protocol as currently being used. |
||
| 729 | * |
||
| 730 | * Useful to check before redirecting based on a URL from user submissions through $_GET or $_POST, |
||
| 731 | * and avoid phishing attacks by redirecting to an attackers server. |
||
| 732 | * |
||
| 733 | * Note: Can't solely rely on PHP's parse_url() , since it is not intended to work with relative URLs |
||
| 734 | * or for security purposes. filter_var($url, FILTER_VALIDATE_URL) has similar problems. |
||
| 735 | * |
||
| 736 | * @param string $url |
||
| 737 | * |
||
| 738 | * @return bool |
||
| 739 | */ |
||
| 740 | public static function is_absolute_url($url) |
||
| 766 | |||
| 767 | /** |
||
| 768 | * Checks if a given URL is relative (or root relative) by checking {@link is_absolute_url()}. |
||
| 769 | * |
||
| 770 | * @param string $url |
||
| 771 | * |
||
| 772 | * @return bool |
||
| 773 | */ |
||
| 774 | public static function is_relative_url($url) |
||
| 778 | |||
| 779 | /** |
||
| 780 | * Checks if the given URL is belonging to this "site" (not an external link). That's the case if |
||
| 781 | * the URL is relative, as defined by {@link is_relative_url()}, or if the host matches |
||
| 782 | * {@link protocolAndHost()}. |
||
| 783 | * |
||
| 784 | * Useful to check before redirecting based on a URL from user submissions through $_GET or $_POST, |
||
| 785 | * and avoid phishing attacks by redirecting to an attackers server. |
||
| 786 | * |
||
| 787 | * @param string $url |
||
| 788 | * |
||
| 789 | * @return bool |
||
| 790 | */ |
||
| 791 | public static function is_site_url($url) |
||
| 801 | |||
| 802 | /** |
||
| 803 | * Given a filesystem reference relative to the site root, return the full file-system path. |
||
| 804 | * |
||
| 805 | * @param string $file |
||
| 806 | * |
||
| 807 | * @return string |
||
| 808 | */ |
||
| 809 | public static function getAbsFile($file) |
||
| 813 | |||
| 814 | /** |
||
| 815 | * Returns true if the given file exists. Filename should be relative to the site root. |
||
| 816 | * |
||
| 817 | * @param $file |
||
| 818 | * |
||
| 819 | * @return bool |
||
| 820 | */ |
||
| 821 | public static function fileExists($file) |
||
| 827 | |||
| 828 | /** |
||
| 829 | * Returns the Absolute URL of the site root. |
||
| 830 | * |
||
| 831 | * @return string |
||
| 832 | */ |
||
| 833 | public static function absoluteBaseURL() |
||
| 840 | |||
| 841 | /** |
||
| 842 | * Returns the Absolute URL of the site root, embedding the current basic-auth credentials into |
||
| 843 | * the URL. |
||
| 844 | * |
||
| 845 | * @return string |
||
| 846 | */ |
||
| 847 | public static function absoluteBaseURLWithAuth() |
||
| 857 | |||
| 858 | /** |
||
| 859 | * Skip any further processing and immediately respond with a redirect to the passed URL. |
||
| 860 | * |
||
| 861 | * @param string $destURL |
||
| 862 | * @throws HTTPResponse_Exception |
||
| 863 | */ |
||
| 864 | protected static function force_redirect($destURL) |
||
| 872 | |||
| 873 | /** |
||
| 874 | * Force the site to run on SSL. |
||
| 875 | * |
||
| 876 | * To use, call from _config.php. For example: |
||
| 877 | * <code> |
||
| 878 | * if (Director::isLive()) Director::forceSSL(); |
||
| 879 | * </code> |
||
| 880 | * |
||
| 881 | * If you don't want your entire site to be on SSL, you can pass an array of PCRE regular expression |
||
| 882 | * patterns for matching relative URLs. For example: |
||
| 883 | * <code> |
||
| 884 | * if (Director::isLive()) Director::forceSSL(array('/^admin/', '/^Security/')); |
||
| 885 | * </code> |
||
| 886 | * |
||
| 887 | * If you want certain parts of your site protected under a different domain, you can specify |
||
| 888 | * the domain as an argument: |
||
| 889 | * <code> |
||
| 890 | * if (Director::isLive()) Director::forceSSL(array('/^admin/', '/^Security/'), 'secure.mysite.com'); |
||
| 891 | * </code> |
||
| 892 | * |
||
| 893 | * Note that the session data will be lost when moving from HTTP to HTTPS. It is your responsibility |
||
| 894 | * to ensure that this won't cause usability problems. |
||
| 895 | * |
||
| 896 | * CAUTION: This does not respect the site environment mode. You should check this |
||
| 897 | * as per the above examples using Director::isLive() or Director::isTest() for example. |
||
| 898 | * |
||
| 899 | * @param array $patterns Array of regex patterns to match URLs that should be HTTPS. |
||
| 900 | * @param string $secureDomain Secure domain to redirect to. Defaults to the current domain. |
||
| 901 | * @return bool true if already on SSL, false if doesn't match patterns (or cannot redirect) |
||
| 902 | * @throws HTTPResponse_Exception Throws exception with redirect, if successful |
||
| 903 | */ |
||
| 904 | public static function forceSSL($patterns = null, $secureDomain = null) |
||
| 942 | |||
| 943 | /** |
||
| 944 | * Force a redirect to a domain starting with "www." |
||
| 945 | */ |
||
| 946 | public static function forceWWW() |
||
| 958 | |||
| 959 | /** |
||
| 960 | * Checks if the current HTTP-Request is an "Ajax-Request" by checking for a custom header set by |
||
| 961 | * jQuery or whether a manually set request-parameter 'ajax' is present. |
||
| 962 | * |
||
| 963 | * @return bool |
||
| 964 | */ |
||
| 965 | public static function is_ajax() |
||
| 976 | |||
| 977 | /** |
||
| 978 | * Returns true if this script is being run from the command line rather than the web server. |
||
| 979 | * |
||
| 980 | * @return bool |
||
| 981 | */ |
||
| 982 | public static function is_cli() |
||
| 986 | |||
| 987 | /** |
||
| 988 | * Can also be checked with {@link Director::isDev()}, {@link Director::isTest()}, and |
||
| 989 | * {@link Director::isLive()}. |
||
| 990 | * |
||
| 991 | * @return bool |
||
| 992 | */ |
||
| 993 | public static function get_environment_type() |
||
| 999 | |||
| 1000 | /** |
||
| 1001 | * This function will return true if the site is in a live environment. For information about |
||
| 1002 | * environment types, see {@link Director::set_environment_type()}. |
||
| 1003 | * |
||
| 1004 | * @return bool |
||
| 1005 | */ |
||
| 1006 | public static function isLive() |
||
| 1010 | |||
| 1011 | /** |
||
| 1012 | * This function will return true if the site is in a development environment. For information about |
||
| 1013 | * environment types, see {@link Director::set_environment_type()}. |
||
| 1014 | * |
||
| 1015 | * @return bool |
||
| 1016 | */ |
||
| 1017 | public static function isDev() |
||
| 1021 | |||
| 1022 | /** |
||
| 1023 | * This function will return true if the site is in a test environment. For information about |
||
| 1024 | * environment types, see {@link Director::set_environment_type()}. |
||
| 1025 | * |
||
| 1026 | * @return bool |
||
| 1027 | */ |
||
| 1028 | public static function isTest() |
||
| 1032 | |||
| 1033 | /** |
||
| 1034 | * Returns an array of strings of the method names of methods on the call that should be exposed |
||
| 1035 | * as global variables in the templates. |
||
| 1036 | * |
||
| 1037 | * @return array |
||
| 1038 | */ |
||
| 1039 | public static function get_template_global_variables() |
||
| 1049 | } |
||
| 1050 |
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: