Passed
Pull Request — master (#365)
by Arman
02:36
created

Loader::resolveFilePath()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 4
nop 0
dl 0
loc 15
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.9.9
13
 */
14
15
namespace Quantum\Loader;
16
17
use Quantum\Loader\Exceptions\LoaderException;
18
use Quantum\App\App;
19
20
/**
21
 * Class Loader
22
 * @package Quantum\Loader
23
 */
24
class Loader
25
{
26
27
    /**
28
     * Current module
29
     * @var string
30
     */
31
    private $module;
32
33
    /**
34
     * Environment
35
     * @var string
36
     */
37
    private $pathPrefix;
38
39
    /**
40
     * File name
41
     * @var string
42
     */
43
    private $fileName;
44
45
    /**
46
     * Hierarchical
47
     * @var bool
48
     */
49
    private $hierarchical;
50
51
    /**
52
     * Exception message
53
     * @var string
54
     */
55
    private $exceptionMessage;
56
57
    /**
58
     * Setups the loader
59
     * @param Setup $setup
60
     * @return $this
61
     */
62
    public function setup(Setup $setup): Loader
63
    {
64
        $this->hierarchical = $setup->getHierarchy();
65
        $this->module = $setup->getModule();
66
        $this->pathPrefix = $setup->getPathPrefix();
67
        $this->fileName = $setup->getFilename();
68
        $this->exceptionMessage = $setup->getExceptionMessage();
69
70
        return $this;
71
    }
72
73
    /**
74
     * Sets new value
75
     * @param string $property
76
     * @param mixed $value
77
     * @return $this
78
     */
79
    public function set(string $property, $value): Loader
80
    {
81
        $this->$property = $value;
82
        return $this;
83
    }
84
85
    /**
86
     * Loads .php files from given directory
87
     * @param string $dir
88
     */
89
    public function loadDir(string $dir)
90
    {
91
        foreach (glob($dir . DS . "*.php") as $filename) {
92
            require_once $filename;
93
        }
94
    }
95
96
    /**
97
     * Loads the content
98
     * @return mixed
99
     * @throws LoaderException
100
     */
101
    public function load()
102
    {
103
        return require $this->getFilePath();
104
    }
105
106
    /**
107
     * Checks if the setup points to valid file
108
     * @return bool
109
     */
110
    public function fileExists(): bool
111
    {
112
        $filePath = $this->resolveFilePath();
113
114
        if (file_exists($filePath)) {
115
            return true;
116
        }
117
118
        if ($this->hierarchical) {
119
            $filePath = App::getBaseDir() . DS . 'shared' . DS . strtolower($this->pathPrefix) . DS . $this->fileName . '.php';
120
            return file_exists($filePath);
121
        }
122
123
        return false;
124
    }
125
126
    /**
127
     * Gets the file path
128
     * @return string
129
     * @throws LoaderException
130
     */
131
    public function getFilePath(): string
132
    {
133
        $filePath = $this->resolveFilePath();
134
135
        if (!file_exists($filePath)) {
136
            if ($this->hierarchical) {
137
                $filePath = App::getBaseDir() . DS . 'shared' . DS . strtolower($this->pathPrefix) . DS . $this->fileName . '.php';
138
139
                if (!file_exists($filePath)) {
140
                    throw new LoaderException(_message($this->exceptionMessage, $this->fileName));
141
                }
142
            } else {
143
                throw new LoaderException(_message($this->exceptionMessage, $this->fileName));
144
            }
145
        }
146
147
        return $filePath;
148
    }
149
150
    /**
151
     * @return string
152
     */
153
    private function resolveFilePath(): string
154
    {
155
        $filePath = '';
156
157
        if ($this->module) {
158
            $filePath = modules_dir() . DS . $this->module . DS;
159
        }
160
161
        if ($this->pathPrefix) {
162
            $filePath .= $this->pathPrefix . DS;
163
        }
164
165
        $filePath .= $this->fileName . '.php';
166
167
        return $filePath;
168
    }
169
}