Skip to content

Commit

Permalink
优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
titrxw committed Feb 25, 2021
1 parent 5c05ec8 commit 45e67ba
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
8 changes: 8 additions & 0 deletions Src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ public function getConfigCachePath() {
return $this->bootstrapCachePath('config/');
}

public function configurationIsCached() {
return is_dir($this->getConfigCachePath());
}

public function routeIsCached() {
return is_dir($this->getRouteCachePath());
}

public function exit() {
$this->container->clear();
}
Expand Down
5 changes: 3 additions & 2 deletions Src/Core/Bootstrap/LoadConfigBootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ public function getBuiltInConfigPath() {
}

public function bootstrap(App $app) {
$loadDir = $app->getConfigCachePath();
if (!file_exists($loadDir)) {
if (!$app->configurationIsCached()) {
$loadDir = BASE_PATH . '/config';
(new Env(BASE_PATH))->load();
} else {
$loadDir = $app->getConfigCachePath();
}

$this->loadConfigFile($this->getBuiltInConfigPath());
Expand Down
10 changes: 10 additions & 0 deletions Src/Core/Provider/ProviderAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace W7\Core\Provider;

use Illuminate\Filesystem\Filesystem;
use W7\App;
use W7\Console\Application;
use W7\Contract\Config\RepositoryInterface;
use W7\Contract\Logger\LoggerFactoryInterface;
Expand Down Expand Up @@ -109,6 +110,9 @@ protected function registerLogger($channel, $config = []) {
}

protected function registerRoute($fileName, $options = []) {
if (App::getApp()->routeIsCached()) {
return true;
}
$routeConfig = [
'namespace' => $this->packageNamespace,
'module' => $this->name,
Expand Down Expand Up @@ -188,6 +192,9 @@ protected function setRootPath($path) {
* @param $key
*/
protected function mergeConfigFrom($path, $key) {
if (App::getApp()->configurationIsCached()) {
return true;
}
$config = $this->getConfig()->get($key, []);
$this->getConfig()->set($key, array_merge(require $path, $config));
}
Expand All @@ -198,6 +205,9 @@ protected function mergeConfigFrom($path, $key) {
* @param $path
*/
protected function loadRouteFrom($path) {
if (App::getApp()->routeIsCached()) {
return true;
}
include $path;
}

Expand Down
4 changes: 2 additions & 2 deletions Src/Core/Route/RouteDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class RouteDispatcher extends GroupCountBased {
public static $routeCacheFileName = 'route.php';

public static function getDispatcherWithRouteMapping(string $routeMapping, $routeCacheGroup = ServerEnum::TYPE_HTTP) {
$routeCacheFile = App::getApp()->getRouteCachePath() . $routeCacheGroup . '.' . self::$routeCacheFileName;
if (file_exists($routeCacheFile)) {
if (App::getApp()->routeIsCached()) {
$routeCacheFile = App::getApp()->getRouteCachePath() . $routeCacheGroup . '.' . self::$routeCacheFileName;
$routeDefinitions = require $routeCacheFile;
if (!is_array($routeDefinitions)) {
throw new \RuntimeException('Invalid cache file "' . $routeCacheFile . '"');
Expand Down

0 comments on commit 45e67ba

Please sign in to comment.