Skip to content

Commit b9cc8fa

Browse files
authored
Merge pull request #4 from DutchCodingCompany/feature/add-cache
Add caching option
2 parents 0dcca11 + ef623bb commit b9cc8fa

File tree

6 files changed

+110
-0
lines changed

6 files changed

+110
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
## 2.2.0 - 2025-08-05
4+
- Add caching option
5+
36
## 2.1.0 - 2025-08-05
47
- Add `JsonSchemaAttributeRule`
58

config/json-schema.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
'directory' => env('JSON_SCHEMA_DIRECTORY', 'schema'),
1515

16+
'auto-cache' => (bool) env('JSON_SCHEMA_AUTO_CACHE', true),
17+
1618
/*
1719
|--------------------------------------------------------------------------
1820
| Filter for file match

src/Commands/Optimize.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace DutchCodingCompany\LaravelJsonSchema\Commands;
4+
5+
use DutchCodingCompany\LaravelJsonSchema\JsonSchemaRepository;
6+
use Illuminate\Console\Command;
7+
use Illuminate\Contracts\Filesystem\Factory as FilesystemFactory;
8+
use Illuminate\Contracts\Filesystem\Filesystem;
9+
use Illuminate\Support\Facades\App;
10+
11+
class Optimize extends Command
12+
{
13+
/**
14+
* The name and signature of the console command.
15+
*
16+
* @var string
17+
*/
18+
protected $signature = 'json-schema:optimize';
19+
20+
/**
21+
* The console command description.
22+
*
23+
* @var string
24+
*/
25+
protected $description = 'Caches available json schemas.';
26+
27+
/**
28+
* Execute the console command.
29+
*/
30+
public function handle(FilesystemFactory $filesystem, JsonSchemaRepository $repository): int
31+
{
32+
$this->call('json-schema:optimize-clear');
33+
34+
$filesystem->disk('local')->put(
35+
$repository->fullCachePath(),
36+
'<?php return '.var_export($repository->schemaFiles(true), true).';'.PHP_EOL,
37+
);
38+
39+
$this->outputComponents()->info('Json schemas cached successfully!');
40+
41+
return static::SUCCESS;
42+
}
43+
}

src/Commands/OptimizeClear.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace DutchCodingCompany\LaravelJsonSchema\Commands;
4+
5+
use DutchCodingCompany\LaravelJsonSchema\JsonSchemaRepository;
6+
use Illuminate\Console\Command;
7+
use Illuminate\Contracts\Filesystem\Factory as FilesystemFactory;
8+
9+
class OptimizeClear extends Command
10+
{
11+
/**
12+
* The name and signature of the console command.
13+
*
14+
* @var string
15+
*/
16+
protected $signature = 'json-schema:optimize-clear';
17+
18+
/**
19+
* The console command description.
20+
*
21+
* @var string
22+
*/
23+
protected $description = 'Clear json schemas cache.';
24+
25+
/**
26+
* Execute the console command.
27+
*/
28+
public function handle(FilesystemFactory $filesystem, JsonSchemaRepository $repository): int
29+
{
30+
$filesystem->disk('local')->delete($repository->fullCachePath());
31+
32+
$this->outputComponents()->info('Json schemas cache cleared successfully!');
33+
34+
return static::SUCCESS;
35+
}
36+
}

src/JsonSchemaRepository.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Contracts\Filesystem\Factory as FilesystemFactory;
66
use Illuminate\Contracts\Filesystem\Filesystem;
7+
use Illuminate\Support\Facades\App;
78
use Illuminate\Support\Str;
89
use JsonException;
910
use LogicException;
@@ -60,11 +61,20 @@ protected function disk(): Filesystem
6061
);
6162
}
6263

64+
final public function fullCachePath(): string
65+
{
66+
return App::bootstrapPath('cache/json_schemas.php');
67+
}
68+
6369
/**
6470
* @return array<string, string>
6571
*/
6672
protected function findSchemaFiles(): array
6773
{
74+
if ($this->filesystem->disk('local')->exists($path = $this->fullCachePath())) {
75+
return include $path;
76+
}
77+
6878
// Grab files
6979
$files = $this->disk()->files(
7080
$directory = $this->getConfig('directory'),

src/ServiceProvider.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace DutchCodingCompany\LaravelJsonSchema;
44

5+
use DutchCodingCompany\LaravelJsonSchema\Commands;
56
use DutchCodingCompany\LaravelJsonSchema\Contracts\JsonSchemaValidator;
67
use Illuminate\Contracts\Filesystem\Factory as FilesystemFactory;
78
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
@@ -30,6 +31,21 @@ public function register(): void
3031

3132
public function bootForConsole(): void
3233
{
34+
// Register commands
35+
$this->commands([
36+
Commands\Optimize::class,
37+
Commands\OptimizeClear::class,
38+
]);
39+
40+
if (config('json-schema.auto-cache')) {
41+
// When enabled, auto-cache json schema paths
42+
$this->optimizes(
43+
optimize: 'json-schema:optimize',
44+
clear: 'json-schema:optimize-clear',
45+
key: 'json-schema',
46+
);
47+
}
48+
3349
$this->publishes([
3450
__DIR__.'/../config/json-schema.php' => config_path('json-schema.php'),
3551
], 'json-schema.config');

0 commit comments

Comments
 (0)