Issues (244)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/N98/Magento/Application.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace N98\Magento;
4
5
use Composer\Autoload\ClassLoader;
6
use Exception;
7
use Mage;
8
use Magento\Mtf\EntryPoint\EntryPoint;
9
use N98\Magento\Application\Config;
10
use N98\Magento\Application\ConfigurationLoader;
11
use N98\Magento\Application\Console\Events;
12
use N98\Util\Console\Helper\MagentoHelper;
13
use N98\Util\OperatingSystem;
14
use RuntimeException;
15
use Symfony\Component\Console\Application as BaseApplication;
16
use Symfony\Component\Console\Command\Command;
17
use Symfony\Component\Console\Event\ConsoleEvent;
18
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
19
use Symfony\Component\Console\Helper\FormatterHelper;
20
use Symfony\Component\Console\Input\ArgvInput;
21
use Symfony\Component\Console\Input\InputDefinition;
22
use Symfony\Component\Console\Input\InputInterface;
23
use Symfony\Component\Console\Input\InputOption;
24
use Symfony\Component\Console\Output\ConsoleOutput;
25
use Symfony\Component\Console\Output\OutputInterface;
26
use Symfony\Component\EventDispatcher\EventDispatcher;
27
use UnexpectedValueException;
28
29
class Application extends BaseApplication
30
{
31
    /**
32
     * @var string
33
     */
34
    const APP_NAME = 'n98-magerun';
35
36
    /**
37
     * @var string
38
     */
39
    const APP_VERSION = '1.101.1';
40
41
    /**
42
     * @var int
43
     */
44
    const MAGENTO_MAJOR_VERSION_1 = 1;
45
46
    /**
47
     * @var int
48
     */
49
    const MAGENTO_MAJOR_VERSION_2 = 2;
50
51
    /**
52
     * @var string
53
     */
54
    private static $logo = "
55
     ___ ___
56
 _ _/ _ ( _ )___ _ __  __ _ __ _ ___ _ _ _  _ _ _
57
| ' \\_, / _ \\___| '  \\/ _` / _` / -_) '_| || | ' \\
58
|_||_/_/\\___/   |_|_|_\\__,_\\__, \\___|_|  \\_,_|_||_|
59
                           |___/
60
";
61
62
    /**
63
     * Shadow copy of the Application parent when using this concrete setAutoExit() implementation
64
     *
65
     * @see \Symfony\Component\Console\Application::$autoExit
66
     * @var bool
67
     */
68
    private $autoExitShadow = true;
69
70
    /**
71
     * @var ClassLoader
72
     */
73
    protected $autoloader;
74
75
    /**
76
     * @var Config
77
     */
78
    protected $config;
79
80
    /**
81
     * @see \N98\Magento\Application::setConfigurationLoader()
82
     * @var ConfigurationLoader
83
     */
84
    private $configurationLoaderInjected;
85
86
    /**
87
     * @var string
88
     */
89
    protected $_magentoRootFolder = null;
90
91
    /**
92
     * @var bool
93
     */
94
    protected $_magentoEnterprise = false;
95
96
    /**
97
     * @var int
98
     */
99
    protected $_magentoMajorVersion = self::MAGENTO_MAJOR_VERSION_1;
100
101
    /**
102
     * @var EntryPoint
103
     */
104
    protected $_magento2EntryPoint = null;
105
106
    /**
107
     * @var bool
108
     */
109
    protected $_isPharMode = false;
110
111
    /**
112
     * @var bool
113
     */
114
    protected $_magerunStopFileFound = false;
115
116
    /**
117
     * @var string
118
     */
119
    protected $_magerunStopFileFolder = null;
120
121
    /**
122
     * @var null
123
     */
124
    protected $_magerunUseDeveloperMode = null;
125
126
    /**
127
     * @var bool
128
     */
129
    protected $_isInitialized = false;
130
131
    /**
132
     * @var EventDispatcher
133
     */
134
    protected $dispatcher;
