| Total Complexity | 46 |
| Total Lines | 556 |
| Duplicated Lines | 0 % |
| Changes | 25 | ||
| Bugs | 1 | Features | 0 |
Complex classes like Jaxon 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 Jaxon, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 64 | class Jaxon |
||
| 65 | { |
||
| 66 | /** |
||
| 67 | * Package version number |
||
| 68 | * |
||
| 69 | * @const string |
||
| 70 | */ |
||
| 71 | const VERSION = 'Jaxon 4.0.0-dev'; |
||
| 72 | |||
| 73 | /* |
||
| 74 | * Request plugins |
||
| 75 | */ |
||
| 76 | const CALLABLE_CLASS = 'CallableClass'; |
||
| 77 | const CALLABLE_DIR = 'CallableDir'; |
||
| 78 | const CALLABLE_FUNCTION = 'CallableFunction'; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * A static instance on this class |
||
| 82 | * |
||
| 83 | * @var Jaxon |
||
| 84 | */ |
||
| 85 | private static $xInstance = null; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * The DI container |
||
| 89 | * |
||
| 90 | * @var Container |
||
| 91 | */ |
||
| 92 | private static $xContainer = null; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var Bootstrap |
||
| 96 | */ |
||
| 97 | protected $xBootstrap; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var Translator |
||
| 101 | */ |
||
| 102 | protected $xTranslator; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var ConfigManager |
||
| 106 | */ |
||
| 107 | protected $xConfigManager; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var PluginManager |
||
| 111 | */ |
||
| 112 | protected $xPluginManager; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var PackageManager |
||
| 116 | */ |
||
| 117 | protected $xPackageManager; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @var CodeGenerator |
||
| 121 | */ |
||
| 122 | protected $xCodeGenerator; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @var CallableRegistry |
||
| 126 | */ |
||
| 127 | protected $xClassRegistry; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @var CallbackManager |
||
| 131 | */ |
||
| 132 | protected $xCallbackManager; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @var ResponseManager |
||
| 136 | */ |
||
| 137 | protected $xResponseManager; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @return void |
||
| 141 | */ |
||
| 142 | private static function initInstance() |
||
| 143 | { |
||
| 144 | // Set the attributes from the container |
||
| 145 | self::$xInstance->xBootstrap = self::$xContainer->g(Bootstrap::class); |
||
| 146 | self::$xInstance->xTranslator = self::$xContainer->g(Translator::class); |
||
| 147 | self::$xInstance->xConfigManager = self::$xContainer->g(ConfigManager::class); |
||
| 148 | self::$xInstance->xPluginManager = self::$xContainer->g(PluginManager::class); |
||
| 149 | self::$xInstance->xPackageManager = self::$xContainer->g(PackageManager::class); |
||
| 150 | self::$xInstance->xCodeGenerator = self::$xContainer->g(CodeGenerator::class); |
||
| 151 | self::$xInstance->xClassRegistry = self::$xContainer->g(CallableRegistry::class); |
||
| 152 | self::$xInstance->xCallbackManager = self::$xContainer->g(CallbackManager::class); |
||
| 153 | self::$xInstance->xResponseManager = self::$xContainer->g(ResponseManager::class); |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Get the static instance |
||
| 158 | * |
||
| 159 | * @return Jaxon |
||
| 160 | */ |
||
| 161 | public static function getInstance(): ?Jaxon |
||
| 162 | { |
||
| 163 | if(self::$xInstance === null) |
||
| 164 | { |
||
| 165 | self::$xInstance = new Jaxon(); |
||
| 166 | self::$xContainer = new Container(self::$xInstance); |
||
| 167 | self::initInstance(); |
||
| 168 | } |
||
| 169 | // Call the on boot callbacks on each call to the jaxon() function. |
||
| 170 | self::$xInstance->xBootstrap->onBoot(); |
||
| 171 | return self::$xInstance; |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * The constructor |
||
| 176 | */ |
||
| 177 | private function __construct() |
||
| 178 | {} |
||
| 179 | |||
| 180 | /** |
||
| 181 | * The current Jaxon version |
||
| 182 | * |
||
| 183 | * @return string |
||
| 184 | */ |
||
| 185 | public function getVersion(): string |
||
| 186 | { |
||
| 187 | return self::VERSION; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Get the DI container |
||
| 192 | * |
||
| 193 | * @return Container |
||
| 194 | */ |
||
| 195 | public function di(): ?Container |
||
| 196 | { |
||
| 197 | return self::$xContainer; |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Get the logger |
||
| 202 | * |
||
| 203 | * @return LoggerInterface |
||
| 204 | */ |
||
| 205 | public function logger(): LoggerInterface |
||
| 206 | { |
||
| 207 | return $this->di()->logger(); |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Set the value of a config option |
||
| 212 | * |
||
| 213 | * @param string $sName The option name |
||
| 214 | * @param mixed $sValue The option value |
||
| 215 | * |
||
| 216 | * @return void |
||
| 217 | */ |
||
| 218 | public function setOption(string $sName, $sValue) |
||
| 219 | { |
||
| 220 | $this->xConfigManager->setOption($sName, $sValue); |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Get the value of a config option |
||
| 225 | * |
||
| 226 | * @param string $sName The option name |
||
| 227 | * @param mixed|null $xDefault The default value, to be returned if the option is not defined |
||
| 228 | * |
||
| 229 | * @return mixed The option value, or null if the option is unknown |
||
| 230 | */ |
||
| 231 | public function getOption(string $sName, $xDefault = null) |
||
| 232 | { |
||
| 233 | return $this->xConfigManager->getOption($sName, $xDefault); |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Check the presence of a config option |
||
| 238 | * |
||
| 239 | * @param string $sName The option name |
||
| 240 | * |
||
| 241 | * @return bool True if the option exists, and false if not |
||
| 242 | */ |
||
| 243 | public function hasOption(string $sName): bool |
||
| 244 | { |
||
| 245 | return $this->xConfigManager->hasOption($sName); |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Read the options from the file, if provided, and return the config |
||
| 250 | * |
||
| 251 | * @param string $sConfigFile The full path to the config file |
||
| 252 | * @param string $sConfigSection The section of the config file to be loaded |
||
| 253 | * |
||
| 254 | * @return ConfigManager |
||
| 255 | * @throws SetupException |
||
| 256 | */ |
||
| 257 | public function config(string $sConfigFile = '', string $sConfigSection = ''): ConfigManager |
||
| 258 | { |
||
| 259 | if(!empty(($sConfigFile = trim($sConfigFile)))) |
||
| 260 | { |
||
| 261 | $this->xConfigManager->load($sConfigFile, trim($sConfigSection)); |
||
| 262 | } |
||
| 263 | return $this->xConfigManager; |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Get a translated string |
||
| 268 | * |
||
| 269 | * @param string $sText The key of the translated string |
||
| 270 | * @param array $aPlaceHolders The placeholders of the translated string |
||
| 271 | * @param string $sLanguage The language of the translated string |
||
| 272 | * |
||
| 273 | * @return string |
||
| 274 | */ |
||
| 275 | public function trans(string $sText, array $aPlaceHolders = [], string $sLanguage = ''): string |
||
| 276 | { |
||
| 277 | return $this->xTranslator->trans($sText, $aPlaceHolders, $sLanguage); |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Get the Response object |
||
| 282 | * |
||
| 283 | * @return ResponseInterface |
||
| 284 | */ |
||
| 285 | public function getResponse(): ResponseInterface |
||
| 286 | { |
||
| 287 | if(($xResponse = $this->xResponseManager->getResponse())) |
||
| 288 | { |
||
| 289 | return $xResponse; |
||
| 290 | } |
||
| 291 | return $this->di()->getResponse(); |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Create a new Jaxon response object |
||
| 296 | * |
||
| 297 | * @return Response |
||
| 298 | */ |
||
| 299 | public function newResponse(): Response |
||
| 300 | { |
||
| 301 | return $this->di()->newResponse(); |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Register a plugin |
||
| 306 | * |
||
| 307 | * Below is a table for priorities and their description: |
||
| 308 | * - 0 to 999: Plugins that are part of or extensions to the jaxon core |
||
| 309 | * - 1000 to 8999: User created plugins, typically, these plugins don't care about order |
||
| 310 | * - 9000 to 9999: Plugins that generally need to be last or near the end of the plugin list |
||
| 311 | * |
||
| 312 | * @param string $sClassName The plugin class |
||
| 313 | * @param string $sPluginName The plugin name |
||
| 314 | * @param integer $nPriority The plugin priority, used to order the plugins |
||
| 315 | * |
||
| 316 | * @return void |
||
| 317 | * @throws SetupException |
||
| 318 | */ |
||
| 319 | public function registerPlugin(string $sClassName, string $sPluginName, int $nPriority = 1000) |
||
| 320 | { |
||
| 321 | $this->xPluginManager->registerPlugin($sClassName, $sPluginName, $nPriority); |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Register a package |
||
| 326 | * |
||
| 327 | * @param string $sClassName The package class |
||
| 328 | * @param array $xPkgOptions The user provided package options |
||
| 329 | * |
||
| 330 | * @return void |
||
| 331 | * @throws SetupException |
||
| 332 | */ |
||
| 333 | public function registerPackage(string $sClassName, array $xPkgOptions = []) |
||
| 334 | { |
||
| 335 | $this->xPackageManager->registerPackage($sClassName, $xPkgOptions); |
||
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Register request handlers, including functions, callable classes and directories. |
||
| 340 | * |
||
| 341 | * @param string $sType The type of request handler being registered |
||
| 342 | * Options include: |
||
| 343 | * - Jaxon::CALLABLE_FUNCTION: a function declared at global scope |
||
| 344 | * - Jaxon::CALLABLE_CLASS: a class who's methods are to be registered |
||
| 345 | * - Jaxon::CALLABLE_DIR: a directory containing classes to be registered |
||
| 346 | * @param string $sName |
||
| 347 | * When registering a function, this is the name of the function |
||
| 348 | * When registering a callable class, this is the class name |
||
| 349 | * When registering a callable directory, this is the full path to the directory |
||
| 350 | * @param array|string $xOptions The related options |
||
| 351 | * |
||
| 352 | * @return void |
||
| 353 | * @throws SetupException |
||
| 354 | */ |
||
| 355 | public function register(string $sType, string $sName, $xOptions = []) |
||
| 356 | { |
||
| 357 | $this->xPluginManager->registerCallable($sType, $sName, $xOptions); |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Get an instance of a registered class |
||
| 362 | * |
||
| 363 | * @param string $sClassName The class name |
||
| 364 | * |
||
| 365 | * @return null|object |
||
| 366 | * @throws SetupException |
||
| 367 | */ |
||
| 368 | public function instance(string $sClassName) |
||
| 369 | { |
||
| 370 | $xCallable = $this->xClassRegistry->getCallableObject($sClassName); |
||
| 371 | return ($xCallable) ? $xCallable->getRegisteredObject() : null; |
||
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Get the factory |
||
| 376 | * |
||
| 377 | * @return Factory |
||
| 378 | */ |
||
| 379 | public function factory(): Factory |
||
| 380 | { |
||
| 381 | return $this->di()->g(Factory::class); |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Get a request to a registered class |
||
| 386 | * |
||
| 387 | * @param string $sClassName The class name |
||
| 388 | * |
||
| 389 | * @return RequestFactory|null |
||
| 390 | * @throws SetupException |
||
| 391 | */ |
||
| 392 | public function request(string $sClassName = ''): ?RequestFactory |
||
| 393 | { |
||
| 394 | return $this->factory()->request($sClassName); |
||
| 395 | } |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Returns the Jaxon Javascript header and wrapper code to be printed into the page |
||
| 399 | * |
||
| 400 | * The javascript code returned by this function is dependent on the plugins |
||
| 401 | * that are included and the functions and classes that are registered. |
||
| 402 | * |
||
| 403 | * @param bool $bIncludeJs Also get the JS files |
||
| 404 | * @param bool $bIncludeCss Also get the CSS files |
||
| 405 | * |
||
| 406 | * @return string |
||
| 407 | * @throws UriException |
||
| 408 | */ |
||
| 409 | public function getScript(bool $bIncludeJs = false, bool $bIncludeCss = false): string |
||
| 410 | { |
||
| 411 | return $this->xCodeGenerator->getScript($bIncludeJs, $bIncludeCss); |
||
| 412 | } |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Return the javascript header code and file includes |
||
| 416 | * |
||
| 417 | * @return string |
||
| 418 | */ |
||
| 419 | public function getJs(): string |
||
| 420 | { |
||
| 421 | return $this->xCodeGenerator->getJs(); |
||
| 422 | } |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Return the CSS header code and file includes |
||
| 426 | * |
||
| 427 | * @return string |
||
| 428 | */ |
||
| 429 | public function getCss(): string |
||
| 430 | { |
||
| 431 | return $this->xCodeGenerator->getCss(); |
||
| 432 | } |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Determine if a call is a jaxon request or a page load request |
||
| 436 | * |
||
| 437 | * @return bool |
||
| 438 | */ |
||
| 439 | public function canProcessRequest(): bool |
||
| 440 | { |
||
| 441 | return $this->di()->getRequestHandler()->canProcessRequest(); |
||
| 442 | } |
||
| 443 | |||
| 444 | /** |
||
| 445 | * If this is a jaxon request, call the requested PHP function, build the response and send it back to the browser |
||
| 446 | * |
||
| 447 | * This is the main server side engine for Jaxon. |
||
| 448 | * It handles all the incoming requests, including the firing of events and handling of the response. |
||
| 449 | * If your RequestURI is the same as your web page, then this function should be called before ANY |
||
| 450 | * headers or HTML is output from your script. |
||
| 451 | * |
||
| 452 | * This function may exit after the request is processed, if the 'core.process.exit' option is set to true. |
||
| 453 | * |
||
| 454 | * @return void |
||
| 455 | * |
||
| 456 | * @throws RequestException |
||
| 457 | * @see <Jaxon\Jaxon->canProcessRequest> |
||
| 458 | */ |
||
| 459 | public function processRequest() |
||
| 460 | { |
||
| 461 | // Check to see if headers have already been sent out, in which case we can't do our job |
||
| 462 | if(headers_sent($sFilename, $nLineNumber)) |
||
| 463 | { |
||
| 464 | $sMessage = $this->xTranslator->trans('errors.output.already-sent', [ |
||
| 465 | 'location' => $sFilename . ':' . $nLineNumber |
||
| 466 | ]) . "\n" . $this->xTranslator->trans('errors.output.advice'); |
||
| 467 | throw new RequestException($sMessage); |
||
| 468 | } |
||
| 469 | |||
| 470 | $this->di()->getRequestHandler()->processRequest(); |
||
| 471 | |||
| 472 | // Clean the processing buffer |
||
| 473 | if(($this->xConfigManager->getOption('core.process.clean'))) |
||
| 474 | { |
||
| 475 | $er = error_reporting(0); |
||
| 476 | while(ob_get_level() > 0) |
||
| 477 | { |
||
| 478 | ob_end_clean(); |
||
| 479 | } |
||
| 480 | error_reporting($er); |
||
| 481 | } |
||
| 482 | if($this->xConfigManager->getOption('core.response.send')) |
||
| 483 | { |
||
| 484 | $this->sendResponse(); |
||
| 485 | } |
||
| 486 | } |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Prints the response to the output stream, thus sending the response to the browser |
||
| 490 | * |
||
| 491 | * @return void |
||
| 492 | */ |
||
| 493 | public function sendResponse() |
||
| 494 | { |
||
| 495 | if(empty($sContent = $this->xResponseManager->getOutput())) |
||
| 496 | { |
||
| 497 | return; |
||
| 498 | } |
||
| 499 | if($this->di()->getRequest()->getMethod() === 'GET') |
||
| 500 | { |
||
| 501 | header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); |
||
| 502 | header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); |
||
| 503 | header("Cache-Control: no-cache, must-revalidate"); |
||
| 504 | header("Pragma: no-cache"); |
||
| 505 | } |
||
| 506 | header('content-type: ' . $this->xResponseManager->getContentType()); |
||
| 507 | print $sContent; |
||
| 508 | if($this->xConfigManager->getOption('core.process.exit')) |
||
| 509 | { |
||
| 510 | exit(); |
||
| 511 | } |
||
| 512 | } |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Get a registered response plugin |
||
| 516 | * |
||
| 517 | * @param string $sName The name of the plugin |
||
| 518 | * |
||
| 519 | * @return ResponsePlugin|null |
||
| 520 | */ |
||
| 521 | public function plugin(string $sName): ?ResponsePlugin |
||
| 524 | } |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Get a package instance |
||
| 528 | * |
||
| 529 | * @param string $sClassName The package class name |
||
| 530 | * |
||
| 531 | * @return Package|null |
||
| 532 | */ |
||
| 533 | public function package(string $sClassName): ?Package |
||
| 534 | { |
||
| 535 | return $this->xPackageManager->getPackage($sClassName); |
||
| 536 | } |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Get the upload plugin |
||
| 540 | * |
||
| 541 | * @return UploadHandler|null |
||
| 542 | */ |
||
| 543 | public function upload(): ?UploadHandler |
||
| 544 | { |
||
| 545 | return $this->di()->getUploadHandler(); |
||
| 546 | } |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Get the request callback manager |
||
| 550 | * |
||
| 551 | * @return CallbackManager |
||
| 552 | */ |
||
| 553 | public function callback(): CallbackManager |
||
| 554 | { |
||
| 555 | return $this->di()->getCallbackManager(); |
||
| 556 | } |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Get the template engine |
||
| 560 | * |
||
| 561 | * @return TemplateEngine |
||
| 562 | */ |
||
| 563 | public function template(): TemplateEngine |
||
| 564 | { |
||
| 565 | return $this->di()->getTemplateEngine(); |
||
| 566 | } |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Get the App instance |
||
| 570 | * |
||
| 571 | * @return App |
||
| 572 | */ |
||
| 573 | public function app(): App |
||
| 574 | { |
||
| 575 | return $this->di()->getApp(); |
||
| 576 | } |
||
| 577 | |||
| 578 | /** |
||
| 579 | * Get the view renderer |
||
| 580 | * |
||
| 581 | * @return ViewRenderer |
||
| 582 | */ |
||
| 583 | public function view(): ViewRenderer |
||
| 584 | { |
||
| 585 | return $this->di()->getViewRenderer(); |
||
| 586 | } |
||
| 587 | |||
| 588 | /** |
||
| 589 | * Get the paginator |
||
| 590 | * |
||
| 591 | * @return Paginator |
||
| 592 | */ |
||
| 593 | public function paginator(): Paginator |
||
| 594 | { |
||
| 595 | return $this->di()->getPaginator(); |
||
| 596 | } |
||
| 597 | |||
| 598 | /** |
||
| 599 | * Get the session manager |
||
| 600 | * |
||
| 601 | * @return SessionInterface |
||
| 602 | */ |
||
| 603 | public function session(): SessionInterface |
||
| 606 | } |
||
| 607 | |||
| 608 | /** |
||
| 609 | * Reset the library and container instances |
||
| 610 | * |
||
| 611 | * @return void |
||
| 612 | * @throws SetupException |
||
| 613 | */ |
||
| 614 | public function reset() |
||
| 615 | { |
||
| 616 | self::$xInstance = null; |
||
| 617 | self::$xContainer = null; |
||
| 618 | // Need to register the default plugins. |
||
| 619 | self::getInstance()->di()->getPluginManager()->registerPlugins(); |
||
| 620 | } |
||
| 621 | } |
||
| 622 |