Complex classes like Application 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Application, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class Application extends BaseApplication |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | const APP_NAME = 'n98-magerun'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | const APP_VERSION = '1.97.23'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var int |
||
| 45 | */ |
||
| 46 | const MAGENTO_MAJOR_VERSION_1 = 1; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var int |
||
| 50 | */ |
||
| 51 | const MAGENTO_MAJOR_VERSION_2 = 2; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | private static $logo = " |
||
| 57 | ___ ___ |
||
| 58 | _ _/ _ ( _ )___ _ __ __ _ __ _ ___ _ _ _ _ _ _ |
||
| 59 | | ' \\_, / _ \\___| ' \\/ _` / _` / -_) '_| || | ' \\ |
||
| 60 | |_||_/_/\\___/ |_|_|_\\__,_\\__, \\___|_| \\_,_|_||_| |
||
| 61 | |___/ |
||
| 62 | "; |
||
| 63 | /** |
||
| 64 | * @var ClassLoader |
||
| 65 | */ |
||
| 66 | protected $autoloader; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var Config |
||
| 70 | */ |
||
| 71 | protected $config; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @see \N98\Magento\Application::setConfigurationLoader() |
||
| 75 | * @var ConfigurationLoader |
||
| 76 | */ |
||
| 77 | private $configurationLoaderInjected; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var string |
||
| 81 | */ |
||
| 82 | protected $_magentoRootFolder = null; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var bool |
||
| 86 | */ |
||
| 87 | protected $_magentoEnterprise = false; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var int |
||
| 91 | */ |
||
| 92 | protected $_magentoMajorVersion = self::MAGENTO_MAJOR_VERSION_1; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var EntryPoint |
||
| 96 | */ |
||
| 97 | protected $_magento2EntryPoint = null; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var bool |
||
| 101 | */ |
||
| 102 | protected $_isPharMode = false; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var bool |
||
| 106 | */ |
||
| 107 | protected $_magerunStopFileFound = false; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var string |
||
| 111 | */ |
||
| 112 | protected $_magerunStopFileFolder = null; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var bool |
||
| 116 | */ |
||
| 117 | protected $_isInitialized = false; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @var EventDispatcher |
||
| 121 | */ |
||
| 122 | protected $dispatcher; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * If root dir is set by root-dir option this flag is true |
||
| 126 | * |
||
| 127 | * @var bool |
||
| 128 | */ |
||
| 129 | protected $_directRootDir = false; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @var bool |
||
| 133 | */ |
||
| 134 | protected $_magentoDetected = false; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @param ClassLoader $autoloader |
||
| 138 | */ |
||
| 139 | public function __construct($autoloader = null) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @return InputDefinition |
||
| 147 | */ |
||
| 148 | protected function getDefaultInputDefinition() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Search for magento root folder |
||
| 190 | * |
||
| 191 | * @param InputInterface $input [optional] |
||
| 192 | * @param OutputInterface $output [optional] |
||
| 193 | * @return void |
||
| 194 | */ |
||
| 195 | public function detectMagento(InputInterface $input = null, OutputInterface $output = null) |
||
| 196 | { |
||
| 197 | // do not detect magento twice |
||
| 198 | if ($this->_magentoDetected) { |
||
| 199 | return; |
||
| 200 | } |
||
| 201 | |||
| 202 | if (null === $input) { |
||
| 203 | $input = new ArgvInput(); |
||
| 204 | } |
||
| 205 | |||
| 206 | if (null === $output) { |
||
| 207 | $output = new ConsoleOutput(); |
||
| 208 | } |
||
| 209 | |||
| 210 | if ($this->getMagentoRootFolder() === null) { |
||
| 211 | $this->_checkRootDirOption($input); |
||
| 212 | $folder = OperatingSystem::getCwd(); |
||
| 213 | } else { |
||
| 214 | $folder = $this->getMagentoRootFolder(); |
||
| 215 | } |
||
| 216 | |||
| 217 | $this->getHelperSet()->set(new MagentoHelper($input, $output), 'magento'); |
||
| 218 | $magentoHelper = $this->getHelperSet()->get('magento'); |
||
| 219 | /* @var $magentoHelper MagentoHelper */ |
||
| 220 | if (!$this->_directRootDir) { |
||
| 221 | $subFolders = $this->config->getDetectSubFolders(); |
||
| 222 | } else { |
||
| 223 | $subFolders = array(); |
||
| 224 | } |
||
| 225 | |||
| 226 | $this->_magentoDetected = $magentoHelper->detect($folder, $subFolders); |
||
| 227 | $this->_magentoRootFolder = $magentoHelper->getRootFolder(); |
||
| 228 | $this->_magentoEnterprise = $magentoHelper->isEnterpriseEdition(); |
||
| 229 | $this->_magentoMajorVersion = $magentoHelper->getMajorVersion(); |
||
| 230 | $this->_magerunStopFileFound = $magentoHelper->isMagerunStopFileFound(); |
||
| 231 | $this->_magerunStopFileFolder = $magentoHelper->getMagerunStopFileFolder(); |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Add own helpers to helperset. |
||
| 236 | * |
||
| 237 | * @return void |
||
| 238 | */ |
||
| 239 | protected function registerHelpers() |
||
| 240 | { |
||
| 241 | $helperSet = $this->getHelperSet(); |
||
| 242 | $config = $this->config->getConfig(); |
||
| 243 | |||
| 244 | // Twig |
||
| 245 | $twigBaseDirs = array( |
||
| 246 | __DIR__ . '/../../../res/twig', |
||
| 247 | ); |
||
| 248 | if (isset($config['twig']['baseDirs']) && is_array($config['twig']['baseDirs'])) { |
||
| 249 | $twigBaseDirs = array_merge(array_reverse($config['twig']['baseDirs']), $twigBaseDirs); |
||
| 250 | } |
||
| 251 | $helperSet->set(new TwigHelper($twigBaseDirs), 'twig'); |
||
| 252 | |||
| 253 | foreach ($config['helpers'] as $helperName => $helperClass) { |
||
| 254 | if (class_exists($helperClass)) { |
||
| 255 | $helperSet->set(new $helperClass(), $helperName); |
||
| 256 | } |
||
| 257 | } |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @param InputInterface $input |
||
| 262 | * |
||
| 263 | * @return ArgvInput|InputInterface |
||
| 264 | */ |
||
| 265 | protected function checkConfigCommandAlias(InputInterface $input) |
||
| 266 | { |
||
| 267 | trigger_error(__METHOD__ . ' moved, use getConfig()->checkConfigCommandAlias()', E_USER_DEPRECATED); |
||
| 268 | |||
| 269 | return $this->config->checkConfigCommandAlias($input); |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @param Command $command |
||
| 274 | */ |
||
| 275 | protected function registerConfigCommandAlias(Command $command) |
||
| 276 | { |
||
| 277 | trigger_error(__METHOD__ . ' moved, use getConfig()->registerConfigCommandAlias() instead', E_USER_DEPRECATED); |
||
| 278 | |||
| 279 | return $this->config->registerConfigCommandAlias($command); |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Adds autoloader prefixes from user's config |
||
| 284 | */ |
||
| 285 | protected function registerCustomAutoloaders() |
||
| 286 | { |
||
| 287 | trigger_error(__METHOD__ . ' moved, use getConfig()->registerCustomAutoloaders() instead', E_USER_DEPRECATED); |
||
| 288 | |||
| 289 | $this->config->registerCustomAutoloaders($this->autoloader); |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @return bool |
||
| 294 | */ |
||
| 295 | protected function hasCustomCommands() |
||
| 296 | { |
||
| 297 | trigger_error(__METHOD__ . ' moved, use config directly instead', E_USER_DEPRECATED); |
||
| 298 | |||
| 299 | $config = $this->config->getConfig(); |
||
| 300 | return isset($config['commands']['customCommands']) && is_array($config['commands']['customCommands']); |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @return void |
||
| 305 | */ |
||
| 306 | protected function registerCustomCommands() |
||
| 307 | { |
||
| 308 | trigger_error(__METHOD__ . ' moved, use getConfig()->registerCustomCommands() instead', E_USER_DEPRECATED); |
||
| 309 | |||
| 310 | $this->config->registerCustomCommands($this); |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @param string $class |
||
| 315 | * @return bool |
||
| 316 | */ |
||
| 317 | protected function isCommandDisabled($class) |
||
| 318 | { |
||
| 319 | trigger_error(__METHOD__ . ' moved, use config directly instead', E_USER_DEPRECATED); |
||
| 320 | |||
| 321 | $config = $this->config->getConfig(); |
||
| 322 | return in_array($class, $config['commands']['disabled']); |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Override standard command registration. We want alias support. |
||
| 327 | * |
||
| 328 | * @param Command $command |
||
| 329 | * |
||
| 330 | * @return Command |
||
| 331 | */ |
||
| 332 | public function add(Command $command) |
||
| 333 | { |
||
| 334 | if ($this->config) { |
||
| 335 | $this->config->registerConfigCommandAlias($command); |
||
| 336 | } |
||
| 337 | |||
| 338 | return parent::add($command); |
||
| 339 | } |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @param bool $mode |
||
| 343 | */ |
||
| 344 | public function setPharMode($mode) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @return bool |
||
| 351 | */ |
||
| 352 | public function isPharMode() |
||
| 353 | { |
||
| 354 | return $this->_isPharMode; |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @TODO Move logic into "EventSubscriber" |
||
| 359 | * |
||
| 360 | * @param OutputInterface $output |
||
| 361 | * @return null|false |
||
| 362 | */ |
||
| 363 | public function checkVarDir(OutputInterface $output) |
||
| 364 | { |
||
| 365 | $tempVarDir = sys_get_temp_dir() . '/magento/var'; |
||
| 366 | if (!OutputInterface::VERBOSITY_NORMAL <= $output->getVerbosity() && !is_dir($tempVarDir)) { |
||
| 367 | return; |
||
| 368 | } |
||
| 369 | |||
| 370 | $this->detectMagento(null, $output); |
||
| 371 | /* If magento is not installed yet, don't check */ |
||
| 372 | if ($this->_magentoRootFolder === null |
||
| 373 | || !file_exists($this->_magentoRootFolder . '/app/etc/local.xml') |
||
| 374 | ) { |
||
| 375 | return; |
||
| 376 | } |
||
| 377 | |||
| 378 | try { |
||
| 379 | $this->initMagento(); |
||
| 380 | } catch (Exception $e) { |
||
| 381 | $message = 'Cannot initialize Magento. Please check your configuration. ' |
||
| 382 | . 'Some n98-magerun command will not work. Got message: '; |
||
| 383 | if (OutputInterface::VERBOSITY_VERY_VERBOSE <= $output->getVerbosity()) { |
||
| 384 | $message .= $e->getTraceAsString(); |
||
| 385 | } else { |
||
| 386 | $message .= $e->getMessage(); |
||
| 387 | } |
||
| 388 | $output->writeln($message); |
||
| 389 | |||
| 390 | return; |
||
| 391 | } |
||
| 392 | |||
| 393 | $configOptions = new \Mage_Core_Model_Config_Options(); |
||
| 394 | $currentVarDir = $configOptions->getVarDir(); |
||
| 395 | |||
| 396 | if ($currentVarDir == $tempVarDir) { |
||
| 397 | $output->writeln(array( |
||
| 398 | sprintf('<warning>Fallback folder %s is used in n98-magerun</warning>', $tempVarDir), |
||
| 399 | '', |
||
| 400 | 'n98-magerun is using the fallback folder. If there is another folder configured for Magento, this ' . |
||
| 401 | 'can cause serious problems.', |
||
| 402 | 'Please refer to https://github.com/netz98/n98-magerun/wiki/File-system-permissions ' . |
||
| 403 | 'for more information.', |
||
| 404 | '', |
||
| 405 | )); |
||
| 406 | } else { |
||
| 407 | $output->writeln(array( |
||
| 408 | sprintf('<warning>Folder %s found, but not used in n98-magerun</warning>', $tempVarDir), |
||
| 409 | '', |
||
| 410 | "This might cause serious problems. n98-magerun is using the configured var-folder " . |
||
| 411 | "<comment>$currentVarDir</comment>", |
||
| 412 | 'Please refer to https://github.com/netz98/n98-magerun/wiki/File-system-permissions ' . |
||
| 413 | 'for more information.', |
||
| 414 | '', |
||
| 415 | )); |
||
| 416 | |||
| 417 | return false; |
||
| 418 | } |
||
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Loads and initializes the Magento application |
||
| 423 | * |
||
| 424 | * @param bool $soft |
||
| 425 | * |
||
| 426 | * @return bool false if magento root folder is not set, true otherwise |
||
| 427 | */ |
||
| 428 | public function initMagento($soft = false) |
||
| 429 | { |
||
| 430 | if ($this->getMagentoRootFolder() === null) { |
||
| 431 | return false; |
||
| 432 | } |
||
| 433 | |||
| 434 | $isMagento2 = $this->_magentoMajorVersion === self::MAGENTO_MAJOR_VERSION_2; |
||
| 435 | if ($isMagento2) { |
||
| 436 | $this->_initMagento2(); |
||
| 437 | } else { |
||
| 438 | $this->_initMagento1($soft); |
||
| 439 | } |
||
| 440 | |||
| 441 | return true; |
||
| 442 | } |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @return string |
||
| 446 | */ |
||
| 447 | public function getHelp() |
||
| 451 | |||
| 452 | public function getLongVersion() |
||
| 456 | |||
| 457 | /** |
||
| 458 | * @return boolean |
||
| 459 | */ |
||
| 460 | public function isMagentoEnterprise() |
||
| 464 | |||
| 465 | /** |
||
| 466 | * @return string |
||
| 467 | */ |
||
| 468 | public function getMagentoRootFolder() |
||
| 472 | |||
| 473 | /** |
||
| 474 | * @param string $magentoRootFolder |
||
| 475 | */ |
||
| 476 | public function setMagentoRootFolder($magentoRootFolder) |
||
| 480 | |||
| 481 | /** |
||
| 482 | * @return int |
||
| 483 | */ |
||
| 484 | public function getMagentoMajorVersion() |
||
| 488 | |||
| 489 | /** |
||
| 490 | * @return ClassLoader |
||
| 491 | */ |
||
| 492 | public function getAutoloader() |
||
| 496 | |||
| 497 | /** |
||
| 498 | * @param ClassLoader $autoloader |
||
| 499 | */ |
||
| 500 | public function setAutoloader(ClassLoader $autoloader) |
||
| 501 | { |
||
| 502 | $this->autoloader = $autoloader; |
||
| 503 | } |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Get config array |
||
| 507 | * |
||
| 508 | * Specify one key per parameter to traverse the config. Then returns null |
||
| 509 | * if the path of the key(s) can not be obtained. |
||
| 510 | * |
||
| 511 | * @param string|int $key ... (optional) |
||
| 512 | * |
||
| 513 | * @return array|null |
||
| 514 | */ |
||
| 515 | public function getConfig($key = null) |
||
| 516 | { |
||
| 517 | $array = $this->config->getConfig(); |
||
| 518 | |||
| 519 | $keys = func_get_args(); |
||
| 520 | foreach ($keys as $key) { |
||
| 521 | if (null === $key) { |
||
| 522 | continue; |
||
| 523 | } |
||
| 524 | if (!isset($array[$key])) { |
||
| 525 | return null; |
||
| 526 | } |
||
| 527 | $array = $array[$key]; |
||
| 528 | } |
||
| 529 | |||
| 530 | return $array; |
||
| 531 | } |
||
| 532 | |||
| 533 | /** |
||
| 534 | * @param array $config |
||
| 535 | */ |
||
| 536 | public function setConfig($config) |
||
| 537 | { |
||
| 538 | $this->config->setConfig($config); |
||
| 539 | } |
||
| 540 | |||
| 541 | /** |
||
| 542 | * @return boolean |
||
| 543 | */ |
||
| 544 | public function isMagerunStopFileFound() |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Runs the current application with possible command aliases |
||
| 551 | * |
||
| 552 | * @param InputInterface $input An Input instance |
||
| 553 | * @param OutputInterface $output An Output instance |
||
| 554 | * |
||
| 555 | * @return integer 0 if everything went fine, or an error code |
||
| 556 | */ |
||
| 557 | public function doRun(InputInterface $input, OutputInterface $output) |
||
| 558 | { |
||
| 559 | $event = new Application\Console\Event($this, $input, $output); |
||
| 560 | $this->dispatcher->dispatch(Events::RUN_BEFORE, $event); |
||
| 561 | |||
| 562 | /** |
||
| 563 | * only for compatibility to old versions. |
||
| 564 | */ |
||
| 565 | $event = new ConsoleEvent(new Command('dummy'), $input, $output); |
||
| 566 | $this->dispatcher->dispatch('console.run.before', $event); |
||
| 567 | |||
| 568 | $input = $this->config->checkConfigCommandAlias($input); |
||
| 569 | if ($output instanceof ConsoleOutput) { |
||
| 570 | $this->checkVarDir($output->getErrorOutput()); |
||
| 571 | } |
||
| 572 | |||
| 573 | return parent::doRun($input, $output); |
||
| 574 | } |
||
| 575 | |||
| 576 | /** |
||
| 577 | * @param InputInterface $input [optional] |
||
| 578 | * @param OutputInterface $output [optional] |
||
| 579 | * |
||
| 580 | * @return int |
||
| 581 | */ |
||
| 582 | public function run(InputInterface $input = null, OutputInterface $output = null) |
||
| 583 | { |
||
| 584 | if (null === $input) { |
||
| 585 | $input = new ArgvInput(); |
||
| 586 | } |
||
| 587 | |||
| 588 | if (null === $output) { |
||
| 589 | $output = new ConsoleOutput(); |
||
| 590 | } |
||
| 591 | $this->_addOutputStyles($output); |
||
| 592 | if ($output instanceof ConsoleOutput) { |
||
| 593 | $this->_addOutputStyles($output->getErrorOutput()); |
||
| 594 | } |
||
| 595 | |||
| 596 | $this->configureIO($input, $output); |
||
| 597 | |||
| 598 | try { |
||
| 599 | $this->init(array(), $input, $output); |
||
| 600 | } catch (Exception $e) { |
||
| 601 | $output = new ConsoleOutput(); |
||
| 602 | $this->renderException($e, $output->getErrorOutput()); |
||
| 603 | } |
||
| 604 | |||
| 605 | $return = parent::run($input, $output); |
||
| 606 | |||
| 607 | // Fix for no return values -> used in interactive shell to prevent error output |
||
| 608 | if ($return === null) { |
||
| 609 | return 0; |
||
| 610 | } |
||
| 611 | |||
| 612 | return $return; |
||
| 613 | } |
||
| 614 | |||
| 615 | /** |
||
| 616 | * @param array $initConfig [optional] |
||
| 617 | * @param InputInterface $input [optional] |
||
| 618 | * @param OutputInterface $output [optional] |
||
| 619 | * |
||
| 620 | * @return void |
||
| 621 | */ |
||
| 622 | public function init(array $initConfig = array(), InputInterface $input = null, OutputInterface $output = null) |
||
| 623 | { |
||
| 624 | if ($this->_isInitialized) { |
||
| 625 | return; |
||
| 626 | } |
||
| 627 | |||
| 628 | // Suppress DateTime warnings |
||
| 629 | date_default_timezone_set(@date_default_timezone_get()); |
||
| 630 | |||
| 631 | // Initialize EventDispatcher early |
||
| 632 | $this->dispatcher = new EventDispatcher(); |
||
| 633 | $this->setDispatcher($this->dispatcher); |
||
| 634 | |||
| 635 | if (null === $input) { |
||
| 636 | $input = new ArgvInput(); |
||
| 637 | } |
||
| 638 | |||
| 639 | if (null === $output) { |
||
| 640 | $output = new ConsoleOutput(); |
||
| 641 | } |
||
| 642 | |||
| 643 | if (null !== $this->config) { |
||
| 644 | throw new UnexpectedValueException(sprintf('Config already initialized')); |
||
| 645 | } |
||
| 646 | |||
| 647 | $loadExternalConfig = !$this->_checkSkipConfigOption($input); |
||
| 648 | |||
| 649 | $this->config = $config = new Config($initConfig, $this->isPharMode(), $output); |
||
| 650 | if ($this->configurationLoaderInjected) { |
||
| 651 | $config->setLoader($this->configurationLoaderInjected); |
||
| 652 | } |
||
| 653 | $config->loadPartialConfig($loadExternalConfig); |
||
| 654 | $this->detectMagento($input, $output); |
||
| 655 | $configLoader = $config->getLoader(); |
||
| 656 | $configLoader->loadStageTwo($this->_magentoRootFolder, $loadExternalConfig, $this->_magerunStopFileFolder); |
||
| 657 | $config->load(); |
||
| 658 | |||
| 659 | if ($autoloader = $this->autoloader) { |
||
| 660 | $config->registerCustomAutoloaders($autoloader); |
||
| 661 | $this->registerEventSubscribers(); |
||
| 662 | $config->registerCustomCommands($this); |
||
| 663 | } |
||
| 664 | |||
| 665 | $this->registerHelpers(); |
||
| 666 | |||
| 667 | $this->_isInitialized = true; |
||
| 668 | } |
||
| 669 | |||
| 670 | /** |
||
| 671 | * @param array $initConfig [optional] |
||
| 672 | * @param InputInterface $input [optional] |
||
| 673 | * @param OutputInterface $output [optional] |
||
| 674 | */ |
||
| 675 | public function reinit($initConfig = array(), InputInterface $input = null, OutputInterface $output = null) |
||
| 676 | { |
||
| 677 | $this->_isInitialized = false; |
||
| 678 | $this->_magentoDetected = false; |
||
| 679 | $this->_magentoRootFolder = null; |
||
| 680 | $this->config = null; |
||
| 681 | $this->init($initConfig, $input, $output); |
||
| 682 | } |
||
| 683 | |||
| 684 | /** |
||
| 685 | * @return void |
||
| 686 | */ |
||
| 687 | protected function registerEventSubscribers() |
||
| 688 | { |
||
| 689 | $config = $this->config->getConfig(); |
||
| 690 | $subscriberClasses = $config['event']['subscriber']; |
||
| 691 | foreach ($subscriberClasses as $subscriberClass) { |
||
| 692 | $subscriber = new $subscriberClass(); |
||
| 693 | $this->dispatcher->addSubscriber($subscriber); |
||
| 694 | } |
||
| 695 | } |
||
| 696 | |||
| 697 | /** |
||
| 698 | * @param InputInterface $input |
||
| 699 | * @return bool |
||
| 700 | */ |
||
| 701 | protected function _checkSkipConfigOption(InputInterface $input) |
||
| 702 | { |
||
| 703 | return $input->hasParameterOption('--skip-config'); |
||
| 704 | } |
||
| 705 | |||
| 706 | /** |
||
| 707 | * @param InputInterface $input |
||
| 708 | * @return string |
||
| 709 | */ |
||
| 710 | protected function _checkRootDirOption(InputInterface $input) |
||
| 711 | { |
||
| 712 | $rootDir = $input->getParameterOption('--root-dir'); |
||
| 713 | if (is_string($rootDir)) { |
||
| 714 | $this->setRootDir($rootDir); |
||
| 715 | } |
||
| 716 | } |
||
| 717 | |||
| 718 | /** |
||
| 719 | * Set root dir (chdir()) of magento directory |
||
| 720 | * |
||
| 721 | * @param string $path to Magento directory |
||
| 722 | */ |
||
| 723 | private function setRootDir($path) |
||
| 724 | { |
||
| 725 | if (isset($path[0]) && '~' === $path[0]) { |
||
| 726 | $path = OperatingSystem::getHomeDir() . substr($path, 1); |
||
| 727 | } |
||
| 728 | |||
| 729 | $folder = realpath($path); |
||
| 730 | $this->_directRootDir = true; |
||
| 731 | if (is_dir($folder)) { |
||
| 732 | chdir($folder); |
||
| 733 | } |
||
| 734 | } |
||
| 735 | |||
| 736 | /** |
||
| 737 | * use require-once inside a function with it's own variable scope w/o any other variables |
||
| 738 | * and $this unbound. |
||
| 739 | * |
||
| 740 | * @param string $path |
||
| 741 | */ |
||
| 742 | private function requireOnce($path) |
||
| 743 | { |
||
| 744 | $requireOnce = function () { |
||
| 745 | require_once func_get_arg(0); |
||
| 746 | }; |
||
| 747 | if (50400 <= PHP_VERSION_ID) { |
||
| 748 | $requireOnce->bindTo(null); |
||
| 749 | } |
||
| 750 | |||
| 751 | $requireOnce($path); |
||
| 752 | } |
||
| 753 | |||
| 754 | /** |
||
| 755 | * @param bool $soft |
||
| 756 | * |
||
| 757 | * @return void |
||
| 758 | */ |
||
| 759 | protected function _initMagento1($soft = false) |
||
| 760 | { |
||
| 761 | if (!class_exists('Mage', false)) { |
||
| 762 | // Create a new AutoloadRestorer to capture currenjt auto-öpaders |
||
| 763 | $restorer = new AutoloadRestorer(); |
||
| 764 | // require app/Mage.php from Magento in a function of it's own to have it's own variable scope |
||
| 765 | $this->requireOnce($this->_magentoRootFolder . '/app/Mage.php'); |
||
| 766 | // Restore auto-loaders that might be removed by extensions that overwrite Varien/Autoload |
||
| 767 | $restorer->restore(); |
||
| 768 | } |
||
| 769 | |||
| 770 | // skip Mage::app init routine and return |
||
| 771 | if ($soft === true) { |
||
| 772 | return; |
||
| 773 | } |
||
| 774 | |||
| 775 | $config = $this->config->getConfig(); |
||
| 776 | $initSettings = $config['init']; |
||
| 777 | |||
| 778 | Mage::app($initSettings['code'], $initSettings['type'], $initSettings['options']); |
||
| 779 | } |
||
| 780 | |||
| 781 | /** |
||
| 782 | * @return void |
||
| 783 | */ |
||
| 784 | protected function _initMagento2() |
||
| 785 | { |
||
| 786 | $this->outputMagerunCompatibilityNotice('2'); |
||
| 787 | } |
||
| 788 | |||
| 789 | /** |
||
| 790 | * Show a hint that this is Magento incompatible with Magerun and how to obtain the correct Magerun for it |
||
| 791 | * |
||
| 792 | * @param string $version of Magento, "1" or "2", that is incompatible |
||
| 793 | */ |
||
| 794 | private function outputMagerunCompatibilityNotice($version) |
||
| 795 | { |
||
| 796 | $file = $version === '2' ? $version : ''; |
||
| 797 | $magentoHint = <<<MAGENTOHINT |
||
| 798 | You are running a Magento $version.x instance. This version of n98-magerun is not compatible |
||
| 799 | with Magento $version.x. Please use n98-magerun$version (version $version) for this shop. |
||
| 800 | |||
| 801 | A current version of the software can be downloaded on github. |
||
| 802 | |||
| 803 | <info>Download with curl |
||
| 804 | ------------------</info> |
||
| 805 | |||
| 806 | <comment>curl -O https://files.magerun.net/n98-magerun$file.phar</comment> |
||
| 807 | |||
| 808 | <info>Download with wget |
||
| 809 | ------------------</info> |
||
| 810 | |||
| 811 | <comment>wget https://files.magerun.net/n98-magerun$file.phar</comment> |
||
| 812 | |||
| 813 | MAGENTOHINT; |
||
| 814 | |||
| 815 | $output = new ConsoleOutput(); |
||
| 816 | |||
| 817 | /** @var $formatter FormatterHelper */ |
||
| 818 | $formatter = $this->getHelperSet()->get('formatter'); |
||
| 819 | |||
| 820 | $output->writeln(array( |
||
| 821 | '', |
||
| 822 | $formatter->formatBlock('Compatibility Notice', 'bg=blue;fg=white', true), |
||
| 823 | '', |
||
| 824 | $magentoHint, |
||
| 825 | )); |
||
| 826 | |||
| 827 | throw new RuntimeException('This version of n98-magerun is not compatible with Magento ' . $version); |
||
| 828 | } |
||
| 829 | |||
| 830 | /** |
||
| 831 | * @return EventDispatcher |
||
| 832 | */ |
||
| 833 | public function getDispatcher() |
||
| 834 | { |
||
| 835 | return $this->dispatcher; |
||
| 836 | } |
||
| 837 | |||
| 838 | /** |
||
| 839 | * @param array $initConfig |
||
| 840 | * @param OutputInterface $output |
||
| 841 | * @return ConfigurationLoader |
||
| 842 | */ |
||
| 843 | public function getConfigurationLoader(array $initConfig, OutputInterface $output) |
||
| 844 | { |
||
| 845 | trigger_error(__METHOD__ . ' moved, use getConfig()->getLoader()', E_USER_DEPRECATED); |
||
| 846 | |||
| 847 | unset($initConfig, $output); |
||
| 848 | |||
| 849 | $loader = $this->config ? $this->config->getLoader() : $this->configurationLoaderInjected; |
||
| 850 | |||
| 851 | if (!$loader) { |
||
| 852 | throw new RuntimeException('ConfigurationLoader is not yet available, initialize it or Config first'); |
||
| 853 | } |
||
| 854 | |||
| 855 | return $loader; |
||
| 856 | } |
||
| 857 | |||
| 858 | /** |
||
| 859 | * @param ConfigurationLoader $configurationLoader |
||
| 860 | * |
||
| 861 | * @return $this |
||
| 862 | */ |
||
| 863 | public function setConfigurationLoader(ConfigurationLoader $configurationLoader) |
||
| 864 | { |
||
| 865 | if ($this->config) { |
||
| 866 | $this->config->setLoader($configurationLoader); |
||
| 867 | } else { |
||
| 868 | /* inject loader to be used later when config is created in */ |
||
| 869 | /* @see N98\Magento\Application::init */ |
||
| 870 | $this->configurationLoaderInjected = $configurationLoader; |
||
| 871 | } |
||
| 872 | |||
| 873 | return $this; |
||
| 874 | } |
||
| 875 | |||
| 876 | /** |
||
| 877 | * @param OutputInterface $output |
||
| 878 | */ |
||
| 879 | protected function _addOutputStyles(OutputInterface $output) |
||
| 884 | } |
||
| 885 |