Skip to content

Commit

Permalink
Add cache warm command (closes #6)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastiandedeyne committed May 8, 2018
1 parent 0b83178 commit fddccc7
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/Console/WarmCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Spatie\Sheets\Console;

use Spatie\Sheets\Sheets;
use Illuminate\Console\Command;

class WarmCommand extends Command
{
protected $signature = 'sheets:warm {--collection=}';

protected $description = 'Warm up the caches for Sheets collections';

/** @var \Spatie\Sheets\Sheets */
protected $sheets;

public function __construct(Sheets $sheets)
{
parent::__construct();

$this->sheets = $sheets;
}

public function handle()
{
if ($this->option('collection')) {
$this->warm($this->option('collection'));

return;
}

$this->sheets->getRegisteredCollectionNames()->each(function (string $collectionName) {
$this->warm($collectionName);
});
}

protected function warm(string $collectionName)
{
$this->sheets->collection($collectionName)->all()->each(function ($sheet) use ($collectionName) {
$this->sheets->collection($collectionName)->get($sheet->slug);
});

$this->info("Warmed up {$this->option('collection')}");
}
}
5 changes: 5 additions & 0 deletions src/Sheets.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public function setDefaultCollection(string $defaultCollection)
$this->defaultCollection = $defaultCollection;
}

public function getRegisteredCollectionNames(): Collection
{
return collect($this->collections)->keys();
}

protected function defaultCollection(): Repository
{
if (empty($this->collections)) {
Expand Down
5 changes: 5 additions & 0 deletions src/SheetsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Filesystem\FilesystemManager;
use Illuminate\Support\ServiceProvider;
use League\CommonMark\CommonMarkConverter;
use Spatie\Sheets\Console\WarmCommand;
use Spatie\Sheets\ContentParsers\MarkdownParser;
use Spatie\Sheets\ContentParsers\MarkdownWithFrontMatterParser;
use Spatie\Sheets\PathParsers\SlugParser;
Expand All @@ -23,6 +24,10 @@ public function boot()
$this->publishes([
__DIR__.'/../config/sheets.php' => config_path('sheets.php'),
], 'config');

$this->commands([
WarmCommand::class,
]);
}
}

Expand Down
43 changes: 43 additions & 0 deletions tests/Integration/Console/WarmCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Spatie\Sheets\Tests\Integration\Console;

use Spatie\Sheets\Sheets;
use Spatie\Sheets\Tests\Integration\TestCase;
use Illuminate\Support\Facades\Artisan;

class WarmCommandTest extends TestCase
{
/** @test */
public function it_warms_up_all_caches()
{
$this->assertFalse($this->app->cache->has('sheets:content-1:__all'));
$this->assertFalse($this->app->cache->has('sheets:content-1:hello-world'));
$this->assertFalse($this->app->cache->has('sheets:content-2:__all'));
$this->assertFalse($this->app->cache->has('sheets:content-2:hello-world'));

Artisan::call('sheets:warm');

$this->assertTrue($this->app->cache->has('sheets:content-1:__all'));
$this->assertTrue($this->app->cache->has('sheets:content-1:hello-world'));
$this->assertTrue($this->app->cache->has('sheets:content-2:__all'));
$this->assertTrue($this->app->cache->has('sheets:content-2:hello-world'));
}

protected function getEnvironmentSetUp($app)
{
$app['config']->set('filesystems.disks.content-1', [
'driver' => 'local',
'root' => __DIR__.'/../../fixtures/content',
]);

$app['config']->set('filesystems.disks.content-2', [
'driver' => 'local',
'root' => __DIR__.'/../../fixtures/content',
]);

$app['config']->set('sheets', [
'collections' => ['content-1', 'content-2'],
]);
}
}

0 comments on commit fddccc7

Please sign in to comment.