File tree Expand file tree Collapse file tree 6 files changed +110
-0
lines changed Expand file tree Collapse file tree 6 files changed +110
-0
lines changed Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 44
55use Illuminate \Contracts \Filesystem \Factory as FilesystemFactory ;
66use Illuminate \Contracts \Filesystem \Filesystem ;
7+ use Illuminate \Support \Facades \App ;
78use Illuminate \Support \Str ;
89use JsonException ;
910use 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 ' ),
Original file line number Diff line number Diff line change 22
33namespace DutchCodingCompany \LaravelJsonSchema ;
44
5+ use DutchCodingCompany \LaravelJsonSchema \Commands ;
56use DutchCodingCompany \LaravelJsonSchema \Contracts \JsonSchemaValidator ;
67use Illuminate \Contracts \Filesystem \Factory as FilesystemFactory ;
78use 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 ' );
You can’t perform that action at this time.
0 commit comments