Passed
Pull Request — master (#97)
by Arman
06:04 queued 02:07
created

Loader::loadFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 1
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.6.0
13
 */
14
15
namespace Quantum\Loader;
16
17
use Quantum\Libraries\Storage\FileSystem;
18
use Quantum\Exceptions\LoaderException;
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
     * File System
59
     * @var \Quantum\Libraries\Storage\FileSystem
60
     */
61
    private $fs;
62
63
    /**
64
     * Loader constructor
65
     * @param \Quantum\Libraries\Storage\FileSystem|null $fs
66
     */
67
    public function __construct(FileSystem $fs = null)
68
    {
69
        $this->fs = $fs;
70
    }
71
72
    /**
73
     * Setups the loader
74
     * @param Setup $setup
75
     * @return $this
76
     */
77
    public function setup(Setup $setup): Loader
78
    {
79
        $this->hierarchical = $setup->getHierarchy();
80
        $this->module = $setup->getModule();
81
        $this->pathPrefix = $setup->getPathPrefix();
82
        $this->fileName = $setup->getFilename();
83
        $this->exceptionMessage = $setup->getExceptionMessage();
84
85
        return $this;
86
    }
87
88
    /**
89
     * Sets new value
90
     * @param string $property
91
     * @param mixed $value
92
     * @return $this
93
     */
94
    public function set(string $property, $value): Loader
95
    {
96
        $this->$property = $value;
97
        return $this;
98
    }
99
100
    /**
101
     * Loads .php files from given directory
102
     * @param string $dir
103
     * @throws LoaderException
104
     */
105
    public function loadDir(string $dir)
106
    {
107
        foreach ($this->fs->glob($dir . DS . "*.php") as $filename) {
108
            $this->fs->require($filename, true);
109
        }
110
    }
111
112
    /**
113
     * Loads the content
114
     * @return mixed
115
     * @throws \Quantum\Exceptions\LoaderException
116
     */
117
    public function load()
118
    {
119
        return require $this->getFilePath();
120
    }
121
122
    /**
123
     * Gets the file path
124
     * @return string
125
     * @throws \Quantum\Exceptions\LoaderException
126
     */
127
    public function getFilePath(): string
128
    {
129
        $filePath = '';
130
131
        if ($this->module) {
132
            $filePath = modules_dir() . DS . $this->module . DS;
133
        }
134
135
        if ($this->pathPrefix) {
136
            $filePath .= $this->pathPrefix . DS;
137
        }
138
139
        $filePath .= $this->fileName . '.php';
140
141
        if (!$this->fs->exists($filePath)) {
142
            if ($this->hierarchical) {
143
                $filePath = base_dir() . DS . 'shared' . DS . $this->pathPrefix . DS . $this->fileName . '.php';
144
145
                if (!$this->fs->exists($filePath)) {
146
                    throw new LoaderException(_message($this->exceptionMessage, $this->fileName));
147
                }
148
            } else {
149
                throw new LoaderException(_message($this->exceptionMessage, $this->fileName));
150
            }
151
        }
152
153
        return $filePath;
154
    }
155
156
}
157