From e01f034ffd482d5e12aa6df94d4d189ea9e79b76 Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Wed, 21 Aug 2019 23:46:38 +0800 Subject: [PATCH] Support caching options --- app/Console/Commands/OptionsCacheCommand.php | 36 +++++++++++++++++++ app/Console/Kernel.php | 1 + app/Services/Option.php | 5 +++ resources/misc/changelogs/en/next.md | 1 + resources/misc/changelogs/zh_CN/next.md | 1 + storage/options/.gitignore | 2 ++ .../CommandsTest/OptionsCacheCommandTest.php | 25 +++++++++++++ 7 files changed, 71 insertions(+) create mode 100644 app/Console/Commands/OptionsCacheCommand.php create mode 100644 storage/options/.gitignore create mode 100644 tests/CommandsTest/OptionsCacheCommandTest.php diff --git a/app/Console/Commands/OptionsCacheCommand.php b/app/Console/Commands/OptionsCacheCommand.php new file mode 100644 index 00000000..32c56b7a --- /dev/null +++ b/app/Console/Commands/OptionsCacheCommand.php @@ -0,0 +1,36 @@ +filesystem = $filesystem; + $this->options = $options; + } + + public function handle() + { + $content = var_export($this->options->all(), true); + $content = 'filesystem->put(storage_path('options/cache.php'), $content); + $this->info('Options cached successfully.'); + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 8ccf2129..1ec2900c 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -17,5 +17,6 @@ class Kernel extends ConsoleKernel Commands\BsInstallCommand::class, Commands\PluginEnableCommand::class, Commands\PluginDisableCommand::class, + Commands\OptionsCacheCommand::class, ]; } diff --git a/app/Services/Option.php b/app/Services/Option.php index 63466892..d2a863e4 100644 --- a/app/Services/Option.php +++ b/app/Services/Option.php @@ -73,4 +73,9 @@ class Option { return $this->items->has($key); } + + public function all(): array + { + return $this->items->all(); + } } diff --git a/resources/misc/changelogs/en/next.md b/resources/misc/changelogs/en/next.md index 07172408..75da5ded 100644 --- a/resources/misc/changelogs/en/next.md +++ b/resources/misc/changelogs/en/next.md @@ -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 diff --git a/resources/misc/changelogs/zh_CN/next.md b/resources/misc/changelogs/zh_CN/next.md index 9c13d7dd..1b61918a 100644 --- a/resources/misc/changelogs/zh_CN/next.md +++ b/resources/misc/changelogs/zh_CN/next.md @@ -5,6 +5,7 @@ - 插件系统:新增 Filters API - 支持以 `php artisan plugin:enable {name}` 的方式开启插件 - 支持以 `php artisan plugin:disable {name}` 的方式关闭插件 +- 允许通过 `php artisan options:cache` 命令缓存站点选项 - 支持指定多个插件目录(在 .env 文件中以逗号分隔) ## 调整 diff --git a/storage/options/.gitignore b/storage/options/.gitignore new file mode 100644 index 00000000..d6b7ef32 --- /dev/null +++ b/storage/options/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore diff --git a/tests/CommandsTest/OptionsCacheCommandTest.php b/tests/CommandsTest/OptionsCacheCommandTest.php new file mode 100644 index 00000000..58302616 --- /dev/null +++ b/tests/CommandsTest/OptionsCacheCommandTest.php @@ -0,0 +1,25 @@ +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), 'once(); + });*/ + + $this->artisan('options:cache')->expectsOutput('Options cached successfully.'); + } +}