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