wpdiggerstudio / wpzylos-cli-core
Stub compiler and file generation utilities for building CLI tools, code generators, and scaffolding systems in the WPZylos ecosystem
Fund package maintenance!
Paypal
Installs: 44
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/wpdiggerstudio/wpzylos-cli-core
Requires
- php: ^8.0
Requires (Dev)
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^9.6 || ^10.0
- squizlabs/php_codesniffer: ^3.7
README
Stub compilation and file generation utilities for building CLI tools and code generators.
๐ Full Documentation | ๐ Report Issues
โจ 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
| Requirement | Version |
|---|---|
| PHP | ^8.0 |
๐ Installation
composer require wpdiggerstudio/wpzylos-cli-core
๐ Quick Start
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
// 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
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
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:
| Method | Description |
|---|---|
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
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:
| Method | Description |
|---|---|
write($path, $content) |
Write content to file |
writeIfNotExists($path, $content) |
Write only if file doesn't exist |
setOverwrite($overwrite) |
Set overwrite mode |
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:
| Method | Description |
|---|---|
toClassName($name) |
Convert my-thing to MyThing |
toVariableName($name) |
Convert my-thing to myThing |
๐ Creating Stubs
Stubs are template files with .stub extension using {{token}} placeholders:
<?php // stubs/service.stub namespace {{namespace}}\Services; class {{class}}Service { public function __construct() { // Service for {{slug}} } }
Common Tokens:
| Token | Description | Example |
|---|---|---|
{{namespace}} |
PHP namespace | MyPlugin |
{{class}} |
Class name | Product |
{{slug}} |
Plugin slug | my-plugin |
{{prefix}} |
Database prefix | myplugin_ |
{{textDomain}} |
Text domain | my-plugin |
{{Slug}} |
PascalCase slug | MyPlugin |
{{PREFIX}} |
Uppercase prefix | MYPLUGIN_ |
๐งช Testing
# Run all tests composer test # Run with coverage ./vendor/bin/phpunit --coverage-html coverage/
๐ Security
When generating files from user input:
// โ Validate class names $name = preg_replace('/[^a-zA-Z0-9_]/', '', $input); // โ Validate paths $realPath = realpath($targetDir); if (!str_starts_with($outputPath, $realPath)) { throw new \InvalidArgumentException('Invalid path'); }
๐ฆ Related Packages
| Package | Description |
|---|---|
| wpzylos-cli-devtool | Development commands (make:controller, etc.) |
| wpzylos-wp-cli | WP-CLI integration |
| wpzylos-core | Application foundation |
๐ Documentation
For comprehensive documentation, tutorials, and API reference, visit wpzylos.com.
โ Support the Project
If you find this package helpful, consider buying me a coffee! Your support helps maintain and improve the WPZylos ecosystem.
๐ License
MIT License. See LICENSE for details.
๐ค Contributing
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
Made with โค๏ธ by WPDiggerStudio