Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prepare files, refactor fieldset stub naming
- Loading branch information
Showing
7 changed files
with
162 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Str; | ||
use Illuminate\Support\Facades\File; | ||
use Statamic\Console\RunsInPlease; | ||
use Statamic\Facades\Config; | ||
use Statamic\Support\Arr; | ||
use Stringy\StaticStringy as Stringy; | ||
use Symfony\Component\Yaml\Yaml; | ||
|
||
class AddSet extends Command | ||
{ | ||
use RunsInPlease; | ||
|
||
/** | ||
* The name of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $name = 'peak:add-set'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = "Add an Article (Bard) set."; | ||
|
||
/** | ||
* The set name. | ||
* | ||
* @var string | ||
*/ | ||
protected $set_name = ''; | ||
|
||
/** | ||
* The set filename. | ||
* | ||
* @var string | ||
*/ | ||
protected $filename = ''; | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return bool|null | ||
*/ | ||
public function handle() | ||
{ | ||
$this->set_name = $this->ask('What should be the name for this set?'); | ||
$this->filename = Stringy::slugify($this->set_name, '_', Config::getShortLocale()); | ||
|
||
try { | ||
$this->checkExistence('Fieldset', "resources/fieldsets/{$this->filename}.yaml"); | ||
$this->checkExistence('Partial', "resources/views/page_builder/_{$this->filename}.antlers.html"); | ||
|
||
$this->createFieldset(); | ||
$this->createPartial(); | ||
$this->updatePageBuilder(); | ||
} catch (\Exception $e) { | ||
return $this->error($e->getMessage()); | ||
} | ||
|
||
$this->info("Peak page builder block '{$this->block_name}' added."); | ||
} | ||
|
||
/** | ||
* Check if a file doesn't already exist. | ||
* | ||
* @return bool|null | ||
*/ | ||
protected function checkExistence($type, $path) | ||
{ | ||
if (File::exists(base_path($path))) { | ||
throw new \Exception("{$type} '{$path}' already exists."); | ||
} | ||
} | ||
|
||
/** | ||
* Create fieldset. | ||
* | ||
* @return bool|null | ||
*/ | ||
protected function createFieldset() | ||
{ | ||
$stub = File::get(__DIR__.'/stubs/fieldset_set.yaml.stub'); | ||
$contents = Str::of($stub) | ||
->replace('{{ name }}', $this->block_name); | ||
|
||
File::put(base_path("resources/fieldsets/{$this->filename}.yaml"), $contents); | ||
} | ||
|
||
/** | ||
* Create partial. | ||
* | ||
* @return bool|null | ||
*/ | ||
protected function createPartial() | ||
{ | ||
$stub = File::get(__DIR__.'/stubs/block.html.stub'); | ||
$contents = Str::of($stub) | ||
->replace('{{ name }}', $this->block_name) | ||
->replace('{{ filename }}', $this->filename); | ||
|
||
File::put(base_path("resources/views/page_builder/_{$this->filename}.antlers.html"), $contents); | ||
} | ||
|
||
/** | ||
* Update page_builder.yaml. | ||
* | ||
* @return bool|null | ||
*/ | ||
protected function updatePageBuilder() | ||
{ | ||
$fieldset = Yaml::parseFile(base_path('resources/fieldsets/page_builder.yaml')); | ||
$newSet = [ | ||
'display' => $this->block_name, | ||
'instructions' => $this->instructions, | ||
'fields' => [ | ||
[ | ||
'import' => $this->filename | ||
] | ||
] | ||
]; | ||
|
||
$existingSets = Arr::get($fieldset, 'fields.0.field.sets'); | ||
$existingSets[$this->filename] = $newSet; | ||
$existingSets = collect($existingSets)->sortBy(function ($value, $key) { | ||
return $key; | ||
})->all(); | ||
|
||
Arr::set($fieldset, 'fields.0.field.sets', $existingSets); | ||
|
||
File::put(base_path('resources/fieldsets/page_builder.yaml'), Yaml::dump($fieldset, 99, 2)); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
title: '{{ name }}' | ||
fields: [ ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
title: '{{ name }}' | ||
fields: | ||
- | ||
handle: size | ||
field: common.size | ||
config: | ||
instructions: 'The size in which the quote should be displayed.' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{{# | ||
@name {{ name }} | ||
@desc The {{ name }} component. | ||
@set page.article.{{ filename }} | ||
#}} | ||
|
||
<div class="size-{{ size }} my-4"> | ||
<h2>🔧<br>{{ name }}</h2> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters