Examples

Practical examples of using WPZylos CLI Core.

Complete Controller Generator

<?php

namespace MyPlugin\Generators;

use WPZylos\Framework\Cli\Core\Generator;

class ControllerGenerator extends Generator
{
    public function generate(string $name, array $options = []): array
    {
        $className = $this->toClassName($name);
        $namespace = $options['namespace'] ?? 'MyPlugin\\Http\\Controllers';
        $viewPath = $options['view'] ?? strtolower($name);

        $content = $this->compiler->compile('controller', [
            'class'     => $className,
            'namespace' => $namespace,
            'view'      => $viewPath,
        ]);

        $path = $this->getOutputPath($name);
        $this->writer->write($path, $content);

        return [$path];
    }

    protected function getStubName(): string
    {
        return 'controller';
    }

    protected function getOutputPath(string $name): string
    {
        $className = $this->toClassName($name);
        return $this->basePath . "/app/Http/Controllers/{$className}Controller.php";
    }
}

Migration Generator

<?php

namespace MyPlugin\Generators;

use WPZylos\Framework\Cli\Core\Generator;

class MigrationGenerator extends Generator
{
    public function generate(string $name, array $options = []): array
    {
        $timestamp = date('Y_m_d_His');
        $className = 'Create' . $this->toClassName($name) . 'Table';
        $table = $options['table'] ?? strtolower($name);

        $content = $this->compiler->compile('migration', [
            'class' => $className,
            'table' => $table,
        ]);

        $filename = "{$timestamp}_{$this->toSnakeCase($name)}.php";
        $path = $this->basePath . "/database/migrations/{$filename}";

        $this->writer->write($path, $content);

        return [$path];
    }

    protected function getStubName(): string
    {
        return 'migration';
    }

    protected function getOutputPath(string $name): string
    {
        return ''; // Dynamic path generation in generate()
    }

    private function toSnakeCase(string $name): string
    {
        return strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $name));
    }
}

Service Generator with Multiple Files

<?php

namespace MyPlugin\Generators;

use WPZylos\Framework\Cli\Core\Generator;

class ServiceGenerator extends Generator
{
    public function generate(string $name, array $options = []): array
    {
        $className = $this->toClassName($name);
        $files = [];

        // Generate service
        $serviceContent = $this->compiler->compile('service', [
            'class'     => $className,
            'namespace' => 'MyPlugin\\Services',
        ]);
        $servicePath = $this->basePath . "/app/Services/{$className}Service.php";
        $this->writer->write($servicePath, $serviceContent);
        $files[] = $servicePath;

        // Generate interface
        if ($options['interface'] ?? true) {
            $interfaceContent = $this->compiler->compile('service-interface', [
                'class'     => $className,
                'namespace' => 'MyPlugin\\Contracts',
            ]);
            $interfacePath = $this->basePath . "/app/Contracts/{$className}ServiceInterface.php";
            $this->writer->write($interfacePath, $interfaceContent);
            $files[] = $interfacePath;
        }

        return $files;
    }

    protected function getStubName(): string
    {
        return 'service';
    }

    protected function getOutputPath(string $name): string
    {
        return '';
    }
}

CLI Command Using Generators

<?php

namespace MyPlugin\Commands;

use WPZylos\Framework\Cli\Core\StubCompiler;
use WPZylos\Framework\Cli\Core\FileWriter;
use MyPlugin\Generators\ControllerGenerator;

class MakeControllerCommand
{
    public function handle(array $args): int
    {
        $name = $args['name'] ?? null;

        if (!$name) {
            echo "Error: Name is required\n";
            return 1;
        }

        $compiler = new StubCompiler(__DIR__ . '/../../stubs');
        $writer = new FileWriter();
        $generator = new ControllerGenerator(
            $compiler,
            $writer,
            dirname(__DIR__, 2)
        );

        try {
            $files = $generator->generate($name, [
                'namespace' => 'MyPlugin\\Http\\Controllers',
            ]);

            foreach ($files as $file) {
                echo "Created: {$file}\n";
            }

            return 0;
        } catch (\Exception $e) {
            echo "Error: {$e->getMessage()}\n";
            return 1;
        }
    }
}

Custom Stub Template

<?php
// stubs/service.stub

declare(strict_types=1);

namespace {{namespace}};

/**
 * {{class}} service.
 *
 * @package {{namespace}}
 */
class {{class}}Service
{
    /**
     * Create instance.
     */
    public function __construct()
    {
        //
    }

    /**
     * Get all items.
     *
     * @return array<int, mixed>
     */
    public function all(): array
    {
        return [];
    }

    /**
     * Find by ID.
     *
     * @param int $id
     * @return mixed|null
     */
    public function find(int $id): mixed
    {
        return null;
    }
}

Batch Generation

<?php

use WPZylos\Framework\Cli\Core\StubCompiler;
use WPZylos\Framework\Cli\Core\FileWriter;

$compiler = new StubCompiler(__DIR__ . '/stubs');
$compiler->setDefaults([
    'namespace'  => 'MyPlugin',
    'textDomain' => 'my-plugin',
]);

$writer = new FileWriter();

// Generate multiple controllers
$controllers = ['User', 'Product', 'Order', 'Cart'];

foreach ($controllers as $name) {
    $content = $compiler->compile('controller', [
        'class' => $name,
        'view'  => strtolower($name),
    ]);

    $path = __DIR__ . "/app/Http/Controllers/{$name}Controller.php";
    $writer->writeIfNotExists($path, $content);

    echo "Generated: {$path}\n";
}