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 |
||
| 30 | class Director implements TemplateGlobalProvider |
||
| 31 | { |
||
| 32 | use Configurable; |
||
| 33 | use HTTPMiddlewareAware; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Specifies this url is relative to the base. |
||
| 37 | * |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | const BASE = 'BASE'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Specifies this url is relative to the site root. |
||
| 44 | * |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | const ROOT = 'ROOT'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * specifies this url is relative to the current request. |
||
| 51 | * |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | const REQUEST = 'REQUEST'; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @config |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | private static $rules = array(); |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Set current page |
||
| 64 | * |
||
| 65 | * @internal |
||
| 66 | * @var SiteTree |
||
| 67 | */ |
||
| 68 | private static $current_page; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @config |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | private static $alternate_base_folder; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Force the base_url to a specific value. |
||
| 78 | * If assigned, default_base_url and the value in the $_SERVER |
||
| 79 | * global is ignored. |
||
| 80 | * Supports back-ticked vars; E.g. '`SS_BASE_URL`' |
||
| 81 | * |
||
| 82 | * @config |
||
| 83 | * @var string |
||
| 84 | */ |
||
| 85 | private static $alternate_base_url; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Base url to populate if cannot be determined otherwise. |
||
| 89 | * Supports back-ticked vars; E.g. '`SS_BASE_URL`' |
||
| 90 | * |
||
| 91 | * @config |
||
| 92 | * @var string |
||
| 93 | */ |
||
| 94 | private static $default_base_url = '`SS_BASE_URL`'; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Assigned environment type |
||
| 98 | * |
||
| 99 | * @internal |
||
| 100 | * @var string |
||
| 101 | */ |
||
| 102 | protected static $environment_type; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Test a URL request, returning a response object. This method is a wrapper around |
||
| 106 | * Director::handleRequest() to assist with functional testing. It will execute the URL given, and |
||
| 107 | * return the result as an HTTPResponse object. |
||
| 108 | * |
||
| 109 | * @param string $url The URL to visit. |
||
| 110 | * @param array $postVars The $_POST & $_FILES variables. |
||
| 111 | * @param array|Session $session The {@link Session} object representing the current session. |
||
| 112 | * By passing the same object to multiple calls of Director::test(), you can simulate a persisted |
||
| 113 | * session. |
||
| 114 | * @param string $httpMethod The HTTP method, such as GET or POST. It will default to POST if |
||
| 115 | * postVars is set, GET otherwise. Overwritten by $postVars['_method'] if present. |
||
| 116 | * @param string $body The HTTP body. |
||
| 117 | * @param array $headers HTTP headers with key-value pairs. |
||
| 118 | * @param array|Cookie_Backend $cookies to populate $_COOKIE. |
||
| 119 | * @param HTTPRequest $request The {@see SS_HTTP_Request} object generated as a part of this request. |
||
| 120 | * |
||
| 121 | * @return HTTPResponse |
||
| 122 | * |
||
| 123 | * @throws HTTPResponse_Exception |
||
| 124 | */ |
||
| 125 | public static function test( |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Mock a request, passing this to the given callback, before resetting. |
||
| 152 | * |
||
| 153 | * @param callable $callback Action to pass the HTTPRequst object |
||
| 154 | * @param string $url The URL to build |
||
| 155 | * @param array $postVars The $_POST & $_FILES variables. |
||
| 156 | * @param array|Session $session The {@link Session} object representing the current session. |
||
| 157 | * By passing the same object to multiple calls of Director::test(), you can simulate a persisted |
||
| 158 | * session. |
||
| 159 | * @param string $httpMethod The HTTP method, such as GET or POST. It will default to POST if |
||
| 160 | * postVars is set, GET otherwise. Overwritten by $postVars['_method'] if present. |
||
| 161 | * @param string $body The HTTP body. |
||
| 162 | * @param array $headers HTTP headers with key-value pairs. |
||
| 163 | * @param array|Cookie_Backend $cookies to populate $_COOKIE. |
||
| 164 | * @param HTTPRequest $request The {@see SS_HTTP_Request} object generated as a part of this request. |
||
| 165 | * @return mixed Result of callback |
||
| 166 | */ |
||
| 167 | public static function mockRequest( |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Process the given URL, creating the appropriate controller and executing it. |
||
| 291 | * |
||
| 292 | * Request processing is handled as follows: |
||
| 293 | * - Director::handleRequest($request) checks each of the Director rules and identifies a controller |
||
| 294 | * to handle this request. |
||
| 295 | * - Controller::handleRequest($request) is then called. This will find a rule to handle the URL, |
||
| 296 | * and call the rule handling method. |
||
| 297 | * - RequestHandler::handleRequest($request) is recursively called whenever a rule handling method |
||
| 298 | * returns a RequestHandler object. |
||
| 299 | * |
||
| 300 | * In addition to request processing, Director will manage the session, and perform the output of |
||
| 301 | * the actual response to the browser. |
||
| 302 | * |
||
| 303 | * @param HTTPRequest $request |
||
| 304 | * @return HTTPResponse |
||
| 305 | * @throws HTTPResponse_Exception |
||
| 306 | */ |
||
| 307 | public function handleRequest(HTTPRequest $request) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Call the given request handler with the given middlewares |
||
| 392 | * Middlewares are specified as Injector service names |
||
| 393 | * |
||
| 394 | * @param $request The request to pass to the handler |
||
| 395 | * @param $middlewareNames The services names of the middlewares to apply |
||
| 396 | * @param $handler The request handler |
||
| 397 | */ |
||
| 398 | protected static function callWithMiddlewares(HTTPRequest $request, array $middlewareNames, callable $handler) |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Return the {@link SiteTree} object that is currently being viewed. If there is no SiteTree |
||
| 424 | * object to return, then this will return the current controller. |
||
| 425 | * |
||
| 426 | * @return SiteTree|Controller |
||
| 427 | */ |
||
| 428 | public static function get_current_page() |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Set the currently active {@link SiteTree} object that is being used to respond to the request. |
||
| 435 | * |
||
| 436 | * @param SiteTree $page |
||
| 437 | */ |
||
| 438 | public static function set_current_page($page) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Turns the given URL into an absolute URL. By default non-site root relative urls will be |
||
| 445 | * evaluated relative to the current base_url. |
||
| 446 | * |
||
| 447 | * @param string $url URL To transform to absolute. |
||
| 448 | * @param string $relativeParent Method to use for evaluating relative urls. |
||
| 449 | * Either one of BASE (baseurl), ROOT (site root), or REQUEST (requested page). |
||
| 450 | * Defaults to BASE, which is the same behaviour as template url resolution. |
||
| 451 | * Ignored if the url is absolute or site root. |
||
| 452 | * |
||
| 453 | * @return string |
||
| 454 | */ |
||
| 455 | public static function absoluteURL($url, $relativeParent = self::BASE) |
||
| 495 | |||
| 496 | /** |
||
| 497 | * A helper to determine the current hostname used to access the site. |
||
| 498 | * The following are used to determine the host (in order) |
||
| 499 | * - Director.alternate_base_url (if it contains a domain name) |
||
| 500 | * - Trusted proxy headers |
||
| 501 | * - HTTP Host header |
||
| 502 | * - SS_BASE_URL env var |
||
| 503 | * - SERVER_NAME |
||
| 504 | * - gethostname() |
||
| 505 | * |
||
| 506 | * @return string |
||
| 507 | */ |
||
| 508 | public static function host(HTTPRequest $request = null) |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Returns the domain part of the URL 'http://www.mysite.com'. Returns FALSE is this environment |
||
| 548 | * variable isn't set. |
||
| 549 | * |
||
| 550 | * @return bool|string |
||
| 551 | */ |
||
| 552 | public static function protocolAndHost(HTTPRequest $request = null) |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Return the current protocol that the site is running under. |
||
| 559 | * |
||
| 560 | * @return string |
||
| 561 | */ |
||
| 562 | public static function protocol(HTTPRequest $request = null) |
||
| 566 | |||
| 567 | /** |
||
| 568 | * Return whether the site is running as under HTTPS. |
||
| 569 | * |
||
| 570 | * @return bool |
||
| 571 | */ |
||
| 572 | public static function is_https(HTTPRequest $request = null) |
||
| 604 | |||
| 605 | /** |
||
| 606 | * Return the root-relative url for the baseurl |
||
| 607 | * |
||
| 608 | * @return string Root-relative url with trailing slash. |
||
| 609 | */ |
||
| 610 | public static function baseURL() |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Returns the root filesystem folder for the site. It will be automatically calculated unless |
||
| 633 | * it is overridden with {@link setBaseFolder()}. |
||
| 634 | * |
||
| 635 | * @return string |
||
| 636 | */ |
||
| 637 | public static function baseFolder() |
||
| 642 | |||
| 643 | /** |
||
| 644 | * Turns an absolute URL or folder into one that's relative to the root of the site. This is useful |
||
| 645 | * when turning a URL into a filesystem reference, or vice versa. |
||
| 646 | * |
||
| 647 | * @param string $url Accepts both a URL or a filesystem path. |
||
| 648 | * |
||
| 649 | * @return string |
||
| 650 | */ |
||
| 651 | public static function makeRelative($url) |
||
| 699 | |||
| 700 | /** |
||
| 701 | * Returns true if a given path is absolute. Works under both *nix and windows systems. |
||
| 702 | * |
||
| 703 | * @param string $path |
||
| 704 | * |
||
| 705 | * @return bool |
||
| 706 | */ |
||
| 707 | public static function is_absolute($path) |
||
| 717 | |||
| 718 | /** |
||
| 719 | * Determine if the url is root relative (i.e. starts with /, but not with //) SilverStripe |
||
| 720 | * considers root relative urls as a subset of relative urls. |
||
| 721 | * |
||
| 722 | * @param string $url |
||
| 723 | * |
||
| 724 | * @return bool |
||
| 725 | */ |
||
| 726 | public static function is_root_relative_url($url) |
||
| 730 | |||
| 731 | /** |
||
| 732 | * Checks if a given URL is absolute (e.g. starts with 'http://' etc.). URLs beginning with "//" |
||
| 733 | * are treated as absolute, as browsers take this to mean the same protocol as currently being used. |
||
| 734 | * |
||
| 735 | * Useful to check before redirecting based on a URL from user submissions through $_GET or $_POST, |
||
| 736 | * and avoid phishing attacks by redirecting to an attackers server. |
||
| 737 | * |
||
| 738 | * Note: Can't solely rely on PHP's parse_url() , since it is not intended to work with relative URLs |
||
| 739 | * or for security purposes. filter_var($url, FILTER_VALIDATE_URL) has similar problems. |
||
| 740 | * |
||
| 741 | * @param string $url |
||
| 742 | * |
||
| 743 | * @return bool |
||
| 744 | */ |
||
| 745 | public static function is_absolute_url($url) |
||
| 771 | |||
| 772 | /** |
||
| 773 | * Checks if a given URL is relative (or root relative) by checking {@link is_absolute_url()}. |
||
| 774 | * |
||
| 775 | * @param string $url |
||
| 776 | * |
||
| 777 | * @return bool |
||
| 778 | */ |
||
| 779 | public static function is_relative_url($url) |
||
| 783 | |||
| 784 | /** |
||
| 785 | * Checks if the given URL is belonging to this "site" (not an external link). That's the case if |
||
| 786 | * the URL is relative, as defined by {@link is_relative_url()}, or if the host matches |
||
| 787 | * {@link protocolAndHost()}. |
||
| 788 | * |
||
| 789 | * Useful to check before redirecting based on a URL from user submissions through $_GET or $_POST, |
||
| 790 | * and avoid phishing attacks by redirecting to an attackers server. |
||
| 791 | * |
||
| 792 | * @param string $url |
||
| 793 | * |
||
| 794 | * @return bool |
||
| 795 | */ |
||
| 796 | public static function is_site_url($url) |
||
| 806 | |||
| 807 | /** |
||
| 808 | * Given a filesystem reference relative to the site root, return the full file-system path. |
||
| 809 | * |
||
| 810 | * @param string $file |
||
| 811 | * |
||
| 812 | * @return string |
||
| 813 | */ |
||
| 814 | public static function getAbsFile($file) |
||
| 818 | |||
| 819 | /** |
||
| 820 | * Returns true if the given file exists. Filename should be relative to the site root. |
||
| 821 | * |
||
| 822 | * @param $file |
||
| 823 | * |
||
| 824 | * @return bool |
||
| 825 | */ |
||
| 826 | public static function fileExists($file) |
||
| 832 | |||
| 833 | /** |
||
| 834 | * Returns the Absolute URL of the site root. |
||
| 835 | * |
||
| 836 | * @return string |
||
| 837 | */ |
||
| 838 | public static function absoluteBaseURL() |
||
| 845 | |||
| 846 | /** |
||
| 847 | * Returns the Absolute URL of the site root, embedding the current basic-auth credentials into |
||
| 848 | * the URL. |
||
| 849 | * |
||
| 850 | * @return string |
||
| 851 | */ |
||
| 852 | public static function absoluteBaseURLWithAuth() |
||
| 862 | |||
| 863 | /** |
||
| 864 | * Skip any further processing and immediately respond with a redirect to the passed URL. |
||
| 865 | * |
||
| 866 | * @param string $destURL |
||
| 867 | * @throws HTTPResponse_Exception |
||
| 868 | */ |
||
| 869 | protected static function force_redirect($destURL) |
||
| 877 | |||
| 878 | /** |
||
| 879 | * Force the site to run on SSL. |
||
| 880 | * |
||
| 881 | * To use, call from _config.php. For example: |
||
| 882 | * <code> |
||
| 883 | * if (Director::isLive()) Director::forceSSL(); |
||
| 884 | * </code> |
||
| 885 | * |
||
| 886 | * If you don't want your entire site to be on SSL, you can pass an array of PCRE regular expression |
||
| 887 | * patterns for matching relative URLs. For example: |
||
| 888 | * <code> |
||
| 889 | * if (Director::isLive()) Director::forceSSL(array('/^admin/', '/^Security/')); |
||
| 890 | * </code> |
||
| 891 | * |
||
| 892 | * If you want certain parts of your site protected under a different domain, you can specify |
||
| 893 | * the domain as an argument: |
||
| 894 | * <code> |
||
| 895 | * if (Director::isLive()) Director::forceSSL(array('/^admin/', '/^Security/'), 'secure.mysite.com'); |
||
| 896 | * </code> |
||
| 897 | * |
||
| 898 | * Note that the session data will be lost when moving from HTTP to HTTPS. It is your responsibility |
||
| 899 | * to ensure that this won't cause usability problems. |
||
| 900 | * |
||
| 901 | * CAUTION: This does not respect the site environment mode. You should check this |
||
| 902 | * as per the above examples using Director::isLive() or Director::isTest() for example. |
||
| 903 | * |
||
| 904 | * @param array $patterns Array of regex patterns to match URLs that should be HTTPS. |
||
| 905 | * @param string $secureDomain Secure domain to redirect to. Defaults to the current domain. |
||
| 906 | * @return bool true if already on SSL, false if doesn't match patterns (or cannot redirect) |
||
| 907 | * @throws HTTPResponse_Exception Throws exception with redirect, if successful |
||
| 908 | */ |
||
| 909 | public static function forceSSL($patterns = null, $secureDomain = null) |
||
| 947 | |||
| 948 | /** |
||
| 949 | * Force a redirect to a domain starting with "www." |
||
| 950 | */ |
||
| 951 | public static function forceWWW() |
||
| 963 | |||
| 964 | /** |
||
| 965 | * Checks if the current HTTP-Request is an "Ajax-Request" by checking for a custom header set by |
||
| 966 | * jQuery or whether a manually set request-parameter 'ajax' is present. |
||
| 967 | * |
||
| 968 | * @return bool |
||
| 969 | */ |
||
| 970 | public static function is_ajax() |
||
| 981 | |||
| 982 | /** |
||
| 983 | * Returns true if this script is being run from the command line rather than the web server. |
||
| 984 | * |
||
| 985 | * @return bool |
||
| 986 | */ |
||
| 987 | public static function is_cli() |
||
| 991 | |||
| 992 | /** |
||
| 993 | * Can also be checked with {@link Director::isDev()}, {@link Director::isTest()}, and |
||
| 994 | * {@link Director::isLive()}. |
||
| 995 | * |
||
| 996 | * @return bool |
||
| 997 | */ |
||
| 998 | public static function get_environment_type() |
||
| 1004 | |||
| 1005 | /** |
||
| 1006 | * This function will return true if the site is in a live environment. For information about |
||
| 1007 | * environment types, see {@link Director::set_environment_type()}. |
||
| 1008 | * |
||
| 1009 | * @return bool |
||
| 1010 | */ |
||
| 1011 | public static function isLive() |
||
| 1015 | |||
| 1016 | /** |
||
| 1017 | * This function will return true if the site is in a development environment. For information about |
||
| 1018 | * environment types, see {@link Director::set_environment_type()}. |
||
| 1019 | * |
||
| 1020 | * @return bool |
||
| 1021 | */ |
||
| 1022 | public static function isDev() |
||
| 1026 | |||
| 1027 | /** |
||
| 1028 | * This function will return true if the site is in a test environment. For information about |
||
| 1029 | * environment types, see {@link Director::set_environment_type()}. |
||
| 1030 | * |
||
| 1031 | * @return bool |
||
| 1032 | */ |
||
| 1033 | public static function isTest() |
||
| 1037 | |||
| 1038 | /** |
||
| 1039 | * Returns an array of strings of the method names of methods on the call that should be exposed |
||
| 1040 | * as global variables in the templates. |
||
| 1041 | * |
||
| 1042 | * @return array |
||
| 1043 | */ |
||
| 1044 | public static function get_template_global_variables() |
||
| 1054 | } |
||
| 1055 |
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: