PHPackages                             wpdiggerstudio/wpzylos-cli-core - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [CLI &amp; Console](/categories/cli)
4. /
5. wpdiggerstudio/wpzylos-cli-core

ActiveLibrary[CLI &amp; Console](/categories/cli)

wpdiggerstudio/wpzylos-cli-core
===============================

Stub compiler and file generation utilities for building CLI tools, code generators, and scaffolding systems in the WPZylos ecosystem

v1.0.0(3mo ago)0441MITPHPPHP ^8.0CI passing

Since Feb 1Pushed 3mo agoCompare

[ Source](https://github.com/WPDiggerStudio/wpzylos-cli-core)[ Packagist](https://packagist.org/packages/wpdiggerstudio/wpzylos-cli-core)[ Docs](https://github.com/WPDiggerStudio/wpzylos-cli-core)[ Fund](https://www.paypal.com/donate/?hosted_button_id=66U4L3HG4TLCC)[ RSS](/packages/wpdiggerstudio-wpzylos-cli-core/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (3)Versions (2)Used By (1)

WPZylos CLI Core
================

[](#wpzylos-cli-core)

[![PHP Version](https://camo.githubusercontent.com/911a83e2aa6fe73660ab613629a95c76622bf03049a7344e80c5ea72d4ef9c7d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253545382e302d626c7565)](https://php.net)[![License](https://camo.githubusercontent.com/f8df3091bbe1149f398a5369b2c39e896766f9f6efba3477c63e9b4aa940ef14/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d677265656e)](LICENSE)[![GitHub](https://camo.githubusercontent.com/dbe820b98864e115173c422b9472b725cfa678bee03b66ff2c453dad95a3d20b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4769744875622d575044696767657253747564696f2d3138313731373f6c6f676f3d676974687562)](https://github.com/WPDiggerStudio/wpzylos-cli-core)

Stub compilation and file generation utilities for building CLI tools and code generators.

📖 **[Full Documentation](https://wpzylos.com)** | 🐛 **[Report Issues](https://github.com/WPDiggerStudio/wpzylos-cli-core/issues)**

---

✨ Features
----------

[](#-features)

- **StubCompiler** — Replace placeholders in stub templates with dynamic values
- **FileWriter** — Write files safely with automatic directory creation
- **Generator Base** — Abstract base class for building custom generators
- **Context-Aware Compilation** — Built-in support for plugin context tokens

---

📋 Requirements
--------------

[](#-requirements)

RequirementVersionPHP^8.0---

🚀 Installation
--------------

[](#-installation)

```
composer require wpdiggerstudio/wpzylos-cli-core
```

---

📖 Quick Start
-------------

[](#-quick-start)

### Basic Stub Compilation

[](#basic-stub-compilation)

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

// Create compiler with 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',
]);

// Write to file
$writer = new FileWriter();
$writer->write('/path/to/ProductController.php', $content);
```

### Plugin Context Compilation

[](#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']
);
```

---

📁 Package Structure
-------------------

[](#-package-structure)

```
wpzylos-cli-core/
├── src/
│   ├── StubCompiler.php    # Template compilation
│   ├── FileWriter.php      # File writing utilities
│   └── Generator.php       # Abstract generator base
├── stubs/
│   ├── controller.stub     # Controller template
│   ├── migration.stub      # Migration template
│   └── request.stub        # Request template
├── tests/                  # PHPUnit tests
└── docs/                   # Documentation

```

---

🏗️ Core Components
------------------

[](#️-core-components)

### StubCompiler

[](#stubcompiler)

Compiles stub templates by replacing `{{token}}` placeholders with values.

```
$compiler = new StubCompiler('/path/to/stubs');

// Set default replacements (applied to all compilations)
$compiler->setDefaults([
    'namespace' => 'MyPlugin',
    'textDomain' => 'my-plugin',
]);

// Compile with additional replacements
$content = $compiler->compile('controller', [
    'class' => 'UserController',
]);

// Get available stub names
$stubs = $compiler->getAvailable(); // ['controller', 'migration', 'request']
```

**Methods:**

MethodDescription`compile($stubName, $replacements)`Compile a stub with token replacements`compileForPlugin($stub, $slug, $prefix, $textDomain, $namespace, $extra)`Compile with plugin context tokens`setDefaults($defaults)`Set default token values`getAvailable()`List available stub names### FileWriter

[](#filewriter)

Safe file writing with automatic directory creation.

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

// Write file (throws if exists and overwrite is false)
$writer->write('/path/to/File.php', $content);

// Write only if file doesn't exist
$written = $writer->writeIfNotExists('/path/to/File.php', $content);

// Enable overwrite mode
$writer->setOverwrite(true);
```

**Methods:**

MethodDescription`write($path, $content)`Write content to file`writeIfNotExists($path, $content)`Write only if file doesn't exist`setOverwrite($overwrite)`Set overwrite mode### Generator (Abstract)

[](#generator-abstract)

Base class for building custom file generators.

```
use WPZylos\Framework\Cli\Core\Generator;

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

        $content = $this->compiler->compile('controller', [
            'class' => $className,
            'namespace' => $options['namespace'] ?? 'App\\Controllers',
        ]);

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

        return [$path];
    }

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

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

**Helper Methods:**

MethodDescription`toClassName($name)`Convert `my-thing` to `MyThing``toVariableName($name)`Convert `my-thing` to `myThing`---

📝 Creating Stubs
----------------

[](#-creating-stubs)

Stubs are template files with `.stub` extension using `{{token}}` placeholders:

```
