Passed
Push — master ( 972297...322a58 )
by Robbie
02:26
created

Bootstrap::defineHttpHost()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 2
1
<?php
2
3
namespace SilverLeague\Console\Framework;
4
5
use SilverStripe\ORM\DB;
6
7
/**
8
 * Loads and configures SilverStripe
9
 *
10
 * @package silverstripe-console
11
 * @author  Robbie Averill <[email protected]>
12
 */
13
class Bootstrap
14
{
15
    /**
16 3
     * Ensure SilverStripe is loaded and configured
17
     */
18 3
    public function initialize()
19
    {
20
        if (!$this->findSilverStripe()) {
21
            echo 'A SilverStripe installation could not be found. Please run ssconsole from your '
22
                . 'SilverStripe root.', PHP_EOL;
23 3
            exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method initialize() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
24 3
        }
25
        $this->getDb();
26
    }
27
28
    /**
29
     * Find and require SilverStripe. This will look in:
30
     *
31
     * - The current working directory (for when installed globally with composer)
32
     * - The next directory up (for when installed locally into a SilverStripe project)
33
     * - The console's "silverstripe" directory (for when installed in a build process)
34
     *
35 3
     * @return bool
36
     */
37 3
    protected function findSilverStripe()
38 3
    {
39 3
        foreach ([getcwd(), CONSOLE_BASE_DIR . '/../', CONSOLE_BASE_DIR . '/silverstripe'] as $rootFolder) {
40 3
            if (file_exists($rootFolder . '/framework/src/Core/Core.php')) {
41 3
                define('SILVERSTRIPE_ROOT_DIR', $rootFolder);
42 3
43
                require_once $rootFolder . '/vendor/autoload.php';
44
                require_once $rootFolder . '/framework/src/Core/Core.php';
45
                return true;
46
            }
47
        }
48
        return false;
49
    }
50
51
    /**
52
     * Get the SilverStripe DB connector
53 3
     *
54
     * @return $this
55 3
     */
56 3
    protected function getDb()
57 3
    {
58
        global $databaseConfig;
59 3
        if ($databaseConfig) {
60
            DB::connect($databaseConfig);
61
        }
62
        return $this;
63
    }
64
}
65