| Total Complexity | 40 |
| Total Lines | 379 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like PluginManager 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 PluginManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 58 | class PluginManager |
||
| 59 | { |
||
| 60 | /** |
||
| 61 | * @var Container |
||
| 62 | */ |
||
| 63 | protected $di; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var Translator |
||
| 67 | */ |
||
| 68 | protected $xTranslator; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var ConfigManager |
||
| 72 | */ |
||
| 73 | protected $xConfigManager; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var ViewManager |
||
| 77 | */ |
||
| 78 | protected $xViewManager; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * The code generator |
||
| 82 | * |
||
| 83 | * @var CodeGenerator |
||
| 84 | */ |
||
| 85 | private $xCodeGenerator; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Request plugins, indexed by name |
||
| 89 | * |
||
| 90 | * @var array<CallableRegistryInterface> |
||
| 91 | */ |
||
| 92 | private $aRegistryPlugins = []; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Request handlers, indexed by name |
||
| 96 | * |
||
| 97 | * @var array<RequestHandlerInterface> |
||
| 98 | */ |
||
| 99 | private $aRequestHandlers = []; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Response plugins, indexed by name |
||
| 103 | * |
||
| 104 | * @var array<ResponsePluginInterface> |
||
| 105 | */ |
||
| 106 | private $aResponsePlugins = []; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * The constructor |
||
| 110 | * |
||
| 111 | * @param Container $di |
||
| 112 | * @param ConfigManager $xConfigManager |
||
| 113 | * @param ViewManager $xViewManager |
||
| 114 | * @param CodeGenerator $xCodeGenerator |
||
| 115 | * @param Translator $xTranslator |
||
| 116 | */ |
||
| 117 | public function __construct(Container $di, ConfigManager $xConfigManager, |
||
| 118 | ViewManager $xViewManager, CodeGenerator $xCodeGenerator, Translator $xTranslator) |
||
| 119 | { |
||
| 120 | $this->di = $di; |
||
| 121 | $this->xConfigManager = $xConfigManager; |
||
| 122 | $this->xViewManager = $xViewManager; |
||
| 123 | $this->xCodeGenerator = $xCodeGenerator; |
||
| 124 | $this->xTranslator = $xTranslator; |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Get the request plugins |
||
| 129 | * |
||
| 130 | * @return array<RequestPlugin> |
||
| 131 | */ |
||
| 132 | public function getRequestHandlers(): array |
||
| 133 | { |
||
| 134 | return $this->aRequestHandlers; |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Get a package instance |
||
| 139 | * |
||
| 140 | * @param string $sClassName The package class name |
||
| 141 | * |
||
| 142 | * @return Package|null |
||
| 143 | */ |
||
| 144 | public function getPackage(string $sClassName): ?Package |
||
| 145 | { |
||
| 146 | return $this->di->get(trim($sClassName, '\\ ')); |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Register a plugin |
||
| 151 | * |
||
| 152 | * Below is a table for priorities and their description: |
||
| 153 | * - 0 to 999: Plugins that are part of or extensions to the jaxon core |
||
| 154 | * - 1000 to 8999: User created plugins, typically, these plugins don't care about order |
||
| 155 | * - 9000 to 9999: Plugins that generally need to be last or near the end of the plugin list |
||
| 156 | * |
||
| 157 | * @param string $sClassName The plugin class |
||
| 158 | * @param string $sPluginName The plugin name |
||
| 159 | * @param integer $nPriority The plugin priority, used to order the plugins |
||
| 160 | * |
||
| 161 | * @return void |
||
| 162 | * @throws SetupException |
||
| 163 | */ |
||
| 164 | public function registerPlugin(string $sClassName, string $sPluginName, int $nPriority = 1000) |
||
| 165 | { |
||
| 166 | $bIsUsed = false; |
||
| 167 | $aInterfaces = class_implements($sClassName); |
||
| 168 | if(in_array(CodeGeneratorInterface::class, $aInterfaces)) |
||
| 169 | { |
||
| 170 | $this->xCodeGenerator->addGenerator($sClassName, $nPriority); |
||
| 171 | $bIsUsed = true; |
||
| 172 | } |
||
| 173 | if(in_array(CallableRegistryInterface::class, $aInterfaces)) |
||
| 174 | { |
||
| 175 | $this->aRegistryPlugins[$sPluginName] = $sClassName; |
||
| 176 | $bIsUsed = true; |
||
| 177 | } |
||
| 178 | if(in_array(RequestHandlerInterface::class, $aInterfaces)) |
||
| 179 | { |
||
| 180 | $this->aRequestHandlers[$sPluginName] = $sClassName; |
||
| 181 | $bIsUsed = true; |
||
| 182 | } |
||
| 183 | if(in_array(ResponsePluginInterface::class, $aInterfaces)) |
||
| 184 | { |
||
| 185 | $this->aResponsePlugins[$sPluginName] = $sClassName; |
||
| 186 | $bIsUsed = true; |
||
| 187 | } |
||
| 188 | |||
| 189 | // This plugin implements the Message interface |
||
| 190 | if(in_array(MessageInterface::class, $aInterfaces)) |
||
| 191 | { |
||
| 192 | $this->di->getDialog()->setMessage($sClassName); |
||
| 193 | $bIsUsed = true; |
||
| 194 | } |
||
| 195 | // This plugin implements the Question interface |
||
| 196 | if(in_array(QuestionInterface::class, $aInterfaces)) |
||
| 197 | { |
||
| 198 | $this->di->getDialog()->setQuestion($sClassName); |
||
| 199 | $bIsUsed = true; |
||
| 200 | } |
||
| 201 | |||
| 202 | if(!$bIsUsed) |
||
| 203 | { |
||
| 204 | // The class is invalid. |
||
| 205 | $sMessage = $this->xTranslator->trans('errors.register.invalid', ['name' => $sClassName]); |
||
| 206 | throw new SetupException($sMessage); |
||
| 207 | } |
||
| 208 | |||
| 209 | // Register the plugin in the DI container, if necessary |
||
| 210 | if(!$this->di->has($sClassName)) |
||
| 211 | { |
||
| 212 | $this->di->auto($sClassName); |
||
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Register a function or callable class |
||
| 218 | * |
||
| 219 | * Call the request plugin with the $sType defined as name. |
||
| 220 | * |
||
| 221 | * @param string $sType The type of request handler being registered |
||
| 222 | * @param string $sCallable The callable entity being registered |
||
| 223 | * @param array|string $xOptions The associated options |
||
| 224 | * |
||
| 225 | * @return void |
||
| 226 | * @throws SetupException |
||
| 227 | */ |
||
| 228 | public function registerCallable(string $sType, string $sCallable, $xOptions = []) |
||
| 229 | { |
||
| 230 | if(isset($this->aRegistryPlugins[$sType]) && |
||
| 231 | ($xPlugin = $this->di->g($this->aRegistryPlugins[$sType]))) |
||
| 232 | { |
||
| 233 | $xPlugin->register($sType, $sCallable, $xPlugin->checkOptions($sCallable, $xOptions)); |
||
| 234 | return; |
||
| 235 | } |
||
| 236 | throw new SetupException($this->xTranslator->trans('errors.register.plugin', |
||
| 237 | ['name' => $sType, 'callable' => $sCallable])); |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Register callables from a section of the config |
||
| 242 | * |
||
| 243 | * @param array $aOptions The content of the config section |
||
| 244 | * @param string $sCallableType The type of callable to register |
||
| 245 | * |
||
| 246 | * @return void |
||
| 247 | * @throws SetupException |
||
| 248 | */ |
||
| 249 | private function registerCallablesFromOptions(array $aOptions, string $sCallableType) |
||
| 262 | } |
||
| 263 | } |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Save items in the DI container |
||
| 268 | * |
||
| 269 | * @param Config $xConfig |
||
| 270 | * |
||
| 271 | * @return void |
||
| 272 | */ |
||
| 273 | private function updateContainer(Config $xConfig) |
||
| 274 | { |
||
| 275 | $aOptions = $xConfig->getOption('container', []); |
||
| 276 | foreach($aOptions as $xKey => $xValue) |
||
| 277 | { |
||
| 278 | if(is_integer($xKey) && is_string($xValue)) |
||
| 279 | { |
||
| 280 | // The value of the entry is the class name |
||
| 281 | $this->di->auto($xValue); |
||
| 282 | } |
||
| 283 | elseif($xValue instanceof Closure) |
||
| 284 | { |
||
| 285 | // The key is the class name. It must be a string. |
||
| 286 | $this->di->set((string)$xKey, $xValue); |
||
| 287 | } |
||
| 288 | else |
||
| 289 | { |
||
| 290 | // The key is the class name. It must be a string. |
||
| 291 | $this->di->val((string)$xKey, $xValue); |
||
| 292 | } |
||
| 293 | } |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Read and set Jaxon options from a JSON config file |
||
| 298 | * |
||
| 299 | * @param Config $xConfig The config options |
||
| 300 | * @param Config|null $xPkgConfig The user provided package options |
||
| 301 | * |
||
| 302 | * @return void |
||
| 303 | * @throws SetupException |
||
| 304 | */ |
||
| 305 | private function registerItemsFromConfig(Config $xConfig, ?Config $xPkgConfig = null) |
||
| 306 | { |
||
| 307 | $aSections = [ |
||
| 308 | 'functions' => Jaxon::CALLABLE_FUNCTION, |
||
| 309 | 'classes' => Jaxon::CALLABLE_CLASS, |
||
| 310 | 'directories' => Jaxon::CALLABLE_DIR, |
||
| 311 | ]; |
||
| 312 | // Register functions, classes and directories |
||
| 313 | foreach($aSections as $sSection => $sCallableType) |
||
| 314 | { |
||
| 315 | $this->registerCallablesFromOptions($xConfig->getOption($sSection, []), $sCallableType); |
||
| 316 | } |
||
| 317 | // Register the view namespaces |
||
| 318 | // Note: the $xPkgConfig can provide a "template" option, which is used to customize |
||
| 319 | // the user defined view namespaces. That's why it is needed here. |
||
| 320 | $this->xViewManager->addNamespaces($xConfig, $xPkgConfig); |
||
| 321 | // Save items in the DI container |
||
| 322 | $this->updateContainer($xConfig); |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Get package options |
||
| 327 | * |
||
| 328 | * @param string $sClassName The package class |
||
| 329 | * |
||
| 330 | * @return array |
||
| 331 | * @throws SetupException |
||
| 332 | */ |
||
| 333 | private function getPackageOptions(string $sClassName): array |
||
| 334 | { |
||
| 335 | if(!is_subclass_of($sClassName, Package::class)) |
||
| 336 | { |
||
| 337 | $sMessage = $this->xTranslator->trans('errors.register.invalid', ['name' => $sClassName]); |
||
| 338 | throw new SetupException($sMessage); |
||
| 339 | } |
||
| 340 | // $this->aPackages contains packages config file paths. |
||
| 341 | $aLibOptions = $sClassName::config(); |
||
| 342 | if(is_string($aLibOptions)) |
||
| 343 | { |
||
| 344 | $aLibOptions = $this->xConfigManager->read($aLibOptions); |
||
| 345 | } |
||
| 346 | if(!is_array($aLibOptions)) |
||
| 347 | { |
||
| 348 | $sMessage = $this->xTranslator->trans('errors.register.invalid', ['name' => $sClassName]); |
||
| 349 | throw new SetupException($sMessage); |
||
| 350 | } |
||
| 351 | return $aLibOptions; |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Register a package |
||
| 356 | * |
||
| 357 | * @param string $sClassName The package class |
||
| 358 | * @param array $aPkgOptions The user provided package options |
||
| 359 | * |
||
| 360 | * @return void |
||
| 361 | * @throws SetupException |
||
| 362 | */ |
||
| 363 | public function registerPackage(string $sClassName, array $aPkgOptions) |
||
| 364 | { |
||
| 365 | $sClassName = trim($sClassName, '\\ '); |
||
| 366 | $aLibOptions = $this->getPackageOptions($sClassName); |
||
| 367 | // Add the package name to the config |
||
| 368 | $aLibOptions['package'] = $sClassName; |
||
| 369 | $xLibConfig = $this->xConfigManager->newConfig($aLibOptions); |
||
| 370 | $xPkgConfig = $this->xConfigManager->newConfig($aPkgOptions); |
||
| 371 | $this->di->registerPackage($sClassName, $xPkgConfig); |
||
| 372 | // Register the declarations in the package config. |
||
| 373 | $this->registerItemsFromConfig($xLibConfig, $xPkgConfig); |
||
| 374 | // Register the package as a code generator. |
||
| 375 | $this->xCodeGenerator->addGenerator($sClassName, 500); |
||
| 376 | } |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Read and set Jaxon options from a JSON config file |
||
| 380 | * |
||
| 381 | * @param Config $xAppConfig The config options |
||
| 382 | * |
||
| 383 | * @return void |
||
| 384 | * @throws SetupException |
||
| 385 | */ |
||
| 386 | public function registerFromConfig(Config $xAppConfig) |
||
| 387 | { |
||
| 388 | $this->registerItemsFromConfig($xAppConfig); |
||
| 389 | |||
| 390 | // Register packages |
||
| 391 | $aPackageConfig = $xAppConfig->getOption('packages', []); |
||
| 392 | foreach($aPackageConfig as $sClassName => $aPkgOptions) |
||
| 393 | { |
||
| 394 | $this->registerPackage($sClassName, $aPkgOptions); |
||
| 395 | } |
||
| 396 | } |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Find the specified response plugin by name and return a reference to it if one exists |
||
| 400 | * |
||
| 401 | * @param string $sName The name of the plugin |
||
| 402 | * @param Response|null $xResponse The response to attach the plugin to |
||
| 403 | * |
||
| 404 | * @return ResponsePlugin|null |
||
| 405 | */ |
||
| 406 | public function getResponsePlugin(string $sName, ?Response $xResponse = null): ?ResponsePlugin |
||
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Register the Jaxon request plugins |
||
| 422 | * |
||
| 423 | * @return void |
||
| 424 | * @throws SetupException |
||
| 425 | */ |
||
| 426 | public function registerPlugins() |
||
| 437 | } |
||
| 438 | } |
||
| 439 |