PHP code example of nexus4812 / php-class-gen

1. Go to this page and download the library: Download nexus4812/php-class-gen library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nexus4812 / php-class-gen example snippets




use PhpGen\ClassGenerator\Config\PhpGenConfig;
use PhpGen\ClassGenerator\Console\Commands\Example\Laravel\LaravelCqrsQueryCommand;

return PhpGenConfig::configure()
    ->withCommands([
        LaravelCqrsQueryCommand::class,
    ])
    ->withComposerAutoload()  // Load PSR-4 mappings from composer.json
    ->withStrictTypes(true);  // Add strict types to generated code



namespace App\Commands;

use PhpGen\ClassGenerator\Blueprint\FileBlueprint;
use PhpGen\ClassGenerator\Console\Commands\Command;
use PhpGen\ClassGenerator\Core\Project;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(name: 'generate:service', description: 'Generate service class')]
final class ServiceGeneratorCommand extends Command
{
    protected function handle(InputInterface $input, OutputInterface $output): Project
    {
        $project = new Project();

        // Add files to generate
        $project->add(
            FileBlueprint::createEmptyClass('App\\Services\\UserService')
                ->defineStructure(function ($class) {
                    $class->setFinal();
                    $class->addMethod('__construct')->setPublic();
                    return $class;
                })
        );

        return $project;
    }
}

// phpgen.php
return PhpGenConfig::configure()
    ->withCommands([
        \App\Commands\ServiceGeneratorCommand::class,
    ])
    ->withComposerAutoload()
    ->withStrictTypes(true);

return PhpGenConfig::configure()
    ->withComposerAutoload()  // Load mappings from composer.json
    ->withPsr4Mapping('App\\', 'app')  // Regular mapping
    ->withPriorityPsr4Mapping('Tests\\', 'tests')  // Override composer.json mapping
    ->withPriorityPsr4Mapping('Legacy\\', 'legacy')  // Handle legacy code
    ->withStrictTypes(true);

PhpGenConfig::configure()
    ->withCommands([...])                    // Register command classes
    ->withPsr4Mapping($namespace, $dir)      // Add PSR-4 mapping
    ->withPriorityPsr4Mapping($ns, $dir)     // Add priority PSR-4 mapping
    ->withComposerAutoload($path)            // Load from composer.json
    ->withStrictTypes(bool)                  // Enable/disable strict types

// Add at least one mapping
return PhpGenConfig::configure()
    ->withPsr4Mapping('App\\', 'app')
    ->withCommands([...]);

// Resolve with priority mapping
return PhpGenConfig::configure()
    ->withComposerAutoload()
    ->withPriorityPsr4Mapping('Tests\\', 'tests')
    ->withCommands([...]);
bash
vendor/bin/php-gen --help
bash
# Start MCP server
./bin/php-gen mcp:server

# Specify custom configuration file
./bin/php-gen mcp:server --config-path=/path/to/phpgen.php