135
136
    /**
137
     * If root dir is set by root-dir option this flag is true
138
     *
139
     * @var bool
140
     */
141
    protected $_directRootDir = false;
142
143
    /**
144
     * @var bool
145
     */
146
    protected $_magentoDetected = false;
147
148
    /**
149
     * @param ClassLoader $autoloader
150
     */
151
    public function __construct($autoloader = null)
152
    {
153
        $this->autoloader = $autoloader;
154
        parent::__construct(self::APP_NAME, self::APP_VERSION);
155
    }
156
157
    /**
158
     * @param bool $boolean
159
     * @return bool previous auto-exit state
160
     */
161
    public function setAutoExit($boolean)
162
    {
163
        $previous = $this->autoExitShadow;
164
        $this->autoExitShadow = $boolean;
165
        parent::setAutoExit($boolean);
166
167
        return $previous;
168
    }
169
170
    /**
171
     * @return InputDefinition
172
     */
173
    protected function getDefaultInputDefinition()
174
    {
175
        $inputDefinition = parent::getDefaultInputDefinition();
176
177
        /**
178
         * Root dir
179
         */
180
        $rootDirOption = new InputOption(
181
            '--root-dir',
182
            '',
183
            InputOption::VALUE_OPTIONAL,
184
            'Force magento root dir. No auto detection'
185
        );
186
        $inputDefinition->addOption($rootDirOption);
187
188
        /**
189
         * Skip config
190
         */
191
        $skipExternalConfig = new InputOption(
192
            '--skip-config',
193
            '',
194
            InputOption::VALUE_NONE,
195
            'Do not load any custom config.'
196
        );
197
        $inputDefinition->addOption($skipExternalConfig);
198
199
        /**
200
         * Skip root check
201
         */
202
        $skipExternalConfig = new InputOption(
203
            '--skip-root-check',
204
            '',
205
            InputOption::VALUE_NONE,
206
            'Do not check if n98-magerun runs as root'
207
        );
208
        $inputDefinition->addOption($skipExternalConfig);
209
210
        /**
211
         * Developer Mode
212
         */
213
        $rootDirOption = new InputOption(
214
            '--developer-mode',
215
            '',
216
            InputOption::VALUE_NONE,
217
            'Instantiate Magento in Developer Mode'
218
        );
219
        $inputDefinition->addOption($rootDirOption);
220
221
        return $inputDefinition;
222
    }
223
224
    /**
225
     * Search for magento root folder
226
     *
227
     * @param InputInterface $input [optional]
228
     * @param OutputInterface $output [optional]
229
     * @return void
230
     */
231
    public function detectMagento(InputInterface $input = null, OutputInterface $output = null)
232
    {
233
        // do not detect magento twice
234
        if ($this->_magentoDetected) {
235
            return;
236
        }
237
238
        if (null === $input) {
239
            $input = new ArgvInput();
240
        }
241
242
        if (null === $output) {
243
            $output = new ConsoleOutput();
244
        }
245
246
        if ($this->getMagentoRootFolder() === null) {
247
            $this->_checkRootDirOption($input);
248
            $folder = OperatingSystem::getCwd();
249
        } else {
250
            $folder = $this->getMagentoRootFolder();
251
        }
252
253
        $this->getHelperSet()->set(new MagentoHelper($input, $output), 'magento');
254
        $magentoHelper = $this->getHelperSet()->get('magento');
255
        /* @var $magentoHelper MagentoHelper */
256
        if (!$this->_directRootDir) {
257
            $subFolders = $this->config->getDetectSubFolders();
258
        } else {
259
            $subFolders = array();
260
        }
261
262
        $this->_magentoDetected = $magentoHelper->detect($folder, $subFolders);
263
        $this->_magentoRootFolder = $magentoHelper->getRootFolder();
264
        $this->_magentoEnterprise = $magentoHelper->isEnterpriseEdition();
265
        $this->_magentoMajorVersion = $magentoHelper->getMajorVersion();
266
        $this->_magerunStopFileFound = $magentoHelper->isMagerunStopFileFound();
267
        $this->_magerunStopFileFolder = $magentoHelper->getMagerunStopFileFolder();
268
        $this->_magerunUseDeveloperMode = ($input->getParameterOption('--developer-mode'));
269
    }
