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 |
||
19 | class Director implements TemplateGlobalProvider { |
||
20 | |||
21 | /** |
||
22 | * Specifies this url is relative to the base. |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | const BASE = 'BASE'; |
||
27 | |||
28 | /** |
||
29 | * Specifies this url is relative to the site root. |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | const ROOT = 'ROOT'; |
||
34 | |||
35 | /** |
||
36 | * specifies this url is relative to the current request. |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | const REQUEST = 'REQUEST'; |
||
41 | |||
42 | /** |
||
43 | * @var array |
||
44 | */ |
||
45 | static private $urlParams; |
||
46 | |||
47 | /** |
||
48 | * @var array |
||
49 | */ |
||
50 | static private $rules = array(); |
||
51 | |||
52 | /** |
||
53 | * @var SiteTree |
||
54 | */ |
||
55 | private static $current_page; |
||
56 | |||
57 | /** |
||
58 | * @config |
||
59 | * |
||
60 | * @var string |
||
61 | */ |
||
62 | private static $alternate_base_folder; |
||
63 | |||
64 | /** |
||
65 | * @config |
||
66 | * |
||
67 | * @var array |
||
68 | */ |
||
69 | private static $dev_servers = array(); |
||
70 | |||
71 | /** |
||
72 | * @config |
||
73 | * |
||
74 | * @var array |
||
75 | */ |
||
76 | private static $test_servers = array(); |
||
77 | |||
78 | /** |
||
79 | * Setting this explicitly specifies the protocol ("http" or "https") used, overriding the normal |
||
80 | * behaviour of Director::is_https introspecting it from the request. False values imply default |
||
81 | * introspection. |
||
82 | * |
||
83 | * @config |
||
84 | * |
||
85 | * @var string |
||
86 | */ |
||
87 | private static $alternate_protocol; |
||
88 | |||
89 | /** |
||
90 | * @config |
||
91 | * |
||
92 | * @var string |
||
93 | */ |
||
94 | private static $alternate_base_url; |
||
95 | |||
96 | /** |
||
97 | * @config |
||
98 | * |
||
99 | * @var string |
||
100 | */ |
||
101 | private static $environment_type; |
||
102 | |||
103 | /** |
||
104 | * Add URL matching rules to the Director. The director is responsible for turning URLs into |
||
105 | * Controller objects. |
||
106 | * |
||
107 | * Higher $priority values will get your rule checked first. We recommend priority 100 for your |
||
108 | * site's rules. The built-in rules are priority 10, standard modules are priority 50. |
||
109 | * |
||
110 | * @deprecated 4.0 Use the "Director.rules" config setting instead |
||
111 | * |
||
112 | * @param int $priority |
||
113 | * @param array $rules |
||
114 | */ |
||
115 | public static function addRules($priority, $rules) { |
||
120 | |||
121 | /** |
||
122 | * Process the given URL, creating the appropriate controller and executing it. |
||
123 | * |
||
124 | * Request processing is handled as follows: |
||
125 | * - Director::direct() creates a new SS_HTTPResponse object and passes this to |
||
126 | * Director::handleRequest(). |
||
127 | * - Director::handleRequest($request) checks each of the Director rules and identifies a controller |
||
128 | * to handle this request. |
||
129 | * - Controller::handleRequest($request) is then called. This will find a rule to handle the URL, |
||
130 | * and call the rule handling method. |
||
131 | * - RequestHandler::handleRequest($request) is recursively called whenever a rule handling method |
||
132 | * returns a RequestHandler object. |
||
133 | * |
||
134 | * In addition to request processing, Director will manage the session, and perform the output of |
||
135 | * the actual response to the browser. |
||
136 | * |
||
137 | * @uses handleRequest() rule-lookup logic is handled by this. |
||
138 | * @uses Controller::run() Controller::run() handles the page logic for a Director::direct() call. |
||
139 | * |
||
140 | * @param string $url |
||
141 | * @param DataModel $model |
||
142 | * |
||
143 | * @throws SS_HTTPResponse_Exception |
||
144 | */ |
||
145 | public static function direct($url, DataModel $model) { |
||
238 | |||
239 | /** |
||
240 | * Test a URL request, returning a response object. This method is the counterpart of |
||
241 | * Director::direct() that is used in functional testing. It will execute the URL given, and |
||
242 | * return the result as an SS_HTTPResponse object. |
||
243 | * |
||
244 | * @uses getControllerForURL() The rule-lookup logic is handled by this. |
||
245 | * @uses Controller::run() Handles the page logic for a Director::direct() call. |
||
246 | * |
||
247 | * @param string $url The URL to visit. |
||
248 | * @param array $postVars The $_POST & $_FILES variables. |
||
249 | * @param array|Session $session The {@link Session} object representing the current session. |
||
250 | * By passing the same object to multiple calls of Director::test(), you can simulate a persisted |
||
251 | * session. |
||
252 | * @param string $httpMethod The HTTP method, such as GET or POST. It will default to POST if |
||
253 | * postVars is set, GET otherwise. Overwritten by $postVars['_method'] if present. |
||
254 | * @param string $body The HTTP body. |
||
255 | * @param array $headers HTTP headers with key-value pairs. |
||
256 | * @param array|Cookie_Backend $cookies to populate $_COOKIE. |
||
257 | * @param HTTP_Request $request The {@see HTTP_Request} object generated as a part of this request. |
||
258 | * |
||
259 | * @return SS_HTTPResponse |
||
260 | * |
||
261 | * @throws SS_HTTPResponse_Exception |
||
262 | */ |
||
263 | public static function test($url, $postVars = null, $session = array(), $httpMethod = null, $body = null, |
||
391 | |||
392 | /** |
||
393 | * Handle an HTTP request, defined with a SS_HTTPRequest object. |
||
394 | * |
||
395 | * @param SS_HTTPRequest $request |
||
396 | * @param Session $session |
||
397 | * @param DataModel $model |
||
398 | * |
||
399 | * @return SS_HTTPResponse|string |
||
400 | */ |
||
401 | protected static function handleRequest(SS_HTTPRequest $request, Session $session, DataModel $model) { |
||
453 | |||
454 | /** |
||
455 | * Set url parameters (should only be called internally by RequestHandler->handleRequest()). |
||
456 | * |
||
457 | * @param array $params |
||
458 | */ |
||
459 | public static function setUrlParams($params) { |
||
462 | |||
463 | /** |
||
464 | * Return the {@link SiteTree} object that is currently being viewed. If there is no SiteTree |
||
465 | * object to return, then this will return the current controller. |
||
466 | * |
||
467 | * @return SiteTree |
||
468 | */ |
||
469 | public static function get_current_page() { |
||
472 | |||
473 | /** |
||
474 | * Set the currently active {@link SiteTree} object that is being used to respond to the request. |
||
475 | * |
||
476 | * @param SiteTree $page |
||
477 | */ |
||
478 | public static function set_current_page($page) { |
||
481 | |||
482 | /** |
||
483 | * Turns the given URL into an absolute URL. By default non-site root relative urls will be |
||
484 | * evaluated relative to the current base_url. |
||
485 | * |
||
486 | * @param string $url URL To transform to absolute. |
||
487 | * @param string $relativeParent Method to use for evaluating relative urls. |
||
488 | * Either one of BASE (baseurl), ROOT (site root), or REQUEST (requested page). |
||
489 | * Defaults to BASE, which is the same behaviour as template url resolution. |
||
490 | * Ignored if the url is absolute or site root. |
||
491 | * |
||
492 | * @return string |
||
493 | */ |
||
494 | public static function absoluteURL($url, $relativeParent = self::BASE) { |
||
536 | |||
537 | /** |
||
538 | * Returns the domain part of the URL 'http://www.mysite.com'. Returns FALSE is this environment |
||
539 | * variable isn't set. |
||
540 | * |
||
541 | * @return bool|string |
||
542 | */ |
||
543 | public static function protocolAndHost() { |
||
571 | |||
572 | /** |
||
573 | * Return the current protocol that the site is running under. |
||
574 | * |
||
575 | * @return string |
||
576 | */ |
||
577 | public static function protocol() { |
||
580 | |||
581 | /** |
||
582 | * Return whether the site is running as under HTTPS. |
||
583 | * |
||
584 | * @return bool |
||
585 | */ |
||
586 | public static function is_https() { |
||
617 | |||
618 | /** |
||
619 | * Returns the root URL for the site. It will be automatically calculated unless it is overridden |
||
620 | * with {@link setBaseURL()}. |
||
621 | * |
||
622 | * @return string |
||
623 | */ |
||
624 | public static function baseURL() { |
||
645 | |||
646 | /** |
||
647 | * Sets the root URL for the website. If the site isn't accessible from the URL you provide, |
||
648 | * weird things will happen. |
||
649 | * |
||
650 | * @deprecated 4.0 Use the "Director.alternate_base_url" config setting instead. |
||
651 | * |
||
652 | * @param string $baseURL |
||
653 | */ |
||
654 | public static function setBaseURL($baseURL) { |
||
658 | |||
659 | /** |
||
660 | * Returns the root filesystem folder for the site. It will be automatically calculated unless |
||
661 | * it is overridden with {@link setBaseFolder()}. |
||
662 | * |
||
663 | * @return string |
||
664 | */ |
||
665 | public static function baseFolder() { |
||
669 | |||
670 | /** |
||
671 | * Sets the root folder for the website. If the site isn't accessible from the folder you provide, |
||
672 | * weird things will happen. |
||
673 | * |
||
674 | * @deprecated 4.0 Use the "Director.alternate_base_folder" config setting instead. |
||
675 | * |
||
676 | * @param string $baseFolder |
||
677 | */ |
||
678 | public static function setBaseFolder($baseFolder) { |
||
682 | |||
683 | /** |
||
684 | * Turns an absolute URL or folder into one that's relative to the root of the site. This is useful |
||
685 | * when turning a URL into a filesystem reference, or vice versa. |
||
686 | * |
||
687 | * @param string $url Accepts both a URL or a filesystem path. |
||
688 | * |
||
689 | * @return string |
||
690 | */ |
||
691 | public static function makeRelative($url) { |
||
736 | |||
737 | /** |
||
738 | * Returns true if a given path is absolute. Works under both *nix and windows systems. |
||
739 | * |
||
740 | * @param string $path |
||
741 | * |
||
742 | * @return bool |
||
743 | */ |
||
744 | public static function is_absolute($path) { |
||
749 | |||
750 | /** |
||
751 | * Determine if the url is root relative (i.e. starts with /, but not with //) SilverStripe |
||
752 | * considers root relative urls as a subset of relative urls. |
||
753 | * |
||
754 | * @param string $url |
||
755 | * |
||
756 | * @return bool |
||
757 | */ |
||
758 | public static function is_root_relative_url($url) { |
||
761 | |||
762 | /** |
||
763 | * Checks if a given URL is absolute (e.g. starts with 'http://' etc.). URLs beginning with "//" |
||
764 | * are treated as absolute, as browsers take this to mean the same protocol as currently being used. |
||
765 | * |
||
766 | * Useful to check before redirecting based on a URL from user submissions through $_GET or $_POST, |
||
767 | * and avoid phishing attacks by redirecting to an attackers server. |
||
768 | * |
||
769 | * Note: Can't solely rely on PHP's parse_url() , since it is not intended to work with relative URLs |
||
770 | * or for security purposes. filter_var($url, FILTER_VALIDATE_URL) has similar problems. |
||
771 | * |
||
772 | * @param string $url |
||
773 | * |
||
774 | * @return bool |
||
775 | */ |
||
776 | public static function is_absolute_url($url) { |
||
801 | |||
802 | /** |
||
803 | * Checks if a given URL is relative (or root relative) by checking {@link is_absolute_url()}. |
||
804 | * |
||
805 | * @param string $url |
||
806 | * |
||
807 | * @return bool |
||
808 | */ |
||
809 | public static function is_relative_url($url) { |
||
812 | |||
813 | /** |
||
814 | * Checks if the given URL is belonging to this "site" (not an external link). That's the case if |
||
815 | * the URL is relative, as defined by {@link is_relative_url()}, or if the host matches |
||
816 | * {@link protocolAndHost()}. |
||
817 | * |
||
818 | * Useful to check before redirecting based on a URL from user submissions through $_GET or $_POST, |
||
819 | * and avoid phishing attacks by redirecting to an attackers server. |
||
820 | * |
||
821 | * @param string $url |
||
822 | * |
||
823 | * @return bool |
||
824 | */ |
||
825 | public static function is_site_url($url) { |
||
834 | |||
835 | /** |
||
836 | * Takes a $_SERVER data array and extracts HTTP request headers. |
||
837 | * |
||
838 | * @param array $server |
||
839 | * |
||
840 | * @return array |
||
841 | */ |
||
842 | public static function extract_request_headers(array $server) { |
||
859 | |||
860 | /** |
||
861 | * Given a filesystem reference relative to the site root, return the full file-system path. |
||
862 | * |
||
863 | * @param string $file |
||
864 | * |
||
865 | * @return string |
||
866 | */ |
||
867 | public static function getAbsFile($file) { |
||
870 | |||
871 | /** |
||
872 | * Returns true if the given file exists. Filename should be relative to the site root. |
||
873 | * |
||
874 | * @param $file |
||
875 | * |
||
876 | * @return bool |
||
877 | */ |
||
878 | public static function fileExists($file) { |
||
883 | |||
884 | /** |
||
885 | * Returns the Absolute URL of the site root. |
||
886 | * |
||
887 | * @return string |
||
888 | */ |
||
889 | public static function absoluteBaseURL() { |
||
895 | |||
896 | /** |
||
897 | * Returns the Absolute URL of the site root, embedding the current basic-auth credentials into |
||
898 | * the URL. |
||
899 | * |
||
900 | * @return string |
||
901 | */ |
||
902 | public static function absoluteBaseURLWithAuth() { |
||
910 | |||
911 | /** |
||
912 | * Skip any further processing and immediately respond with a redirect to the passed URL. |
||
913 | * |
||
914 | * @param string $destURL |
||
915 | */ |
||
916 | protected static function force_redirect($destURL) { |
||
926 | |||
927 | /** |
||
928 | * Force the site to run on SSL. |
||
929 | * |
||
930 | * To use, call from _config.php. For example: |
||
931 | * <code> |
||
932 | * if (Director::isLive()) Director::forceSSL(); |
||
933 | * </code> |
||
934 | * |
||
935 | * If you don't want your entire site to be on SSL, you can pass an array of PCRE regular expression |
||
936 | * patterns for matching relative URLs. For example: |
||
937 | * <code> |
||
938 | * if (Director::isLive()) Director::forceSSL(array('/^admin/', '/^Security/')); |
||
939 | * </code> |
||
940 | * |
||
941 | * If you want certain parts of your site protected under a different domain, you can specify |
||
942 | * the domain as an argument: |
||
943 | * <code> |
||
944 | * if (Director::isLive()) Director::forceSSL(array('/^admin/', '/^Security/'), 'secure.mysite.com'); |
||
945 | * </code> |
||
946 | * |
||
947 | * Note that the session data will be lost when moving from HTTP to HTTPS. It is your responsibility |
||
948 | * to ensure that this won't cause usability problems. |
||
949 | * |
||
950 | * CAUTION: This does not respect the site environment mode. You should check this |
||
951 | * as per the above examples using Director::isLive() or Director::isTest() for example. |
||
952 | * |
||
953 | * @param array $patterns Array of regex patterns to match URLs that should be HTTPS. |
||
954 | * @param string $secureDomain Secure domain to redirect to. Defaults to the current domain. |
||
955 | * |
||
956 | * @return bool|string String of URL when unit tests running, boolean FALSE if patterns don't match request URI. |
||
957 | */ |
||
958 | public static function forceSSL($patterns = null, $secureDomain = null) { |
||
1002 | |||
1003 | /** |
||
1004 | * Force a redirect to a domain starting with "www." |
||
1005 | */ |
||
1006 | public static function forceWWW() { |
||
1014 | |||
1015 | /** |
||
1016 | * Checks if the current HTTP-Request is an "Ajax-Request" by checking for a custom header set by |
||
1017 | * jQuery or whether a manually set request-parameter 'ajax' is present. |
||
1018 | * |
||
1019 | * @return bool |
||
1020 | */ |
||
1021 | public static function is_ajax() { |
||
1031 | |||
1032 | /** |
||
1033 | * Returns true if this script is being run from the command line rather than the web server. |
||
1034 | * |
||
1035 | * @return bool |
||
1036 | */ |
||
1037 | public static function is_cli() { |
||
1040 | |||
1041 | /** |
||
1042 | * Set the environment type of the current site. |
||
1043 | * |
||
1044 | * Typically, a SilverStripe site have a number of environments: |
||
1045 | * - Development environments, such a copy on your local machine. |
||
1046 | * - Test sites, such as the one you show the client before going live. |
||
1047 | * - The live site itself. |
||
1048 | * |
||
1049 | * The behaviour of these environments often varies slightly. For example, development sites may |
||
1050 | * have errors dumped to the screen, and order confirmation emails might be sent to the developer |
||
1051 | * instead of the client. |
||
1052 | * |
||
1053 | * To help with this, SilverStripe supports the notion of an environment type. The environment |
||
1054 | * type can be dev, test, or live. |
||
1055 | * |
||
1056 | * You can set it explicitly with {@link Director::set_environment_type()}. Or you can use |
||
1057 | * {@link Director::$dev_servers} and {@link Director::$test_servers} to set it implicitly, based |
||
1058 | * on the value of $_SERVER['HTTP_HOST']. If the HTTP_HOST value is one of the servers listed, |
||
1059 | * then the environment type will be test or dev. Otherwise, the environment type will be live. |
||
1060 | * |
||
1061 | * Dev mode can also be forced by putting ?isDev=1 in your URL, which will ask you to log in and |
||
1062 | * then push the site into dev mode for the remainder of the session. Putting ?isDev=0 onto the URL |
||
1063 | * can turn it back. |
||
1064 | * |
||
1065 | * Test mode can also be forced by putting ?isTest=1 in your URL, which will ask you to log in and |
||
1066 | * then push the site into test mode for the remainder of the session. Putting ?isTest=0 onto the URL |
||
1067 | * can turn it back. |
||
1068 | * |
||
1069 | * Generally speaking, these methods will be called from your _config.php file. |
||
1070 | * |
||
1071 | * Once the environment type is set, it can be checked with {@link Director::isDev()}, |
||
1072 | * {@link Director::isTest()}, and {@link Director::isLive()}. |
||
1073 | * |
||
1074 | * @deprecated 4.0 Use the "Director.environment_type" config setting instead |
||
1075 | * |
||
1076 | * @param $et string |
||
1077 | */ |
||
1078 | public static function set_environment_type($et) { |
||
1087 | |||
1088 | /** |
||
1089 | * Can also be checked with {@link Director::isDev()}, {@link Director::isTest()}, and |
||
1090 | * {@link Director::isLive()}. |
||
1091 | * |
||
1092 | * @return bool|string |
||
1093 | */ |
||
1094 | public static function get_environment_type() { |
||
1105 | |||
1106 | /** |
||
1107 | * This function will return true if the site is in a live environment. For information about |
||
1108 | * environment types, see {@link Director::set_environment_type()}. |
||
1109 | * |
||
1110 | * @return bool |
||
1111 | */ |
||
1112 | public static function isLive() { |
||
1115 | |||
1116 | /** |
||
1117 | * This function will return true if the site is in a development environment. For information about |
||
1118 | * environment types, see {@link Director::set_environment_type()}. |
||
1119 | * |
||
1120 | * @return bool |
||
1121 | */ |
||
1122 | public static function isDev() { |
||
1137 | |||
1138 | /** |
||
1139 | * This function will return true if the site is in a test environment. For information about |
||
1140 | * environment types, see {@link Director::set_environment_type()}. |
||
1141 | * |
||
1142 | * @return bool |
||
1143 | */ |
||
1144 | public static function isTest() { |
||
1162 | |||
1163 | /** |
||
1164 | * Check or update any temporary environment specified in the session. |
||
1165 | * |
||
1166 | * @return null|string |
||
1167 | */ |
||
1168 | protected static function session_environment() { |
||
1192 | |||
1193 | /** |
||
1194 | * Returns an array of strings of the method names of methods on the call that should be exposed |
||
1195 | * as global variables in the templates. |
||
1196 | * |
||
1197 | * @return array |
||
1198 | */ |
||
1199 | public static function get_template_global_variables() { |
||
1208 | } |
||
1209 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.