Completed
Push — master ( b3f98f...654a3a )
by Arman
20s queued 16s
created

AppTrait::loadComponentHelperFunctions()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 3
nop 0
dl 0
loc 19
rs 9.7998
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.7
13
 */
14
15
namespace Quantum\App\Traits;
16
17
use Quantum\Libraries\Logger\Factories\LoggerFactory;
18
use Quantum\Libraries\Lang\Exceptions\LangException;
19
use Quantum\Environment\Exceptions\EnvException;
20
use Quantum\Config\Exceptions\ConfigException;
21
use Quantum\App\Exceptions\BaseException;
22
use Quantum\Di\Exceptions\DiException;
23
use Quantum\Environment\Environment;
24
use Quantum\Libraries\Lang\Lang;
25
use Quantum\Tracer\ErrorHandler;
26
use Quantum\Config\Config;
27
use Quantum\Loader\Loader;
28
use Quantum\Loader\Setup;
29
use ReflectionException;
30
use Quantum\App\App;
31
use Quantum\Di\Di;
32
33
/**
34
 * Class AppTrait
35
 * @package Quantum\App
36
 */
37
trait AppTrait
38
{
39
40
    /**
41
     * Sets the app base directory
42
     * @param string $baseDir
43
     */
44
    public static function setBaseDir(string $baseDir)
45
    {
46
        self::$baseDir = $baseDir;
0 ignored issues
show
Bug Best Practice introduced by
The property baseDir does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
47
    }
48
49
    /**
50
     * Gets the app base directory
51
     * @return string
52
     */
53
    public static function getBaseDir(): string
54
    {
55
        return self::$baseDir;
56
    }
57
58
    /**
59
     * Loads component helper functions
60
     * @throws DiException
61
     * @throws ReflectionException
62
     */
63
    protected function loadComponentHelperFunctions(): void
64
    {
65
        $loader = Di::get(Loader::class);
66
67
        $components = [
68
            'Environment',
69
            'Config',
70
            'Router',
71
            'Model',
72
            'Hook',
73
            'Http',
74
            'View',
75
            'App',
76
        ];
77
78
        foreach ($components as $component) {
79
            $componentHelperPath = dirname(__DIR__, 2) . DS . $component . DS . 'Helpers';
80
            if (is_dir($componentHelperPath)) {
81
                $loader->loadDir($componentHelperPath);
82
            }
83
        }
84
    }
85
86
    /**
87
     * Loads library helper functions
88
     * @throws DiException
89
     * @throws ReflectionException
90
     */
91
    protected function loadLibraryHelperFunctions()
92
    {
93
        $loader = Di::get(Loader::class);
94
        $loader->loadDir(dirname(__DIR__, 2) . DS . 'Libraries' . DS . '*' . DS . 'Helpers');
95
    }
96
97
    /**
98
     * Loads app helper functions
99
     * @throws DiException
100
     * @throws ReflectionException
101
     */
102
    protected function loadAppHelperFunctions(): void
103
    {
104
        $loader = Di::get(Loader::class);
105
        $loader->loadDir(App::getBaseDir() . DS . 'helpers');
106
    }
107
108
    /**
109
     * @return void
110
     * @throws DiException
111
     * @throws ReflectionException
112
     * @throws EnvException
113
     * @throws BaseException
114
     */
115
    protected function loadEnvironment()
116
    {
117
        Environment::getInstance()->load(new Setup('config', 'env'));
118
    }
119
120
    /**
121
     * @return void
122
     * @throws DiException
123
     * @throws ReflectionException
124
     * @throws ConfigException
125
     */
126
    protected function loadConfig()
127
    {
128
        Config::getInstance()->load(new Setup('config', 'config'));
129
    }
130
131
    /**
132
     * @return void
133
     * @throws BaseException
134
     * @throws ConfigException
135
     * @throws DiException
136
     * @throws ReflectionException
137
     */
138
    protected function setupErrorHandler()
139
    {
140
        ErrorHandler::getInstance()->setup(LoggerFactory::get());
141
    }
142
143
    /**
144
     * @return void
145
     * @throws BaseException
146
     * @throws DiException
147
     * @throws ReflectionException
148
     * @throws LangException
149
     */
150
    protected function loadLanguage()
151
    {
152
        $lang = Lang::getInstance();
153
154
        if ($lang->isEnabled()) {
155
            $lang->load();
156
        }
157
    }
158
}