270
271
    /**
272
     * Add own helpers to helperset.
273
     *
274
     * @return void
275
     */
276
    protected function registerHelpers()
277
    {
278
        $helperSet = $this->getHelperSet();
279
        $config = $this->config->getConfig();
280
281
        foreach ($config['helpers'] as $helperName => $helperClass) {
282
            if (!class_exists($helperClass)) {
283
                throw new RuntimeException(
284
                    sprintf('Nonexistent helper class: "%s", check helpers configuration', $helperClass)
285
                );
286
            }
287
288
            // Twig helper needs the config-file
289
            $helper = 'N98\Util\Console\Helper\TwigHelper' === $helperClass
290
                ? new $helperClass($this->config)
291
                : new $helperClass()
292
            ;
293
            $helperSet->set($helper, $helperName);
294
        }
295
    }
296
297
    /**
298
     * @param InputInterface $input
299
     *
300
     * @return ArgvInput|InputInterface
301
     */
302
    protected function checkConfigCommandAlias(InputInterface $input)
303
    {
304
        trigger_error(__METHOD__ . ' moved, use getConfig()->checkConfigCommandAlias()', E_USER_DEPRECATED);
305
306
        return $this->config->checkConfigCommandAlias($input);
307
    }
308
309
    /**
310
     * @param Command $command
311
     */
312
    protected function registerConfigCommandAlias(Command $command)
313
    {
314
        trigger_error(__METHOD__ . ' moved, use getConfig()->registerConfigCommandAlias() instead', E_USER_DEPRECATED);
315
316
        return $this->config->registerConfigCommandAlias($command);
317
    }
318
319
    /**
320
     * Adds autoloader prefixes from user's config
321
     */
322
    protected function registerCustomAutoloaders()
323
    {
324
        trigger_error(__METHOD__ . ' moved, use getConfig()->registerCustomAutoloaders() instead', E_USER_DEPRECATED);
325
326
        $this->config->registerCustomAutoloaders($this->autoloader);
327
    }
328
329
    /**
330
     * @return bool
331
     */
332
    protected function hasCustomCommands()
333
    {
334
        trigger_error(__METHOD__ . ' moved, use config directly instead', E_USER_DEPRECATED);
335
336
        return 0 < count($this->config->getConfig(array('commands', 'customCommands')));
337
    }
338
339
    /**
340
     * @return void
341
     */
342
    protected function registerCustomCommands()
343
    {
344
        trigger_error(__METHOD__ . ' moved, use getConfig()->registerCustomCommands() instead', E_USER_DEPRECATED);
345
346
        $this->config->registerCustomCommands($this);
347
    }
348
349
    /**
350
     * @param string $class
351
     * @return bool
352
     */
353
    protected function isCommandDisabled($class)
354
    {
355
        trigger_error(__METHOD__ . ' moved, use config directly instead', E_USER_DEPRECATED);
356
357
        $config = $this->config->getConfig();
358
359
        return in_array($class, $config['commands']['disabled']);
360
    }
361
362
    /**
363
     * Override standard command registration. We want alias support.
364
     *
365
     * @param Command $command
366
     *
367
     * @return Command
368
     */
369
    public function add(Command $command)
370
    {
371
        if ($this->config) {
372
            $this->config->registerConfigCommandAlias($command);
373
        }
374
375
        return parent::add($command);
376
    }
377
378
    /**
379
     * @param bool $mode
380
     */
381
    public function setPharMode($mode)
382
    {
383
        $this->_isPharMode = $mode;
384
    }
385
386
    /**
387
     * @return bool
388
     */
389
    public function isPharMode()
