Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 17 | class Director implements TemplateGlobalProvider { |
||
| 18 | |||
| 19 | static private $urlParams; |
||
| 20 | |||
| 21 | static private $rules = array(); |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var SiteTree |
||
| 25 | */ |
||
| 26 | private static $current_page; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @config |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | private static $alternate_base_folder; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @config |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | private static $dev_servers = array(); |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @config |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | private static $test_servers = array(); |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Setting this explicitly specifies the protocol (http or https) used, overriding |
||
| 48 | * the normal behaviour of Director::is_https introspecting it from the request |
||
| 49 | * |
||
| 50 | * @config |
||
| 51 | * @var string - "http" or "https" to force the protocol, or false-ish to use default introspection from request |
||
| 52 | */ |
||
| 53 | private static $alternate_protocol; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @config |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | private static $alternate_base_url; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @config |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | private static $environment_type; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Add URL matching rules to the Director. |
||
| 69 | * |
||
| 70 | * The director is responsible for turning URLs into Controller objects. |
||
| 71 | * |
||
| 72 | * @deprecated 4.0 Use the "Director.rules" config setting instead |
||
| 73 | * @param $priority The priority of the rules; higher values will get your rule checked first. We recommend |
||
| 74 | * priority 100 for your site's rules. The built-in rules are priority 10, standard modules are |
||
| 75 | * priority 50. |
||
| 76 | */ |
||
| 77 | public static function addRules($priority, $rules) { |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Process the given URL, creating the appropriate controller and executing it. |
||
| 85 | * |
||
| 86 | * Request processing is handled as follows: |
||
| 87 | * - Director::direct() creates a new SS_HTTPResponse object and passes this to Director::handleRequest(). |
||
| 88 | * - Director::handleRequest($request) checks each of the Director rules and identifies a controller to handle |
||
| 89 | * this request. |
||
| 90 | * - Controller::handleRequest($request) is then called. This will find a rule to handle the URL, and call the |
||
| 91 | * rule handling method. |
||
| 92 | * - RequestHandler::handleRequest($request) is recursively called whenever a rule handling method returns a |
||
| 93 | * RequestHandler object. |
||
| 94 | * |
||
| 95 | * In addition to request processing, Director will manage the session, and perform the output of the actual |
||
| 96 | * response to the browser. |
||
| 97 | * |
||
| 98 | * @param $url String, the URL the user is visiting, without the querystring. |
||
| 99 | * @uses handleRequest() rule-lookup logic is handled by this. |
||
| 100 | * @uses Controller::run() Controller::run() handles the page logic for a Director::direct() call. |
||
| 101 | */ |
||
| 102 | public static function direct($url, DataModel $model) { |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Test a URL request, returning a response object. |
||
| 198 | * |
||
| 199 | * This method is the counterpart of Director::direct() that is used in functional testing. It will execute the |
||
| 200 | * URL given, and return the result as an SS_HTTPResponse object. |
||
| 201 | * |
||
| 202 | * @param string $url The URL to visit |
||
| 203 | * @param array $postVars The $_POST & $_FILES variables |
||
| 204 | * @param Session $session The {@link Session} object representing the current session. By passing the same |
||
| 205 | * object to multiple calls of Director::test(), you can simulate a persisted session. |
||
| 206 | * @param string $httpMethod The HTTP method, such as GET or POST. It will default to POST if postVars is set, |
||
| 207 | * GET otherwise. Overwritten by $postVars['_method'] if present. |
||
| 208 | * @param string $body The HTTP body |
||
| 209 | * @param array $headers HTTP headers with key-value pairs |
||
| 210 | * @param array|Cookie_Backend $cookies to populate $_COOKIE |
||
| 211 | * @param HTTP_Request $request The {@see HTTP_Request} object generated as a part of this request |
||
| 212 | * @return SS_HTTPResponse |
||
| 213 | * |
||
| 214 | * @uses getControllerForURL() The rule-lookup logic is handled by this. |
||
| 215 | * @uses Controller::run() Controller::run() handles the page logic for a Director::direct() call. |
||
| 216 | */ |
||
| 217 | public static function test($url, $postVars = null, $session = array(), $httpMethod = null, $body = null, |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Handle an HTTP request, defined with a SS_HTTPRequest object. |
||
| 345 | * |
||
| 346 | * @return SS_HTTPResponse|string |
||
| 347 | */ |
||
| 348 | protected static function handleRequest(SS_HTTPRequest $request, Session $session, DataModel $model) { |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Set url parameters (should only be called internally by RequestHandler->handleRequest()). |
||
| 403 | * |
||
| 404 | * @param $params array |
||
| 405 | */ |
||
| 406 | public static function setUrlParams($params) { |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Return the {@link SiteTree} object that is currently being viewed. If there is no SiteTree object to return, |
||
| 412 | * then this will return the current controller. |
||
| 413 | * |
||
| 414 | * @return SiteTree |
||
| 415 | */ |
||
| 416 | public static function get_current_page() { |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Set the currently active {@link SiteTree} object that is being used to respond to the request. |
||
| 422 | * |
||
| 423 | * @param SiteTree $page |
||
| 424 | */ |
||
| 425 | public static function set_current_page($page) { |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Turns the given URL into an absolute URL. |
||
| 431 | * By default non-site root relative urls will be evaluated relative to the current request. |
||
| 432 | * |
||
| 433 | * @param string $url URL To transform to absolute |
||
| 434 | * @param bool $relativeToSiteBase Flag indicating if non-site root relative urls should be |
||
| 435 | * evaluated relative to the site BaseURL instead of the current url. |
||
| 436 | * @return string The fully qualified URL |
||
| 437 | */ |
||
| 438 | public static function absoluteURL($url, $relativeToSiteBase = false) { |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Returns the part of the URL, 'http://www.mysite.com'. |
||
| 465 | * |
||
| 466 | * @return boolean|string The domain from the PHP environment. Returns FALSE is this environment variable isn't |
||
| 467 | * set. |
||
| 468 | */ |
||
| 469 | public static function protocolAndHost() { |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Return the current protocol that the site is running under. |
||
| 496 | * |
||
| 497 | * @return string |
||
| 498 | */ |
||
| 499 | public static function protocol() { |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Return whether the site is running as under HTTPS. |
||
| 505 | * |
||
| 506 | * @return boolean |
||
| 507 | */ |
||
| 508 | public static function is_https() { |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Returns the root URL for the site. |
||
| 546 | * |
||
| 547 | * It will be automatically calculated unless it is overridden with |
||
| 548 | * {@link setBaseURL()}. |
||
| 549 | * |
||
| 550 | * @return string |
||
| 551 | */ |
||
| 552 | public static function baseURL() { |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Sets the root URL for the website. |
||
| 576 | * If the site isn't accessible from the URL you provide, weird things will happen. |
||
| 577 | * |
||
| 578 | * @deprecated 4.0 Use the "Director.alternate_base_url" config setting instead |
||
| 579 | */ |
||
| 580 | public static function setBaseURL($baseURL) { |
||
| 584 | |||
| 585 | /** |
||
| 586 | * Returns the root filesystem folder for the site. |
||
| 587 | * It will be automatically calculated unless it is overridden with {@link setBaseFolder()}. |
||
| 588 | */ |
||
| 589 | public static function baseFolder() { |
||
| 593 | |||
| 594 | /** |
||
| 595 | * Sets the root folder for the website. |
||
| 596 | * If the site isn't accessible from the folder you provide, weird things will happen. |
||
| 597 | * |
||
| 598 | * @deprecated 4.0 Use the "Director.alternate_base_folder" config setting instead |
||
| 599 | */ |
||
| 600 | public static function setBaseFolder($baseFolder) { |
||
| 604 | |||
| 605 | /** |
||
| 606 | * Turns an absolute URL or folder into one that's relative to the root of |
||
| 607 | * the site. This is useful when turning a URL into a filesystem reference, |
||
| 608 | * or vice versa. |
||
| 609 | * |
||
| 610 | * @param string $url Accepts both a URL or a filesystem path |
||
| 611 | * @return string Either a relative URL if the checks succeeded, or the |
||
| 612 | * original (possibly absolute) URL. |
||
| 613 | */ |
||
| 614 | public static function makeRelative($url) { |
||
| 661 | |||
| 662 | /** |
||
| 663 | * Returns true if a given path is absolute. Works under both *nix and windows |
||
| 664 | * systems |
||
| 665 | * |
||
| 666 | * @param string $path |
||
| 667 | * @return bool |
||
| 668 | */ |
||
| 669 | public static function is_absolute($path) { |
||
| 673 | |||
| 674 | /** |
||
| 675 | * Checks if a given URL is absolute (e.g. starts with 'http://' etc.). |
||
| 676 | * URLs beginning with "//" are treated as absolute, as browsers take this to mean |
||
| 677 | * the same protocol as currently being used. |
||
| 678 | * |
||
| 679 | * Useful to check before redirecting based on a URL from user submissions |
||
| 680 | * through $_GET or $_POST, and avoid phishing attacks by redirecting |
||
| 681 | * to an attackers server. |
||
| 682 | * |
||
| 683 | * Note: Can't solely rely on PHP's parse_url() , since it is not intended to work with relative URLs |
||
| 684 | * or for security purposes. filter_var($url, FILTER_VALIDATE_URL) has similar problems. |
||
| 685 | * |
||
| 686 | * @param string $url |
||
| 687 | * @return boolean |
||
| 688 | */ |
||
| 689 | public static function is_absolute_url($url) { |
||
| 714 | |||
| 715 | /** |
||
| 716 | * Checks if a given URL is relative by checking {@link is_absolute_url()}. |
||
| 717 | * |
||
| 718 | * @param string $url |
||
| 719 | * @return boolean |
||
| 720 | */ |
||
| 721 | public static function is_relative_url($url) { |
||
| 724 | |||
| 725 | /** |
||
| 726 | * Checks if the given URL is belonging to this "site" (not an external link). |
||
| 727 | * That's the case if the URL is relative, as defined by {@link is_relative_url()}, |
||
| 728 | * or if the host matches {@link protocolAndHost()}. |
||
| 729 | * |
||
| 730 | * Useful to check before redirecting based on a URL from user submissions |
||
| 731 | * through $_GET or $_POST, and avoid phishing attacks by redirecting |
||
| 732 | * to an attackers server. |
||
| 733 | * |
||
| 734 | * @param string $url |
||
| 735 | * @return boolean |
||
| 736 | */ |
||
| 737 | public static function is_site_url($url) { |
||
| 746 | |||
| 747 | /** |
||
| 748 | * Takes a $_SERVER data array and extracts HTTP request headers. |
||
| 749 | * |
||
| 750 | * @param array $data |
||
| 751 | * @return array |
||
| 752 | */ |
||
| 753 | public static function extract_request_headers(array $server) { |
||
| 770 | |||
| 771 | /** |
||
| 772 | * Given a filesystem reference relative to the site root, return the full file-system path. |
||
| 773 | * |
||
| 774 | * @param string $file |
||
| 775 | * @return string |
||
| 776 | */ |
||
| 777 | public static function getAbsFile($file) { |
||
| 780 | |||
| 781 | /** |
||
| 782 | * Returns true if the given file exists. |
||
| 783 | * @param $file Filename specified relative to the site root |
||
| 784 | */ |
||
| 785 | public static function fileExists($file) { |
||
| 790 | |||
| 791 | /** |
||
| 792 | * Returns the Absolute URL of the site root. |
||
| 793 | */ |
||
| 794 | public static function absoluteBaseURL() { |
||
| 797 | |||
| 798 | /** |
||
| 799 | * Returns the Absolute URL of the site root, embedding the current basic-auth credentials into the URL. |
||
| 800 | */ |
||
| 801 | public static function absoluteBaseURLWithAuth() { |
||
| 809 | |||
| 810 | /** |
||
| 811 | * Skip any further processing and immediately respond with a redirect to the passed URL. |
||
| 812 | * |
||
| 813 | * @param string $destURL - The URL to redirect to |
||
| 814 | */ |
||
| 815 | protected static function force_redirect($destURL) { |
||
| 825 | |||
| 826 | /** |
||
| 827 | * Force the site to run on SSL. |
||
| 828 | * |
||
| 829 | * To use, call from _config.php. For example: |
||
| 830 | * <code> |
||
| 831 | * if(Director::isLive()) Director::forceSSL(); |
||
| 832 | * </code> |
||
| 833 | * |
||
| 834 | * If you don't want your entire site to be on SSL, you can pass an array of PCRE regular expression |
||
| 835 | * patterns for matching relative URLs. For example: |
||
| 836 | * <code> |
||
| 837 | * if(Director::isLive()) Director::forceSSL(array('/^admin/', '/^Security/')); |
||
| 838 | * </code> |
||
| 839 | * |
||
| 840 | * If you want certain parts of your site protected under a different domain, you can specify |
||
| 841 | * the domain as an argument: |
||
| 842 | * <code> |
||
| 843 | * if(Director::isLive()) Director::forceSSL(array('/^admin/', '/^Security/'), 'secure.mysite.com'); |
||
| 844 | * </code> |
||
| 845 | * |
||
| 846 | * Note that the session data will be lost when moving from HTTP to HTTPS. |
||
| 847 | * It is your responsibility to ensure that this won't cause usability problems. |
||
| 848 | * |
||
| 849 | * CAUTION: This does not respect the site environment mode. You should check this |
||
| 850 | * as per the above examples using Director::isLive() or Director::isTest() for example. |
||
| 851 | * |
||
| 852 | * @param array $patterns Array of regex patterns to match URLs that should be HTTPS |
||
| 853 | * @param string $secureDomain Secure domain to redirect to. Defaults to the current domain |
||
| 854 | * @return boolean|string String of URL when unit tests running, boolean FALSE if patterns don't match request URI |
||
| 855 | */ |
||
| 856 | public static function forceSSL($patterns = null, $secureDomain = null) { |
||
| 900 | |||
| 901 | /** |
||
| 902 | * Force a redirect to a domain starting with "www." |
||
| 903 | */ |
||
| 904 | public static function forceWWW() { |
||
| 912 | |||
| 913 | /** |
||
| 914 | * Checks if the current HTTP-Request is an "Ajax-Request" |
||
| 915 | * by checking for a custom header set by jQuery or |
||
| 916 | * wether a manually set request-parameter 'ajax' is present. |
||
| 917 | * |
||
| 918 | * @return boolean |
||
| 919 | */ |
||
| 920 | public static function is_ajax() { |
||
| 930 | |||
| 931 | /** |
||
| 932 | * Returns true if this script is being run from the command line rather than the webserver. |
||
| 933 | * |
||
| 934 | * @return boolean |
||
| 935 | */ |
||
| 936 | public static function is_cli() { |
||
| 939 | |||
| 940 | //////////////////////////////////////////////////////////////////////////////////////////// |
||
| 941 | // Environment type methods |
||
| 942 | //////////////////////////////////////////////////////////////////////////////////////////// |
||
| 943 | |||
| 944 | /** |
||
| 945 | * Set the environment type of the current site. |
||
| 946 | * |
||
| 947 | * Typically, a SilverStripe site have a number of environments: |
||
| 948 | * - development environments, such a copy on your local machine. |
||
| 949 | * - test sites, such as the one you show the client before going live. |
||
| 950 | * - the live site itself. |
||
| 951 | * |
||
| 952 | * The behaviour of these environments often varies slightly. For example, development sites may have errors |
||
| 953 | * dumped to the screen, and order confirmation emails might be sent to the developer instead of the client. |
||
| 954 | * |
||
| 955 | * To help with this, SilverStripe supports the notion of an environment type. The environment type can be dev, |
||
| 956 | * test, or live. |
||
| 957 | * |
||
| 958 | * You can set it explicitly with Director::set_environment_tpye(). Or you can use |
||
| 959 | * {@link Director::$dev_servers} and {@link Director::$test_servers} to set it implicitly, based on the |
||
| 960 | * value of $_SERVER['HTTP_HOST']. If the HTTP_HOST value is one of the servers listed, then the environment type |
||
| 961 | * will be test or dev. Otherwise, the environment type will be live. |
||
| 962 | * |
||
| 963 | * Dev mode can also be forced by putting ?isDev=1 in your URL, which will ask you to log in and then push the |
||
| 964 | * site into dev mode for the remainder of the session. Putting ?isDev=0 onto the URL can turn it back. |
||
| 965 | * |
||
| 966 | * Test mode can also be forced by putting ?isTest=1 in your URL, which will ask you to log in and then push the |
||
| 967 | * site into test mode for the remainder of the session. Putting ?isTest=0 onto the URL can turn it back. |
||
| 968 | * |
||
| 969 | * Generally speaking, these methods will be called from your _config.php file. |
||
| 970 | * |
||
| 971 | * Once the environment type is set, it can be checked with {@link Director::isDev()}, {@link Director::isTest()}, |
||
| 972 | * and {@link Director::isLive()}. |
||
| 973 | * |
||
| 974 | * @deprecated 4.0 Use the "Director.environment_type" config setting instead |
||
| 975 | * @param $et string The environment type: dev, test, or live. |
||
| 976 | */ |
||
| 977 | public static function set_environment_type($et) { |
||
| 986 | |||
| 987 | /** |
||
| 988 | * Can also be checked with {@link Director::isDev()}, {@link Director::isTest()}, and {@link Director::isLive()}. |
||
| 989 | * |
||
| 990 | * @return string 'dev', 'test' or 'live' |
||
| 991 | */ |
||
| 992 | public static function get_environment_type() { |
||
| 1003 | |||
| 1004 | /* |
||
| 1005 | * This function will return true if the site is in a live environment. |
||
| 1006 | * For information about environment types, see {@link Director::set_environment_type()}. |
||
| 1007 | */ |
||
| 1008 | public static function isLive() { |
||
| 1011 | |||
| 1012 | /** |
||
| 1013 | * This function will return true if the site is in a development environment. |
||
| 1014 | * For information about environment types, see {@link Director::set_environment_type()}. |
||
| 1015 | */ |
||
| 1016 | View Code Duplication | public static function isDev() { |
|
| 1031 | |||
| 1032 | /** |
||
| 1033 | * This function will return true if the site is in a test environment. |
||
| 1034 | * For information about environment types, see {@link Director::set_environment_type()}. |
||
| 1035 | */ |
||
| 1036 | View Code Duplication | public static function isTest() { |
|
| 1054 | |||
| 1055 | /** |
||
| 1056 | * Check or update any temporary environment specified in the session |
||
| 1057 | * |
||
| 1058 | * @return string 'test', 'dev', or null |
||
| 1059 | */ |
||
| 1060 | protected static function session_environment() { |
||
| 1084 | |||
| 1085 | /** |
||
| 1086 | * @return array Returns an array of strings of the method names of methods on the call that should be exposed |
||
| 1087 | * as global variables in the templates. |
||
| 1088 | */ |
||
| 1089 | public static function get_template_global_variables() { |
||
| 1098 | } |
||
| 1099 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.