Skip to content

Commit 18468cc

Browse files
committed
code optimize
1 parent 0b6f3d0 commit 18468cc

File tree

2 files changed

+27
-33
lines changed

2 files changed

+27
-33
lines changed

framework/Application.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ final class Application
3333
*/
3434
protected $config = [];
3535

36+
/**
37+
* All application config
38+
*
39+
* @var array
40+
*/
41+
protected $allConfig = [];
42+
3643
/**
3744
* Application instance
3845
*
@@ -63,10 +70,16 @@ final class Application
6370
*/
6471
public function __construct()
6572
{
66-
// $this->config = Configuration::get('app');
6773
self::$instance = $this;
6874
$this->request = new Request();
6975
$this->response = new Response();
76+
$this->loadDotEnv();
77+
$this->allConfig = Configuration::all();
78+
$this->loadAppConfig();
79+
$this->loadHelpers();
80+
$this->setDefaultTimezone();
81+
$this->registerErrorHandler();
82+
$this->registerShutdownHandler();
7083
}
7184

7285
/**
@@ -94,12 +107,6 @@ protected function setDefaultTimezone()
94107
*/
95108
public function run()
96109
{
97-
$this->loadHelpers();
98-
$this->loadDotEnv();
99-
$this->loadAppConfig();
100-
$this->setDefaultTimezone();
101-
$this->registerErrorHandler();
102-
$this->registerShutdownHandler();
103110
$this->registerRoutes();
104111
$this->registerApiRoutes();
105112
$this->registerMiddlewares();
@@ -113,7 +120,7 @@ public function run()
113120
*/
114121
protected function loadAppConfig()
115122
{
116-
$this->config = Configuration::get('app');
123+
$this->config = $this->allConfig['app'];
117124
}
118125

119126
/**
@@ -236,7 +243,7 @@ public function shutdownHandler()
236243
/**
237244
* Load helper functions
238245
*/
239-
public static function loadHelpers()
246+
public function loadHelpers()
240247
{
241248
require_once(ROOT . DS . 'framework' . DS . 'Helpers.php');
242249
}
@@ -254,12 +261,6 @@ public function loadDotEnv()
254261
*/
255262
public function runConsoleCommand($argv)
256263
{
257-
$this->loadHelpers();
258-
$this->loadDotEnv();
259-
$this->loadAppConfig();
260-
$this->setDefaultTimezone();
261-
$this->registerErrorHandler();
262-
$this->registerShutdownHandler();
263264
Command::run($argv);
264265
}
265266
}

framework/Configuration.php

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,24 @@ class Configuration
1717
*/
1818
public static function get($key = null)
1919
{
20-
$config = self::register($key);
21-
if ($config) {
22-
return $config;
20+
$all = self::all();
21+
if (array_key_exists($key, $all)) {
22+
return $all[$key];
2323
}
24-
die("Config key \"{$key}\" not found");
24+
throw new Exception("Config key \"{$key}\" not found");
2525
}
2626

2727
/**
28-
* register all config files
28+
* get all configs
2929
*/
30-
public static function register($key)
30+
public static function all()
3131
{
32-
$fileArray = scandir(base_path('config'));
33-
$fileArray = array_diff($fileArray, ['.', '..']);
34-
35-
$configFileArray = [];
36-
37-
foreach ($fileArray as $configFile) {
38-
$getFileName = explode('.', $configFile);
39-
// if ($getFileName[0] == 'Config') {
40-
// continue;
41-
// }
42-
$configFileArray[$getFileName[0]] = include_file("config/{$configFile}");
32+
$path = ROOT . DS . 'config' . DS . 'Config.php';
33+
if (!file_exists($path)) {
34+
throw new Exception("Config file not found");
4335
}
44-
return $configFileArray[$key];
36+
return include(ROOT . DS . 'config' . DS . 'Config.php');
4537
}
4638

39+
4740
}

0 commit comments

Comments
 (0)