From 82ef4262b481b5007618a562c8d5b30f0b025af7 Mon Sep 17 00:00:00 2001 From: Antonio Carlos Ribeiro Date: Thu, 5 Mar 2020 00:03:44 +0100 Subject: [PATCH] Move parser to Block class --- src/Commands/BlockMake.php | 10 +- src/Commands/ListBlocks.php | 18 ++- src/Services/Blocks/Block.php | 104 +++++++++++-- src/Services/Blocks/BlockCollection.php | 116 +++++++++++++- src/Services/Blocks/Parser.php | 193 ------------------------ 5 files changed, 226 insertions(+), 215 deletions(-) delete mode 100644 src/Services/Blocks/Parser.php diff --git a/src/Commands/BlockMake.php b/src/Commands/BlockMake.php index b5547cdd8..911f66912 100644 --- a/src/Commands/BlockMake.php +++ b/src/Commands/BlockMake.php @@ -6,7 +6,7 @@ use Illuminate\Console\Command; use A17\Twill\Services\Blocks\Block; use Illuminate\Filesystem\Filesystem; -use A17\Twill\Services\Blocks\Parser; +use A17\Twill\Services\Blocks\BlockCollection; class BlockMake extends Command { @@ -34,15 +34,15 @@ class BlockMake extends Command /** * @param Filesystem $files - * @param Parser $blockParser + * @param BlockCollection $blockCollection */ - public function __construct(Filesystem $files, Parser $blockParser) + public function __construct(Filesystem $files, BlockCollection $blockCollection) { parent::__construct(); $this->files = $files; - $this->blockParser = $blockParser; + $this->blockCollection = $blockCollection; } /** @@ -119,7 +119,7 @@ public function getBlockFile($name) public function getBlockByName($block, $sources = []) { - return $this->blockParser->all()->findByName($block, $sources); + return $this->blockCollection->all()->findByName($block, $sources); } public function getIconFile($icon) diff --git a/src/Commands/ListBlocks.php b/src/Commands/ListBlocks.php index 7ce76b0bc..2af4ef9e5 100644 --- a/src/Commands/ListBlocks.php +++ b/src/Commands/ListBlocks.php @@ -4,7 +4,7 @@ use Illuminate\Support\Str; use Illuminate\Console\Command; -use A17\Twill\Services\Blocks\Parser as BlocksParser; +use A17\Twill\Services\Blocks\BlockCollection; class ListBlocks extends Command { @@ -28,11 +28,20 @@ class ListBlocks extends Command */ protected $description = 'List all available Twill blocks'; - public function __construct(BlocksParser $blocksParser) + /** + * Blocks collection. + * + * @var BlockCollection + */ + protected $blocks; + + public function __construct(BlockCollection $blocks) { parent::__construct(); - $this->blocksParser = $blocksParser; + $this->blocks = $blocks; + + $this->blocks->parse(); } /** @@ -47,8 +56,7 @@ protected function getBlocks() $typeFiltered = $this->option('blocks') || $this->option('repeaters'); - $blocks = $this->blocksParser - ->all() + $blocks = $this->blocks ->reject(function ($block) use ($sourceFiltered) { return $sourceFiltered && !$this->option($block->source); }) diff --git a/src/Services/Blocks/Block.php b/src/Services/Blocks/Block.php index 7c3f14c39..814f817cf 100644 --- a/src/Services/Blocks/Block.php +++ b/src/Services/Blocks/Block.php @@ -57,19 +57,20 @@ class Block */ public $file; - /** - * @var string - */ - public $fileName; - /** * @var string */ public $contents; - public function __construct($blockData = []) + public function __construct($file, $type, $source) { - $this->absorbData($blockData); + $this->file = $file; + + $this->type = $type; + + $this->source = $source; + + $this->parse(); } public function absorbData($data) @@ -85,10 +86,22 @@ public function absorbData($data) $this->icon = $data['icon']; $this->isNewFormat = $data['new_format']; $this->inferredType = $data['inferred_type']; - $this->file = $data['file']; $this->contents = $data['contents']; - $this->fileName = $this->file->getFilename(); + return $this; + } + + /** + * @param string $path + * @return Block + */ + public function setPath(string $path): Block + { + $this->path = $path; + + $this->parse(); + + return $this; } public function setSource($source) @@ -127,4 +140,77 @@ public function legacyArray() ], ]; } + + public function parse() + { + $contents = file_get_contents((string) $this->file->getPathName()); + + $name = Str::before($this->file->getFilename(), '.blade.php'); + + [$title, $inferredType] = $this->parseProperty( + 'title', + $contents, + $name + ); + + [$icon] = $this->parseProperty('icon', $contents, $name); + + [$trigger] = $this->parseProperty('trigger', $contents, $name); + + return $this->absorbData([ + 'title' => $title, + 'trigger' => $trigger, + 'name' => $name, + 'type' => $type ?? $inferredType, + 'icon' => $icon, + 'new_format' => $this->isUpgradedBlock($contents), + 'inferred_type' => $inferredType, + 'contents' => $contents, + ]); + } + + public function parseProperty($property, $block, $blockName) + { + preg_match("/@tw-{$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}." + ); + } + + public function isUpgradedBlock($block) + { + preg_match("/@tw-.*\(\'(.*)\'\)/", $block, $matches); + + return filled($matches); + } + + public function getFileName() + { + return $this->file->getFileName(); + } } diff --git a/src/Services/Blocks/BlockCollection.php b/src/Services/Blocks/BlockCollection.php index f4fdc9668..bacfdf8f4 100644 --- a/src/Services/Blocks/BlockCollection.php +++ b/src/Services/Blocks/BlockCollection.php @@ -2,17 +2,36 @@ namespace A17\Twill\Services\Blocks; -use Illuminate\Support\Str; use Illuminate\Support\Collection; +use Illuminate\Filesystem\Filesystem; class BlockCollection extends Collection { + /** + * @var \Illuminate\Support\Collection + */ + protected $paths; + + /** + * @var Filesystem + */ + protected $files; + + /** + * @param mixed $items + * @param Filesystem $files + */ + public function __construct($items = [], Filesystem $files = null) + { + parent::__construct($items); + + $this->files = $files; + } + public function findByName($search, $sources = []) { $block = new Block(); - $name = $block->makeName($search); - return (new static($this->items)) ->filter(function ($block) use ($search, $sources) { return $block->name == $search && @@ -24,4 +43,95 @@ public function findByName($search, $sources = []) }) ->first(); } + + public function getAllowedBlocksList() + { + return $this->all()->mapWithKeys(function ($block) { + return $block->legacyArray(); + }); + } + + public function listBlocks($directory, $source, $type = null) + { + if (!$this->files->exists($directory)) { + return collect(); + } + + return collect($this->files->files($directory))->map(function ( + $file + ) use ($source, $type) { + return new Block($file, $type, $source); + }); + } + + public function generatePaths() + { + $this->paths = [ + [ + 'path' => __DIR__ . '/../../Commands/stubs/blocks', + 'source' => Block::SOURCE_TWILL, + 'type' => 'block', + ], + [ + 'path' => __DIR__ . '/../../Commands/stubs/repeaters', + 'source' => Block::SOURCE_TWILL, + 'type' => 'repeater', + ], + [ + 'path' => resource_path('views/admin/blocks'), + 'source' => Block::SOURCE_APP, + 'type' => null, + ], + [ + 'path' => resource_path('views/admin/repeaters'), + 'source' => Block::SOURCE_APP, + 'type' => 'repeater', + ], + ]; + + return $this; + } + + public function detectCustomSources($block) + { + if ($block->source === Block::SOURCE_APP) { + if ( + $this->all() + ->where('fileName', $block->getFileName()) + ->where('source', Block::SOURCE_TWILL) + ->isNotEmpty() + ) { + return Block::SOURCE_CUSTOM; + } + } + + return $block->source; + } + + public function parse() + { + $this->generatePaths(); + + $this->items = collect($this->paths)->reduce(function ($keep, $path) { + $this->listBlocks( + $path['path'], + $path['source'], + $path['type'] + )->each(function ($block) use ($keep) { + $keep->push($block); + + return $keep; + }); + + return $keep; + }, collect()); + + $this->items = $this->items + ->each(function ($block) { + $block->setSource($this->detectCustomSources($block)); + }) + ->toArray(); + + return $this; + } } diff --git a/src/Services/Blocks/Parser.php b/src/Services/Blocks/Parser.php deleted file mode 100644 index a8f63766f..000000000 --- a/src/Services/Blocks/Parser.php +++ /dev/null @@ -1,193 +0,0 @@ -files = $files; - } - - public function all() - { - return $this->parse()->blocks; - } - - public function parse() - { - $this->generatePaths(); - - $this->blocks = (new BlockCollection($this->paths))->reduce(function ( - $keep, - $path - ) { - $this->listBlocks( - $path['path'], - $path['source'], - $path['type'] - )->each(function ($block) use ($keep) { - $keep->push($block); - - return $keep; - }); - - return $keep; - }, - new BlockCollection()); - - $this->blocks = $this->blocks->each(function ($block) { - $block->setSource($this->detectCustomSources($block)); - }); - - return $this; - } - - public function detectCustomSources($block) - { - if ($block->source === Block::SOURCE_APP) { - if ( - $this->blocks - ->where('fileName', $block->fileName) - ->where('source', Block::SOURCE_TWILL) - ->isNotEmpty() - ) { - return Block::SOURCE_CUSTOM; - } - } - - return $block->source; - } - - public function generatePaths() - { - $this->paths = [ - [ - 'path' => __DIR__ . '/../../Commands/stubs/blocks', - 'source' => Block::SOURCE_TWILL, - 'type' => 'block', - ], - [ - 'path' => __DIR__ . '/../../Commands/stubs/repeaters', - 'source' => Block::SOURCE_TWILL, - 'type' => 'repeater', - ], - [ - 'path' => resource_path('views/admin/blocks'), - 'source' => Block::SOURCE_APP, - 'type' => null, - ], - [ - 'path' => resource_path('views/admin/repeaters'), - 'source' => Block::SOURCE_APP, - 'type' => 'repeater', - ], - ]; - - return $this; - } - - public function listBlocks($directory, $source, $type = null) - { - if (!$this->files->exists($directory)) { - return collect(); - } - - return collect($this->files->files($directory))->map(function ( - $file - ) use ($source, $type) { - return $this->parseFile($file, $type)->setSource($source); - }); - } - - public function parseFile($file, $type = null) - { - $contents = file_get_contents((string) $file); - - $name = Str::before($file->getFilename(), '.blade.php'); - - [$title, $inferredType] = $this->getProperty('title', $contents, $name); - [$icon] = $this->getProperty('icon', $contents, $name); - [$trigger] = $this->getProperty('trigger', $contents, $name); - - return new Block([ - 'title' => $title, - 'trigger' => $trigger, - 'name' => $name, - 'type' => $type ?? $inferredType, - 'icon' => $icon, - 'new_format' => $this->isUpgradedBlock($contents), - 'inferred_type' => $inferredType, - 'file' => $file, - 'contents' => $contents, - ]); - } - - public function getProperty($property, $block, $blockName) - { - preg_match("/@tw-{$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}." - ); - } - - public function isUpgradedBlock($block) - { - preg_match("/@tw-.*\(\'(.*)\'\)/", $block, $matches); - - return filled($matches); - } - - public function getAllowedBlocksList() - { - return $this->all()->mapWithKeys(function ($block) { - return $block->legacyArray(); - }); - } -}