Complex classes like Symphony 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 Symphony, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | abstract class Symphony implements Singleton |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * An instance of the Symphony class, either `Administration` or `Frontend`. |
||
| 18 | * @var Symphony |
||
| 19 | */ |
||
| 20 | protected static $_instance = null; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * An instance of the Profiler class |
||
| 24 | * @var Profiler |
||
| 25 | */ |
||
| 26 | protected static $Profiler = null; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * An instance of the `Configuration` class |
||
| 30 | * @var Configuration |
||
| 31 | */ |
||
| 32 | private static $Configuration = null; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * An instance of the `Database` class |
||
| 36 | * @var MySQL |
||
| 37 | */ |
||
| 38 | private static $Database = null; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * An instance of the `ExtensionManager` class |
||
| 42 | * @var ExtensionManager |
||
| 43 | */ |
||
| 44 | private static $ExtensionManager = null; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * An instance of the `Log` class |
||
| 48 | * @var Log |
||
| 49 | */ |
||
| 50 | private static $Log = null; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * The current page namespace, used for translations |
||
| 54 | * @since Symphony 2.3 |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | private static $namespace = false; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * An instance of the Cookies class |
||
| 61 | * @var Cookies |
||
| 62 | */ |
||
| 63 | public static $Cookies = null; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * An instance of the Session class |
||
| 67 | * @var Session |
||
| 68 | */ |
||
| 69 | public static $Session = null; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * An instance of the SessionFlash class |
||
| 73 | * @var Session |
||
| 74 | */ |
||
| 75 | public static $Flash = null; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * An instance of the currently logged in Author |
||
| 79 | * @var Author |
||
| 80 | */ |
||
| 81 | public static $Author = null; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * A previous exception that has been fired. Defaults to null. |
||
| 85 | * @since Symphony 2.3.2 |
||
| 86 | * @var Exception |
||
| 87 | */ |
||
| 88 | private static $exception = null; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * The Symphony constructor initialises the class variables of Symphony. At present |
||
| 92 | * constructor has a couple of responsibilities: |
||
| 93 | * - Start a profiler instance |
||
| 94 | * - If magic quotes are enabled, clean `$_SERVER`, `$_COOKIE`, `$_GET`, `$_POST` and `$_REQUEST` arrays. |
||
| 95 | * - Initialise the correct Language for the currently logged in Author. |
||
| 96 | * - Start the session and adjust the error handling if the user is logged in |
||
| 97 | * |
||
| 98 | * The `$_REQUEST` array has been added in 2.7.0 |
||
| 99 | */ |
||
| 100 | protected function __construct() |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Setter for the Symphony Log and Error Handling system |
||
| 136 | * |
||
| 137 | * @since Symphony 2.6.0 |
||
| 138 | */ |
||
| 139 | public static function initialiseErrorHandler() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Accessor for the Symphony instance, whether it be Frontend |
||
| 149 | * or Administration |
||
| 150 | * |
||
| 151 | * @since Symphony 2.2 |
||
| 152 | * @throws Exception |
||
| 153 | * @return Symphony |
||
| 154 | */ |
||
| 155 | public static function Engine() |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Setter for `$Configuration`. This function initialise the configuration |
||
| 168 | * object and populate its properties based on the given `$array`. Since |
||
| 169 | * Symphony 2.6.5, it will also set Symphony's date constants. |
||
| 170 | * |
||
| 171 | * @since Symphony 2.3 |
||
| 172 | * @param array $data |
||
| 173 | * An array of settings to be stored into the Configuration object |
||
| 174 | */ |
||
| 175 | public static function initialiseConfiguration(array $data = array()) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Accessor for the current `Configuration` instance. This contains |
||
| 197 | * representation of the the Symphony config file. |
||
| 198 | * |
||
| 199 | * @return Configuration |
||
| 200 | */ |
||
| 201 | public static function Configuration() |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Is XSRF enabled for this Symphony install? |
||
| 208 | * |
||
| 209 | * @since Symphony 2.4 |
||
| 210 | * @return boolean |
||
| 211 | */ |
||
| 212 | public static function isXSRFEnabled() |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Accessor for the current `Profiler` instance. |
||
| 219 | * |
||
| 220 | * @since Symphony 2.3 |
||
| 221 | * @return Profiler |
||
| 222 | */ |
||
| 223 | public static function Profiler() |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Setter for `$Log`. This function uses the configuration |
||
| 230 | * settings in the 'log' group in the Configuration to create an instance. Date |
||
| 231 | * formatting options are also retrieved from the configuration. |
||
| 232 | * |
||
| 233 | * @param string $filename (optional) |
||
| 234 | * The file to write the log to, if omitted this will default to `ACTIVITY_LOG` |
||
| 235 | * @throws Exception |
||
| 236 | * @return bool|void |
||
| 237 | */ |
||
| 238 | public static function initialiseLog($filename = null) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Accessor for the current `Log` instance |
||
| 284 | * |
||
| 285 | * @since Symphony 2.3 |
||
| 286 | * @return Log |
||
| 287 | */ |
||
| 288 | public static function Log() |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Setter for `$Session`. This will use PHP's parse_url |
||
| 295 | * function on the current URL to set a session using the `session_name` |
||
| 296 | * defined in the Symphony configuration. The is either admin or public. |
||
| 297 | * The session will last for the time defined in configuration. |
||
| 298 | * |
||
| 299 | * @since Symphony 3.0.0 |
||
| 300 | */ |
||
| 301 | public function initialiseSessionAndCookies() |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Accessor for the current `$Session` instance. |
||
| 356 | * |
||
| 357 | * @since 3.0.0 |
||
| 358 | * @return Session |
||
| 359 | */ |
||
| 360 | public static function Session() |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Accessor for the current `$Cookies` instance. |
||
| 367 | * |
||
| 368 | * @since 2.0.0 |
||
| 369 | * @return Cookies |
||
| 370 | */ |
||
| 371 | public static function Cookies() |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Accessor for the current `$Flash` instance. |
||
| 378 | * |
||
| 379 | * @since 3.0.0 |
||
| 380 | * @return SessionFlash |
||
| 381 | */ |
||
| 382 | public static function Flash() |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Gets the configuerd session timeout as seconds, based on the environment instance |
||
| 389 | * @return int |
||
| 390 | * The seconds |
||
| 391 | */ |
||
| 392 | private function getSessionTimeout() |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Setter for `$ExtensionManager` using the current |
||
| 409 | * Symphony instance as the parent. If for some reason this fails, |
||
| 410 | * a Symphony Error page will be thrown |
||
| 411 | * |
||
| 412 | * @param boolean $force (optional) |
||
| 413 | * When set to true, this function will always create a new |
||
| 414 | * instance of ExtensionManager, replacing self::$ExtensionManager. |
||
| 415 | * @return void |
||
| 416 | */ |
||
| 417 | public static function initialiseExtensionManager($force = false) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Accessor for the current `$ExtensionManager` instance. |
||
| 432 | * |
||
| 433 | * @since Symphony 2.2 |
||
| 434 | * @return ExtensionManager |
||
| 435 | */ |
||
| 436 | public static function ExtensionManager() |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Setter for `$Database`, accepts a Database object. If `$database` |
||
| 443 | * is omitted, this function will set `$Database` to be of the `MySQL` |
||
| 444 | * class. |
||
| 445 | * |
||
| 446 | * @since Symphony 2.3 |
||
| 447 | * @param stdClass $database (optional) |
||
| 448 | * The class to handle all Database operations, if omitted this function |
||
| 449 | * will set `self::$Database` to be an instance of the `MySQL` class. |
||
| 450 | * @return boolean |
||
| 451 | * This function will always return true |
||
| 452 | */ |
||
| 453 | public static function setDatabase(stdClass $database = null) |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Accessor for the current `$Database` instance. |
||
| 466 | * |
||
| 467 | * @return MySQL |
||
| 468 | */ |
||
| 469 | public static function Database() |
||
| 473 | |||
| 474 | /** |
||
| 475 | * This will initialise the Database class and attempt to create a connection |
||
| 476 | * using the connection details provided in the Symphony configuration. If any |
||
| 477 | * errors occur whilst doing so, a Symphony Error Page is displayed. |
||
| 478 | * |
||
| 479 | * @throws SymphonyErrorPage |
||
| 480 | * @return boolean |
||
| 481 | * This function will return true if the `$Database` was |
||
| 482 | * initialised successfully. |
||
| 483 | */ |
||
| 484 | public static function initialiseDatabase() |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Accessor for the current `$Author` instance. |
||
| 534 | * |
||
| 535 | * @since Symphony 2.5.0 |
||
| 536 | * @return Author |
||
| 537 | */ |
||
| 538 | public static function Author() |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Attempts to log an Author in given a username and password. |
||
| 545 | * If the password is not hashed, it will be hashed using the sha1 |
||
| 546 | * algorithm. The username and password will be sanitized before |
||
| 547 | * being used to query the Database. If an Author is found, they |
||
| 548 | * will be logged in and the sanitized username and password (also hashed) |
||
| 549 | * will be saved as values in the `$Session`. |
||
| 550 | * |
||
| 551 | * @see toolkit.Cryptography#hash() |
||
| 552 | * @throws DatabaseException |
||
| 553 | * @param string $username |
||
| 554 | * The Author's username. This will be sanitized before use. |
||
| 555 | * @param string $password |
||
| 556 | * The Author's password. This will be sanitized and then hashed before use |
||
| 557 | * @param boolean $isHash |
||
| 558 | * If the password provided is already hashed, setting this parameter to |
||
| 559 | * true will stop it becoming rehashed. By default it is false. |
||
| 560 | * @return boolean |
||
| 561 | * True if the Author was logged in, false otherwise |
||
| 562 | */ |
||
| 563 | public static function login($username, $password, $isHash = false) |
||
| 608 | |||
| 609 | /** |
||
| 610 | * Symphony allows Authors to login via the use of tokens instead of |
||
| 611 | * a username and password. A token is derived from concatenating the |
||
| 612 | * Author's username and password and applying the sha1 hash to |
||
| 613 | * it, from this, a portion of the hash is used as the token. This is a useful |
||
| 614 | * feature often used when setting up other Authors accounts or if an |
||
| 615 | * Author forgets their password. |
||
| 616 | * |
||
| 617 | * @param string $token |
||
| 618 | * The Author token, which is a portion of the hashed string concatenation |
||
| 619 | * of the Author's username and password |
||
| 620 | * @throws DatabaseException |
||
| 621 | * @return boolean |
||
| 622 | * True if the Author is logged in, false otherwise |
||
| 623 | */ |
||
| 624 | public static function loginFromToken($token) |
||
| 674 | |||
| 675 | /** |
||
| 676 | * This function will destroy the currently logged in `$Author` |
||
| 677 | * session, essentially logging them out. |
||
| 678 | * |
||
| 679 | * @see core.Session#expire() |
||
| 680 | */ |
||
| 681 | public static function logout() |
||
| 685 | |||
| 686 | /** |
||
| 687 | * This function determines whether an there is a currently logged in |
||
| 688 | * Author for Symphony by using the `$Session`'s username |
||
| 689 | * and password. If an Author is found, they will be logged in, otherwise |
||
| 690 | * the `$Session` will be destroyed. |
||
| 691 | * |
||
| 692 | * @see login() |
||
| 693 | * @return boolean |
||
| 694 | */ |
||
| 695 | public static function isLoggedIn() |
||
| 705 | |||
| 706 | /** |
||
| 707 | * Returns the most recent version found in the `/install/migrations` folder. |
||
| 708 | * Returns a version string to be used in `version_compare()` if an updater |
||
| 709 | * has been found. Returns `FALSE` otherwise. |
||
| 710 | * |
||
| 711 | * @since Symphony 2.3.1 |
||
| 712 | * @return string|boolean |
||
| 713 | */ |
||
| 714 | public static function getMigrationVersion() |
||
| 725 | |||
| 726 | /** |
||
| 727 | * Checks if an update is available and applicable for the current installation. |
||
| 728 | * |
||
| 729 | * @since Symphony 2.3.1 |
||
| 730 | * @return boolean |
||
| 731 | */ |
||
| 732 | public static function isUpgradeAvailable() |
||
| 743 | |||
| 744 | /** |
||
| 745 | * Checks if the installer/upgrader is available. |
||
| 746 | * |
||
| 747 | * @since Symphony 2.3.1 |
||
| 748 | * @return boolean |
||
| 749 | */ |
||
| 750 | public static function isInstallerAvailable() |
||
| 754 | |||
| 755 | /** |
||
| 756 | * A wrapper for throwing a new Symphony Error page. |
||
| 757 | * |
||
| 758 | * This methods sets the `GenericExceptionHandler::$enabled` value to `true`. |
||
| 759 | * |
||
| 760 | * @see core.SymphonyErrorPage |
||
| 761 | * @param string|XMLElement $message |
||
| 762 | * A description for this error, which can be provided as a string |
||
| 763 | * or as an XMLElement. |
||
| 764 | * @param string $heading |
||
| 765 | * A heading for the error page |
||
| 766 | * @param integer $status |
||
| 767 | * Properly sets the HTTP status code for the response. Defaults to |
||
| 768 | * `Page::HTTP_STATUS_ERROR`. Use `Page::HTTP_STATUS_XXX` to set this value. |
||
| 769 | * @param string $template |
||
| 770 | * A string for the error page template to use, defaults to 'generic'. This |
||
| 771 | * can be the name of any template file in the `TEMPLATES` directory. |
||
| 772 | * A template using the naming convention of `tpl.*.php`. |
||
| 773 | * @param array $additional |
||
| 774 | * Allows custom information to be passed to the Symphony Error Page |
||
| 775 | * that the template may want to expose, such as custom Headers etc. |
||
| 776 | * @throws SymphonyErrorPage |
||
| 777 | */ |
||
| 778 | public static function throwCustomError($message, $heading = 'Symphony Fatal Error', $status = Page::HTTP_STATUS_ERROR, $template = 'generic', array $additional = array()) |
||
| 783 | |||
| 784 | /** |
||
| 785 | * Setter accepts a previous Exception. Useful for determining the context |
||
| 786 | * of a current exception (ie. detecting recursion). |
||
| 787 | * |
||
| 788 | * @since Symphony 2.3.2 |
||
| 789 | * @param Exception $ex |
||
| 790 | */ |
||
| 791 | public static function setException(Exception $ex) |
||
| 795 | |||
| 796 | /** |
||
| 797 | * Accessor for `self::$exception`. |
||
| 798 | * |
||
| 799 | * @since Symphony 2.3.2 |
||
| 800 | * @return Exception|null |
||
| 801 | */ |
||
| 802 | public static function getException() |
||
| 806 | |||
| 807 | /** |
||
| 808 | * Returns the page namespace based on the current URL. |
||
| 809 | * A few examples: |
||
| 810 | * |
||
| 811 | * /login |
||
| 812 | * /publish |
||
| 813 | * /blueprints/datasources |
||
| 814 | * [...] |
||
| 815 | * /extension/$extension_name/$page_name |
||
| 816 | * |
||
| 817 | * This method is especially useful in couple with the translation function. |
||
| 818 | * |
||
| 819 | * @see toolkit#__() |
||
| 820 | * @return string |
||
| 821 | * The page namespace, without any action string (e.g. "new", "saved") or |
||
| 822 | * any value that depends upon the single setup (e.g. the section handle in |
||
| 823 | * /publish/$handle) |
||
| 824 | */ |
||
| 825 | public static function getPageNamespace() |
||
| 855 | } |
||
| 856 |
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: