| Total Complexity | 82 |
| Total Lines | 496 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Framework 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.
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 Framework, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 153 | class Framework extends Kernel |
||
| 154 | { |
||
| 155 | /** |
||
| 156 | * Framework::$config |
||
| 157 | * |
||
| 158 | * Framework Container Config |
||
| 159 | * |
||
| 160 | * @var Framework\Containers\Config |
||
| 161 | */ |
||
| 162 | public $config; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Framework::$globals |
||
| 166 | * |
||
| 167 | * Framework Container Globals |
||
| 168 | * |
||
| 169 | * @var Framework\Containers\Globals |
||
| 170 | */ |
||
| 171 | public $globals; |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Framework::$environment |
||
| 175 | * |
||
| 176 | * Framework Container Environment |
||
| 177 | * |
||
| 178 | * @var Framework\Containers\Environment |
||
| 179 | */ |
||
| 180 | public $environment; |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Framework::$models |
||
| 184 | * |
||
| 185 | * Framework Container Models |
||
| 186 | * |
||
| 187 | * @var Framework\Containers\Models |
||
| 188 | */ |
||
| 189 | public $models; |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Framework::$modules |
||
| 193 | * |
||
| 194 | * Framework Container Modules |
||
| 195 | * |
||
| 196 | * @var Framework\Containers\Modules |
||
| 197 | */ |
||
| 198 | public $modules; |
||
| 199 | |||
| 200 | // ------------------------------------------------------------------------ |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Framework::__construct |
||
| 204 | */ |
||
| 205 | protected function __construct() |
||
| 206 | { |
||
| 207 | parent::__construct(); |
||
| 208 | |||
| 209 | if (profiler() !== false) { |
||
| 210 | profiler()->watch('Starting O2System Framework'); |
||
| 211 | } |
||
| 212 | |||
| 213 | // Add App Views Folder |
||
| 214 | output()->addFilePath(PATH_APP); |
||
| 215 | |||
| 216 | if (profiler() !== false) { |
||
| 217 | profiler()->watch('Starting Framework Services'); |
||
| 218 | } |
||
| 219 | |||
| 220 | $services = [ |
||
| 221 | 'Services\Hooks' => 'hooks', |
||
| 222 | 'Services\Shutdown' => 'shutdown', |
||
| 223 | 'Services\Logger' => 'logger', |
||
| 224 | 'Services\Loader' => 'loader', |
||
| 225 | ]; |
||
| 226 | |||
| 227 | foreach ($services as $className => $classOffset) { |
||
| 228 | $this->services->load($className, $classOffset); |
||
| 229 | } |
||
| 230 | |||
| 231 | // Instantiate Config Container |
||
| 232 | if (profiler() !== false) { |
||
| 233 | profiler()->watch('Starting Config Container'); |
||
| 234 | } |
||
| 235 | |||
| 236 | $this->config = new Framework\Containers\Config(); |
||
| 237 | |||
| 238 | // Instantiate Globals Container |
||
| 239 | if (profiler() !== false) { |
||
| 240 | profiler()->watch('Starting Globals Container'); |
||
| 241 | } |
||
| 242 | $this->globals = new Framework\Containers\Globals(); |
||
| 243 | |||
| 244 | // Instantiate Environment Container |
||
| 245 | if (profiler() !== false) { |
||
| 246 | profiler()->watch('Starting Environment Container'); |
||
| 247 | } |
||
| 248 | $this->environment = new Framework\Containers\Environment(); |
||
| 249 | |||
| 250 | // Instantiate Models Container |
||
| 251 | if (profiler() !== false) { |
||
| 252 | profiler()->watch('Starting Models Container'); |
||
| 253 | } |
||
| 254 | |||
| 255 | $this->models = new Framework\Containers\Models(); |
||
| 256 | |||
| 257 | // Instantiate Modules Container |
||
| 258 | if (profiler() !== false) { |
||
| 259 | profiler()->watch('Starting Modules Container'); |
||
| 260 | } |
||
| 261 | $this->modules = new Framework\Containers\Modules(); |
||
| 262 | |||
| 263 | if (config()->loadFile('cache') === true) { |
||
| 264 | // Instantiate Cache Service |
||
| 265 | if (profiler() !== false) { |
||
| 266 | profiler()->watch('Starting Cache Service'); |
||
| 267 | } |
||
| 268 | |||
| 269 | $this->services->add(new Framework\Services\Cache(config('cache', true)), 'cache'); |
||
| 270 | |||
| 271 | // Language Service Load Registry |
||
| 272 | if (profiler() !== false) { |
||
| 273 | profiler()->watch('Loading Language Registry'); |
||
| 274 | } |
||
| 275 | |||
| 276 | language()->loadRegistry(); |
||
| 277 | |||
| 278 | // Modules Service Load Registry |
||
| 279 | if (profiler() !== false) { |
||
| 280 | profiler()->watch('Loading Modules Registry'); |
||
| 281 | } |
||
| 282 | $this->modules->loadRegistry(); |
||
| 283 | } |
||
| 284 | |||
| 285 | if (profiler() !== false) { |
||
| 286 | profiler()->watch('Starting O2System Framework Hooks Pre System'); |
||
| 287 | } |
||
| 288 | hooks()->callEvent(Framework\Services\Hooks::PRE_SYSTEM); |
||
| 289 | } |
||
| 290 | |||
| 291 | // ------------------------------------------------------------------------ |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Framework::__reconstruct |
||
| 295 | */ |
||
| 296 | protected function __reconstruct() |
||
| 297 | { |
||
| 298 | // Modules default app |
||
| 299 | if (null !== ($defaultApp = config('app'))) { |
||
| 300 | if (false !== ($defaultModule = modules()->getApp($defaultApp))) { |
||
| 301 | // Register Domain App Module Namespace |
||
| 302 | loader()->addNamespace($defaultModule->getNamespace(), $defaultModule->getRealPath()); |
||
| 303 | |||
| 304 | // Push Domain App Module |
||
| 305 | modules()->push($defaultModule); |
||
| 306 | } elseif (false !== ($defaultModule = modules()->getModule($defaultApp))) { |
||
| 307 | // Register Path Module Namespace |
||
| 308 | loader()->addNamespace($defaultModule->getNamespace(), $defaultModule->getRealPath()); |
||
| 309 | |||
| 310 | // Push Path Module |
||
| 311 | modules()->push($defaultModule); |
||
| 312 | } |
||
| 313 | } |
||
| 314 | |||
| 315 | if (profiler() !== false) { |
||
| 316 | profiler()->watch('Calling Hooks Service: Post System'); |
||
| 317 | } |
||
| 318 | hooks()->callEvent(Framework\Services\Hooks::POST_SYSTEM); |
||
| 319 | |||
| 320 | if (is_cli()) { |
||
| 321 | $this->cliHandler(); |
||
| 322 | } else { |
||
| 323 | $this->httpHandler(); |
||
| 324 | } |
||
| 325 | } |
||
| 326 | |||
| 327 | // ------------------------------------------------------------------------ |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Framework::cliHandler |
||
| 331 | * |
||
| 332 | * @return void |
||
| 333 | * @throws \ReflectionException |
||
| 334 | */ |
||
| 335 | private function cliHandler() |
||
| 336 | { |
||
| 337 | // Instantiate CLI Router Service |
||
| 338 | $this->services->load(Kernel\Cli\Router::class); |
||
| 339 | |||
| 340 | if (profiler() !== false) { |
||
| 341 | profiler()->watch('Parse Router Request'); |
||
| 342 | } |
||
| 343 | router()->parseRequest(); |
||
| 344 | |||
| 345 | if ($commander = router()->getCommander()) { |
||
| 346 | if ($commander instanceof Kernel\Cli\Router\DataStructures\Commander) { |
||
| 347 | // Autoload Language |
||
| 348 | language()->loadFile($commander->getParameter()); |
||
| 349 | language()->loadFile($commander->getRequestMethod()); |
||
| 350 | language()->loadFile($commander->getParameter() . '/' . $commander->getRequestMethod()); |
||
| 351 | |||
| 352 | // Autoload Model |
||
| 353 | foreach ($this->modules as $module) { |
||
| 354 | if (in_array($module->getType(), ['KERNEL', 'FRAMEWORK'])) { |
||
| 355 | continue; |
||
| 356 | } |
||
| 357 | $module->loadModel(); |
||
| 358 | } |
||
| 359 | |||
| 360 | // Autoload Model |
||
| 361 | $modelClassName = str_replace('Commanders', 'Models', $commander->getName()); |
||
| 362 | |||
| 363 | if (class_exists($modelClassName)) { |
||
| 364 | $this->models->load($modelClassName, 'commander'); |
||
| 365 | } |
||
| 366 | |||
| 367 | // Initialize Controller |
||
| 368 | if (profiler() !== false) { |
||
| 369 | profiler()->watch('Calling Hooks Service: Pre Commander'); |
||
| 370 | } |
||
| 371 | hooks()->callEvent(Framework\Services\Hooks::PRE_COMMANDER); |
||
| 372 | |||
| 373 | if (profiler() !== false) { |
||
| 374 | profiler()->watch('Instantiating Requested Commander: ' . $commander->getClass()); |
||
| 375 | } |
||
| 376 | $requestCommander = $commander->getInstance(); |
||
| 377 | |||
| 378 | if (profiler() !== false) { |
||
| 379 | profiler()->watch('Calling Hooks Service: Post Commander'); |
||
| 380 | } |
||
| 381 | hooks()->callEvent(Framework\Services\Hooks::POST_COMMANDER); |
||
| 382 | |||
| 383 | if (profiler() !== false) { |
||
| 384 | profiler()->watch('Execute Requested Commander: ' . $commander->getClass()); |
||
| 385 | } |
||
| 386 | $requestCommander->execute(); |
||
| 387 | |||
| 388 | exit(EXIT_SUCCESS); |
||
| 389 | } |
||
| 390 | } |
||
| 391 | } |
||
| 392 | |||
| 393 | // ------------------------------------------------------------------------ |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Framework::httpHandler |
||
| 397 | * |
||
| 398 | * @return void |
||
| 399 | * @throws \ReflectionException |
||
| 400 | */ |
||
| 401 | private function httpHandler() |
||
| 649 | } |
||
| 650 | } |
||
| 651 | } |
||
| 652 |