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 | * @var array |
||
| 59 | */ |
||
| 60 | static private $urlParams; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | static private $rules = array(); |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var SiteTree |
||
| 69 | */ |
||
| 70 | private static $current_page; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @config |
||
| 74 | * |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | private static $alternate_base_folder; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @config |
||
| 81 | * |
||
| 82 | * @var array |
||
| 83 | */ |
||
| 84 | private static $dev_servers = array(); |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @config |
||
| 88 | * |
||
| 89 | * @var array |
||
| 90 | */ |
||
| 91 | private static $test_servers = array(); |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Setting this explicitly specifies the protocol ("http" or "https") used, overriding the normal |
||
| 95 | * behaviour of Director::is_https introspecting it from the request. False values imply default |
||
| 96 | * introspection. |
||
| 97 | * |
||
| 98 | * @config |
||
| 99 | * |
||
| 100 | * @var string |
||
| 101 | */ |
||
| 102 | private static $alternate_protocol; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @config |
||
| 106 | * |
||
| 107 | * @var string |
||
| 108 | */ |
||
| 109 | private static $alternate_host; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @config |
||
| 113 | * |
||
| 114 | * @var string |
||
| 115 | */ |
||
| 116 | private static $alternate_base_url; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @config |
||
| 120 | * |
||
| 121 | * @var string |
||
| 122 | */ |
||
| 123 | private static $environment_type; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Process the given URL, creating the appropriate controller and executing it. |
||
| 127 | * |
||
| 128 | * Request processing is handled as follows: |
||
| 129 | * - Director::direct() creates a new HTTPResponse object and passes this to |
||
| 130 | * Director::handleRequest(). |
||
| 131 | * - Director::handleRequest($request) checks each of the Director rules and identifies a controller |
||
| 132 | * to handle this request. |
||
| 133 | * - Controller::handleRequest($request) is then called. This will find a rule to handle the URL, |
||
| 134 | * and call the rule handling method. |
||
| 135 | * - RequestHandler::handleRequest($request) is recursively called whenever a rule handling method |
||
| 136 | * returns a RequestHandler object. |
||
| 137 | * |
||
| 138 | * In addition to request processing, Director will manage the session, and perform the output of |
||
| 139 | * the actual response to the browser. |
||
| 140 | * |
||
| 141 | * @uses handleRequest() rule-lookup logic is handled by this. |
||
| 142 | * @uses TestController::handleRequest() This handles the page logic for a Director::direct() call. |
||
| 143 | * @param string $url |
||
| 144 | * @param DataModel $model |
||
| 145 | * @throws HTTPResponse_Exception |
||
| 146 | */ |
||
| 147 | public static function direct($url, DataModel $model) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Test a URL request, returning a response object. This method is the counterpart of |
||
| 253 | * Director::direct() that is used in functional testing. It will execute the URL given, and |
||
| 254 | * return the result as an HTTPResponse object. |
||
| 255 | * |
||
| 256 | * @uses TestController::handleRequest() Handles the page logic for a Director::direct() call. |
||
| 257 | * |
||
| 258 | * @param string $url The URL to visit. |
||
| 259 | * @param array $postVars The $_POST & $_FILES variables. |
||
| 260 | * @param array|Session $session The {@link Session} object representing the current session. |
||
| 261 | * By passing the same object to multiple calls of Director::test(), you can simulate a persisted |
||
| 262 | * session. |
||
| 263 | * @param string $httpMethod The HTTP method, such as GET or POST. It will default to POST if |
||
| 264 | * postVars is set, GET otherwise. Overwritten by $postVars['_method'] if present. |
||
| 265 | * @param string $body The HTTP body. |
||
| 266 | * @param array $headers HTTP headers with key-value pairs. |
||
| 267 | * @param array|Cookie_Backend $cookies to populate $_COOKIE. |
||
| 268 | * @param HTTPRequest $request The {@see SS_HTTP_Request} object generated as a part of this request. |
||
| 269 | * |
||
| 270 | * @return HTTPResponse |
||
| 271 | * |
||
| 272 | * @throws HTTPResponse_Exception |
||
| 273 | */ |
||
| 274 | public static function test( |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Handle an HTTP request, defined with a HTTPRequest object. |
||
| 423 | * |
||
| 424 | * @skipUpgrade |
||
| 425 | * @param HTTPRequest $request |
||
| 426 | * @param Session $session |
||
| 427 | * @param DataModel $model |
||
| 428 | * @return HTTPResponse|string |
||
| 429 | */ |
||
| 430 | protected static function handleRequest(HTTPRequest $request, Session $session, DataModel $model) |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Set url parameters (should only be called internally by RequestHandler->handleRequest()). |
||
| 488 | * |
||
| 489 | * @param array $params |
||
| 490 | */ |
||
| 491 | public static function setUrlParams($params) |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Return the {@link SiteTree} object that is currently being viewed. If there is no SiteTree |
||
| 498 | * object to return, then this will return the current controller. |
||
| 499 | * |
||
| 500 | * @return SiteTree|Controller |
||
| 501 | */ |
||
| 502 | public static function get_current_page() |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Set the currently active {@link SiteTree} object that is being used to respond to the request. |
||
| 509 | * |
||
| 510 | * @param SiteTree $page |
||
| 511 | */ |
||
| 512 | public static function set_current_page($page) |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Turns the given URL into an absolute URL. By default non-site root relative urls will be |
||
| 519 | * evaluated relative to the current base_url. |
||
| 520 | * |
||
| 521 | * @param string $url URL To transform to absolute. |
||
| 522 | * @param string $relativeParent Method to use for evaluating relative urls. |
||
| 523 | * Either one of BASE (baseurl), ROOT (site root), or REQUEST (requested page). |
||
| 524 | * Defaults to BASE, which is the same behaviour as template url resolution. |
||
| 525 | * Ignored if the url is absolute or site root. |
||
| 526 | * |
||
| 527 | * @return string |
||
| 528 | */ |
||
| 529 | public static function absoluteURL($url, $relativeParent = self::BASE) |
||
| 569 | |||
| 570 | /** |
||
| 571 | * A helper to determine the current hostname used to access the site. |
||
| 572 | * The following are used to determine the host (in order) |
||
| 573 | * - Director.alternate_host |
||
| 574 | * - Director.alternate_base_url (if it contains a domain name) |
||
| 575 | * - Trusted proxy headers |
||
| 576 | * - HTTP Host header |
||
| 577 | * - SS_HOST env var |
||
| 578 | * - SERVER_NAME |
||
| 579 | * - gethostname() |
||
| 580 | * |
||
| 581 | * @return string |
||
| 582 | */ |
||
| 583 | public static function host() |
||
| 625 | |||
| 626 | /** |
||
| 627 | * Returns the domain part of the URL 'http://www.mysite.com'. Returns FALSE is this environment |
||
| 628 | * variable isn't set. |
||
| 629 | * |
||
| 630 | * @return bool|string |
||
| 631 | */ |
||
| 632 | public static function protocolAndHost() |
||
| 636 | |||
| 637 | /** |
||
| 638 | * Return the current protocol that the site is running under. |
||
| 639 | * |
||
| 640 | * @return string |
||
| 641 | */ |
||
| 642 | public static function protocol() |
||
| 646 | |||
| 647 | /** |
||
| 648 | * Return whether the site is running as under HTTPS. |
||
| 649 | * |
||
| 650 | * @return bool |
||
| 651 | */ |
||
| 652 | public static function is_https() |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Returns the root URL for the site. It will be automatically calculated unless it is overridden |
||
| 687 | * with {@link setBaseURL()}. |
||
| 688 | * |
||
| 689 | * @return string |
||
| 690 | */ |
||
| 691 | public static function baseURL() |
||
| 713 | |||
| 714 | /** |
||
| 715 | * Sets the root URL for the website. If the site isn't accessible from the URL you provide, |
||
| 716 | * weird things will happen. |
||
| 717 | * |
||
| 718 | * @deprecated 4.0 Use the "Director.alternate_base_url" config setting instead. |
||
| 719 | * |
||
| 720 | * @param string $baseURL |
||
| 721 | */ |
||
| 722 | public static function setBaseURL($baseURL) |
||
| 727 | |||
| 728 | /** |
||
| 729 | * Returns the root filesystem folder for the site. It will be automatically calculated unless |
||
| 730 | * it is overridden with {@link setBaseFolder()}. |
||
| 731 | * |
||
| 732 | * @return string |
||
| 733 | */ |
||
| 734 | public static function baseFolder() |
||
| 739 | |||
| 740 | /** |
||
| 741 | * Sets the root folder for the website. If the site isn't accessible from the folder you provide, |
||
| 742 | * weird things will happen. |
||
| 743 | * |
||
| 744 | * @deprecated 4.0 Use the "Director.alternate_base_folder" config setting instead. |
||
| 745 | * |
||
| 746 | * @param string $baseFolder |
||
| 747 | */ |
||
| 748 | public static function setBaseFolder($baseFolder) |
||
| 753 | |||
| 754 | /** |
||
| 755 | * Turns an absolute URL or folder into one that's relative to the root of the site. This is useful |
||
| 756 | * when turning a URL into a filesystem reference, or vice versa. |
||
| 757 | * |
||
| 758 | * @param string $url Accepts both a URL or a filesystem path. |
||
| 759 | * |
||
| 760 | * @return string |
||
| 761 | */ |
||
| 762 | public static function makeRelative($url) |
||
| 810 | |||
| 811 | /** |
||
| 812 | * Returns true if a given path is absolute. Works under both *nix and windows systems. |
||
| 813 | * |
||
| 814 | * @param string $path |
||
| 815 | * |
||
| 816 | * @return bool |
||
| 817 | */ |
||
| 818 | public static function is_absolute($path) |
||
| 828 | |||
| 829 | /** |
||
| 830 | * Determine if the url is root relative (i.e. starts with /, but not with //) SilverStripe |
||
| 831 | * considers root relative urls as a subset of relative urls. |
||
| 832 | * |
||
| 833 | * @param string $url |
||
| 834 | * |
||
| 835 | * @return bool |
||
| 836 | */ |
||
| 837 | public static function is_root_relative_url($url) |
||
| 841 | |||
| 842 | /** |
||
| 843 | * Checks if a given URL is absolute (e.g. starts with 'http://' etc.). URLs beginning with "//" |
||
| 844 | * are treated as absolute, as browsers take this to mean the same protocol as currently being used. |
||
| 845 | * |
||
| 846 | * Useful to check before redirecting based on a URL from user submissions through $_GET or $_POST, |
||
| 847 | * and avoid phishing attacks by redirecting to an attackers server. |
||
| 848 | * |
||
| 849 | * Note: Can't solely rely on PHP's parse_url() , since it is not intended to work with relative URLs |
||
| 850 | * or for security purposes. filter_var($url, FILTER_VALIDATE_URL) has similar problems. |
||
| 851 | * |
||
| 852 | * @param string $url |
||
| 853 | * |
||
| 854 | * @return bool |
||
| 855 | */ |
||
| 856 | public static function is_absolute_url($url) |
||
| 882 | |||
| 883 | /** |
||
| 884 | * Checks if a given URL is relative (or root relative) by checking {@link is_absolute_url()}. |
||
| 885 | * |
||
| 886 | * @param string $url |
||
| 887 | * |
||
| 888 | * @return bool |
||
| 889 | */ |
||
| 890 | public static function is_relative_url($url) |
||
| 894 | |||
| 895 | /** |
||
| 896 | * Checks if the given URL is belonging to this "site" (not an external link). That's the case if |
||
| 897 | * the URL is relative, as defined by {@link is_relative_url()}, or if the host matches |
||
| 898 | * {@link protocolAndHost()}. |
||
| 899 | * |
||
| 900 | * Useful to check before redirecting based on a URL from user submissions through $_GET or $_POST, |
||
| 901 | * and avoid phishing attacks by redirecting to an attackers server. |
||
| 902 | * |
||
| 903 | * @param string $url |
||
| 904 | * |
||
| 905 | * @return bool |
||
| 906 | */ |
||
| 907 | public static function is_site_url($url) |
||
| 917 | |||
| 918 | /** |
||
| 919 | * Takes a $_SERVER data array and extracts HTTP request headers. |
||
| 920 | * |
||
| 921 | * @param array $server |
||
| 922 | * |
||
| 923 | * @return array |
||
| 924 | */ |
||
| 925 | public static function extract_request_headers(array $server) |
||
| 947 | |||
| 948 | /** |
||
| 949 | * Given a filesystem reference relative to the site root, return the full file-system path. |
||
| 950 | * |
||
| 951 | * @param string $file |
||
| 952 | * |
||
| 953 | * @return string |
||
| 954 | */ |
||
| 955 | public static function getAbsFile($file) |
||
| 959 | |||
| 960 | /** |
||
| 961 | * Returns true if the given file exists. Filename should be relative to the site root. |
||
| 962 | * |
||
| 963 | * @param $file |
||
| 964 | * |
||
| 965 | * @return bool |
||
| 966 | */ |
||
| 967 | public static function fileExists($file) |
||
| 973 | |||
| 974 | /** |
||
| 975 | * Returns the Absolute URL of the site root. |
||
| 976 | * |
||
| 977 | * @return string |
||
| 978 | */ |
||
| 979 | public static function absoluteBaseURL() |
||
| 986 | |||
| 987 | /** |
||
| 988 | * Returns the Absolute URL of the site root, embedding the current basic-auth credentials into |
||
| 989 | * the URL. |
||
| 990 | * |
||
| 991 | * @return string |
||
| 992 | */ |
||
| 993 | public static function absoluteBaseURLWithAuth() |
||
| 1003 | |||
| 1004 | /** |
||
| 1005 | * Skip any further processing and immediately respond with a redirect to the passed URL. |
||
| 1006 | * |
||
| 1007 | * @param string $destURL |
||
| 1008 | */ |
||
| 1009 | protected static function force_redirect($destURL) |
||
| 1020 | |||
| 1021 | /** |
||
| 1022 | * Force the site to run on SSL. |
||
| 1023 | * |
||
| 1024 | * To use, call from _config.php. For example: |
||
| 1025 | * <code> |
||
| 1026 | * if (Director::isLive()) Director::forceSSL(); |
||
| 1027 | * </code> |
||
| 1028 | * |
||
| 1029 | * If you don't want your entire site to be on SSL, you can pass an array of PCRE regular expression |
||
| 1030 | * patterns for matching relative URLs. For example: |
||
| 1031 | * <code> |
||
| 1032 | * if (Director::isLive()) Director::forceSSL(array('/^admin/', '/^Security/')); |
||
| 1033 | * </code> |
||
| 1034 | * |
||
| 1035 | * If you want certain parts of your site protected under a different domain, you can specify |
||
| 1036 | * the domain as an argument: |
||
| 1037 | * <code> |
||
| 1038 | * if (Director::isLive()) Director::forceSSL(array('/^admin/', '/^Security/'), 'secure.mysite.com'); |
||
| 1039 | * </code> |
||
| 1040 | * |
||
| 1041 | * Note that the session data will be lost when moving from HTTP to HTTPS. It is your responsibility |
||
| 1042 | * to ensure that this won't cause usability problems. |
||
| 1043 | * |
||
| 1044 | * CAUTION: This does not respect the site environment mode. You should check this |
||
| 1045 | * as per the above examples using Director::isLive() or Director::isTest() for example. |
||
| 1046 | * |
||
| 1047 | * @param array $patterns Array of regex patterns to match URLs that should be HTTPS. |
||
| 1048 | * @param string $secureDomain Secure domain to redirect to. Defaults to the current domain. |
||
| 1049 | * |
||
| 1050 | * @return bool|string String of URL when unit tests running, boolean FALSE if patterns don't match request URI. |
||
| 1051 | */ |
||
| 1052 | public static function forceSSL($patterns = null, $secureDomain = null) |
||
| 1097 | |||
| 1098 | /** |
||
| 1099 | * Force a redirect to a domain starting with "www." |
||
| 1100 | */ |
||
| 1101 | public static function forceWWW() |
||
| 1113 | |||
| 1114 | /** |
||
| 1115 | * Checks if the current HTTP-Request is an "Ajax-Request" by checking for a custom header set by |
||
| 1116 | * jQuery or whether a manually set request-parameter 'ajax' is present. |
||
| 1117 | * |
||
| 1118 | * @return bool |
||
| 1119 | */ |
||
| 1120 | public static function is_ajax() |
||
| 1131 | |||
| 1132 | /** |
||
| 1133 | * Returns true if this script is being run from the command line rather than the web server. |
||
| 1134 | * |
||
| 1135 | * @return bool |
||
| 1136 | */ |
||
| 1137 | public static function is_cli() |
||
| 1141 | |||
| 1142 | /** |
||
| 1143 | * Set the environment type of the current site. |
||
| 1144 | * |
||
| 1145 | * Typically, a SilverStripe site have a number of environments: |
||
| 1146 | * - Development environments, such a copy on your local machine. |
||
| 1147 | * - Test sites, such as the one you show the client before going live. |
||
| 1148 | * - The live site itself. |
||
| 1149 | * |
||
| 1150 | * The behaviour of these environments often varies slightly. For example, development sites may |
||
| 1151 | * have errors dumped to the screen, and order confirmation emails might be sent to the developer |
||
| 1152 | * instead of the client. |
||
| 1153 | * |
||
| 1154 | * To help with this, SilverStripe supports the notion of an environment type. The environment |
||
| 1155 | * type can be dev, test, or live. |
||
| 1156 | * |
||
| 1157 | * You can set it explicitly with {@link Director::set_environment_type()}. Or you can use |
||
| 1158 | * {@link Director::$dev_servers} and {@link Director::$test_servers} to set it implicitly, based |
||
| 1159 | * on the value of $_SERVER['HTTP_HOST']. If the HTTP_HOST value is one of the servers listed, |
||
| 1160 | * then the environment type will be test or dev. Otherwise, the environment type will be live. |
||
| 1161 | * |
||
| 1162 | * Dev mode can also be forced by putting ?isDev=1 in your URL, which will ask you to log in and |
||
| 1163 | * then push the site into dev mode for the remainder of the session. Putting ?isDev=0 onto the URL |
||
| 1164 | * can turn it back. |
||
| 1165 | * |
||
| 1166 | * Test mode can also be forced by putting ?isTest=1 in your URL, which will ask you to log in and |
||
| 1167 | * then push the site into test mode for the remainder of the session. Putting ?isTest=0 onto the URL |
||
| 1168 | * can turn it back. |
||
| 1169 | * |
||
| 1170 | * Generally speaking, these methods will be called from your _config.php file. |
||
| 1171 | * |
||
| 1172 | * Once the environment type is set, it can be checked with {@link Director::isDev()}, |
||
| 1173 | * {@link Director::isTest()}, and {@link Director::isLive()}. |
||
| 1174 | * |
||
| 1175 | * @deprecated 4.0 Use the "Director.environment_type" config setting instead |
||
| 1176 | * |
||
| 1177 | * @param $et string |
||
| 1178 | */ |
||
| 1179 | public static function set_environment_type($et) |
||
| 1191 | |||
| 1192 | /** |
||
| 1193 | * Can also be checked with {@link Director::isDev()}, {@link Director::isTest()}, and |
||
| 1194 | * {@link Director::isLive()}. |
||
| 1195 | * |
||
| 1196 | * @return bool|string |
||
| 1197 | */ |
||
| 1198 | public static function get_environment_type() |
||
| 1210 | |||
| 1211 | /** |
||
| 1212 | * This function will return true if the site is in a live environment. For information about |
||
| 1213 | * environment types, see {@link Director::set_environment_type()}. |
||
| 1214 | * |
||
| 1215 | * @return bool |
||
| 1216 | */ |
||
| 1217 | public static function isLive() |
||
| 1221 | |||
| 1222 | /** |
||
| 1223 | * This function will return true if the site is in a development environment. For information about |
||
| 1224 | * environment types, see {@link Director::set_environment_type()}. |
||
| 1225 | * |
||
| 1226 | * @return bool |
||
| 1227 | */ |
||
| 1228 | public static function isDev() |
||
| 1244 | |||
| 1245 | /** |
||
| 1246 | * This function will return true if the site is in a test environment. For information about |
||
| 1247 | * environment types, see {@link Director::set_environment_type()}. |
||
| 1248 | * |
||
| 1249 | * @return bool |
||
| 1250 | */ |
||
| 1251 | public static function isTest() |
||
| 1272 | |||
| 1273 | /** |
||
| 1274 | * Check or update any temporary environment specified in the session. |
||
| 1275 | * |
||
| 1276 | * @return null|string |
||
| 1277 | */ |
||
| 1278 | protected static function session_environment() |
||
| 1303 | |||
| 1304 | /** |
||
| 1305 | * Returns an array of strings of the method names of methods on the call that should be exposed |
||
| 1306 | * as global variables in the templates. |
||
| 1307 | * |
||
| 1308 | * @return array |
||
| 1309 | */ |
||
| 1310 | public static function get_template_global_variables() |
||
| 1320 | } |
||
| 1321 |
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: