Complex classes like TApplication 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 TApplication, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
93 | class TApplication extends \Prado\TComponent |
||
94 | { |
||
95 | /** |
||
96 | * Page service ID |
||
97 | */ |
||
98 | const PAGE_SERVICE_ID = 'page'; |
||
99 | /** |
||
100 | * Application configuration file name |
||
101 | */ |
||
102 | const CONFIG_FILE_XML = 'application.xml'; |
||
103 | /** |
||
104 | * File extension for external config files |
||
105 | */ |
||
106 | const CONFIG_FILE_EXT_XML = '.xml'; |
||
107 | /** |
||
108 | * Configuration file type, application.xml and config.xml |
||
109 | */ |
||
110 | const CONFIG_TYPE_XML = 'xml'; |
||
111 | /** |
||
112 | * Application configuration file name |
||
113 | */ |
||
114 | const CONFIG_FILE_PHP = 'application.php'; |
||
115 | /** |
||
116 | * File extension for external config files |
||
117 | */ |
||
118 | const CONFIG_FILE_EXT_PHP = '.php'; |
||
119 | /** |
||
120 | * Configuration file type, application.php and config.php |
||
121 | */ |
||
122 | const CONFIG_TYPE_PHP = 'php'; |
||
123 | /** |
||
124 | * Runtime directory name |
||
125 | */ |
||
126 | const RUNTIME_PATH = 'runtime'; |
||
127 | /** |
||
128 | * Config cache file |
||
129 | */ |
||
130 | const CONFIGCACHE_FILE = 'config.cache'; |
||
131 | /** |
||
132 | * Global data file |
||
133 | */ |
||
134 | const GLOBAL_FILE = 'global.cache'; |
||
135 | |||
136 | /** |
||
137 | * @var array list of events that define application lifecycles |
||
138 | */ |
||
139 | private static $_steps = [ |
||
140 | 'onBeginRequest', |
||
141 | 'onLoadState', |
||
142 | 'onLoadStateComplete', |
||
143 | 'onAuthentication', |
||
144 | 'onAuthenticationComplete', |
||
145 | 'onAuthorization', |
||
146 | 'onAuthorizationComplete', |
||
147 | 'onPreRunService', |
||
148 | 'runService', |
||
149 | 'onSaveState', |
||
150 | 'onSaveStateComplete', |
||
151 | 'onPreFlushOutput', |
||
152 | 'flushOutput' |
||
153 | ]; |
||
154 | |||
155 | /** |
||
156 | * @var string application ID |
||
157 | */ |
||
158 | private $_id; |
||
159 | /** |
||
160 | * @var string unique application ID |
||
161 | */ |
||
162 | private $_uniqueID; |
||
163 | /** |
||
164 | * @var bool whether the request is completed |
||
165 | */ |
||
166 | private $_requestCompleted = false; |
||
167 | /** |
||
168 | * @var int application state |
||
169 | */ |
||
170 | private $_step; |
||
171 | /** |
||
172 | * @var array available services and their configurations indexed by service IDs |
||
173 | */ |
||
174 | private $_services; |
||
175 | /** |
||
176 | * @var IService current service instance |
||
177 | */ |
||
178 | private $_service; |
||
179 | /** |
||
180 | * @var array list of loaded application modules |
||
181 | */ |
||
182 | private $_modules = []; |
||
183 | /** |
||
184 | * @var array list of application modules yet to be loaded |
||
185 | */ |
||
186 | private $_lazyModules = []; |
||
187 | /** |
||
188 | * @var \Prado\Collections\TMap list of application parameters |
||
189 | */ |
||
190 | private $_parameters; |
||
191 | /** |
||
192 | * @var string configuration file |
||
193 | */ |
||
194 | private $_configFile; |
||
195 | /** |
||
196 | * @var string configuration file extension |
||
197 | */ |
||
198 | private $_configFileExt; |
||
199 | /** |
||
200 | * @var string configuration type |
||
201 | */ |
||
202 | private $_configType; |
||
203 | /** |
||
204 | * @var string application base path |
||
205 | */ |
||
206 | private $_basePath; |
||
207 | /** |
||
208 | * @var string directory storing application state |
||
209 | */ |
||
210 | private $_runtimePath; |
||
211 | /** |
||
212 | * @var bool if any global state is changed during the current request |
||
213 | */ |
||
214 | private $_stateChanged = false; |
||
215 | /** |
||
216 | * @var array global variables (persistent across sessions, requests) |
||
217 | */ |
||
218 | private $_globals = []; |
||
219 | /** |
||
220 | * @var string cache file |
||
221 | */ |
||
222 | private $_cacheFile; |
||
223 | /** |
||
224 | * @var TErrorHandler error handler module |
||
225 | */ |
||
226 | private $_errorHandler; |
||
227 | /** |
||
228 | * @var THttpRequest request module |
||
229 | */ |
||
230 | private $_request; |
||
231 | /** |
||
232 | * @var THttpResponse response module |
||
233 | */ |
||
234 | private $_response; |
||
235 | /** |
||
236 | * @var THttpSession session module, could be null |
||
237 | */ |
||
238 | private $_session; |
||
239 | /** |
||
240 | * @var ICache cache module, could be null |
||
241 | */ |
||
242 | private $_cache; |
||
243 | /** |
||
244 | * @var IStatePersister application state persister |
||
245 | */ |
||
246 | private $_statePersister; |
||
247 | /** |
||
248 | * @var IUser user instance, could be null |
||
249 | */ |
||
250 | private $_user; |
||
251 | /** |
||
252 | * @var TGlobalization module, could be null |
||
253 | */ |
||
254 | private $_globalization; |
||
255 | /** |
||
256 | * @var TSecurityManager security manager module |
||
257 | */ |
||
258 | private $_security; |
||
259 | /** |
||
260 | * @var TAssetManager asset manager module |
||
261 | */ |
||
262 | private $_assetManager; |
||
263 | /** |
||
264 | * @var TAuthorizationRuleCollection collection of authorization rules |
||
265 | */ |
||
266 | private $_authRules; |
||
267 | /** |
||
268 | * @var TApplicationMode application mode |
||
269 | */ |
||
270 | private $_mode = TApplicationMode::Debug; |
||
271 | |||
272 | /** |
||
273 | * @var string Customizable page service ID |
||
274 | */ |
||
275 | private $_pageServiceID = self::PAGE_SERVICE_ID; |
||
276 | |||
277 | /** |
||
278 | * Constructor. |
||
279 | * Sets application base path and initializes the application singleton. |
||
280 | * Application base path refers to the root directory storing application |
||
281 | * data and code not directly accessible by Web users. |
||
282 | * By default, the base path is assumed to be the <b>protected</b> |
||
283 | * directory under the directory containing the current running script. |
||
284 | * @param string $basePath application base path or configuration file path. |
||
285 | * If the parameter is a file, it is assumed to be the application |
||
286 | * configuration file, and the directory containing the file is treated |
||
287 | * as the application base path. |
||
288 | * If it is a directory, it is assumed to be the application base path, |
||
289 | * and within that directory, a file named <b>application.xml</b> |
||
290 | * will be looked for. If found, the file is considered as the application |
||
291 | * configuration file. |
||
292 | * @param bool $cacheConfig whether to cache application configuration. Defaults to true. |
||
293 | * @param int $configType configuration type. Defaults to CONFIG_TYPE_XML. |
||
294 | * @throws TConfigurationException if configuration file cannot be read or the runtime path is invalid. |
||
295 | */ |
||
296 | 55 | public function __construct($basePath = 'protected', $cacheConfig = true, $configType = self::CONFIG_TYPE_XML) |
|
314 | |||
315 | /** |
||
316 | * Resolves application-relevant paths. |
||
317 | * This method is invoked by the application constructor |
||
318 | * to determine the application configuration file, |
||
319 | * application root path and the runtime path. |
||
320 | * @param string $basePath the application root path or the application configuration file |
||
321 | * @see setBasePath |
||
322 | * @see setRuntimePath |
||
323 | * @see setConfigurationFile |
||
324 | */ |
||
325 | 55 | protected function resolvePaths($basePath) |
|
359 | |||
360 | /** |
||
361 | * Executes the lifecycles of the application. |
||
362 | * This is the main entry function that leads to the running of the whole |
||
363 | * Prado application. |
||
364 | */ |
||
365 | public function run() |
||
389 | |||
390 | /** |
||
391 | * Completes current request processing. |
||
392 | * This method can be used to exit the application lifecycles after finishing |
||
393 | * the current cycle. |
||
394 | */ |
||
395 | public function completeRequest() |
||
399 | |||
400 | /** |
||
401 | * @return bool whether the current request is processed. |
||
402 | */ |
||
403 | public function getRequestCompleted() |
||
407 | |||
408 | /** |
||
409 | * Returns a global value. |
||
410 | * |
||
411 | * A global value is one that is persistent across users sessions and requests. |
||
412 | * @param string $key the name of the value to be returned |
||
413 | * @param mixed $defaultValue the default value. If $key is not found, $defaultValue will be returned |
||
414 | * @return mixed the global value corresponding to $key |
||
415 | */ |
||
416 | 3 | public function getGlobalState($key, $defaultValue = null) |
|
420 | |||
421 | /** |
||
422 | * Sets a global value. |
||
423 | * |
||
424 | * A global value is one that is persistent across users sessions and requests. |
||
425 | * Make sure that the value is serializable and unserializable. |
||
426 | * @param string $key the name of the value to be set |
||
427 | * @param mixed $value the global value to be set |
||
428 | * @param null|mixed $defaultValue the default value. If $key is not found, $defaultValue will be returned |
||
429 | * @param bool $forceSave wheter to force an immediate GlobalState save. defaults to false |
||
430 | */ |
||
431 | 3 | public function setGlobalState($key, $value, $defaultValue = null, $forceSave = false) |
|
443 | |||
444 | /** |
||
445 | * Clears a global value. |
||
446 | * |
||
447 | * The value cleared will no longer be available in this request and the following requests. |
||
448 | * @param string $key the name of the value to be cleared |
||
449 | */ |
||
450 | public function clearGlobalState($key) |
||
455 | |||
456 | /** |
||
457 | * Loads global values from persistent storage. |
||
458 | * This method is invoked when {@link onLoadState OnLoadState} event is raised. |
||
459 | * After this method, values that are stored in previous requests become |
||
460 | * available to the current request via {@link getGlobalState}. |
||
461 | */ |
||
462 | protected function loadGlobals() |
||
466 | |||
467 | /** |
||
468 | * Saves global values into persistent storage. |
||
469 | * This method is invoked when {@link onSaveState OnSaveState} event is raised. |
||
470 | */ |
||
471 | 3 | protected function saveGlobals() |
|
478 | |||
479 | /** |
||
480 | * @return string application ID |
||
481 | */ |
||
482 | 3 | public function getID() |
|
486 | |||
487 | /** |
||
488 | * @param string $value application ID |
||
489 | */ |
||
490 | public function setID($value) |
||
494 | |||
495 | /** |
||
496 | * @return string page service ID |
||
497 | */ |
||
498 | 55 | public function getPageServiceID() |
|
502 | |||
503 | /** |
||
504 | * @param string $value page service ID |
||
505 | */ |
||
506 | public function setPageServiceID($value) |
||
510 | |||
511 | /** |
||
512 | * @return string an ID that uniquely identifies this Prado application from the others |
||
513 | */ |
||
514 | 7 | public function getUniqueID() |
|
518 | |||
519 | /** |
||
520 | * @return TApplicationMode application mode. Defaults to TApplicationMode::Debug. |
||
521 | */ |
||
522 | 32 | public function getMode() |
|
526 | |||
527 | /** |
||
528 | * @param TApplicationMode $value application mode |
||
529 | */ |
||
530 | public function setMode($value) |
||
534 | |||
535 | /** |
||
536 | * @return string the directory containing the application configuration file (absolute path) |
||
537 | */ |
||
538 | public function getBasePath() |
||
542 | |||
543 | /** |
||
544 | * @param string $value the directory containing the application configuration file |
||
545 | */ |
||
546 | 55 | public function setBasePath($value) |
|
550 | |||
551 | /** |
||
552 | * @return string the application configuration file (absolute path) |
||
553 | */ |
||
554 | public function getConfigurationFile() |
||
558 | |||
559 | /** |
||
560 | * @param string $value the application configuration file (absolute path) |
||
561 | */ |
||
562 | public function setConfigurationFile($value) |
||
566 | |||
567 | /** |
||
568 | * @return string the application configuration file (absolute path) |
||
569 | */ |
||
570 | 9 | public function getConfigurationType() |
|
574 | |||
575 | /** |
||
576 | * @param string $value the application configuration type. 'xml' and 'php' are valid values |
||
577 | */ |
||
578 | 55 | public function setConfigurationType($value) |
|
582 | |||
583 | /** |
||
584 | * @return string the application configuration type. default is 'xml' |
||
585 | */ |
||
586 | public function getConfigurationFileExt() |
||
599 | |||
600 | /** |
||
601 | * @return string the default configuration file name |
||
602 | */ |
||
603 | 55 | public function getConfigurationFileName() |
|
617 | |||
618 | /** |
||
619 | * @return string the directory storing cache data and application-level persistent data. (absolute path) |
||
620 | */ |
||
621 | 3 | public function getRuntimePath() |
|
625 | |||
626 | /** |
||
627 | * @param string $value the directory storing cache data and application-level persistent data. (absolute path) |
||
628 | */ |
||
629 | 55 | public function setRuntimePath($value) |
|
638 | |||
639 | /** |
||
640 | * @return IService the currently requested service |
||
641 | */ |
||
642 | public function getService() |
||
646 | |||
647 | /** |
||
648 | * @param IService $value the currently requested service |
||
649 | */ |
||
650 | public function setService($value) |
||
654 | |||
655 | /** |
||
656 | * Adds a module to application. |
||
657 | * Note, this method does not do module initialization. |
||
658 | * @param string $id ID of the module |
||
659 | * @param null|IModule $module module object or null if the module has not been loaded yet |
||
660 | */ |
||
661 | 14 | public function setModule($id, IModule $module = null) |
|
669 | |||
670 | /** |
||
671 | * @param mixed $id |
||
672 | * @return IModule the module with the specified ID, null if not found |
||
673 | */ |
||
674 | 15 | public function getModule($id) |
|
688 | |||
689 | /** |
||
690 | * Returns a list of application modules indexed by module IDs. |
||
691 | * Modules that have not been loaded yet are returned as null objects. |
||
692 | * @return array list of loaded application modules, indexed by module IDs |
||
693 | */ |
||
694 | public function getModules() |
||
698 | |||
699 | /** |
||
700 | * Returns the list of application parameters. |
||
701 | * Since the parameters are returned as a {@link \Prado\Collections\TMap} object, you may use |
||
702 | * the returned result to access, add or remove individual parameters. |
||
703 | * @return \Prado\Collections\TMap the list of application parameters |
||
704 | */ |
||
705 | public function getParameters() |
||
709 | |||
710 | /** |
||
711 | * @return THttpRequest the request module |
||
712 | */ |
||
713 | 18 | public function getRequest() |
|
721 | |||
722 | /** |
||
723 | * @param THttpRequest $request the request module |
||
724 | */ |
||
725 | 42 | public function setRequest(THttpRequest $request) |
|
729 | |||
730 | /** |
||
731 | * @return THttpResponse the response module |
||
732 | */ |
||
733 | 1 | public function getResponse() |
|
741 | |||
742 | /** |
||
743 | * @param THttpRequest $response the request module |
||
744 | */ |
||
745 | 8 | public function setResponse(THttpResponse $response) |
|
749 | |||
750 | /** |
||
751 | * @return THttpSession the session module, null if session module is not installed |
||
752 | */ |
||
753 | public function getSession() |
||
761 | |||
762 | /** |
||
763 | * @param THttpSession $session the session module |
||
764 | */ |
||
765 | 10 | public function setSession(THttpSession $session) |
|
766 | { |
||
767 | 10 | $this->_session = $session; |
|
768 | 10 | } |
|
769 | |||
770 | /** |
||
771 | * @return TErrorHandler the error handler module |
||
772 | */ |
||
773 | public function getErrorHandler() |
||
781 | |||
782 | /** |
||
783 | * @param TErrorHandler $handler the error handler module |
||
784 | */ |
||
785 | public function setErrorHandler(TErrorHandler $handler) |
||
789 | |||
790 | /** |
||
791 | * @return TSecurityManager the security manager module |
||
792 | */ |
||
793 | 2 | public function getSecurityManager() |
|
801 | |||
802 | /** |
||
803 | * @param TSecurityManager $sm the security manager module |
||
804 | */ |
||
805 | 9 | public function setSecurityManager(TSecurityManager $sm) |
|
809 | |||
810 | /** |
||
811 | * @return TAssetManager asset manager |
||
812 | */ |
||
813 | 1 | public function getAssetManager() |
|
821 | |||
822 | /** |
||
823 | * @param TAssetManager $value asset manager |
||
824 | */ |
||
825 | 6 | public function setAssetManager(TAssetManager $value) |
|
829 | |||
830 | /** |
||
831 | * @return IStatePersister application state persister |
||
832 | */ |
||
833 | 3 | public function getApplicationStatePersister() |
|
841 | |||
842 | /** |
||
843 | * @param IStatePersister $persister application state persister |
||
844 | */ |
||
845 | 2 | public function setApplicationStatePersister(IStatePersister $persister) |
|
849 | |||
850 | /** |
||
851 | * @return ICache the cache module, null if cache module is not installed |
||
852 | */ |
||
853 | 20 | public function getCache() |
|
857 | |||
858 | /** |
||
859 | * @param \Prado\Caching\ICache $cache the cache module |
||
860 | */ |
||
861 | 17 | public function setCache(\Prado\Caching\ICache $cache) |
|
865 | |||
866 | /** |
||
867 | * @return IUser the application user |
||
868 | */ |
||
869 | public function getUser() |
||
873 | |||
874 | /** |
||
875 | * @param \Prado\Security\IUser $user the application user |
||
876 | */ |
||
877 | public function setUser(\Prado\Security\IUser $user) |
||
881 | |||
882 | /** |
||
883 | * @param bool $createIfNotExists whether to create globalization if it does not exist |
||
884 | * @return TGlobalization globalization module |
||
885 | */ |
||
886 | public function getGlobalization($createIfNotExists = true) |
||
894 | |||
895 | /** |
||
896 | * @param \Prado\I18N\TGlobalization $glob globalization module |
||
897 | */ |
||
898 | public function setGlobalization(\Prado\I18N\TGlobalization $glob) |
||
902 | |||
903 | /** |
||
904 | * @return TAuthorizationRuleCollection list of authorization rules for the current request |
||
905 | */ |
||
906 | public function getAuthorizationRules() |
||
913 | |||
914 | protected function getApplicationConfigurationClass() |
||
918 | |||
919 | protected function internalLoadModule($id, $force = false) |
||
942 | /** |
||
943 | * Applies an application configuration. |
||
944 | * @param TApplicationConfiguration $config the configuration |
||
945 | * @param bool $withinService whether the configuration is specified within a service. |
||
946 | */ |
||
947 | public function applyConfiguration($config, $withinService = false) |
||
1021 | |||
1022 | /** |
||
1023 | * Loads configuration and initializes application. |
||
1024 | * Configuration file will be read and parsed (if a valid cached version exists, |
||
1025 | * it will be used instead). Then, modules are created and initialized; |
||
1026 | * Afterwards, the requested service is created and initialized. |
||
1027 | * @throws TConfigurationException if module is redefined of invalid type, or service not defined or of invalid type |
||
1028 | */ |
||
1029 | protected function initApplication() |
||
1053 | |||
1054 | /** |
||
1055 | * Starts the specified service. |
||
1056 | * The service instance will be created. Its properties will be initialized |
||
1057 | * and the configurations will be applied, if any. |
||
1058 | * @param string $serviceID service ID |
||
1059 | */ |
||
1060 | public function startService($serviceID) |
||
1093 | |||
1094 | /** |
||
1095 | * Raises OnError event. |
||
1096 | * This method is invoked when an exception is raised during the lifecycles |
||
1097 | * of the application. |
||
1098 | * @param mixed $param event parameter |
||
1099 | */ |
||
1100 | public function onError($param) |
||
1106 | |||
1107 | /** |
||
1108 | * Raises OnBeginRequest event. |
||
1109 | * At the time when this method is invoked, application modules are loaded |
||
1110 | * and initialized, user request is resolved and the corresponding service |
||
1111 | * is loaded and initialized. The application is about to start processing |
||
1112 | * the user request. |
||
1113 | */ |
||
1114 | public function onBeginRequest() |
||
1118 | |||
1119 | /** |
||
1120 | * Raises OnAuthentication event. |
||
1121 | * This method is invoked when the user request needs to be authenticated. |
||
1122 | */ |
||
1123 | public function onAuthentication() |
||
1127 | |||
1128 | /** |
||
1129 | * Raises OnAuthenticationComplete event. |
||
1130 | * This method is invoked right after the user request is authenticated. |
||
1131 | */ |
||
1132 | public function onAuthenticationComplete() |
||
1136 | |||
1137 | /** |
||
1138 | * Raises OnAuthorization event. |
||
1139 | * This method is invoked when the user request needs to be authorized. |
||
1140 | */ |
||
1141 | public function onAuthorization() |
||
1145 | |||
1146 | /** |
||
1147 | * Raises OnAuthorizationComplete event. |
||
1148 | * This method is invoked right after the user request is authorized. |
||
1149 | */ |
||
1150 | public function onAuthorizationComplete() |
||
1154 | |||
1155 | /** |
||
1156 | * Raises OnLoadState event. |
||
1157 | * This method is invoked when the application needs to load state (probably stored in session). |
||
1158 | */ |
||
1159 | public function onLoadState() |
||
1164 | |||
1165 | /** |
||
1166 | * Raises OnLoadStateComplete event. |
||
1167 | * This method is invoked right after the application state has been loaded. |
||
1168 | */ |
||
1169 | public function onLoadStateComplete() |
||
1173 | |||
1174 | /** |
||
1175 | * Raises OnPreRunService event. |
||
1176 | * This method is invoked right before the service is to be run. |
||
1177 | */ |
||
1178 | public function onPreRunService() |
||
1182 | |||
1183 | /** |
||
1184 | * Runs the requested service. |
||
1185 | */ |
||
1186 | public function runService() |
||
1192 | |||
1193 | /** |
||
1194 | * Raises OnSaveState event. |
||
1195 | * This method is invoked when the application needs to save state (probably stored in session). |
||
1196 | */ |
||
1197 | public function onSaveState() |
||
1202 | |||
1203 | /** |
||
1204 | * Raises OnSaveStateComplete event. |
||
1205 | * This method is invoked right after the application state has been saved. |
||
1206 | */ |
||
1207 | public function onSaveStateComplete() |
||
1211 | |||
1212 | /** |
||
1213 | * Raises OnPreFlushOutput event. |
||
1214 | * This method is invoked right before the application flushes output to client. |
||
1215 | */ |
||
1216 | public function onPreFlushOutput() |
||
1220 | |||
1221 | /** |
||
1222 | * Flushes output to client side. |
||
1223 | * @param bool $continueBuffering whether to continue buffering after flush if buffering was active |
||
1224 | */ |
||
1225 | public function flushOutput($continueBuffering = true) |
||
1229 | |||
1230 | /** |
||
1231 | * Raises OnEndRequest event. |
||
1232 | * This method is invoked when the application completes the processing of the request. |
||
1233 | */ |
||
1234 | public function onEndRequest() |
||
1240 | } |
||
1241 |
If you suppress an error, we recommend checking for the error condition explicitly: