WebrootLocator   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 98
rs 10
c 0
b 0
f 0
ccs 16
cts 16
cp 1
wmc 15
lcom 1
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A locateWebroot() 0 10 3
A searchWorkingDirectory() 0 12 3
A searchScriptDirectory() 0 19 4
B searchPath() 0 20 5
1
<?php
2
3
namespace Buttress\Concrete\Locator;
4
5
/**
6
 * Locate a concrete5 webroot
7
 * @package Buttress\Concrete\Locator
8
 */
9
class WebrootLocator
10
{
11
12
    /**
13
     * Locate a webroot
14
     *
15
     * @param string $scriptDirectory
16
     * @param string $workingDirectory
17
     * @param bool $recursive
18
     * @return null|string
19 12
     */
20
    public function locateWebroot($scriptDirectory, $workingDirectory, $recursive = true)
21
    {
22 12
        if ($root = $this->searchWorkingDirectory($workingDirectory, $recursive)) {
23 6
            return $root;
24
        }
25
26
        if ($root = $this->searchScriptDirectory($scriptDirectory, $recursive)) {
27 12
            return $root;
28 3
        }
29
    }
30
31
    /**
32 9
     * Search a working directory for a site
33 6
     *
34
     * @param $workingDirectory
35
     * @param bool $recursive
36 3
     * @return mixed
37
     */
38
    public function searchWorkingDirectory($workingDirectory, $recursive = true)
39
    {
40
        // Check the current working directory
41
        if ($webroot = $this->searchPath($workingDirectory, $recursive)) {
42
            return $webroot;
43
        }
44
45 12
        // Check the current working directory with /web added the end
46
        if ($webroot = $this->searchPath($workingDirectory . '/web', $recursive)) {
47
            return $webroot;
48 12
        }
49
    }
50
51 12
    /**
52 9
     * Search the current script directory for a webroot
53
     * This would only happen if this is installed as a concrete5 package
54
     *
55
     * @param $scriptDirectory
56 12
     * @param bool $recursive
57 12
     * @return string|null
58 8
     */
59
    public function searchScriptDirectory($scriptDirectory, $recursive = true)
60 12
    {
61
        // Check the current directory
62
        if ($webroot = $this->searchPath($scriptDirectory, false)) {
63
            return $webroot;
64
        }
65
66
        // Check one level up
67
        if ($webroot = $this->searchPath(dirname($scriptDirectory), false)) {
68
            return $webroot;
69
        }
70
71
        // Check two levels up and do it recursively if needed
72
        if ($webroot = $this->searchPath(dirname(dirname($scriptDirectory)), $recursive)) {
73
            return $webroot;
74
        }
75
76
        return null;
77
    }
78
79
    /**
80
     * Search a path
81
     *
82
     * @param $path
83
     * @param bool $recursive
84
     * @return bool|null|string
85
     */
86
    public function searchPath($path, $recursive = true)
87
    {
88
        // If we can find a realpath to this path
89
        if ($path = realpath($path)) {
90
            do {
91
                // Check if an index.php file exists
92
                if (file_exists($path . '/index.php')) {
93
                    return $path;
94
                }
95
96
                if (!$recursive) {
97
                    break;
98
                }
99
                // If not, set the path to the parent directory and check again
100
                $path = dirname($path);
101
            } while (strlen($path) > 1);
102
        }
103
104
        return null;
105
    }
106
}
107