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 |
||
| 13 | abstract class Symphony implements Singleton |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * An instance of the Symphony class, either `Administration` or `Frontend`. |
||
| 17 | * @var Symphony |
||
| 18 | */ |
||
| 19 | protected static $_instance = null; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * An instance of the Profiler class |
||
| 23 | * @var Profiler |
||
| 24 | */ |
||
| 25 | protected static $Profiler = null; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * An instance of the `Configuration` class |
||
| 29 | * @var Configuration |
||
| 30 | */ |
||
| 31 | private static $Configuration = null; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * An instance of the `Database` class |
||
| 35 | * @var MySQL |
||
| 36 | */ |
||
| 37 | private static $Database = null; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * An instance of the `ExtensionManager` class |
||
| 41 | * @var ExtensionManager |
||
| 42 | */ |
||
| 43 | private static $ExtensionManager = null; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * An instance of the `Log` class |
||
| 47 | * @var Log |
||
| 48 | */ |
||
| 49 | private static $Log = null; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The current page namespace, used for translations |
||
| 53 | * @since Symphony 2.3 |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | private static $namespace = false; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * An instance of the Cookie class |
||
| 60 | * @var Cookie |
||
| 61 | */ |
||
| 62 | public static $Cookie = null; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * An instance of the currently logged in Author |
||
| 66 | * @var Author |
||
| 67 | */ |
||
| 68 | public static $Author = null; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * A previous exception that has been fired. Defaults to null. |
||
| 72 | * @since Symphony 2.3.2 |
||
| 73 | * @var Exception |
||
| 74 | */ |
||
| 75 | private static $exception = null; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * The Symphony constructor initialises the class variables of Symphony. At present |
||
| 79 | * constructor has a couple of responsibilities: |
||
| 80 | * - Start a profiler instance |
||
| 81 | * - If magic quotes are enabled, clean `$_SERVER`, `$_COOKIE`, `$_GET` and `$_POST` arrays |
||
| 82 | * - Initialise the correct Language for the currently logged in Author. |
||
| 83 | * - Start the session and adjust the error handling if the user is logged in |
||
| 84 | */ |
||
| 85 | protected function __construct() |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Setter for the Symphony Log and Error Handling system |
||
| 111 | * |
||
| 112 | * @since Symphony 2.6.0 |
||
| 113 | */ |
||
| 114 | public static function initialiseErrorHandler() |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Accessor for the Symphony instance, whether it be Frontend |
||
| 124 | * or Administration |
||
| 125 | * |
||
| 126 | * @since Symphony 2.2 |
||
| 127 | * @throws Exception |
||
| 128 | * @return Symphony |
||
| 129 | */ |
||
| 130 | public static function Engine() |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Setter for `$Configuration`. This function initialise the configuration |
||
| 143 | * object and populate its properties based on the given `$array`. Since |
||
| 144 | * Symphony 2.6.5, it will also set Symphony's date constants. |
||
| 145 | * |
||
| 146 | * @since Symphony 2.3 |
||
| 147 | * @param array $data |
||
| 148 | * An array of settings to be stored into the Configuration object |
||
| 149 | */ |
||
| 150 | public static function initialiseConfiguration(array $data = array()) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Accessor for the current `Configuration` instance. This contains |
||
| 172 | * representation of the the Symphony config file. |
||
| 173 | * |
||
| 174 | * @return Configuration |
||
| 175 | */ |
||
| 176 | public static function Configuration() |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Is XSRF enabled for this Symphony install? |
||
| 183 | * |
||
| 184 | * @since Symphony 2.4 |
||
| 185 | * @return boolean |
||
| 186 | */ |
||
| 187 | public static function isXSRFEnabled() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Accessor for the current `Profiler` instance. |
||
| 194 | * |
||
| 195 | * @since Symphony 2.3 |
||
| 196 | * @return Profiler |
||
| 197 | */ |
||
| 198 | public static function Profiler() |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Setter for `$Log`. This function uses the configuration |
||
| 205 | * settings in the 'log' group in the Configuration to create an instance. Date |
||
| 206 | * formatting options are also retrieved from the configuration. |
||
| 207 | * |
||
| 208 | * @param string $filename (optional) |
||
| 209 | * The file to write the log to, if omitted this will default to `ACTIVITY_LOG` |
||
| 210 | * @throws Exception |
||
| 211 | * @return bool|void |
||
| 212 | */ |
||
| 213 | public static function initialiseLog($filename = null) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Accessor for the current `Log` instance |
||
| 235 | * |
||
| 236 | * @since Symphony 2.3 |
||
| 237 | * @return Log |
||
| 238 | */ |
||
| 239 | public static function Log() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Setter for `$Cookie`. This will use PHP's parse_url |
||
| 246 | * function on the current URL to set a cookie using the cookie_prefix |
||
| 247 | * defined in the Symphony configuration. The cookie will last two |
||
| 248 | * weeks. |
||
| 249 | * |
||
| 250 | * This function also defines two constants, `__SYM_COOKIE_PATH__` |
||
| 251 | * and `__SYM_COOKIE_PREFIX__`. |
||
| 252 | * |
||
| 253 | * @deprecated Prior to Symphony 2.3.2, the constant `__SYM_COOKIE_PREFIX_` |
||
| 254 | * had a typo where it was missing the second underscore. Symphony will |
||
| 255 | * support both constants, `__SYM_COOKIE_PREFIX_` and `__SYM_COOKIE_PREFIX__` |
||
| 256 | * until Symphony 3.0 |
||
| 257 | */ |
||
| 258 | public static function initialiseCookie() |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Accessor for the current `$Cookie` instance. |
||
| 272 | * |
||
| 273 | * @since Symphony 2.5.0 |
||
| 274 | * @return Cookie |
||
| 275 | */ |
||
| 276 | public static function Cookie() |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Setter for `$ExtensionManager` using the current |
||
| 283 | * Symphony instance as the parent. If for some reason this fails, |
||
| 284 | * a Symphony Error page will be thrown |
||
| 285 | * @param Boolean $force (optional) |
||
| 286 | * When set to true, this function will always create a new |
||
| 287 | * instance of ExtensionManager, replacing self::$ExtensionManager. |
||
| 288 | */ |
||
| 289 | public static function initialiseExtensionManager($force=false) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Accessor for the current `$ExtensionManager` instance. |
||
| 304 | * |
||
| 305 | * @since Symphony 2.2 |
||
| 306 | * @return ExtensionManager |
||
| 307 | */ |
||
| 308 | public static function ExtensionManager() |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Setter for `$Database`, accepts a Database object. If `$database` |
||
| 315 | * is omitted, this function will set `$Database` to be of the `MySQL` |
||
| 316 | * class. |
||
| 317 | * |
||
| 318 | * @since Symphony 2.3 |
||
| 319 | * @param StdClass $database (optional) |
||
| 320 | * The class to handle all Database operations, if omitted this function |
||
| 321 | * will set `self::$Database` to be an instance of the `MySQL` class. |
||
| 322 | * @return boolean |
||
| 323 | * This function will always return true |
||
| 324 | */ |
||
| 325 | public static function setDatabase(StdClass $database = null) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Accessor for the current `$Database` instance. |
||
| 338 | * |
||
| 339 | * @return MySQL |
||
| 340 | */ |
||
| 341 | public static function Database() |
||
| 345 | |||
| 346 | /** |
||
| 347 | * This will initialise the Database class and attempt to create a connection |
||
| 348 | * using the connection details provided in the Symphony configuration. If any |
||
| 349 | * errors occur whilst doing so, a Symphony Error Page is displayed. |
||
| 350 | * |
||
| 351 | * @throws SymphonyErrorPage |
||
| 352 | * @return boolean |
||
| 353 | * This function will return true if the `$Database` was |
||
| 354 | * initialised successfully. |
||
| 355 | */ |
||
| 356 | public static function initialiseDatabase() |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Accessor for the current `$Author` instance. |
||
| 408 | * |
||
| 409 | * @since Symphony 2.5.0 |
||
| 410 | * @return Author |
||
| 411 | */ |
||
| 412 | public static function Author() |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Attempts to log an Author in given a username and password. |
||
| 419 | * If the password is not hashed, it will be hashed using the sha1 |
||
| 420 | * algorithm. The username and password will be sanitized before |
||
| 421 | * being used to query the Database. If an Author is found, they |
||
| 422 | * will be logged in and the sanitized username and password (also hashed) |
||
| 423 | * will be saved as values in the `$Cookie`. |
||
| 424 | * |
||
| 425 | * @see toolkit.Cryptography#hash() |
||
| 426 | * @throws DatabaseException |
||
| 427 | * @param string $username |
||
| 428 | * The Author's username. This will be sanitized before use. |
||
| 429 | * @param string $password |
||
| 430 | * The Author's password. This will be sanitized and then hashed before use |
||
| 431 | * @param boolean $isHash |
||
| 432 | * If the password provided is already hashed, setting this parameter to |
||
| 433 | * true will stop it becoming rehashed. By default it is false. |
||
| 434 | * @return boolean |
||
| 435 | * True if the Author was logged in, false otherwise |
||
| 436 | */ |
||
| 437 | public static function login($username, $password, $isHash = false) |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Symphony allows Authors to login via the use of tokens instead of |
||
| 483 | * a username and password. A token is derived from concatenating the |
||
| 484 | * Author's username and password and applying the sha1 hash to |
||
| 485 | * it, from this, a portion of the hash is used as the token. This is a useful |
||
| 486 | * feature often used when setting up other Authors accounts or if an |
||
| 487 | * Author forgets their password. |
||
| 488 | * |
||
| 489 | * @param string $token |
||
| 490 | * The Author token, which is a portion of the hashed string concatenation |
||
| 491 | * of the Author's username and password |
||
| 492 | * @throws DatabaseException |
||
| 493 | * @return boolean |
||
| 494 | * True if the Author is logged in, false otherwise |
||
| 495 | */ |
||
| 496 | public static function loginFromToken($token) |
||
| 542 | |||
| 543 | /** |
||
| 544 | * This function will destroy the currently logged in `$Author` |
||
| 545 | * session, essentially logging them out. |
||
| 546 | * |
||
| 547 | * @see core.Cookie#expire() |
||
| 548 | */ |
||
| 549 | public static function logout() |
||
| 553 | |||
| 554 | /** |
||
| 555 | * This function determines whether an there is a currently logged in |
||
| 556 | * Author for Symphony by using the `$Cookie`'s username |
||
| 557 | * and password. If the instance is not found, they will be logged |
||
| 558 | * in using the cookied credentials. |
||
| 559 | * |
||
| 560 | * @see login() |
||
| 561 | * @return boolean |
||
| 562 | */ |
||
| 563 | public static function isLoggedIn() |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Returns the most recent version found in the `/install/migrations` folder. |
||
| 576 | * Returns a version string to be used in `version_compare()` if an updater |
||
| 577 | * has been found. Returns `FALSE` otherwise. |
||
| 578 | * |
||
| 579 | * @since Symphony 2.3.1 |
||
| 580 | * @return string|boolean |
||
| 581 | */ |
||
| 582 | public static function getMigrationVersion() |
||
| 593 | |||
| 594 | /** |
||
| 595 | * Checks if an update is available and applicable for the current installation. |
||
| 596 | * |
||
| 597 | * @since Symphony 2.3.1 |
||
| 598 | * @return boolean |
||
| 599 | */ |
||
| 600 | public static function isUpgradeAvailable() |
||
| 611 | |||
| 612 | /** |
||
| 613 | * Checks if the installer/upgrader is available. |
||
| 614 | * |
||
| 615 | * @since Symphony 2.3.1 |
||
| 616 | * @return boolean |
||
| 617 | */ |
||
| 618 | public static function isInstallerAvailable() |
||
| 622 | |||
| 623 | /** |
||
| 624 | * A wrapper for throwing a new Symphony Error page. |
||
| 625 | * |
||
| 626 | * @see core.SymphonyErrorPage |
||
| 627 | * @param string|XMLElement $message |
||
| 628 | * A description for this error, which can be provided as a string |
||
| 629 | * or as an XMLElement. |
||
| 630 | * @param string $heading |
||
| 631 | * A heading for the error page |
||
| 632 | * @param integer $status |
||
| 633 | * Properly sets the HTTP status code for the response. Defaults to |
||
| 634 | * `Page::HTTP_STATUS_ERROR`. Use `Page::HTTP_STATUS_XXX` to set this value. |
||
| 635 | * @param string $template |
||
| 636 | * A string for the error page template to use, defaults to 'generic'. This |
||
| 637 | * can be the name of any template file in the `TEMPLATES` directory. |
||
| 638 | * A template using the naming convention of `tpl.*.php`. |
||
| 639 | * @param array $additional |
||
| 640 | * Allows custom information to be passed to the Symphony Error Page |
||
| 641 | * that the template may want to expose, such as custom Headers etc. |
||
| 642 | * @throws SymphonyErrorPage |
||
| 643 | */ |
||
| 644 | public static function throwCustomError($message, $heading = 'Symphony Fatal Error', $status = Page::HTTP_STATUS_ERROR, $template = 'generic', array $additional = array()) |
||
| 648 | |||
| 649 | /** |
||
| 650 | * Setter accepts a previous Exception. Useful for determining the context |
||
| 651 | * of a current exception (ie. detecting recursion). |
||
| 652 | * |
||
| 653 | * @since Symphony 2.3.2 |
||
| 654 | * @param Exception $ex |
||
| 655 | */ |
||
| 656 | public static function setException(Exception $ex) |
||
| 660 | |||
| 661 | /** |
||
| 662 | * Accessor for `self::$exception`. |
||
| 663 | * |
||
| 664 | * @since Symphony 2.3.2 |
||
| 665 | * @return Exception|null |
||
| 666 | */ |
||
| 667 | public static function getException() |
||
| 671 | |||
| 672 | /** |
||
| 673 | * Returns the page namespace based on the current URL. |
||
| 674 | * A few examples: |
||
| 675 | * |
||
| 676 | * /login |
||
| 677 | * /publish |
||
| 678 | * /blueprints/datasources |
||
| 679 | * [...] |
||
| 680 | * /extension/$extension_name/$page_name |
||
| 681 | * |
||
| 682 | * This method is especially useful in couple with the translation function. |
||
| 683 | * |
||
| 684 | * @see toolkit#__() |
||
| 685 | * @return string |
||
| 686 | * The page namespace, without any action string (e.g. "new", "saved") or |
||
| 687 | * any value that depends upon the single setup (e.g. the section handle in |
||
| 688 | * /publish/$handle) |
||
| 689 | */ |
||
| 690 | public static function getPageNamespace() |
||
| 720 | } |
||
| 721 | |||
| 973 |
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: