Usage

This guide covers using the CLI Core components.

StubCompiler

Basic Usage

use WPZylos\Framework\Cli\Core\StubCompiler;

// Create compiler with path to stubs directory
$compiler = new StubCompiler('/path/to/stubs');

// Compile a stub with replacements
$content = $compiler->compile('controller', [
    'namespace' => 'MyPlugin\\Http\\Controllers',
    'class'     => 'Product',
    'view'      => 'products',
]);

Default Replacements

Set values that apply to all compilations:

$compiler->setDefaults([
    'namespace'  => 'MyPlugin',
    'textDomain' => 'my-plugin',
]);

// These will be included automatically
$content = $compiler->compile('controller', [
    'class' => 'Product', // namespace and textDomain added automatically
]);

Plugin Context Compilation

Compile with plugin-specific tokens:

$content = $compiler->compileForPlugin(
    'controller',
    slug: 'my-plugin',
    prefix: 'myplugin_',
    textDomain: 'my-plugin',
    namespace: 'MyPlugin',
    extra: [
        'class' => 'Product',
        'view'  => 'products',
    ]
);

This automatically adds:

  • {{slug}} — Plugin slug
  • {{prefix}} — Database prefix
  • {{textDomain}} — Text domain
  • {{namespace}} — PHP namespace
  • {{Slug}} — PascalCase slug
  • {{PREFIX}} — Uppercase prefix

List Available Stubs

$stubs = $compiler->getAvailable();
// ['controller', 'migration', 'request', ...]

FileWriter

Basic Writing

use WPZylos\Framework\Cli\Core\FileWriter;

$writer = new FileWriter();
$writer->write('/path/to/File.php', $content);

Prevent Overwriting

// Default: throws exception if file exists
$writer = new FileWriter(overwrite: false);

try {
    $writer->write('/path/to/existing.php', $content);
} catch (\RuntimeException $e) {
    echo "File already exists!";
}

Write If Not Exists

// Returns false if file exists, true if written
$written = $writer->writeIfNotExists('/path/to/File.php', $content);

if (!$written) {
    echo "File already exists, skipped.";
}

Enable Overwrite

$writer->setOverwrite(true);
$writer->write('/path/to/File.php', $content); // Overwrites existing

Custom Directory Permissions

$writer = new FileWriter(overwrite: false, dirPermissions: 0775);

Generator Base

Creating a Custom Generator

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

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

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

        $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";
    }
}

Using the Generator

$compiler = new StubCompiler('/path/to/stubs');
$writer = new FileWriter();
$generator = new ControllerGenerator($compiler, $writer, '/path/to/plugin');

$files = $generator->generate('product');
// ['/path/to/plugin/app/Http/Controllers/ProductController.php']

Name Conversion Helpers

$generator->toClassName('my-thing');    // 'MyThing'
$generator->toClassName('my_thing');    // 'MyThing'
$generator->toVariableName('my-thing'); // 'myThing'

Next Steps