390
    {
391
        return $this->_isPharMode;
392
    }
393
394
    /**
395
     * @TODO Move logic into "EventSubscriber"
396
     *
397
     * @param OutputInterface $output
398
     * @return null|false
399
     */
400
    public function checkVarDir(OutputInterface $output)
401
    {
402
        $tempVarDir = sys_get_temp_dir() . '/magento/var';
403
        if (!OutputInterface::VERBOSITY_NORMAL <= $output->getVerbosity() && !is_dir($tempVarDir)) {
404
            return;
405
        }
406
407
        $this->detectMagento(null, $output);
408
        /* If magento is not installed yet, don't check */
409
        if ($this->_magentoRootFolder === null
410
            || !file_exists($this->_magentoRootFolder . '/app/etc/local.xml')
411
        ) {
412
            return;
413
        }
414
415
        try {
416
            $this->initMagento();
417
        } catch (Exception $e) {
418
            $message = 'Cannot initialize Magento. Please check your configuration. '
419
                . 'Some n98-magerun command will not work. Got message: ';
420
            if (OutputInterface::VERBOSITY_VERY_VERBOSE <= $output->getVerbosity()) {
421
                $message .= $e->getTraceAsString();
422
            } else {
423
                $message .= $e->getMessage();
424
            }
425
            $output->writeln($message);
426
427
            return;
428
        }
429
430
        $configOptions = new \Mage_Core_Model_Config_Options();
431
        $currentVarDir = $configOptions->getVarDir();
432
433
        if ($currentVarDir == $tempVarDir) {
434
            $output->writeln(array(
435
                sprintf('<warning>Fallback folder %s is used in n98-magerun</warning>', $tempVarDir),
436
                '',
437
                'n98-magerun is using the fallback folder. If there is another folder configured for Magento, this ' .
438
                'can cause serious problems.',
439
                'Please refer to https://github.com/netz98/n98-magerun/wiki/File-system-permissions ' .
440
                'for more information.',
441
                '',
442
            ));
443
        } else {
444
            $output->writeln(array(
445
                sprintf('<warning>Folder %s found, but not used in n98-magerun</warning>', $tempVarDir),
446
                '',
447
                "This might cause serious problems. n98-magerun is using the configured var-folder " .
448
                "<comment>$currentVarDir</comment>",
449
                'Please refer to https://github.com/netz98/n98-magerun/wiki/File-system-permissions ' .
450
                'for more information.',
451
                '',
452
            ));
453
454
            return false;
455
        }
456
    }
457
458
    /**
459
     * Loads and initializes the Magento application
460
     *
461
     * @param bool $soft
462
     *
463
     * @return bool false if magento root folder is not set, true otherwise
464
     */
465
    public function initMagento($soft = false)
466
    {
467
        if ($this->getMagentoRootFolder() === null) {
468
            return false;
469
        }
470
471
        $isMagento2 = $this->_magentoMajorVersion === self::MAGENTO_MAJOR_VERSION_2;
472
        if ($isMagento2) {
473
            $this->_initMagento2();
474
        } else {
475
            $this->_initMagento1($soft);
476
        }
477
478
        return true;
479
    }
480
481
    /**
482
     * @return string
483
     */
484
    public function getHelp()
485
    {
486
        return self::$logo . parent::getHelp();
487
    }
488
489
    public function getLongVersion()
490
    {
491
        return parent::getLongVersion() . ' by <info>netz98 GmbH</info>';
492
    }
493
494
    /**
495
     * @return boolean
496
     */
497
    public function isMagentoEnterprise()
498
    {
499
        return $this->_magentoEnterprise;
500
    }
501
502
    /**
503
     * @return string
504
     */
505
    public function getMagentoRootFolder()
506
    {
507
        return $this->_magentoRootFolder;
508
    }
509
510
    /**
511
     * @param string $magentoRootFolder
512
     */
513
    public function setMagentoRootFolder($magentoRootFolder)
