diff --git a/composer.json b/composer.json index cc4e96c..2094373 100644 --- a/composer.json +++ b/composer.json @@ -41,7 +41,10 @@ "laravel": { "providers": [ "A17\\Blast\\BlastServiceProvider" - ] + ], + "aliases": { + "DataStore": "A17\\Blast\\Facades\\DataStore" + } } }, "autoload-dev": { diff --git a/src/BlastServiceProvider.php b/src/BlastServiceProvider.php index 5279c9c..f10f8e0 100644 --- a/src/BlastServiceProvider.php +++ b/src/BlastServiceProvider.php @@ -2,15 +2,17 @@ namespace A17\Blast; +use A17\Blast\DataStore; +use Illuminate\Support\Str; use A17\Blast\Commands\Demo; -use A17\Blast\Commands\GenerateStories; -use A17\Blast\Commands\GenerateUIDocs; use A17\Blast\Commands\Launch; use A17\Blast\Commands\Publish; use Illuminate\Support\Facades\Blade; +use A17\Blast\Commands\GenerateUIDocs; +use A17\Blast\Commands\GenerateStories; use Illuminate\Support\ServiceProvider; +use Illuminate\Filesystem\Filesystem; use Illuminate\View\Compilers\BladeCompiler; -use Illuminate\Support\Str; final class BlastServiceProvider extends ServiceProvider { @@ -18,6 +20,7 @@ public function register(): void { $this->mergeConfigFrom(__DIR__ . '/../config/blast.php', 'blast'); $this->registerCommands(); + $this->registerServices(); } public function boot(): void @@ -43,6 +46,13 @@ private function registerCommands(): void } } + private function registerServices(): void + { + $this->app->singleton('blast.datastore', function () { + return new DataStore($this->app->make(FileSystem::class)); + }); + } + private function bootResources(): void { $this->loadViewsFrom(__DIR__ . '/../resources/views', 'blast'); diff --git a/src/Commands/GenerateStories.php b/src/Commands/GenerateStories.php index a3f0249..8efc41f 100644 --- a/src/Commands/GenerateStories.php +++ b/src/Commands/GenerateStories.php @@ -6,7 +6,7 @@ use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Arr; use Illuminate\Support\Str; -use A17\Blast\DataStore; +use A17\Blast\Facades\DataStore; use A17\Blast\Traits\Helpers; class GenerateStories extends Command @@ -32,19 +32,13 @@ class GenerateStories extends Command */ protected $filesystem; - /** - * @var DataStore - */ - protected $dataStore; - /** * @param Filesystem $filesystem */ - public function __construct(Filesystem $filesystem, DataStore $dataStore) + public function __construct(Filesystem $filesystem) { parent::__construct(); - $this->dataStore = $dataStore; $this->filesystem = $filesystem; $this->storyViewsPath = base_path('resources/views/stories'); $this->vendorPath = $this->getVendorPath(); @@ -321,7 +315,7 @@ private function buildChildTemplate($item) $options = $item['options']; if (Arr::has($options, 'preset')) { - $preset = $this->dataStore->get($options['preset']); + $preset = DataStore::get($options['preset']); if (is_array($preset) && !empty($preset)) { foreach ($preset as $key => $settings) { @@ -345,10 +339,10 @@ private function buildChildTemplate($item) foreach ($presetArgs as $key => $preset) { if (is_array($preset)) { $args = array_map(function ($item) { - return $this->dataStore->get($item)['args'] ?? []; + return DataStore::get($item)['args'] ?? []; }, $preset); } else { - $args = $this->dataStore->get($preset)['args'] ?? []; + $args = DataStore::get($preset)['args'] ?? []; } $options['args'][$key] = $args; diff --git a/src/DataStore.php b/src/DataStore.php index c6b0962..6412a5d 100644 --- a/src/DataStore.php +++ b/src/DataStore.php @@ -30,17 +30,22 @@ public function __construct(Filesystem $filesystem) $this->filesystem = $filesystem; $this->filesystem->ensureDirectoryExists($this->dataPath); - $this->getComponentsData(); } public function get($key = null) { + $filename = explode('.', $key); + + if (empty($this->data[$filename[0]])) { + $this->getComponentData($filename[0]); + } + if ($key) { return Arr::get($this->data, $key); } } - private function getComponentsData() + private function getComponentData($key) { if (!$this->filesystem->exists($this->dataPath)) { return 1; @@ -53,15 +58,18 @@ private function getComponentsData() if ($file->getExtension() == 'php') { $filename = str_replace('.php', '', $file->getFilename()); - $this->data[$filename] = include base_path( - $file->getPathname(), - ); + if ($key === $filename) { + $this->data[$filename] = include base_path( + $file->getPathname(), + ); + } } } $this->data = array_map([$this, 'parsePresetArgs'], $this->data); } } + private function parsePresetArgs($data = null) { $parsed = array_map(function ($item) { diff --git a/src/Facades/DataStore.php b/src/Facades/DataStore.php new file mode 100644 index 0000000..3c30dd4 --- /dev/null +++ b/src/Facades/DataStore.php @@ -0,0 +1,18 @@ +