Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@
"laravel": {
"providers": [
"A17\\Blast\\BlastServiceProvider"
]
],
"aliases": {
"DataStore": "A17\\Blast\\Facades\\DataStore"
}
}
},
"autoload-dev": {
Expand Down
16 changes: 13 additions & 3 deletions src/BlastServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@

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
{
public function register(): void
{
$this->mergeConfigFrom(__DIR__ . '/../config/blast.php', 'blast');
$this->registerCommands();
$this->registerServices();
}

public function boot(): void
Expand All @@ -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');
Expand Down
16 changes: 5 additions & 11 deletions src/Commands/GenerateStories.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
18 changes: 13 additions & 5 deletions src/DataStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
18 changes: 18 additions & 0 deletions src/Facades/DataStore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace A17\Blast\Facades;

use Illuminate\Support\Facades\Facade;

class DataStore extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'blast.datastore';
}
}