Completed
Push — master ( 4848aa...64d9da )
by Dmitry
03:44
created

Filesystem::exists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

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