Completed
Push — dev ( 37de2d...9cf0b1 )
by Zach
02:13
created

NamespaceResolver::getRootNamespace()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Yarak\Helpers;
4
5
use Yarak\Config\Config;
6
7
class NamespaceResolver
8
{
9
    /**
10
     * Resolve namespace for the given dir path, add additional values.
11
     *
12
     * @param string $dir
13
     * @param string $additional
14
     *
15
     * @return string|null
16
     */
17
    public static function resolve($dir, $additional = '')
18
    {
19
        $config = Config::getInstance();
20
21
        if ($config->has(['namespaces', $dir])) {
22
            $namespace = $config->get(['namespaces', $dir]);
23
        } elseif ($config->has(['application', $dir.'Dir'])) {
24
            $namespace = self::resolveFromRegisteredDir($dir);
25
        } else {
26
            $namespace = self::resolveFromRelativePath($dir);
27
        }
28
29
        if ($namespace !== null && $additional) {
30
            return Str::append($namespace, '\\').ucfirst($additional);
31
        }
32
33
        return $namespace;
34
    }
35
36
    /**
37
     * Resolve a namespace from a registered directory.
38
     *
39
     * @param string $dir
40
     *
41
     * @return string
42
     */
43
    public static function resolveFromRegisteredDir($dir)
44
    {
45
        $config = Config::getInstance();
46
47
        $method = 'get'.ucfirst($dir).'Directory';
48
49
        if (method_exists($config, $method)) {
50
            return self::resolveFromAbsolutePath($config->$method());
51
        }
52
53
        return null;
54
    }
55
56
    /**
57
     * Reslove a namespace from a path relative to app root directory.
58
     *
59
     * @param string $path
60
     *
61
     * @return string
62
     */
63
    public static function resolveFromRelativePath($path)
64
    {
65
        $pathArray = array_filter(explode(DIRECTORY_SEPARATOR, $path));
66
67
        array_unshift($pathArray, self::getRootNamespace());
68
69
        return implode('\\', array_map('ucfirst', $pathArray));
70
    }
71
72
    /**
73
     * Resolve a namespace from an absolute path.
74
     *
75
     * @param string $path
76
     *
77
     * @return string
78
     */
79
    public static function resolveFromAbsolutePath($path)
80
    {
81
        $config = Config::getInstance();
82
83
        $appPathArray = array_filter(explode('/', $config->getAppPath()));
84
85
        $relativePath = array_diff(
86
            array_filter(explode(DIRECTORY_SEPARATOR, $path)),
87
            $appPathArray
88
        );
89
90
        array_unshift($relativePath, self::getRootNamespace());
91
92
        return implode('\\', array_map('ucfirst', $relativePath));
93
    }
94
95
    /**
96
     * Get the app root namespace.
97
     *
98
     * @return string
99
     */
100
    public static function getRootNamespace()
101
    {
102
        $config = Config::getInstance();
103
104
        if ($config->has(['namespaces', 'root'])) {
105
            return $config->get(['namespaces', 'root']);
106
        }
107
108
        return 'App';
109
    }
110
}
111