Skip to content

Commit

Permalink
Merge pull request #3 from digital-brew/sp-update
Browse files Browse the repository at this point in the history
Update makeInstance method to work recursively on all blocks.
  • Loading branch information
yarovikov authored Nov 2, 2024
2 parents d4767f8 + 7ad7880 commit 4057e05
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/Providers/BlockServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Yarovikov\Gutengood\Providers;

use Illuminate\Support\ServiceProvider;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use WP_Block_Type_Registry;

class BlockServiceProvider extends ServiceProvider
Expand Down Expand Up @@ -46,24 +48,28 @@ public function boot(): void
public function makeInstances(): void
{
$this->blocks = collect();
collect(glob($this->app->basePath('app/' . $this->folder . '/*.php')))->map(
function (string $file): void {
$src = $this->formatFile($this->folder, $file);

$directory = $this->app->basePath('app/' . $this->folder);
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));

foreach ($iterator as $file) {
if ($file->isFile() && $file->getExtension() === 'php') {
$src = $this->formatFile($this->folder, $file->getPathname());

$this->app->bind("block.$src->handle", function () use ($src) {
return new $src->class();
});

$this->blocks->push("block.$src->handle");
}
);
}
}

public function formatFile(string $class, string $file): object
{
return (object) [
'handle' => substr(strtolower(basename(preg_replace('/[A-Z]/', '-$0', $file), '.php')), 1),
'class' => '\\App\\' . str_replace('/', '\\', $class) . '\\' . basename($file, '.php'),
'class' => '\\App\\' . str_replace('/', '\\', str_replace('.php', '', str_replace('/app/', '',strstr($file, '/app/Editor/')))),
];
}

Expand Down

0 comments on commit 4057e05

Please sign in to comment.