Skip to content

Commit

Permalink
Move parser to Block class
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioribeiro committed Mar 8, 2020
1 parent 42053e2 commit 82ef426
Show file tree
Hide file tree
Showing 5 changed files with 226 additions and 215 deletions.
10 changes: 5 additions & 5 deletions src/Commands/BlockMake.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 13 additions & 5 deletions src/Commands/ListBlocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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();
}

/**
Expand All @@ -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);
})
Expand Down
104 changes: 95 additions & 9 deletions src/Services/Blocks/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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();
}
}
116 changes: 113 additions & 3 deletions src/Services/Blocks/BlockCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand All @@ -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;
}
}
Loading

0 comments on commit 82ef426

Please sign in to comment.