| Total Complexity | 40 |
| Total Lines | 466 |
| 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 |
||
| 38 | class Jaxon implements LoggerAwareInterface |
||
| 39 | { |
||
| 40 | use Features\Config; |
||
| 41 | use Features\Translator; |
||
| 42 | use LoggerAwareTrait; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Package version number |
||
| 46 | * |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | private $sVersion = 'Jaxon 3.2.0'; |
||
| 50 | |||
| 51 | /* |
||
| 52 | * Plugin types |
||
| 53 | */ |
||
| 54 | // Response plugin |
||
| 55 | const PLUGIN_RESPONSE = 'ResponsePlugin'; |
||
| 56 | // Request plugin |
||
| 57 | const PLUGIN_REQUEST = 'RequestPlugin'; |
||
| 58 | // Package plugin |
||
| 59 | const PLUGIN_PACKAGE = 'PackagePlugin'; |
||
| 60 | |||
| 61 | /* |
||
| 62 | * Request plugins |
||
| 63 | */ |
||
| 64 | const CALLABLE_CLASS = 'CallableClass'; |
||
| 65 | const CALLABLE_DIR = 'CallableDir'; |
||
| 66 | const CALLABLE_FUNCTION = 'CallableFunction'; |
||
| 67 | // For uploaded files. |
||
| 68 | const FILE_UPLOAD = 'FileUpload'; |
||
| 69 | // For compatibility with previous versions |
||
| 70 | const CALLABLE_OBJECT = 'CallableClass'; // Same as CALLABLE_CLASS |
||
| 71 | const USER_FUNCTION = 'CallableFunction'; // Same as CALLABLE_FUNCTION |
||
| 72 | |||
| 73 | /** |
||
| 74 | * A static instance on this class |
||
| 75 | * |
||
| 76 | * @var Jaxon |
||
| 77 | */ |
||
| 78 | private static $xInstance = null; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * The DI container |
||
| 82 | * |
||
| 83 | * @var Container |
||
| 84 | */ |
||
| 85 | private static $xContainer = null; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Get the static instance |
||
| 89 | * |
||
| 90 | * @return Jaxon |
||
| 91 | */ |
||
| 92 | public static function getInstance() |
||
| 93 | { |
||
| 94 | if(self::$xInstance == null) |
||
| 95 | { |
||
| 96 | self::$xInstance = new Jaxon(); |
||
| 97 | } |
||
| 98 | return self::$xInstance; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * The constructor |
||
| 103 | */ |
||
| 104 | public function __construct() |
||
| 105 | { |
||
| 106 | // Set the default logger |
||
| 107 | $this->setLogger(new NullLogger()); |
||
| 108 | |||
| 109 | if(self::$xContainer == null) |
||
| 110 | { |
||
| 111 | self::$xContainer = new Container($this->getDefaultOptions()); |
||
| 112 | /* |
||
| 113 | * Register the Jaxon request and response plugins |
||
| 114 | */ |
||
| 115 | $this->di()->getPluginManager()->registerRequestPlugins(); |
||
| 116 | $this->di()->getPluginManager()->registerResponsePlugins(); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Get the DI container |
||
| 122 | * |
||
| 123 | * @return Container |
||
| 124 | */ |
||
| 125 | public function di() |
||
| 126 | { |
||
| 127 | return self::$xContainer; |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * The current Jaxon version |
||
| 132 | * |
||
| 133 | * @return string |
||
| 134 | */ |
||
| 135 | public function getVersion() |
||
| 136 | { |
||
| 137 | return $this->sVersion; |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Get the logger |
||
| 142 | * |
||
| 143 | * @return LoggerInterface |
||
| 144 | */ |
||
| 145 | public function logger() |
||
| 146 | { |
||
| 147 | return $this->logger; |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Get the config reader |
||
| 152 | * |
||
| 153 | * @return ConfigReader |
||
| 154 | */ |
||
| 155 | public function config() |
||
| 156 | { |
||
| 157 | return $this->di()->get(ConfigReader::class); |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Get the default options of all components of the library |
||
| 162 | * |
||
| 163 | * @return array<string,string|boolean|integer> |
||
| 164 | */ |
||
| 165 | private function getDefaultOptions() |
||
| 166 | { |
||
| 167 | // The default configuration settings. |
||
| 168 | return [ |
||
| 169 | 'core.version' => $this->getVersion(), |
||
| 170 | 'core.language' => 'en', |
||
| 171 | 'core.encoding' => 'utf-8', |
||
| 172 | 'core.decode_utf8' => false, |
||
| 173 | 'core.prefix.function' => 'jaxon_', |
||
| 174 | 'core.prefix.class' => 'Jaxon', |
||
| 175 | // 'core.request.uri' => '', |
||
| 176 | 'core.request.mode' => 'asynchronous', |
||
| 177 | 'core.request.method' => 'POST', // W3C: Method is case sensitive |
||
| 178 | 'core.response.send' => true, |
||
| 179 | 'core.response.merge.ap' => true, |
||
| 180 | 'core.response.merge.js' => true, |
||
| 181 | 'core.debug.on' => false, |
||
| 182 | 'core.debug.verbose' => false, |
||
| 183 | 'core.process.exit' => true, |
||
| 184 | 'core.process.clean' => false, |
||
| 185 | 'core.process.timeout' => 6000, |
||
| 186 | 'core.error.handle' => false, |
||
| 187 | 'core.error.log_file' => '', |
||
| 188 | 'core.jquery.no_conflict' => false, |
||
| 189 | 'core.upload.enabled' => true, |
||
| 190 | 'js.lib.output_id' => 0, |
||
| 191 | 'js.lib.queue_size' => 0, |
||
| 192 | 'js.lib.load_timeout' => 2000, |
||
| 193 | 'js.lib.show_status' => false, |
||
| 194 | 'js.lib.show_cursor' => true, |
||
| 195 | 'js.app.dir' => '', |
||
| 196 | 'js.app.minify' => true, |
||
| 197 | 'js.app.options' => '', |
||
| 198 | ]; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Get the Global Response object |
||
| 203 | * |
||
| 204 | * @return \Jaxon\Response\Response |
||
| 205 | */ |
||
| 206 | public function getResponse() |
||
| 207 | { |
||
| 208 | return $this->di()->getResponse(); |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Create a new Jaxon response object |
||
| 213 | * |
||
| 214 | * @return \Jaxon\Response\Response |
||
| 215 | */ |
||
| 216 | public function newResponse() |
||
| 217 | { |
||
| 218 | return $this->di()->newResponse(); |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Register a plugin |
||
| 223 | * |
||
| 224 | * Below is a table for priorities and their description: |
||
| 225 | * - 0 thru 999: Plugins that are part of or extensions to the jaxon core |
||
| 226 | * - 1000 thru 8999: User created plugins, typically, these plugins don't care about order |
||
| 227 | * - 9000 thru 9999: Plugins that generally need to be last or near the end of the plugin list |
||
| 228 | * |
||
| 229 | * @param Plugin $xPlugin An instance of a plugin |
||
| 230 | * @param integer $nPriority The plugin priority, used to order the plugins |
||
| 231 | * |
||
| 232 | * @return void |
||
| 233 | */ |
||
| 234 | public function registerPlugin(Plugin $xPlugin, $nPriority = 1000) |
||
| 235 | { |
||
| 236 | $this->di()->getPluginManager()->registerPlugin($xPlugin, $nPriority); |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Register request handlers, including functions, callable classes and directories. |
||
| 241 | * |
||
| 242 | * @param string $sType The type of request handler being registered |
||
| 243 | * Options include: |
||
| 244 | * - Jaxon::CALLABLE_FUNCTION: a function declared at global scope |
||
| 245 | * - Jaxon::CALLABLE_CLASS: a class who's methods are to be registered |
||
| 246 | * - Jaxon::CALLABLE_DIR: a directory containing classes to be registered |
||
| 247 | * - Jaxon::PACKAGE: a package |
||
| 248 | * @param string $sName |
||
| 249 | * When registering a function, this is the name of the function |
||
| 250 | * When registering a callable class, this is the class name |
||
| 251 | * When registering a callable directory, this is the full path to the directory |
||
| 252 | * When registering a package or a plugin, this is the corresponding class name |
||
| 253 | * @param array|string $xOptions The related options |
||
| 254 | * |
||
| 255 | * @return void |
||
| 256 | */ |
||
| 257 | public function register($sType, $sName, $xOptions = []) |
||
| 258 | { |
||
| 259 | if($sType == self::CALLABLE_DIR || |
||
| 260 | $sType == self::CALLABLE_CLASS || |
||
| 261 | $sType == self::CALLABLE_FUNCTION) |
||
| 262 | { |
||
| 263 | $this->di()->getPluginManager()->registerCallable($sType, $sName, $xOptions); |
||
| 264 | return; |
||
| 265 | } |
||
| 266 | /* |
||
| 267 | if($sType == self::PLUGIN_RESPONSE) |
||
| 268 | { |
||
| 269 | $this->di()->getPluginManager()->registerRequestPlugin($sName, $xOptions); |
||
| 270 | return; |
||
| 271 | } |
||
| 272 | if($sType == self::PLUGIN_REQUEST) |
||
| 273 | { |
||
| 274 | $this->di()->getPluginManager()->registerResponsePlugin($sName, $xOptions); |
||
| 275 | return; |
||
| 276 | } |
||
| 277 | */ |
||
| 278 | if($sType == self::PLUGIN_PACKAGE && is_array($xOptions)) |
||
| 279 | { |
||
| 280 | $this->di()->getPluginManager()->registerPackage($sName, $xOptions); |
||
| 281 | return; |
||
| 282 | } |
||
| 283 | // Todo: throw an error |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Get an instance of a registered class |
||
| 288 | * |
||
| 289 | * @param string $sClassName The class name |
||
| 290 | * |
||
| 291 | * @return mixed |
||
| 292 | */ |
||
| 293 | public function instance($sClassName) |
||
| 294 | { |
||
| 295 | $xCallable = $this->di()->getCallableRegistry()->getCallableObject($sClassName); |
||
| 296 | return ($xCallable) ? $xCallable->getRegisteredObject() : null; |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Get a request to a registered class |
||
| 301 | * |
||
| 302 | * @param string $sClassName The class name |
||
| 303 | * |
||
| 304 | * @return \Jaxon\Request\Factory\CallableClass\Request |
||
| 305 | */ |
||
| 306 | public function request($sClassName) |
||
| 307 | { |
||
| 308 | $xInstance = $this->instance($sClassName); |
||
| 309 | return ($xInstance) ? $xInstance->rq() : null; |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Returns the Jaxon Javascript header and wrapper code to be printed into the page |
||
| 314 | * |
||
| 315 | * The javascript code returned by this function is dependent on the plugins |
||
| 316 | * that are included and the functions and classes that are registered. |
||
| 317 | * |
||
| 318 | * @param boolean $bIncludeJs Also get the JS files |
||
| 319 | * @param boolean $bIncludeCss Also get the CSS files |
||
| 320 | * |
||
| 321 | * @return string |
||
| 322 | */ |
||
| 323 | public function getScript($bIncludeJs = false, $bIncludeCss = false) |
||
| 324 | { |
||
| 325 | return $this->di()->getCodeGenerator()->getScript($bIncludeJs, $bIncludeCss); |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Print the jaxon Javascript header and wrapper code into your page |
||
| 330 | * |
||
| 331 | * The javascript code returned by this function is dependent on the plugins |
||
| 332 | * that are included and the functions and classes that are registered. |
||
| 333 | * |
||
| 334 | * @param boolean $bIncludeJs Also print the JS files |
||
| 335 | * @param boolean $bIncludeCss Also print the CSS files |
||
| 336 | * |
||
| 337 | * @return void |
||
| 338 | */ |
||
| 339 | public function printScript($bIncludeJs = false, $bIncludeCss = false) |
||
| 340 | { |
||
| 341 | print $this->getScript($bIncludeJs, $bIncludeCss); |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Return the javascript header code and file includes |
||
| 346 | * |
||
| 347 | * @return string |
||
| 348 | */ |
||
| 349 | public function getJs() |
||
| 350 | { |
||
| 351 | return $this->di()->getCodeGenerator()->getJs(); |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Return the CSS header code and file includes |
||
| 356 | * |
||
| 357 | * @return string |
||
| 358 | */ |
||
| 359 | public function getCss() |
||
| 360 | { |
||
| 361 | return $this->di()->getCodeGenerator()->getCss(); |
||
| 362 | } |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Determine if a call is a jaxon request or a page load request |
||
| 366 | * |
||
| 367 | * @return boolean |
||
| 368 | */ |
||
| 369 | public function canProcessRequest() |
||
| 370 | { |
||
| 371 | return $this->di()->getRequestHandler()->canProcessRequest(); |
||
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * If this is a jaxon request, call the requested PHP function, build the response and send it back to the browser |
||
| 376 | * |
||
| 377 | * This is the main server side engine for Jaxon. |
||
| 378 | * It handles all the incoming requests, including the firing of events and handling of the response. |
||
| 379 | * If your RequestURI is the same as your web page, then this function should be called before ANY |
||
| 380 | * headers or HTML is output from your script. |
||
| 381 | * |
||
| 382 | * This function may exit after the request is processed, if the 'core.process.exit' option is set to true. |
||
| 383 | * |
||
| 384 | * @return void |
||
| 385 | * |
||
| 386 | * @see <Jaxon\Jaxon->canProcessRequest> |
||
| 387 | */ |
||
| 388 | public function processRequest() |
||
| 389 | { |
||
| 390 | // Check to see if headers have already been sent out, in which case we can't do our job |
||
| 391 | if(headers_sent($filename, $linenumber)) |
||
| 392 | { |
||
| 393 | echo $this->trans('errors.output.already-sent', [ |
||
| 394 | 'location' => $filename . ':' . $linenumber |
||
| 395 | ]), "\n", $this->trans('errors.output.advice'); |
||
| 396 | exit(); |
||
| 397 | } |
||
| 398 | |||
| 399 | $this->di()->getRequestHandler()->processRequest(); |
||
| 400 | |||
| 401 | if(($this->getOption('core.response.send'))) |
||
| 402 | { |
||
| 403 | $this->di()->getResponseManager()->sendOutput(); |
||
| 404 | |||
| 405 | if(($this->getOption('core.process.exit'))) |
||
| 406 | { |
||
| 407 | exit(); |
||
| 408 | } |
||
| 409 | } |
||
| 410 | } |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Get a registered response plugin |
||
| 414 | * |
||
| 415 | * @param string $sName The name of the plugin |
||
| 416 | * |
||
| 417 | * @return \Jaxon\Plugin\Response |
||
| 418 | */ |
||
| 419 | public function plugin($sName) |
||
| 422 | } |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Get a package instance |
||
| 426 | * |
||
| 427 | * @param string $sClassName The package class name |
||
| 428 | * |
||
| 429 | * @return \Jaxon\Plugin\Package |
||
| 430 | */ |
||
| 431 | public function package($sClassName) |
||
| 432 | { |
||
| 433 | return $this->di()->getPluginManager()->getPackage($sClassName); |
||
| 434 | } |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Get the upload plugin |
||
| 438 | * |
||
| 439 | * @return \Jaxon\Request\Plugin\FileUpload |
||
| 440 | */ |
||
| 441 | public function upload() |
||
| 442 | { |
||
| 443 | return $this->di()->getPluginManager()->getRequestPlugin(self::FILE_UPLOAD); |
||
| 444 | } |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Get the request callback manager |
||
| 448 | * |
||
| 449 | * @return \Jaxon\Request\Handler\Callback |
||
| 450 | */ |
||
| 451 | public function callback() |
||
| 452 | { |
||
| 453 | return $this->di()->getRequestHandler()->getCallbackManager(); |
||
| 454 | } |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Get the dialog wrapper |
||
| 458 | * |
||
| 459 | * @return \Jaxon\Utils\Dialogs\Dialog |
||
| 460 | */ |
||
| 461 | public function dialog() |
||
| 462 | { |
||
| 463 | return $this->di()->getDialog(); |
||
| 464 | } |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Get the template engine |
||
| 468 | * |
||
| 469 | * @return \Jaxon\Utils\Template\Engine |
||
| 470 | */ |
||
| 471 | public function template() |
||
| 472 | { |
||
| 473 | return $this->di()->getTemplateEngine(); |
||
| 474 | } |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Get the App instance |
||
| 478 | * |
||
| 479 | * @return \Jaxon\App\App |
||
| 480 | */ |
||
| 481 | public function app() |
||
| 482 | { |
||
| 483 | return $this->di()->getApp(); |
||
| 484 | } |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Get the view renderer |
||
| 488 | * |
||
| 489 | * @return \Jaxon\Utils\View\Renderer |
||
| 490 | */ |
||
| 491 | public function view() |
||
| 492 | { |
||
| 493 | return $this->di()->getViewRenderer(); |
||
| 494 | } |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Get the session manager |
||
| 498 | * |
||
| 499 | * @return \Jaxon\Contracts\Session |
||
| 500 | */ |
||
| 501 | public function session() |
||
| 504 | } |
||
| 505 | } |
||
| 506 |