Fix cleaning options cache

This commit is contained in:
Pig Fang 2019-08-26 11:01:49 +08:00
parent 5465399eda
commit eb0818dc27
4 changed files with 17 additions and 4 deletions

View File

@ -5,6 +5,7 @@ namespace App\Console\Commands;
use App\Services\Option; use App\Services\Option;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem; use Illuminate\Filesystem\Filesystem;
use Illuminate\Foundation\Application;
class OptionsCacheCommand extends Command class OptionsCacheCommand extends Command
{ {
@ -12,11 +13,15 @@ class OptionsCacheCommand extends Command
protected $description = 'Cache Blessing Skin options'; protected $description = 'Cache Blessing Skin options';
public function handle(Filesystem $filesystem, Option $options) public function handle(Filesystem $filesystem, Application $app)
{ {
$content = var_export($options->all(), true); $path = storage_path('options/cache.php');
$filesystem->delete($path);
$app->forgetInstance(Option::class);
$content = var_export(resolve(Option::class)->all(), true);
$content = '<?php'.PHP_EOL.'return '.$content.';'; $content = '<?php'.PHP_EOL.'return '.$content.';';
$filesystem->put(storage_path('options/cache.php'), $content); $filesystem->put($path, $content);
$this->info('Options cached successfully.'); $this->info('Options cached successfully.');
} }
} }

View File

@ -7,6 +7,7 @@ use Illuminate\Support\Arr;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Composer\Semver\Comparator; use Composer\Semver\Comparator;
use App\Services\PackageManager; use App\Services\PackageManager;
use Illuminate\Filesystem\Filesystem;
class UpdateController extends Controller class UpdateController extends Controller
{ {
@ -40,7 +41,7 @@ class UpdateController extends Controller
return json(['available' => $this->canUpdate()]); return json(['available' => $this->canUpdate()]);
} }
public function download(Request $request, PackageManager $package) public function download(Request $request, PackageManager $package, Filesystem $filesystem)
{ {
if (! $this->canUpdate()) { if (! $this->canUpdate()) {
return json([]); return json([]);
@ -52,6 +53,9 @@ class UpdateController extends Controller
try { try {
$package->download($this->info['url'], $path)->extract(base_path()); $package->download($this->info['url'], $path)->extract(base_path());
// Delete options cache. This allows us to update the version info which is recorded as an option.
$filesystem->delete(storage_path('options/cache.php'));
return json(trans('admin.update.complete'), 0); return json(trans('admin.update.complete'), 0);
} catch (Exception $e) { } catch (Exception $e) {
report($e); report($e);

View File

@ -11,6 +11,7 @@ class OptionsCacheCommandTest extends TestCase
{ {
$this->mock(Filesystem::class, function ($mock) { $this->mock(Filesystem::class, function ($mock) {
$mock->shouldReceive('exists')->andReturn(false); $mock->shouldReceive('exists')->andReturn(false);
$mock->shouldReceive('delete')->with(storage_path('options/cache.php'))->once();
$mock->shouldReceive('put') $mock->shouldReceive('put')
->withArgs(function ($path, $content) { ->withArgs(function ($path, $content) {
$this->assertEquals(storage_path('options/cache.php'), $path); $this->assertEquals(storage_path('options/cache.php'), $path);

View File

@ -89,6 +89,9 @@ class UpdateControllerTest extends TestCase
$mock->shouldReceive('extract')->andReturn(true); $mock->shouldReceive('extract')->andReturn(true);
$mock->shouldReceive('progress'); $mock->shouldReceive('progress');
}); });
$this->mock(\Illuminate\Filesystem\Filesystem::class, function ($mock) {
$mock->shouldReceive('delete')->with(storage_path('options/cache.php'))->once();
});
$this->getJson('/admin/update/download?action=download') $this->getJson('/admin/update/download?action=download')
->assertJson(['code' => 0, 'message' => trans('admin.update.complete')]); ->assertJson(['code' => 0, 'message' => trans('admin.update.complete')]);