Completed
Push — master ( 7f96b3...40f202 )
by Dmitry
03:32
created

Filesystem::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Basis;
4
5
class Filesystem
6
{
7
    private $app;
8
    protected $root;
9
10 11
    public function __construct(Application $app, $root)
11
    {
12 11
        $this->app = $app;
13 11
        $this->root = $root;
14 11
    }
15
16 11
    public function exists()
17
    {
18 11
        $path = call_user_func_array([$this, 'getPath'], func_get_args());
19 11
        return is_dir($path) || file_exists($path);
20
    }
21
22 11
    public function getPath()
23
    {
24 11
        if (func_get_args()) {
25 11
            $chain = func_get_args();
26 11
            array_unshift($chain, $this->root);
27 11
            foreach ($chain as $k => $v) {
28 11
                if (!strlen($v)) {
29 11
                    unset($chain[$k]);
30
                }
31
            }
32 11
            return implode(DIRECTORY_SEPARATOR, array_values($chain));
33
        }
34
35 11
        return $this->root;
36
    }
37
38 11
    public function listClasses($namespace = '')
39
    {
40 11
        $location = "src";
41
42 11
        if($namespace) {
43 11
            $location .= '/'.str_replace('\\', DIRECTORY_SEPARATOR, $namespace);
44
        }
45
46 11
        $files = $this->listFiles($location);
47 11
        $classes = [];
48
49 11
        $namespace = $this->completeClassName($namespace);
50
51 11
        foreach($files as $file) {
52 11
            $class = str_replace(['\\', '/'], '\\', $file);
53 11
            $class = substr($class, 0, -4);
54 11
            if($namespace) {
55 11
                $class = $namespace.'\\'.$class;
56
            }
57 11
            $classes[] = $class;
58
        }
59
60 11
        return $classes;
61
    }
62
63 11
    public function listFiles($location)
64
    {
65 11
        $absolute = $this->getPath($location);
66 11
        if (!is_dir($absolute)) {
67
            return [];
68
        }
69
70 11
        $result = [];
71 11
        $relative = substr($absolute, strlen($this->getPath()));
72 11
        foreach (scandir($absolute) as $file) {
73 11
            if ($file != '.' && $file != '..') {
74 11
                if (is_file("$absolute/$file")) {
75 11
                    $result[] = $file;
76
                } else {
77 2
                    foreach ($this->listFiles("$relative/$file") as $child) {
78 11
                        $result[] = "$file/$child";
79
                    }
80
                }
81
            }
82
        }
83
84 11
        return $result;
85
    }
86
87 11
    public function completeClassName($namespace)
88
    {
89 11
        $config = $this->app->get(Config::class);
90 11
        if($config['app.namespace']) {
91 11
            $namespace = $config['app.namespace'].($namespace ? '\\'.$namespace:'');
92
        }
93 11
        return $namespace;
94
    }
95
96
}
97