Skip to content

Commit

Permalink
Parse file blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioribeiro committed Mar 8, 2020
1 parent 14c51f3 commit 6ed3e5b
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 66 deletions.
64 changes: 0 additions & 64 deletions src/Commands/ListBlockTypes.php

This file was deleted.

70 changes: 70 additions & 0 deletions src/Commands/ListBlocks.php
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);
}
}
3 changes: 3 additions & 0 deletions src/Commands/stubs/blocks/gallery.blade.php
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',
Expand Down
3 changes: 3 additions & 0 deletions src/Commands/stubs/blocks/image.blade.php
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',
Expand Down
3 changes: 3 additions & 0 deletions src/Commands/stubs/blocks/text.blade.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
@title('Text')
@icon('text')

@formField('input', [
'name' => 'title',
'label' => 'Title',
Expand Down
3 changes: 3 additions & 0 deletions src/Commands/stubs/blocks/title.blade.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
@title('Title')
@icon('editor')

@formField('input', [
'name' => 'title',
'label' => 'Title',
Expand Down
112 changes: 112 additions & 0 deletions src/Services/Blocks/Parser.php
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}.");
}
}
4 changes: 2 additions & 2 deletions src/TwillServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use A17\Twill\Commands\ModuleMake;
use A17\Twill\Commands\BlockMake;
use A17\Twill\Commands\ListIcons;
use A17\Twill\Commands\ListBlockTypes;
use A17\Twill\Commands\ListBlocks;
use A17\Twill\Commands\RefreshLQIP;
use A17\Twill\Commands\Update;
use A17\Twill\Http\ViewComposers\ActiveNavigation;
Expand Down Expand Up @@ -273,7 +273,7 @@ private function registerCommands()
ModuleMake::class,
BlockMake::class,
ListIcons::class,
ListBlockTypes::class,
ListBlocks::class,
CreateSuperAdmin::class,
RefreshLQIP::class,
GenerateBlocks::class,
Expand Down

0 comments on commit 6ed3e5b

Please sign in to comment.