Support caching options

This commit is contained in:
Pig Fang 2019-08-21 23:46:38 +08:00
parent 061c9d7f56
commit e01f034ffd
7 changed files with 71 additions and 0 deletions

View File

@ -0,0 +1,36 @@
<?php
namespace App\Console\Commands;
use App\Services\Option;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
class OptionsCacheCommand extends Command
{
protected $signature = 'options:cache';
protected $description = 'Cache Blessing Skin options';
/** @var Filesystem */
protected $filesystem;
/** @var Option */
protected $options;
public function __construct(Filesystem $filesystem, Option $options)
{
parent::__construct();
$this->filesystem = $filesystem;
$this->options = $options;
}
public function handle()
{
$content = var_export($this->options->all(), true);
$content = '<?php'.PHP_EOL.'return '.$content.';';
$this->filesystem->put(storage_path('options/cache.php'), $content);
$this->info('Options cached successfully.');
}
}

View File

@ -17,5 +17,6 @@ class Kernel extends ConsoleKernel
Commands\BsInstallCommand::class,
Commands\PluginEnableCommand::class,
Commands\PluginDisableCommand::class,
Commands\OptionsCacheCommand::class,
];
}

View File

@ -73,4 +73,9 @@ class Option
{
return $this->items->has($key);
}
public function all(): array
{
return $this->items->all();
}
}

View File

@ -5,6 +5,7 @@
- Plugin system: Added Filters API.
- Allow to enable a plugin by running `php artisan plugin:enable {name}`.
- Allow to disable a plugin by running `php artisan plugin:disable {name}`.
- Allow to cache options by running `php artisan options:cache`.
- Support multiple plugins directories. (Splited by comma in ".env" file.)
## Tweaked

View File

@ -5,6 +5,7 @@
- 插件系统:新增 Filters API
- 支持以 `php artisan plugin:enable {name}` 的方式开启插件
- 支持以 `php artisan plugin:disable {name}` 的方式关闭插件
- 允许通过 `php artisan options:cache` 命令缓存站点选项
- 支持指定多个插件目录(在 .env 文件中以逗号分隔)
## 调整

2
storage/options/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*
!.gitignore

View File

@ -0,0 +1,25 @@
<?php
namespace Tests;
use Illuminate\Support\Str;
use Illuminate\Filesystem\Filesystem;
class OptionsCacheCommandTest extends TestCase
{
public function testRun()
{
/*$this->mock(Filesystem::class, function ($mock) {
$mock->shouldReceive('put')
/*->withArgs(function ($path, $content) {
$this->assertEquals(storage_path('options/cache.php'), $path);
$this->assertTrue(Str::startsWith($content), '<?php');
return true;
})
->once();
});*/
$this->artisan('options:cache')->expectsOutput('Options cached successfully.');
}
}