Completed
Push — master ( 0c8d1b...0c71fb )
by Tom
06:41 queued 02:14
created

MagerunBootstrap::includeIfExists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
/**
3
 * this file is part of magerun
4
 *
5
 * @author Tom Klingenberg <https://github.com/ktomk>
6
 */
7
namespace N98;
8
9
use Composer\Autoload\ClassLoader;
10
use ErrorException;
11
12
class MagerunBootstrap
13
{
14
    /**
15
     * @param ClassLoader|null $loader [optional]
16
     * @return Magento\Application
17
     * @throws ErrorException
18
     */
19
    public static function createApplication(ClassLoader $loader = null)
20
    {
21
        if (null === $loader) {
22
            $loader = self::getLoader();
23
        }
24
25
        $application = new Magento\Application($loader);
26
27
        return $application;
28
    }
29
30
    /**
31
     * @throws ErrorException
32
     * @return ClassLoader
33
     */
34
    public static function getLoader()
35
    {
36
        if (
37
            !($loader = self::includeIfExists(__DIR__ . '/../../vendor/autoload.php'))
38
            && !($loader = self::includeIfExists(__DIR__ . '/../../../../autoload.php'))
39
        ) {
40
            throw new ErrorException(
41
                'You must set up the project dependencies, run the following commands:' . PHP_EOL .
42
                'curl -s http://getcomposer.org/installer | php' . PHP_EOL .
43
                'php composer.phar install' . PHP_EOL
44
            );
45
        }
46
47
        return $loader;
48
    }
49
50
    /**
51
     * @param string $file
52
     * @return mixed
53
     */
54
    public static function includeIfExists($file)
55
    {
56
        if (file_exists($file)) {
57
            return include $file;
58
        }
59
    }
60
}
61