514
    {
515
        $this->_magentoRootFolder = $magentoRootFolder;
516
    }
517
518
    /**
519
     * @return int
520
     */
521
    public function getMagentoMajorVersion()
522
    {
523
        return $this->_magentoMajorVersion;
524
    }
525
526
    /**
527
     * @return ClassLoader
528
     */
529
    public function getAutoloader()
530
    {
531
        return $this->autoloader;
532
    }
533
534
    /**
535
     * @param ClassLoader $autoloader
536
     */
537
    public function setAutoloader(ClassLoader $autoloader)
538
    {
539
        $this->autoloader = $autoloader;
540
    }
541
542
    /**
543
     * Get config array
544
     *
545
     * Specify one key per parameter to traverse the config. Then returns null
546
     * if the path of the key(s) can not be obtained.
547
     *
548
     * @param string|int $key ... (optional)
549
     *
550
     * @return array|null
551
     */
552
    public function getConfig($key = null)
0 ignored issues
show
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
553
    {
554
        $array = $this->config->getConfig();
555
556
        $keys = func_get_args();
557
        foreach ($keys as $key) {
558
            if (null === $key) {
559
                continue;
560
            }
561
            if (!isset($array[$key])) {
562
                return null;
563
            }
564
            $array = $array[$key];
565
        }
566
567
        return $array;
568
    }
569
570
    /**
571
     * @param array $config
572
     */
573
    public function setConfig($config)
574
    {
575
        $this->config->setConfig($config);
576
    }
577
578
    /**
579
     * @return boolean
580
     */
581
    public function isMagerunStopFileFound()
582
    {
583
        return $this->_magerunStopFileFound;
584
    }
585
586
    /**
587
     * Runs the current application with possible command aliases
588
     *
589
     * @param InputInterface $input An Input instance
590
     * @param OutputInterface $output An Output instance
591
     *
592
     * @return integer 0 if everything went fine, or an error code
593
     */
594
    public function doRun(InputInterface $input, OutputInterface $output)
595
    {
596
        $event = new Application\Console\Event($this, $input, $output);
597
        $this->dispatcher->dispatch(Events::RUN_BEFORE, $event);
598
599
        /**
600
         * only for compatibility to old versions.
601
         */
602
        $event = new ConsoleEvent(new Command('dummy'), $input, $output);
603
        $this->dispatcher->dispatch('console.run.before', $event);
604
605
        $input = $this->config->checkConfigCommandAlias($input);
606
        if ($output instanceof ConsoleOutput) {
607
            $this->checkVarDir($output->getErrorOutput());
608
        }
609
610
        return parent::doRun($input, $output);
611
    }
612
613
    /**
614
     * @param InputInterface $input [optional]
615
     * @param OutputInterface $output [optional]
616
     *
617
     * @return int
618
     */
619
    public function run(InputInterface $input = null, OutputInterface $output = null)
620
    {
621
        if (null === $input) {
622
            $input = new ArgvInput();
623
        }
624
625
        if (null === $output) {
626
            $output = new ConsoleOutput();
627
        }
628
        $this->_addOutputStyles($output);
629
        if ($output instanceof ConsoleOutput) {
630
            $this->_addOutputStyles($output->getErrorOutput());
631
        }
632
633
        $this->configureIO($input, $output);
634
635
        try {
636
            $this->init(array(), $input, $output);
637
        } catch (Exception $e) {
638
            $output = new ConsoleOutput();
639
            $this->renderException($e, $output->getErrorOutput());
640
        }
641
642
        $return = parent::run($input, $output);
643
644
        // Fix for no return values -> used in interactive shell to prevent error output
645
        if ($return === null) {
646
            return 0;
647
        }
648
649
        return $return;
650
    }
651
652
    /**
653
     * @param array $initConfig [optional]
654
     * @param InputInterface $input [optional]
655
     * @param OutputInterface $output [optional]
656
     *
657
     * @return void
658
     */
659
    public function init(array $initConfig = array(), InputInterface $input = null, OutputInterface $output = null)
