Skip to content

Commit

Permalink
optimize ConfigService && add cache command for route&config
Browse files Browse the repository at this point in the history
  • Loading branch information
xujiajun committed Oct 18, 2017
1 parent bb61ebf commit bc03bd3
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 180 deletions.
75 changes: 70 additions & 5 deletions src/Framework/Config/ConfigService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace TastPHP\Framework\Config;

use Symfony\Component\Filesystem\Filesystem;
use TastPHP\Framework\Container\Container;

/**
Expand All @@ -11,15 +12,34 @@
class ConfigService
{
protected $app;
protected static $configCacheDir = __BASEDIR__ . "/var/cache/config";
protected $enabledCache = false;
protected static $filesystem;

public function __construct(Container $app)
{
$this->app = $app;
}

public function setEnabledCache()
{
$this->enabledCache = true;
}

public function register()
{
$configs = YamlService::parse(file_get_contents(__BASEDIR__ . '/config/config.yml'));
$configCacheFile = self::$configCacheDir . "/config.php";

if (file_exists($configCacheFile)) {
$configs = require $configCacheFile;
} else {
$configs = YamlService::parse(file_get_contents(__BASEDIR__ . '/config/config.yml'));

if ($this->enabledCache) {
$this->createCache($configs, $configCacheFile);
}
}

$this->registerAppConfig($configs);
date_default_timezone_set($this->app['timezone']);
$this->registerBusinessConfig($configs);
Expand All @@ -29,7 +49,17 @@ public function parseResource($resource, $isCustom = false)
{
$config = [];
if (true == $isCustom) {
$config = YamlService::parse(file_get_contents(__BASEDIR__ . "/src/{$resource}"));

$configResourceDir = self::$configCacheDir . "/" . substr($resource, 0, -11);
$configCacheFile = $configResourceDir . "/config.php";
if (file_exists($configCacheFile)) {
$config = require $configCacheFile;
} else {
$config = YamlService::parse(file_get_contents(__BASEDIR__ . "/src/{$resource}"));
if ($this->enabledCache) {
$this->createCache($config, $configCacheFile);
}
}
}

if (false == $isCustom) {
Expand All @@ -40,8 +70,15 @@ public function parseResource($resource, $isCustom = false)
$config['timezone'] = 'UTC';
$config['name'] = 'tastphp';

if (file_exists(__BASEDIR__ . "/config/{$resource}")) {
$appConfigCacheFile = self::$configCacheDir . "/app.php";

if (file_exists($appConfigCacheFile)) {
$config = require $appConfigCacheFile;
} else {
$config = YamlService::parse(file_get_contents(__BASEDIR__ . "/config/{$resource}"));
if ($this->enabledCache) {
$this->createCache($config, $appConfigCacheFile);
}
}
}

Expand All @@ -58,8 +95,16 @@ public function parse($serviceName, $default = [])
$config = [];
$serviceName = strtolower($serviceName);

if (file_exists(__BASEDIR__ . "/config/{$serviceName}.yml")) {
$config = YamlService::parse(file_get_contents(__BASEDIR__ . "/config/{$serviceName}.yml"));
$serviceConfigCacheFile = self::$configCacheDir . "/{$serviceName}.php";
if (file_exists($serviceConfigCacheFile)) {
$config = require $serviceConfigCacheFile;
} else {
if (file_exists(__BASEDIR__ . "/config/{$serviceName}.yml")) {
$config = YamlService::parse(file_get_contents(__BASEDIR__ . "/config/{$serviceName}.yml"));
if ($this->enabledCache) {
$this->createCache($config, $serviceConfigCacheFile);
}
}
}

if (!$config) {
Expand Down Expand Up @@ -144,4 +189,24 @@ private function registerBusinessConfig($configs)
}
}
}

public static function getFilesystem()
{
if (!(self::$filesystem instanceof Filesystem)) {
self::$filesystem = new Filesystem;
}

return self::$filesystem;
}

public static function createCache($config, $configCacheFile, $configCacheDir)
{
if (empty($configCacheDir)) {
$configCacheDir = self::$configCacheDir;
}
$fs = self::getFilesystem();
$fs->mkdir($configCacheDir);
$content = "<?php return " . var_export($config, true) . ";";
file_put_contents($configCacheFile, $content);
}
}
26 changes: 26 additions & 0 deletions src/Framework/Console/Command/CacheConfigCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace TastPHP\Framework\Console\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use TastPHP\Framework\Config\ConfigService;
use TastPHP\Framework\Kernel;

class CacheConfigCommand extends BaseCommand
{
protected function configure()
{
$this
->setName('cache:config')
->setDescription('Cache config');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$configService = new ConfigService(new Kernel());
$configService->setEnabledCache();
$configService->register();
$output->writeln("<fg=black;bg=green>You have success cached config!</>");
}
}
25 changes: 25 additions & 0 deletions src/Framework/Console/Command/CacheRouteCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace TastPHP\Framework\Console\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use TastPHP\Framework\Kernel;
use TastPHP\Framework\Router\RouterServiceProvider;

class CacheRouteCommand extends BaseCommand
{
protected function configure()
{
$this
->setName('cache:route')
->setDescription('Cache route');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$routerService = new RouterServiceProvider(new Kernel());
$routerService->register();
$output->writeln("<fg=black;bg=green>You have success cached route!</>");
}
}
34 changes: 0 additions & 34 deletions src/Framework/Console/Command/GenerateAdminController.php

This file was deleted.

39 changes: 0 additions & 39 deletions src/Framework/Console/Command/GenerateAdminRoutesCommand.php

This file was deleted.

63 changes: 0 additions & 63 deletions src/Framework/Console/Command/Template/adminController.txt

This file was deleted.

30 changes: 0 additions & 30 deletions src/Framework/Console/Command/Template/adminRoutes.txt

This file was deleted.

Loading

0 comments on commit bc03bd3

Please sign in to comment.