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 |
||
| 32 | class Director implements TemplateGlobalProvider |
||
| 33 | { |
||
| 34 | use Configurable; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Specifies this url is relative to the base. |
||
| 38 | * |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | const BASE = 'BASE'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Specifies this url is relative to the site root. |
||
| 45 | * |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | const ROOT = 'ROOT'; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * specifies this url is relative to the current request. |
||
| 52 | * |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | const REQUEST = 'REQUEST'; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @config |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | private static $rules = array(); |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Set current page |
||
| 65 | * |
||
| 66 | * @internal |
||
| 67 | * @var SiteTree |
||
| 68 | */ |
||
| 69 | private static $current_page; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @config |
||
| 73 | * @var string |
||
| 74 | */ |
||
| 75 | private static $alternate_base_folder; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Force the base_url to a specific value. |
||
| 79 | * If assigned, default_base_url and the value in the $_SERVER |
||
| 80 | * global is ignored. |
||
| 81 | * Supports back-ticked vars; E.g. '`SS_BASE_URL`' |
||
| 82 | * |
||
| 83 | * @config |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | private static $alternate_base_url; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Base url to populate if cannot be determined otherwise. |
||
| 90 | * Supports back-ticked vars; E.g. '`SS_BASE_URL`' |
||
| 91 | * |
||
| 92 | * @config |
||
| 93 | * @var string |
||
| 94 | */ |
||
| 95 | private static $default_base_url = '`SS_BASE_URL`'; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Assigned environment type |
||
| 99 | * |
||
| 100 | * @internal |
||
| 101 | * @var string |
||
| 102 | */ |
||
| 103 | protected static $environment_type; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Process the given URL, creating the appropriate controller and executing it. |
||
| 107 | * |
||
| 108 | * Request processing is handled as follows: |
||
| 109 | * - Director::direct() creates a new HTTPResponse object and passes this to |
||
| 110 | * Director::handleRequest(). |
||
| 111 | * - Director::handleRequest($request) checks each of the Director rules and identifies a controller |
||
| 112 | * to handle this request. |
||
| 113 | * - Controller::handleRequest($request) is then called. This will find a rule to handle the URL, |
||
| 114 | * and call the rule handling method. |
||
| 115 | * - RequestHandler::handleRequest($request) is recursively called whenever a rule handling method |
||
| 116 | * returns a RequestHandler object. |
||
| 117 | * |
||
| 118 | * In addition to request processing, Director will manage the session, and perform the output of |
||
| 119 | * the actual response to the browser. |
||
| 120 | * |
||
| 121 | * @uses handleRequest() rule-lookup logic is handled by this. |
||
| 122 | * @uses TestController::handleRequest() This handles the page logic for a Director::direct() call. |
||
| 123 | * @param string $url |
||
| 124 | * @param DataModel $model |
||
| 125 | * @throws HTTPResponse_Exception |
||
| 126 | */ |
||
| 127 | public static function direct($url, DataModel $model) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Test a URL request, returning a response object. This method is the counterpart of |
||
| 233 | * Director::direct() that is used in functional testing. It will execute the URL given, and |
||
| 234 | * return the result as an HTTPResponse object. |
||
| 235 | * |
||
| 236 | * @uses TestController::handleRequest() Handles the page logic for a Director::direct() call. |
||
| 237 | * |
||
| 238 | * @param string $url The URL to visit. |
||
| 239 | * @param array $postVars The $_POST & $_FILES variables. |
||
| 240 | * @param array|Session $session The {@link Session} object representing the current session. |
||
| 241 | * By passing the same object to multiple calls of Director::test(), you can simulate a persisted |
||
| 242 | * session. |
||
| 243 | * @param string $httpMethod The HTTP method, such as GET or POST. It will default to POST if |
||
| 244 | * postVars is set, GET otherwise. Overwritten by $postVars['_method'] if present. |
||
| 245 | * @param string $body The HTTP body. |
||
| 246 | * @param array $headers HTTP headers with key-value pairs. |
||
| 247 | * @param array|Cookie_Backend $cookies to populate $_COOKIE. |
||
| 248 | * @param HTTPRequest $request The {@see SS_HTTP_Request} object generated as a part of this request. |
||
| 249 | * |
||
| 250 | * @return HTTPResponse |
||
| 251 | * |
||
| 252 | * @throws HTTPResponse_Exception |
||
| 253 | */ |
||
| 254 | public static function test( |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Handle an HTTP request, defined with a HTTPRequest object. |
||
| 395 | * |
||
| 396 | * @skipUpgrade |
||
| 397 | * @param HTTPRequest $request |
||
| 398 | * @param Session $session |
||
| 399 | * @param DataModel $model |
||
| 400 | * @return HTTPResponse|string |
||
| 401 | */ |
||
| 402 | protected static function handleRequest(HTTPRequest $request, Session $session, DataModel $model) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Return the {@link SiteTree} object that is currently being viewed. If there is no SiteTree |
||
| 455 | * object to return, then this will return the current controller. |
||
| 456 | * |
||
| 457 | * @return SiteTree|Controller |
||
| 458 | */ |
||
| 459 | public static function get_current_page() |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Set the currently active {@link SiteTree} object that is being used to respond to the request. |
||
| 466 | * |
||
| 467 | * @param SiteTree $page |
||
| 468 | */ |
||
| 469 | public static function set_current_page($page) |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Turns the given URL into an absolute URL. By default non-site root relative urls will be |
||
| 476 | * evaluated relative to the current base_url. |
||
| 477 | * |
||
| 478 | * @param string $url URL To transform to absolute. |
||
| 479 | * @param string $relativeParent Method to use for evaluating relative urls. |
||
| 480 | * Either one of BASE (baseurl), ROOT (site root), or REQUEST (requested page). |
||
| 481 | * Defaults to BASE, which is the same behaviour as template url resolution. |
||
| 482 | * Ignored if the url is absolute or site root. |
||
| 483 | * |
||
| 484 | * @return string |
||
| 485 | */ |
||
| 486 | public static function absoluteURL($url, $relativeParent = self::BASE) |
||
| 526 | |||
| 527 | /** |
||
| 528 | * A helper to determine the current hostname used to access the site. |
||
| 529 | * The following are used to determine the host (in order) |
||
| 530 | * - Director.alternate_base_url (if it contains a domain name) |
||
| 531 | * - Trusted proxy headers |
||
| 532 | * - HTTP Host header |
||
| 533 | * - SS_BASE_URL env var |
||
| 534 | * - SERVER_NAME |
||
| 535 | * - gethostname() |
||
| 536 | * |
||
| 537 | * @return string |
||
| 538 | */ |
||
| 539 | public static function host() |
||
| 592 | |||
| 593 | /** |
||
| 594 | * Returns the domain part of the URL 'http://www.mysite.com'. Returns FALSE is this environment |
||
| 595 | * variable isn't set. |
||
| 596 | * |
||
| 597 | * @return bool|string |
||
| 598 | */ |
||
| 599 | public static function protocolAndHost() |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Return the current protocol that the site is running under. |
||
| 606 | * |
||
| 607 | * @return string |
||
| 608 | */ |
||
| 609 | public static function protocol() |
||
| 613 | |||
| 614 | /** |
||
| 615 | * Return whether the site is running as under HTTPS. |
||
| 616 | * |
||
| 617 | * @return bool |
||
| 618 | */ |
||
| 619 | public static function is_https() |
||
| 663 | |||
| 664 | /** |
||
| 665 | * Return the root-relative url for the baseurl |
||
| 666 | * |
||
| 667 | * @return string Root-relative url with trailing slash. |
||
| 668 | */ |
||
| 669 | public static function baseURL() |
||
| 689 | |||
| 690 | /** |
||
| 691 | * Returns the root filesystem folder for the site. It will be automatically calculated unless |
||
| 692 | * it is overridden with {@link setBaseFolder()}. |
||
| 693 | * |
||
| 694 | * @return string |
||
| 695 | */ |
||
| 696 | public static function baseFolder() |
||
| 701 | |||
| 702 | /** |
||
| 703 | * Turns an absolute URL or folder into one that's relative to the root of the site. This is useful |
||
| 704 | * when turning a URL into a filesystem reference, or vice versa. |
||
| 705 | * |
||
| 706 | * @param string $url Accepts both a URL or a filesystem path. |
||
| 707 | * |
||
| 708 | * @return string |
||
| 709 | */ |
||
| 710 | public static function makeRelative($url) |
||
| 758 | |||
| 759 | /** |
||
| 760 | * Returns true if a given path is absolute. Works under both *nix and windows systems. |
||
| 761 | * |
||
| 762 | * @param string $path |
||
| 763 | * |
||
| 764 | * @return bool |
||
| 765 | */ |
||
| 766 | public static function is_absolute($path) |
||
| 776 | |||
| 777 | /** |
||
| 778 | * Determine if the url is root relative (i.e. starts with /, but not with //) SilverStripe |
||
| 779 | * considers root relative urls as a subset of relative urls. |
||
| 780 | * |
||
| 781 | * @param string $url |
||
| 782 | * |
||
| 783 | * @return bool |
||
| 784 | */ |
||
| 785 | public static function is_root_relative_url($url) |
||
| 789 | |||
| 790 | /** |
||
| 791 | * Checks if a given URL is absolute (e.g. starts with 'http://' etc.). URLs beginning with "//" |
||
| 792 | * are treated as absolute, as browsers take this to mean the same protocol as currently being used. |
||
| 793 | * |
||
| 794 | * Useful to check before redirecting based on a URL from user submissions through $_GET or $_POST, |
||
| 795 | * and avoid phishing attacks by redirecting to an attackers server. |
||
| 796 | * |
||
| 797 | * Note: Can't solely rely on PHP's parse_url() , since it is not intended to work with relative URLs |
||
| 798 | * or for security purposes. filter_var($url, FILTER_VALIDATE_URL) has similar problems. |
||
| 799 | * |
||
| 800 | * @param string $url |
||
| 801 | * |
||
| 802 | * @return bool |
||
| 803 | */ |
||
| 804 | public static function is_absolute_url($url) |
||
| 830 | |||
| 831 | /** |
||
| 832 | * Checks if a given URL is relative (or root relative) by checking {@link is_absolute_url()}. |
||
| 833 | * |
||
| 834 | * @param string $url |
||
| 835 | * |
||
| 836 | * @return bool |
||
| 837 | */ |
||
| 838 | public static function is_relative_url($url) |
||
| 842 | |||
| 843 | /** |
||
| 844 | * Checks if the given URL is belonging to this "site" (not an external link). That's the case if |
||
| 845 | * the URL is relative, as defined by {@link is_relative_url()}, or if the host matches |
||
| 846 | * {@link protocolAndHost()}. |
||
| 847 | * |
||
| 848 | * Useful to check before redirecting based on a URL from user submissions through $_GET or $_POST, |
||
| 849 | * and avoid phishing attacks by redirecting to an attackers server. |
||
| 850 | * |
||
| 851 | * @param string $url |
||
| 852 | * |
||
| 853 | * @return bool |
||
| 854 | */ |
||
| 855 | public static function is_site_url($url) |
||
| 865 | |||
| 866 | /** |
||
| 867 | * Takes a $_SERVER data array and extracts HTTP request headers. |
||
| 868 | * |
||
| 869 | * @param array $server |
||
| 870 | * |
||
| 871 | * @return array |
||
| 872 | */ |
||
| 873 | public static function extract_request_headers(array $server) |
||
| 895 | |||
| 896 | /** |
||
| 897 | * Given a filesystem reference relative to the site root, return the full file-system path. |
||
| 898 | * |
||
| 899 | * @param string $file |
||
| 900 | * |
||
| 901 | * @return string |
||
| 902 | */ |
||
| 903 | public static function getAbsFile($file) |
||
| 907 | |||
| 908 | /** |
||
| 909 | * Returns true if the given file exists. Filename should be relative to the site root. |
||
| 910 | * |
||
| 911 | * @param $file |
||
| 912 | * |
||
| 913 | * @return bool |
||
| 914 | */ |
||
| 915 | public static function fileExists($file) |
||
| 921 | |||
| 922 | /** |
||
| 923 | * Returns the Absolute URL of the site root. |
||
| 924 | * |
||
| 925 | * @return string |
||
| 926 | */ |
||
| 927 | public static function absoluteBaseURL() |
||
| 934 | |||
| 935 | /** |
||
| 936 | * Returns the Absolute URL of the site root, embedding the current basic-auth credentials into |
||
| 937 | * the URL. |
||
| 938 | * |
||
| 939 | * @return string |
||
| 940 | */ |
||
| 941 | public static function absoluteBaseURLWithAuth() |
||
| 951 | |||
| 952 | /** |
||
| 953 | * Skip any further processing and immediately respond with a redirect to the passed URL. |
||
| 954 | * |
||
| 955 | * @param string $destURL |
||
| 956 | */ |
||
| 957 | protected static function force_redirect($destURL) |
||
| 968 | |||
| 969 | /** |
||
| 970 | * Force the site to run on SSL. |
||
| 971 | * |
||
| 972 | * To use, call from _config.php. For example: |
||
| 973 | * <code> |
||
| 974 | * if (Director::isLive()) Director::forceSSL(); |
||
| 975 | * </code> |
||
| 976 | * |
||
| 977 | * If you don't want your entire site to be on SSL, you can pass an array of PCRE regular expression |
||
| 978 | * patterns for matching relative URLs. For example: |
||
| 979 | * <code> |
||
| 980 | * if (Director::isLive()) Director::forceSSL(array('/^admin/', '/^Security/')); |
||
| 981 | * </code> |
||
| 982 | * |
||
| 983 | * If you want certain parts of your site protected under a different domain, you can specify |
||
| 984 | * the domain as an argument: |
||
| 985 | * <code> |
||
| 986 | * if (Director::isLive()) Director::forceSSL(array('/^admin/', '/^Security/'), 'secure.mysite.com'); |
||
| 987 | * </code> |
||
| 988 | * |
||
| 989 | * Note that the session data will be lost when moving from HTTP to HTTPS. It is your responsibility |
||
| 990 | * to ensure that this won't cause usability problems. |
||
| 991 | * |
||
| 992 | * CAUTION: This does not respect the site environment mode. You should check this |
||
| 993 | * as per the above examples using Director::isLive() or Director::isTest() for example. |
||
| 994 | * |
||
| 995 | * @param array $patterns Array of regex patterns to match URLs that should be HTTPS. |
||
| 996 | * @param string $secureDomain Secure domain to redirect to. Defaults to the current domain. |
||
| 997 | * |
||
| 998 | * @return bool|string String of URL when unit tests running, boolean FALSE if patterns don't match request URI. |
||
| 999 | */ |
||
| 1000 | public static function forceSSL($patterns = null, $secureDomain = null) |
||
| 1045 | |||
| 1046 | /** |
||
| 1047 | * Force a redirect to a domain starting with "www." |
||
| 1048 | */ |
||
| 1049 | public static function forceWWW() |
||
| 1061 | |||
| 1062 | /** |
||
| 1063 | * Checks if the current HTTP-Request is an "Ajax-Request" by checking for a custom header set by |
||
| 1064 | * jQuery or whether a manually set request-parameter 'ajax' is present. |
||
| 1065 | * |
||
| 1066 | * @return bool |
||
| 1067 | */ |
||
| 1068 | public static function is_ajax() |
||
| 1079 | |||
| 1080 | /** |
||
| 1081 | * Returns true if this script is being run from the command line rather than the web server. |
||
| 1082 | * |
||
| 1083 | * @return bool |
||
| 1084 | */ |
||
| 1085 | public static function is_cli() |
||
| 1089 | |||
| 1090 | /** |
||
| 1091 | * Set the environment type of the current site. |
||
| 1092 | * |
||
| 1093 | * Typically, a SilverStripe site have a number of environments: |
||
| 1094 | * - Development environments, such a copy on your local machine. |
||
| 1095 | * - Test sites, such as the one you show the client before going live. |
||
| 1096 | * - The live site itself. |
||
| 1097 | * |
||
| 1098 | * The behaviour of these environments often varies slightly. For example, development sites may |
||
| 1099 | * have errors dumped to the screen, and order confirmation emails might be sent to the developer |
||
| 1100 | * instead of the client. |
||
| 1101 | * |
||
| 1102 | * To help with this, SilverStripe supports the notion of an environment type. The environment |
||
| 1103 | * type can be dev, test, or live. |
||
| 1104 | * |
||
| 1105 | * Dev mode can also be forced by putting ?isDev=1 in your URL, which will ask you to log in and |
||
| 1106 | * then push the site into dev mode for the remainder of the session. Putting ?isDev=0 onto the URL |
||
| 1107 | * can turn it back. |
||
| 1108 | * |
||
| 1109 | * Test mode can also be forced by putting ?isTest=1 in your URL, which will ask you to log in and |
||
| 1110 | * then push the site into test mode for the remainder of the session. Putting ?isTest=0 onto the URL |
||
| 1111 | * can turn it back. |
||
| 1112 | * |
||
| 1113 | * Generally speaking, these methods will be called from your _config.php file. |
||
| 1114 | * |
||
| 1115 | * Once the environment type is set, it can be checked with {@link Director::isDev()}, |
||
| 1116 | * {@link Director::isTest()}, and {@link Director::isLive()}. |
||
| 1117 | * |
||
| 1118 | * @param string $environment |
||
| 1119 | */ |
||
| 1120 | public static function set_environment_type($environment) |
||
| 1129 | |||
| 1130 | /** |
||
| 1131 | * Can also be checked with {@link Director::isDev()}, {@link Director::isTest()}, and |
||
| 1132 | * {@link Director::isLive()}. |
||
| 1133 | * |
||
| 1134 | * @return bool |
||
| 1135 | */ |
||
| 1136 | public static function get_environment_type() |
||
| 1155 | |||
| 1156 | /** |
||
| 1157 | * This function will return true if the site is in a live environment. For information about |
||
| 1158 | * environment types, see {@link Director::set_environment_type()}. |
||
| 1159 | * |
||
| 1160 | * @return bool |
||
| 1161 | */ |
||
| 1162 | public static function isLive() |
||
| 1166 | |||
| 1167 | /** |
||
| 1168 | * This function will return true if the site is in a development environment. For information about |
||
| 1169 | * environment types, see {@link Director::set_environment_type()}. |
||
| 1170 | * |
||
| 1171 | * @return bool |
||
| 1172 | */ |
||
| 1173 | public static function isDev() |
||
| 1177 | |||
| 1178 | /** |
||
| 1179 | * This function will return true if the site is in a test environment. For information about |
||
| 1180 | * environment types, see {@link Director::set_environment_type()}. |
||
| 1181 | * |
||
| 1182 | * @return bool |
||
| 1183 | */ |
||
| 1184 | public static function isTest() |
||
| 1188 | |||
| 1189 | /** |
||
| 1190 | * Check or update any temporary environment specified in the session. |
||
| 1191 | * |
||
| 1192 | * @return null|string |
||
| 1193 | */ |
||
| 1194 | public static function session_environment() |
||
| 1219 | |||
| 1220 | /** |
||
| 1221 | * Returns an array of strings of the method names of methods on the call that should be exposed |
||
| 1222 | * as global variables in the templates. |
||
| 1223 | * |
||
| 1224 | * @return array |
||
| 1225 | */ |
||
| 1226 | public static function get_template_global_variables() |
||
| 1236 | } |
||
| 1237 |
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: