eudiegoborgs / hyperf-cgen
Custom class generator for hyperf
Requires
- php: >=8.1
- hyperf/command: ^3.1
- symfony/property-access: ^6.3
- symfony/serializer: ^6.3
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- mockery/mockery: ^1.0
- phpstan/phpstan: ^1.0
- phpunit/phpunit: >=7.0
- swoole/ide-helper: ^4.5
Suggests
- swow/swow: Required to create swow components.
README
CGen is a Custom Class Generator library to optimize your hyperf development experience.
If you use this library, you can make stub for your classes and create code structure with only one command.
Installation
composer require eudiegoborgs/hyperf-cgen
Configuration
Publish the config file so you can define your own rules
php bin/hyperf.php vendor:publish eudiegoborgs/hyperf-cgen
The config file will show up in the following path:
config/autoload/cgen.php
Here in the file you can define your own creation rules for specific classes
<?php declare(strict_types=1); return [ 'generator' => [ 'example_type' => [ 'namespace' => 'CyBorgs\\Hyperf\\CGen\\Custom', 'stub' => __DIR__ . '/stubs/class.stub', 'run_previous' => [ 'other_type', 'interface' ] ], 'other_type' => [ 'namespace' => 'CyBorgs\\Hyperf\\CGen\\OtherCustom', 'stub' => __DIR__ . '/stubs/other_class.stub', 'prefix' => 'Prefix', 'suffix' => 'Suffix', ], 'interface' => [ 'namespace' => 'CyBorgs\\Hyperf\\CGen\\Custom\\%CLASS%\\Interfaces', 'stub' => __DIR__ . '/stubs/interface.stub', 'suffix' => 'Interface', ], ], 'default' => [ 'stub' => 'path/to/real/stub/example_type.stub', 'namespace' => 'App\\Example\\Default' ] ];
Create stub files
This is a stub for other type:
// path/to/real/stub/other_type.stub <?php declare(strict_types=1); namespace %NAMESPACE%; class %CLASS% { }
This is a stub for example type interface:
// path/to/real/stub/example_type_interface.stub <?php declare(strict_types=1); namespace %NAMESPACE%; interface %CLASS% { }
This is a stub for example type:
// path/to/real/stub/example_type_interface.stub <?php declare(strict_types=1); namespace %NAMESPACE%; use %other_type.NAMESPACE%\%other_type.CLASS%; class %CLASS% { public function __construct( private %other_type.CLASS% %other_type.VARIABLE_NAME% ) {} }
Usage
To create a new class you need run this command:
php bin/hyperf.php cgen:create example_type MyClassName
After you run command, this lib will create for you files using your stubs with this structure:
src/
- Example/
- Namespace/
- MyClassName/
- Interfaces/
- MyClassNameInterface.php
- MyClassName.php
- OtherNamespace/
PrefixMyClassNameSuffix.php
Show the PrefixMyClassNameSuffix.php file
<?php declare(strict_types=1); namespace CyBorgs\Hyperf\CGen\OtherCustom; class PrefixMyClassNameSuffix { }
Show the MyClassNameInterface.php file
<?php declare(strict_types=1); namespace CyBorgs\Hyperf\CGen\Custom\MyClassName\Interfaces; class MyClassNameInterface { }
Show the MyClassName.php file
<?php declare(strict_types=1); namespace CyBorgs\Hyperf\CGen\Custom; use CyBorgs\Hyperf\CGen\OtherCustom\PrefixMyClassNameSuffix; class MyClassName { public function __construct( private PrefixMyClassNameSuffix $classTest ) {} }