Completed
Push — master ( 9ca9d9...72ab7d )
by Dmitry
04:11 queued 01:35
created

Filesystem::getPath()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

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