-
Notifications
You must be signed in to change notification settings - Fork 578
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
14c51f3
commit 6ed3e5b
Showing
8 changed files
with
196 additions
and
66 deletions.
There are no files selected for viewing
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,70 @@ | ||
<?php | ||
|
||
namespace A17\Twill\Commands; | ||
|
||
use Illuminate\Support\Str; | ||
use Illuminate\Console\Command; | ||
use A17\Twill\Services\Blocks\Parser as BlocksParser; | ||
|
||
class ListBlocks extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = | ||
'twill:list:blocks ' . | ||
'{--t|--twill : List only Twill\'s internal blocks} ' . | ||
'{--c|--custom : List only user custom blocks} ' . | ||
'{--a|--app : List only legacy application blocks}' . | ||
'{--b|--blocks : List only blocks}' . | ||
'{--r|--repeaters : List only repeaters}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'List all available Twill blocks'; | ||
|
||
public function __construct(BlocksParser $blocksParser) | ||
{ | ||
parent::__construct(); | ||
|
||
$this->blocksParser = $blocksParser; | ||
} | ||
|
||
/** | ||
* Executes the console command. | ||
* | ||
* @return mixed | ||
*/ | ||
public function handle() | ||
{ | ||
$sourceFiltered = | ||
$this->option('twill') || | ||
$this->option('custom') || | ||
$this->option('app'); | ||
|
||
$typeFiltered = $this->option('blocks') || $this->option('repeaters'); | ||
|
||
$blocks = $this->blocksParser | ||
->all() | ||
->reject(function ($block) use ($sourceFiltered) { | ||
return $sourceFiltered && !$this->option($block['source']); | ||
}) | ||
->reject(function ($block) use ($typeFiltered) { | ||
return $typeFiltered && !$this->option(Str::plural($block['type'])); | ||
}); | ||
|
||
$headers = $blocks | ||
->first() | ||
->keys() | ||
->map(function ($key) { | ||
return Str::studly($key); | ||
}); | ||
|
||
$this->table($headers, $blocks); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,6 @@ | ||
@title('Gallery') | ||
@icon('editor') | ||
|
||
@formField('select', [ | ||
'name' => 'variation', | ||
'label' => 'Gallery variation', | ||
|
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 |
---|---|---|
@@ -1,3 +1,6 @@ | ||
@title('Image') | ||
@icon('image') | ||
|
||
@formField('medias', [ | ||
'name' => 'image', // role | ||
'label' => 'Image', | ||
|
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 |
---|---|---|
@@ -1,3 +1,6 @@ | ||
@title('Text') | ||
@icon('text') | ||
|
||
@formField('input', [ | ||
'name' => 'title', | ||
'label' => 'Title', | ||
|
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 |
---|---|---|
@@ -1,3 +1,6 @@ | ||
@title('Title') | ||
@icon('editor') | ||
|
||
@formField('input', [ | ||
'name' => 'title', | ||
'label' => 'Title', | ||
|
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,112 @@ | ||
<?php | ||
|
||
namespace A17\Twill\Services\Blocks; | ||
|
||
use A17\Twill\Commands\Behaviors\Blocks; | ||
use Illuminate\Config\Repository as Config; | ||
use Illuminate\Filesystem\Filesystem; | ||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\Composer; | ||
use Illuminate\Support\Str; | ||
|
||
class Parser | ||
{ | ||
/** | ||
* @var \Illuminate\Support\Collection | ||
*/ | ||
protected $blocks; | ||
|
||
/** | ||
* @var \Illuminate\Support\Collection | ||
*/ | ||
protected $paths; | ||
|
||
/** | ||
* @var Filesystem | ||
*/ | ||
protected $files; | ||
|
||
/** | ||
* @param Filesystem $files | ||
*/ | ||
public function __construct(Filesystem $files) | ||
{ | ||
$this->files = $files; | ||
|
||
$this->generatePaths()->parseAllBlocks(); | ||
} | ||
|
||
public function all() | ||
{ | ||
return $this->blocks; | ||
} | ||
|
||
public function parseAllBlocks() | ||
{ | ||
$this->blocks = collect($this->paths)->reduce(function ($keep, $path) { | ||
return $keep->merge($this->listBlocks($path['path'], $path['source'], $path['type'])); | ||
}, collect()); | ||
} | ||
|
||
public function generatePaths() | ||
{ | ||
$this->paths = [ | ||
['path' => config_path('twill-utils/blocks'), 'source' => 'custom', 'type' => 'block'], | ||
['path' => config_path('twill-utils/repeaters'), 'source' => 'custom', 'type' => 'repeater'], | ||
['path' => __DIR__ . '/../../Commands/stubs/blocks', 'source' => 'twill', 'type' => 'block'], | ||
['path' => __DIR__ . '/../../Commands/stubs/repeaters', 'source' => 'twill', 'type' => 'repeater'], | ||
['path' => resource_path('views/admin/blocks'), 'source' => 'app', 'type' => null], | ||
]; | ||
|
||
return $this; | ||
} | ||
|
||
public function listBlocks($directory, $source, $type = null) | ||
{ | ||
return collect($this->files->files($directory))->map(function ($file) use ($source, $type) { | ||
return $this->parseFile($file, $type)->merge(['source' => $source]); | ||
}); | ||
} | ||
|
||
public function parseFile($file, $type = null) | ||
{ | ||
$block = file_get_contents((string) $file); | ||
|
||
$blockName = Str::before($file->getFilename(),'.blade.php'); | ||
|
||
[$title, $inferredType] = $this->getProperty('title', $block, $blockName); | ||
[$icon] = $this->getProperty('icon', $block, $blockName); | ||
[$trigger] = $this->getProperty('trigger', $block, $blockName); | ||
|
||
return collect([ | ||
'title' => $title, | ||
'trigger' => $trigger, | ||
'name' => $blockName, | ||
'type' => $type ?? $inferredType, | ||
'icon' => $icon, | ||
]); | ||
} | ||
|
||
public function getProperty($property, $block, $blockName) | ||
{ | ||
preg_match("/@{$property}\(\'(.*)\'\)/", $block, $matches); | ||
|
||
if (filled($matches)) { | ||
return [$matches[1], 'block']; | ||
} | ||
|
||
if ($value = config("twill.block_editor.blocks.{$blockName}.{$property}")) { | ||
return [$value, 'block']; | ||
} | ||
|
||
if ($value = config("twill.block_editor.repeaters.{$blockName}.{$property}")) { | ||
return [$value, 'repeater']; | ||
} | ||
|
||
if ($property !== 'title') { | ||
return [null, null]; | ||
} | ||
|
||
throw new \Exception("Property '{$property}' not found on block {$blockName}."); | ||
} | ||
} |
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