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 |
||
31 | class Director implements TemplateGlobalProvider |
||
32 | { |
||
33 | use Configurable; |
||
34 | use Injectable; |
||
35 | use HTTPMiddlewareAware; |
||
36 | |||
37 | /** |
||
38 | * Specifies this url is relative to the base. |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | const BASE = 'BASE'; |
||
43 | |||
44 | /** |
||
45 | * Specifies this url is relative to the site root. |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | const ROOT = 'ROOT'; |
||
50 | |||
51 | /** |
||
52 | * specifies this url is relative to the current request. |
||
53 | * |
||
54 | * @var string |
||
55 | */ |
||
56 | const REQUEST = 'REQUEST'; |
||
57 | |||
58 | /** |
||
59 | * @config |
||
60 | * @var array |
||
61 | */ |
||
62 | private static $rules = array(); |
||
63 | |||
64 | /** |
||
65 | * Set current page |
||
66 | * |
||
67 | * @internal |
||
68 | * @var SiteTree |
||
69 | */ |
||
70 | private static $current_page; |
||
71 | |||
72 | /** |
||
73 | * @config |
||
74 | * @var string |
||
75 | */ |
||
76 | private static $alternate_base_folder; |
||
77 | |||
78 | /** |
||
79 | * Force the base_url to a specific value. |
||
80 | * If assigned, default_base_url and the value in the $_SERVER |
||
81 | * global is ignored. |
||
82 | * Supports back-ticked vars; E.g. '`SS_BASE_URL`' |
||
83 | * |
||
84 | * @config |
||
85 | * @var string |
||
86 | */ |
||
87 | private static $alternate_base_url; |
||
88 | |||
89 | /** |
||
90 | * Base url to populate if cannot be determined otherwise. |
||
91 | * Supports back-ticked vars; E.g. '`SS_BASE_URL`' |
||
92 | * |
||
93 | * @config |
||
94 | * @var string |
||
95 | */ |
||
96 | private static $default_base_url = '`SS_BASE_URL`'; |
||
97 | |||
98 | /** |
||
99 | * Assigned environment type |
||
100 | * |
||
101 | * @internal |
||
102 | * @var string |
||
103 | */ |
||
104 | protected static $environment_type; |
||
105 | |||
106 | /** |
||
107 | * Test a URL request, returning a response object. This method is a wrapper around |
||
108 | * Director::handleRequest() to assist with functional testing. It will execute the URL given, and |
||
109 | * return the result as an HTTPResponse object. |
||
110 | * |
||
111 | * @param string $url The URL to visit. |
||
112 | * @param array $postVars The $_POST & $_FILES variables. |
||
113 | * @param array|Session $session The {@link Session} object representing the current session. |
||
114 | * By passing the same object to multiple calls of Director::test(), you can simulate a persisted |
||
115 | * session. |
||
116 | * @param string $httpMethod The HTTP method, such as GET or POST. It will default to POST if |
||
117 | * postVars is set, GET otherwise. Overwritten by $postVars['_method'] if present. |
||
118 | * @param string $body The HTTP body. |
||
119 | * @param array $headers HTTP headers with key-value pairs. |
||
120 | * @param array|Cookie_Backend $cookies to populate $_COOKIE. |
||
121 | * @param HTTPRequest $request The {@see SS_HTTP_Request} object generated as a part of this request. |
||
122 | * |
||
123 | * @return HTTPResponse |
||
124 | * |
||
125 | * @throws HTTPResponse_Exception |
||
126 | */ |
||
127 | public static function test( |
||
151 | |||
152 | /** |
||
153 | * Mock a request, passing this to the given callback, before resetting. |
||
154 | * |
||
155 | * @param callable $callback Action to pass the HTTPRequst object |
||
156 | * @param string $url The URL to build |
||
157 | * @param array $postVars The $_POST & $_FILES variables. |
||
158 | * @param array|Session $session The {@link Session} object representing the current session. |
||
159 | * By passing the same object to multiple calls of Director::test(), you can simulate a persisted |
||
160 | * session. |
||
161 | * @param string $httpMethod The HTTP method, such as GET or POST. It will default to POST if |
||
162 | * postVars is set, GET otherwise. Overwritten by $postVars['_method'] if present. |
||
163 | * @param string $body The HTTP body. |
||
164 | * @param array $headers HTTP headers with key-value pairs. |
||
165 | * @param array|Cookie_Backend $cookies to populate $_COOKIE. |
||
166 | * @param HTTPRequest $request The {@see SS_HTTP_Request} object generated as a part of this request. |
||
167 | * @return mixed Result of callback |
||
168 | */ |
||
169 | public static function mockRequest( |
||
290 | |||
291 | /** |
||
292 | * Process the given URL, creating the appropriate controller and executing it. |
||
293 | * |
||
294 | * Request processing is handled as follows: |
||
295 | * - Director::handleRequest($request) checks each of the Director rules and identifies a controller |
||
296 | * to handle this request. |
||
297 | * - Controller::handleRequest($request) is then called. This will find a rule to handle the URL, |
||
298 | * and call the rule handling method. |
||
299 | * - RequestHandler::handleRequest($request) is recursively called whenever a rule handling method |
||
300 | * returns a RequestHandler object. |
||
301 | * |
||
302 | * In addition to request processing, Director will manage the session, and perform the output of |
||
303 | * the actual response to the browser. |
||
304 | * |
||
305 | * @param HTTPRequest $request |
||
306 | * @return HTTPResponse |
||
307 | * @throws HTTPResponse_Exception |
||
308 | */ |
||
309 | public function handleRequest(HTTPRequest $request) |
||
379 | |||
380 | /** |
||
381 | * Return the {@link SiteTree} object that is currently being viewed. If there is no SiteTree |
||
382 | * object to return, then this will return the current controller. |
||
383 | * |
||
384 | * @return SiteTree|Controller |
||
385 | */ |
||
386 | public static function get_current_page() |
||
390 | |||
391 | /** |
||
392 | * Set the currently active {@link SiteTree} object that is being used to respond to the request. |
||
393 | * |
||
394 | * @param SiteTree $page |
||
395 | */ |
||
396 | public static function set_current_page($page) |
||
400 | |||
401 | /** |
||
402 | * Turns the given URL into an absolute URL. By default non-site root relative urls will be |
||
403 | * evaluated relative to the current base_url. |
||
404 | * |
||
405 | * @param string $url URL To transform to absolute. |
||
406 | * @param string $relativeParent Method to use for evaluating relative urls. |
||
407 | * Either one of BASE (baseurl), ROOT (site root), or REQUEST (requested page). |
||
408 | * Defaults to BASE, which is the same behaviour as template url resolution. |
||
409 | * Ignored if the url is absolute or site root. |
||
410 | * |
||
411 | * @return string |
||
412 | */ |
||
413 | public static function absoluteURL($url, $relativeParent = self::BASE) |
||
453 | |||
454 | /** |
||
455 | * A helper to determine the current hostname used to access the site. |
||
456 | * The following are used to determine the host (in order) |
||
457 | * - Director.alternate_base_url (if it contains a domain name) |
||
458 | * - Trusted proxy headers |
||
459 | * - HTTP Host header |
||
460 | * - SS_BASE_URL env var |
||
461 | * - SERVER_NAME |
||
462 | * - gethostname() |
||
463 | * |
||
464 | * @param HTTPRequest $request |
||
465 | * @return string |
||
466 | */ |
||
467 | public static function host(HTTPRequest $request = null) |
||
500 | |||
501 | /** |
||
502 | * Returns the domain part of the URL 'http://www.mysite.com'. Returns FALSE is this environment |
||
503 | * variable isn't set. |
||
504 | * |
||
505 | * @param HTTPRequest $request |
||
506 | * @return bool|string |
||
507 | */ |
||
508 | public static function protocolAndHost(HTTPRequest $request = null) |
||
512 | |||
513 | /** |
||
514 | * Return the current protocol that the site is running under. |
||
515 | * |
||
516 | * @param HTTPRequest $request |
||
517 | * @return string |
||
518 | */ |
||
519 | public static function protocol(HTTPRequest $request = null) |
||
523 | |||
524 | /** |
||
525 | * Return whether the site is running as under HTTPS. |
||
526 | * |
||
527 | * @param HTTPRequest $request |
||
528 | * @return bool |
||
529 | */ |
||
530 | public static function is_https(HTTPRequest $request = null) |
||
558 | |||
559 | /** |
||
560 | * Return the root-relative url for the baseurl |
||
561 | * |
||
562 | * @return string Root-relative url with trailing slash. |
||
563 | */ |
||
564 | public static function baseURL() |
||
584 | |||
585 | /** |
||
586 | * Returns the root filesystem folder for the site. It will be automatically calculated unless |
||
587 | * it is overridden with {@link setBaseFolder()}. |
||
588 | * |
||
589 | * @return string |
||
590 | */ |
||
591 | public static function baseFolder() |
||
596 | |||
597 | /** |
||
598 | * Turns an absolute URL or folder into one that's relative to the root of the site. This is useful |
||
599 | * when turning a URL into a filesystem reference, or vice versa. |
||
600 | * |
||
601 | * @param string $url Accepts both a URL or a filesystem path. |
||
602 | * |
||
603 | * @return string |
||
604 | */ |
||
605 | public static function makeRelative($url) |
||
653 | |||
654 | /** |
||
655 | * Returns true if a given path is absolute. Works under both *nix and windows systems. |
||
656 | * |
||
657 | * @param string $path |
||
658 | * |
||
659 | * @return bool |
||
660 | */ |
||
661 | public static function is_absolute($path) |
||
671 | |||
672 | /** |
||
673 | * Determine if the url is root relative (i.e. starts with /, but not with //) SilverStripe |
||
674 | * considers root relative urls as a subset of relative urls. |
||
675 | * |
||
676 | * @param string $url |
||
677 | * |
||
678 | * @return bool |
||
679 | */ |
||
680 | public static function is_root_relative_url($url) |
||
684 | |||
685 | /** |
||
686 | * Checks if a given URL is absolute (e.g. starts with 'http://' etc.). URLs beginning with "//" |
||
687 | * are treated as absolute, as browsers take this to mean the same protocol as currently being used. |
||
688 | * |
||
689 | * Useful to check before redirecting based on a URL from user submissions through $_GET or $_POST, |
||
690 | * and avoid phishing attacks by redirecting to an attackers server. |
||
691 | * |
||
692 | * Note: Can't solely rely on PHP's parse_url() , since it is not intended to work with relative URLs |
||
693 | * or for security purposes. filter_var($url, FILTER_VALIDATE_URL) has similar problems. |
||
694 | * |
||
695 | * @param string $url |
||
696 | * |
||
697 | * @return bool |
||
698 | */ |
||
699 | public static function is_absolute_url($url) |
||
725 | |||
726 | /** |
||
727 | * Checks if a given URL is relative (or root relative) by checking {@link is_absolute_url()}. |
||
728 | * |
||
729 | * @param string $url |
||
730 | * |
||
731 | * @return bool |
||
732 | */ |
||
733 | public static function is_relative_url($url) |
||
737 | |||
738 | /** |
||
739 | * Checks if the given URL is belonging to this "site" (not an external link). That's the case if |
||
740 | * the URL is relative, as defined by {@link is_relative_url()}, or if the host matches |
||
741 | * {@link protocolAndHost()}. |
||
742 | * |
||
743 | * Useful to check before redirecting based on a URL from user submissions through $_GET or $_POST, |
||
744 | * and avoid phishing attacks by redirecting to an attackers server. |
||
745 | * |
||
746 | * @param string $url |
||
747 | * |
||
748 | * @return bool |
||
749 | */ |
||
750 | public static function is_site_url($url) |
||
760 | |||
761 | /** |
||
762 | * Given a filesystem reference relative to the site root, return the full file-system path. |
||
763 | * |
||
764 | * @param string $file |
||
765 | * |
||
766 | * @return string |
||
767 | */ |
||
768 | public static function getAbsFile($file) |
||
772 | |||
773 | /** |
||
774 | * Returns true if the given file exists. Filename should be relative to the site root. |
||
775 | * |
||
776 | * @param $file |
||
777 | * |
||
778 | * @return bool |
||
779 | */ |
||
780 | public static function fileExists($file) |
||
786 | |||
787 | /** |
||
788 | * Returns the Absolute URL of the site root. |
||
789 | * |
||
790 | * @return string |
||
791 | */ |
||
792 | public static function absoluteBaseURL() |
||
799 | |||
800 | /** |
||
801 | * Returns the Absolute URL of the site root, embedding the current basic-auth credentials into |
||
802 | * the URL. |
||
803 | * |
||
804 | * @param HTTPRequest|null $request |
||
805 | * @return string |
||
806 | */ |
||
807 | public static function absoluteBaseURLWithAuth(HTTPRequest $request = null) |
||
817 | |||
818 | /** |
||
819 | * Skip any further processing and immediately respond with a redirect to the passed URL. |
||
820 | * |
||
821 | * @param string $destURL |
||
822 | * @throws HTTPResponse_Exception |
||
823 | */ |
||
824 | protected static function force_redirect($destURL) |
||
832 | |||
833 | /** |
||
834 | * Force the site to run on SSL. |
||
835 | * |
||
836 | * To use, call from _config.php. For example: |
||
837 | * <code> |
||
838 | * if (Director::isLive()) Director::forceSSL(); |
||
839 | * </code> |
||
840 | * |
||
841 | * If you don't want your entire site to be on SSL, you can pass an array of PCRE regular expression |
||
842 | * patterns for matching relative URLs. For example: |
||
843 | * <code> |
||
844 | * if (Director::isLive()) Director::forceSSL(array('/^admin/', '/^Security/')); |
||
845 | * </code> |
||
846 | * |
||
847 | * If you want certain parts of your site protected under a different domain, you can specify |
||
848 | * the domain as an argument: |
||
849 | * <code> |
||
850 | * if (Director::isLive()) Director::forceSSL(array('/^admin/', '/^Security/'), 'secure.mysite.com'); |
||
851 | * </code> |
||
852 | * |
||
853 | * Note that the session data will be lost when moving from HTTP to HTTPS. It is your responsibility |
||
854 | * to ensure that this won't cause usability problems. |
||
855 | * |
||
856 | * CAUTION: This does not respect the site environment mode. You should check this |
||
857 | * as per the above examples using Director::isLive() or Director::isTest() for example. |
||
858 | * |
||
859 | * @param array $patterns Array of regex patterns to match URLs that should be HTTPS. |
||
860 | * @param string $secureDomain Secure domain to redirect to. Defaults to the current domain. |
||
861 | * @return bool true if already on SSL, false if doesn't match patterns (or cannot redirect) |
||
862 | * @throws HTTPResponse_Exception Throws exception with redirect, if successful |
||
863 | */ |
||
864 | public static function forceSSL($patterns = null, $secureDomain = null) |
||
902 | |||
903 | /** |
||
904 | * Force a redirect to a domain starting with "www." |
||
905 | */ |
||
906 | public static function forceWWW() |
||
918 | |||
919 | /** |
||
920 | * Checks if the current HTTP-Request is an "Ajax-Request" by checking for a custom header set by |
||
921 | * jQuery or whether a manually set request-parameter 'ajax' is present. |
||
922 | * |
||
923 | * @param HTTPRequest $request |
||
924 | * @return bool |
||
925 | */ |
||
926 | public static function is_ajax(HTTPRequest $request = null) |
||
938 | |||
939 | /** |
||
940 | * Returns true if this script is being run from the command line rather than the web server. |
||
941 | * |
||
942 | * @return bool |
||
943 | */ |
||
944 | public static function is_cli() |
||
948 | |||
949 | /** |
||
950 | * Can also be checked with {@link Director::isDev()}, {@link Director::isTest()}, and |
||
951 | * {@link Director::isLive()}. |
||
952 | * |
||
953 | * @return bool |
||
954 | */ |
||
955 | public static function get_environment_type() |
||
961 | |||
962 | /** |
||
963 | * This function will return true if the site is in a live environment. For information about |
||
964 | * environment types, see {@link Director::set_environment_type()}. |
||
965 | * |
||
966 | * @return bool |
||
967 | */ |
||
968 | public static function isLive() |
||
972 | |||
973 | /** |
||
974 | * This function will return true if the site is in a development environment. For information about |
||
975 | * environment types, see {@link Director::set_environment_type()}. |
||
976 | * |
||
977 | * @return bool |
||
978 | */ |
||
979 | public static function isDev() |
||
983 | |||
984 | /** |
||
985 | * This function will return true if the site is in a test environment. For information about |
||
986 | * environment types, see {@link Director::set_environment_type()}. |
||
987 | * |
||
988 | * @return bool |
||
989 | */ |
||
990 | public static function isTest() |
||
994 | |||
995 | /** |
||
996 | * Returns an array of strings of the method names of methods on the call that should be exposed |
||
997 | * as global variables in the templates. |
||
998 | * |
||
999 | * @return array |
||
1000 | */ |
||
1001 | public static function get_template_global_variables() |
||
1011 | |||
1012 | /** |
||
1013 | * Helper to validate or check the current request object |
||
1014 | * |
||
1015 | * @param HTTPRequest $request |
||
1016 | * @return HTTPRequest Request object if one is both current and valid |
||
1017 | */ |
||
1018 | protected static function currentRequest(HTTPRequest $request = null) |
||
1027 | } |
||
1028 |
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: