| 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\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\Environment\Exceptions\EnvException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Quantum\Config\Exceptions\ConfigException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Quantum\App\Exceptions\BaseException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Quantum\Di\Exceptions\DiException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use Quantum\Environment\Environment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use Quantum\Tracer\ErrorHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use Quantum\Loader\Loader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | use Quantum\Config\Config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |  |     protected function loadCoreDependencies() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $file = dirname(__DIR__) . DS . 'Config' . DS . 'dependencies.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $coreDependencies = (is_file($file) && is_array($deps = require $file)) ? $deps : []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         Di::registerDependencies($coreDependencies); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * Loads component helper functions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @throws DiException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @throws ReflectionException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     protected function loadComponentHelperFunctions(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $loader = Di::get(Loader::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $components = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             'Environment', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             'Config', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             'Router', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             'Model', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             'Hook', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             'Http', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             'View', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             'App', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         foreach ($components as $component) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             $componentHelperPath = dirname(__DIR__, 2) . DS . $component . DS . 'Helpers'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             if (is_dir($componentHelperPath)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                 $loader->loadDir($componentHelperPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * Loads library helper functions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @throws DiException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @throws ReflectionException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     protected function loadLibraryHelperFunctions() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         $loader = Di::get(Loader::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         $loader->loadDir(dirname(__DIR__, 2) . DS . 'Libraries' . DS . '*' . DS . 'Helpers'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * Loads app helper functions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * @throws DiException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * @throws ReflectionException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 110 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |     protected function loadAppHelperFunctions(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |         $loader = Di::get(Loader::class); | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |         $loader->loadDir(App::getBaseDir() . DS . 'helpers'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * Loads module helper functions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * @throws DiException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * @throws ReflectionException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     protected function loadModuleHelperFunctions(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         $loader = Di::get(Loader::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         $loader->loadDir(App::getBaseDir() . DS . 'modules' . DS . '*' . DS . 'helpers'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * @throws DiException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * @throws ReflectionException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * @throws EnvException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * @throws BaseException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     protected function loadEnvironment() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         Environment::getInstance()->load(new Setup('config', 'env')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      * @throws DiException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      * @throws ReflectionException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      * @throws ConfigException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     protected function loadAppConfig() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         Config::getInstance()->import(new Setup('config', 'app')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |      * @throws BaseException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |      * @throws ConfigException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |      * @throws DiException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |      * @throws ReflectionException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     protected function setupErrorHandler() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         ErrorHandler::getInstance()->setup(LoggerFactory::get()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      * @throws BaseException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      * @throws DiException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |      * @throws ReflectionException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |      * @throws LangException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |     protected function loadLanguage() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |         $lang = LangFactory::get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |         if ($lang->isEnabled()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |             $lang->load(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 177 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 178 |  |  | } |