Passed
Pull Request — master (#190)
by Arman
03:12
created

AppTrait::setBaseDir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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.5
13
 */
14
15
namespace Quantum\App\Traits;
16
17
use Quantum\Libraries\Config\Exceptions\ConfigException;
18
use Quantum\Libraries\Logger\Factories\LoggerFactory;
19
use Quantum\Libraries\Lang\Exceptions\LangException;
20
use Quantum\Environment\Exceptions\EnvException;
21
use Quantum\Di\Exceptions\DiException;
22
use Quantum\Exceptions\BaseException;
23
use Quantum\Environment\Environment;
24
use Quantum\Libraries\Config\Config;
25
use Quantum\Tracer\ErrorHandler;
26
use Quantum\Libraries\Lang\Lang;
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 the core helper functions
60
     * @throws DiException
61
     * @throws ReflectionException
62
     */
63
    protected function loadCoreHelperFunctions()
64
    {
65
        $loader = Di::get(Loader::class);
66
        $loader->loadDir(dirname(__DIR__, 2) . DS . 'Helpers');
67
    }
68
69
    /**
70
     * Loads library helper functions
71
     * @throws DiException
72
     * @throws ReflectionException
73
     */
74
    protected function loadLibraryHelperFunctions()
75
    {
76
        $loader = Di::get(Loader::class);
77
        $loader->loadDir(dirname(__DIR__, 2) . DS . 'Libraries' . DS . '*' . DS . 'Helpers');
78
    }
79
80
    /**
81
     * Loads app helper functions
82
     * @throws DiException
83
     * @throws ReflectionException
84
     */
85
    protected function loadAppHelperFunctions(): void
86
    {
87
        $loader = Di::get(Loader::class);
88
        $loader->loadDir(App::getBaseDir() . DS . 'helpers');
89
    }
90
91
    /**
92
     * @return void
93
     * @throws DiException
94
     * @throws ReflectionException
95
     * @throws EnvException
96
     * @throws BaseException
97
     */
98
    protected function loadEnvironment()
99
    {
100
        Environment::getInstance()->load(new Setup('config', 'env'));
101
    }
102
103
    /**
104
     * @return void
105
     * @throws DiException
106
     * @throws ReflectionException
107
     * @throws ConfigException
108
     */
109
    protected function loadConfig()
110
    {
111
        Config::getInstance()->load(new Setup('config', 'config'));
112
    }
113
114
    /**
115
     * @return void
116
     * @throws BaseException
117
     * @throws ConfigException
118
     * @throws DiException
119
     * @throws ReflectionException
120
     */
121
    protected function setupErrorHandler()
122
    {
123
        ErrorHandler::getInstance()->setup(LoggerFactory::get());
124
    }
125
126
    /**
127
     * @return void
128
     * @throws BaseException
129
     * @throws DiException
130
     * @throws ReflectionException
131
     * @throws LangException
132
     */
133
    protected function loadLanguage()
134
    {
135
        $lang = Lang::getInstance();
136
137
        if ($lang->isEnabled()) {
138
            $lang->load();
139
        }
140
    }
141
}