Completed
Push — master ( 36a29e...573c79 )
by Christian
02:38
created

HomePathDeterminator::detectHomeDirectory()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
rs 8.5906
cc 5
eloc 12
nc 5
nop 0
1
<?php
2
3
/**
4
 * This file is part of tenside/core.
5
 *
6
 * (c) Christian Schiffler <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * This project is provided in good faith and hope to be usable by anyone.
12
 *
13
 * @package    tenside/core
14
 * @author     Christian Schiffler <[email protected]>
15
 * @copyright  2015 Christian Schiffler <[email protected]>
16
 * @license    https://github.com/tenside/core/blob/master/LICENSE MIT
17
 * @link       https://github.com/tenside/core
18
 * @filesource
19
 */
20
21
namespace Tenside\Core\Util;
22
23
/**
24
 * This class provides information about the home path to use throughout tenside.
25
 */
26
class HomePathDeterminator
27
{
28
    /**
29
     * The cached value.
30
     *
31
     * @var string
32
     */
33
    private $home;
34
35
    /**
36
     * Create a new instance.
37
     *
38
     * @param string $home The home dir if desired to override (null to autodetect).
0 ignored issues
show
Documentation introduced by
Should the type for parameter $home not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
39
     */
40
    public function __construct($home = null)
41
    {
42
        $this->home = $home;
43
    }
44
45
    /**
46
     * Retrieve the home directory.
47
     *
48
     * @return string
49
     */
50
    public function homeDir()
51
    {
52
        if (null !== $this->home) {
53
            return $this->home;
54
        }
55
56
        return $this->home = $this->detectHomeDirectory();
57
    }
58
59
    /**
60
     * Retrieve the directory to store tenside related data.
61
     *
62
     * @return string
63
     */
64
    public function tensideDataDir()
65
    {
66
        return $this->homeDir() . DIRECTORY_SEPARATOR . 'tenside';
67
    }
68
69
    /**
70
     * Determine the correct working directory.
71
     *
72
     * @return string
73
     *
74
     * @throws \RuntimeException When the home directory is not /web.
75
     */
76
    private function detectHomeDirectory()
77
    {
78
        // Environment variable COMPOSER points to the composer.json we should use.
79
        if (false !== ($home = getenv('COMPOSER'))) {
80
            return dirname($home);
81
        }
82
83
        if ('' !== \Phar::running()) {
84
            // Strip scheme "phar://" prefix and "tenside.phar" suffix.
85
            $home = dirname(substr(\Phar::running(), 7));
86
        } else {
87
            $home = getcwd();
88
        }
89
90
        if ((PHP_SAPI !== 'cli') && (substr($home, -4) !== DIRECTORY_SEPARATOR . 'web')) {
91
            throw new \RuntimeException(
92
                'Tenside is intended to be run from within the web directory but it appears you are running it from ' .
93
                basename($home)
94
            );
95
        }
96
97
        return dirname($home);
98
    }
99
}
100