Completed
Push — master ( f84b1b...3c5fbc )
by Arman
14s
created

AppTrait::setupErrorHandler()   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 0
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 3.0.0
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\Libraries\Lang\Factories\LangFactory;
20
use Quantum\Config\Exceptions\ConfigException;
21
use Quantum\App\Exceptions\BaseException;
22
use Quantum\Di\Exceptions\DiException;
23
use Quantum\Tracer\ErrorHandler;
24
use Quantum\Loader\Loader;
25
use Quantum\Config\Config;
26
use Quantum\Loader\Setup;
27
use ReflectionException;
28
use Quantum\App\App;
29
use Quantum\Di\Di;
30
31
/**
32
 * Class AppTrait
33
 * @package Quantum\App
34
 */
35
trait AppTrait
36
{
37
    /**
38
     * Sets the app base directory
39
     * @param string $baseDir
40
     */
41
    public static function setBaseDir(string $baseDir)
42
    {
43
        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...
44
    }
45
46
    /**
47
     * Gets the app base directory
48
     * @return string
49
     */
50
    public static function getBaseDir(): string
51
    {
52
        return self::$baseDir;
53
    }
54
55
    protected function loadCoreDependencies()
56
    {
57
        $file = dirname(__DIR__) . DS . 'Config' . DS . 'dependencies.php';
58
59
        $coreDependencies = (is_file($file) && is_array($deps = require $file)) ? $deps : [];
60
61
        Di::registerDependencies($coreDependencies);
62
    }
63
64
    /**
65
     * Loads component helper functions
66
     * @throws DiException
67
     * @throws ReflectionException
68
     */
69
    protected function loadComponentHelperFunctions(): void
70
    {
71
        $loader = Di::get(Loader::class);
72
73
        $components = [
74
            'Environment',
75
            'Config',
76
            'Router',
77
            'Service',
78
            'Model',
79
            'Hook',
80
            'Http',
81
            'View',
82
            'App',
83
        ];
84
85
        foreach ($components as $component) {
86
            $componentHelperPath = dirname(__DIR__, 2) . DS . $component . DS . 'Helpers';
87
            if (is_dir($componentHelperPath)) {
88
                $loader->loadDir($componentHelperPath);
89
            }
90
        }
91
    }
92
93
    /**
94
     * Loads library helper functions
95
     * @throws DiException
96
     * @throws ReflectionException
97
     */
98
    protected function loadLibraryHelperFunctions()
99
    {
100
        $loader = Di::get(Loader::class);
101
        $loader->loadDir(dirname(__DIR__, 2) . DS . 'Libraries' . DS . '*' . DS . 'Helpers');
102
    }
103
104
    /**
105
     * Loads app helper functions
106
     * @throws DiException
107
     * @throws ReflectionException
108
     */
109
    protected function loadAppHelperFunctions(): void
110
    {
111
        $loader = Di::get(Loader::class);
112
        $loader->loadDir(App::getBaseDir() . DS . 'helpers');
113
    }
114
115
    /**
116
     * Loads module helper functions
117
     * @throws DiException
118
     * @throws ReflectionException
119
     */
120
    protected function loadModuleHelperFunctions(): void
121
    {
122
        $loader = Di::get(Loader::class);
123
        $loader->loadDir(App::getBaseDir() . DS . 'modules' . DS . '*' . DS . 'helpers');
124
    }
125
126
    /**
127
     * @return void
128
     * @throws DiException
129
     * @throws ReflectionException
130
     * @throws ConfigException
131
     */
132
    protected function loadAppConfig()
133
    {
134
        if (!config()->has('app')) {
135
            Config::getInstance()->import(new Setup('config', 'app'));
136
        }
137
    }
138
139
    /**
140
     * @return void
141
     * @throws BaseException
142
     * @throws ConfigException
143
     * @throws DiException
144
     * @throws ReflectionException
145
     */
146
    protected function setupErrorHandler()
147
    {
148
        ErrorHandler::getInstance()->setup(LoggerFactory::get());
149
    }
150
151
    /**
152
     * @return void
153
     * @throws BaseException
154
     * @throws DiException
155
     * @throws ReflectionException
156
     * @throws LangException
157
     */
158
    protected function loadLanguage()
159
    {
160
        $lang = LangFactory::get();
161
162
        if ($lang->isEnabled()) {
163
            $lang->load();
164
        }
165
    }
166
}
167