Completed
Push — master ( 2761e7...346e09 )
by Dmitry
05:23
created

Filesystem::listFiles()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 7

Importance

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