Completed
Push — master ( d22295...288421 )
by Dmitry
04:25
created

Filesystem::listFiles()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7.0222

Importance

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