Filesystem::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

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