Completed
Push — master ( 95b8e4...b19727 )
by Dmitry
06:52
created

Filesystem::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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