src/Blocks/BlockRuntimeDescription.php line 52

Open in your IDE?
  1. <?php
  2. namespace App\Blocks;
  3. use Symfony\Component\Yaml\Yaml;
  4. use Twig\Environment;
  5. class BlockRuntimeDescription
  6. {
  7.     private const TEMPLATE_FILE_NAME 'block.html.twig';
  8.     public string $path;
  9.     public string $name;
  10.     public string $version;
  11.     public string $title;
  12.     public string $description;
  13.     public string $category;
  14.     public array $keywords = [];
  15.     public bool $overriden false;
  16.     public string $iconPath;
  17.     public string $icon;
  18.     public ?string $overridenClass null;
  19.     public function __construct(string $basePathstring $blockName)
  20.     {
  21.         $yaml Yaml::parseFile($basePath DIRECTORY_SEPARATOR "$blockNameDIRECTORY_SEPARATOR lcfirst('block.yml'));
  22.         if (!isset($yaml['version'], $yaml['title'], $yaml['description'], $yaml['category'])) {
  23.             throw new \InvalidArgumentException("Block $blockName is missing required fields in block.yml file.");
  24.         }
  25.         $this->path $basePath DIRECTORY_SEPARATOR $blockName;
  26.         $this->name $blockName;
  27.         $this->version $yaml['version'];
  28.         $this->title $yaml['title'];
  29.         $this->description $yaml['description'];
  30.         $this->category $yaml['category'];
  31.         $this->keywords $yaml['keywords'] ?? [];
  32.         $this->iconPath $this->path DIRECTORY_SEPARATOR . ($yaml['icon'] ?? 'assets/icon.svg');
  33.         if (file_exists($this->iconPath)) {
  34.             $this->icon file_get_contents($this->iconPath);
  35.         }
  36.     }
  37.     public function register(callable $callback)
  38.     {
  39.         $config = [
  40.             'name' => $this->name,
  41.             'title' => __($this->title),
  42.             'description' => __($this->description),
  43.             'render_callback' => function ($block$content ''$preview false$postId 0$wpBlock null$context false) use ($callback) {
  44.                 $callback($block$preview$context);
  45.             },
  46.             'category' => $this->category,
  47.             'keywords' => $this->keywords,
  48.             'class' => $this->name,
  49.             'path' => $this->path,
  50.             'mode' => 'edit',
  51.             'supports' => ['anchor' => true'align' => ['wide''full'], 'spacing' => ['margin' => ['top''bottom'], 'padding' => true], 'color' => ['text' => true'background' => true'gradients' => true]],
  52.             'icon' => $this->icon
  53.         ];
  54.         acf_register_block_type($config);
  55.     }
  56.     public function override(string $namespacestring $newBasePath)
  57.     {
  58.         // Try to override the class
  59.         $classPath $newBasePath DIRECTORY_SEPARATOR $this->name DIRECTORY_SEPARATOR $this->name '.php';
  60.         if (file_exists($classPath)) {
  61.             $this->overridenClass $namespace '\\' $this->name '\\' $this->name;
  62.         }
  63.         // Try to override the configuration
  64.         $configPath $newBasePath DIRECTORY_SEPARATOR $this->name DIRECTORY_SEPARATOR 'block.yml';
  65.         if (file_exists($configPath)) {
  66.             $this->overrideConfiguration($namespaceYaml::parseFile($configPath));
  67.         }
  68.     }
  69.     public function overrideConfiguration(string $namespace, array $yaml)
  70.     {
  71.         $this->overriden true;
  72.         foreach ($yaml as $key => $value) {
  73.             $this->$key $value;
  74.         }
  75.     }
  76.     public function getTemplate()
  77.     {
  78.         return '@' $this->name '/' self::TEMPLATE_FILE_NAME;
  79.     }
  80. }