660
    {
661
        if ($this->_isInitialized) {
662
            return;
663
        }
664
665
        // Suppress DateTime warnings
666
        date_default_timezone_set(@date_default_timezone_get());
667
668
        // Initialize EventDispatcher early
669
        $this->dispatcher = new EventDispatcher();
670
        $this->setDispatcher($this->dispatcher);
671
672
        $input = $input ?: new ArgvInput();
673
        $output = $output ?: new ConsoleOutput();
674
675
        if (null !== $this->config) {
676
            throw new UnexpectedValueException(sprintf('Config already initialized'));
677
        }
678
679
        $loadExternalConfig = !$input->hasParameterOption('--skip-config');
680
681
        $this->config = $config = new Config($initConfig, $this->isPharMode(), $output);
682
        if ($this->configurationLoaderInjected) {
683
            $config->setLoader($this->configurationLoaderInjected);
684
        }
685
        $config->loadPartialConfig($loadExternalConfig);
686
        $this->detectMagento($input, $output);
687
        $configLoader = $config->getLoader();
688
        $configLoader->loadStageTwo($this->_magentoRootFolder, $loadExternalConfig, $this->_magerunStopFileFolder);
689
        $config->load();
690
691
        if ($autoloader = $this->autoloader) {
692
            $config->registerCustomAutoloaders($autoloader);
693
            $this->registerEventSubscribers();
694
            $config->registerCustomCommands($this);
695
        }
696
697
        $this->registerHelpers();
698
699
        $this->_isInitialized = true;
700
    }
701
702
    /**
703
     * @param array $initConfig [optional]
704
     * @param InputInterface $input [optional]
705
     * @param OutputInterface $output [optional]
706
     */
707
    public function reinit($initConfig = array(), InputInterface $input = null, OutputInterface $output = null)
708
    {
709
        $this->_isInitialized = false;
710
        $this->_magentoDetected = false;
711
        $this->_magentoRootFolder = null;
712
        $this->config = null;
713
        $this->init($initConfig, $input, $output);
714
    }
715
716
    /**
717
     * @return void
718
     */
719
    protected function registerEventSubscribers()
720
    {
721
        $config = $this->config->getConfig();
722
        $subscriberClasses = $config['event']['subscriber'];
723
        foreach ($subscriberClasses as $subscriberClass) {
724
            $subscriber = new $subscriberClass();
725
            $this->dispatcher->addSubscriber($subscriber);
726
        }
727
    }
728
729
    /**
730
     * @param InputInterface $input
731
     * @return bool
732
     * @deprecated 1.97.27
733
     */
734
    protected function _checkSkipConfigOption(InputInterface $input)
735
    {
736
        trigger_error(
737
            __METHOD__ . ' removed, use $input->hasParameterOption(\'--skip-config\') instead',
738
            E_USER_DEPRECATED
739
        );
740
741
        return $input->hasParameterOption('--skip-config');
742
    }
743
744
    /**
745
     * @param InputInterface $input
746
     * @return string
747
     */
748
    protected function _checkRootDirOption(InputInterface $input)
749
    {
750
        $rootDir = $input->getParameterOption('--root-dir');
751
        if (is_string($rootDir)) {
752
            $this->setRootDir($rootDir);
753
        }
754
    }
755
756
    /**
757
     * Set root dir (chdir()) of magento directory
758
     *
759
     * @param string $path to Magento directory
760
     */
761
    private function setRootDir($path)
762
    {
763
        if (isset($path[0]) && '~' === $path[0]) {
764
            $path = OperatingSystem::getHomeDir() . substr($path, 1);
765
        }
766
767
        $folder = realpath($path);
768
        $this->_directRootDir = true;
769
        if (is_dir($folder)) {
770
            chdir($folder);
771
        }
772
    }
773
774
    /**
775
     * @param bool $soft
776
     *
777
     * @return void
778
     */
779
    protected function _initMagento1($soft = false)
780
    {
781
        // Load Mage class definition
782
        Initialiser::bootstrap($this->_magentoRootFolder);
783
784
        // skip Mage::app init routine and return
785
        if ($soft === true) {
786
            return;
787
        }
788
789
        $initSettings = $this->config->getConfig('init');
790
791
        Mage::app($initSettings['code'], $initSettings['type'], $initSettings['options']);
792
        if ($this->_magerunUseDeveloperMode) {
793
            Mage::setIsDeveloperMode(true);
794
        }
795
    }
796
797
    /**
798
     * @return void
799
     */
800
    protected function _initMagento2()
801
    {
802
        $this->outputMagerunCompatibilityNotice('2');
803
    }
804
805
    /**
806
     * Show a hint that this is Magento incompatible with Magerun and how to obtain the correct Magerun for it
807
     *
808
     * @param string $version of Magento, "1" or "2", that is incompatible
809
     */
810
    private function outputMagerunCompatibilityNotice($version)
811
    {
812
        $file = $version === '2' ? $version : '';
813
        $magentoHint = <<<MAGENTOHINT
814
You are running a Magento $version.x instance. This version of n98-magerun is not compatible
815
with Magento $version.x. Please use n98-magerun$version (version $version) for this shop.
816
817
A current version of the software can be downloaded on github.
818
819
<info>Download with curl
820
------------------</info>
821
822
    <comment>curl -O https://files.magerun.net/n98-magerun$file.phar</comment>
823
824
<info>Download with wget
825
------------------</info>
826
827
    <comment>wget https://files.magerun.net/n98-magerun$file.phar</comment>
828
829
MAGENTOHINT;
830
831
        $output = new ConsoleOutput();
832
833
        /** @var $formatter FormatterHelper */
834
        $formatter = $this->getHelperSet()->get('formatter');
835
836
        $output->writeln(array(
837
            '',
838
            $formatter->formatBlock('Compatibility Notice', 'bg=blue;fg=white', true),
839
            '',
840
            $magentoHint,
841
        ));
842
843
        throw new RuntimeException('This version of n98-magerun is not compatible with Magento ' . $version);
844
    }
845
846
    /**
847
     * @return EventDispatcher
848
     */
849
    public function getDispatcher()
850
    {
851
        return $this->dispatcher;
852
    }
853
854
    /**
855
     * @param array $initConfig
856
     * @param OutputInterface $output
857
     * @return ConfigurationLoader
858
     */
859
    public function getConfigurationLoader(array $initConfig, OutputInterface $output)
860
    {
861
        trigger_error(__METHOD__ . ' moved, use getConfig()->getLoader()', E_USER_DEPRECATED);
862
863
        unset($initConfig, $output);
864
865
        $loader = $this->config ? $this->config->getLoader() : $this->configurationLoaderInjected;
866
867
        if (!$loader) {
868
            throw new RuntimeException('ConfigurationLoader is not yet available, initialize it or Config first');
869
        }
870
871
        return $loader;
872
    }
873
874
    /**
875
     * @param ConfigurationLoader $configurationLoader
876
     *
877
     * @return $this
878
     */
879
    public function setConfigurationLoader(ConfigurationLoader $configurationLoader)
880
    {
881
        if ($this->config) {
882
            $this->config->setLoader($configurationLoader);
883
        } else {
884
            /* inject loader to be used later when config is created in */
885
            /* @see N98\Magento\Application::init */
886
            $this->configurationLoaderInjected = $configurationLoader;
887
        }
888
889
        return $this;
890
    }
891
892
    /**
893
     * @param OutputInterface $output
894
     */
895
    protected function _addOutputStyles(OutputInterface $output)
896
    {
897
        $output->getFormatter()->setStyle('debug', new OutputFormatterStyle('magenta', 'white'));
898
        $output->getFormatter()->setStyle('warning', new OutputFormatterStyle('red', 'yellow', array('bold')));
899
    }
900
}
901