PHP code example of rnr1721 / codecraft

1. Go to this page and download the library: Download rnr1721/codecraft 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/ */

    

rnr1721 / codecraft example snippets




use rnr1721\CodeCraft\CodeCraft;
use rnr1721\CodeCraft\AdapterRegistry;
use rnr1721\CodeCraft\Adapters\PhpAdapter;

// Create registry and register adapters
$registry = new AdapterRegistry();
$registry->register(new PhpAdapter());
$codecraft = new CodeCraft($registry);

// Create a PHP class - type detected from file extension
$phpClass = $codecraft->create('app/Models/User.php', [
    'namespace' => 'App\\Models',
    'name' => 'User',
    'extends' => 'Model',
    'properties' => [
        [
            'name' => 'name',
            'type' => 'string',
            'visibility' => 'private'
        ]
    ],
    'methods' => [
        [
            'name' => 'getName',
            'visibility' => 'public',
            'returnType' => 'string',
            'body' => 'return $this->name;'
        ]
    ]
]);

// Write to file immediately
$codecraft->write('app/Models/User.php', [
    'namespace' => 'App\\Models',
    'name' => 'User',
    'extends' => 'Model'
]);

// Edit existing file
$codecraft->editFile('app/Models/User.php', [
    [
        'type' => 'add_method',
        'method' => [
            'name' => 'setName',
            'visibility' => 'public',
            'params' => [['name' => 'name', 'type' => 'string']],
            'body' => '$this->name = $name; return $this;'
        ]
    ]
]);

use rnr1721\CodeCraft\CodeCraftFactory;
use rnr1721\CodeCraft\AdapterRegistry;
use rnr1721\CodeCraft\Adapters\PhpAdapter;

// Using factory for easier setup
$registry = new AdapterRegistry();
$registry->register(new PhpAdapter());

$factory = new CodeCraftFactory($registry);
$codecraft = $factory->create();

// Or create with no adapters
$emptyCodecraft = $factory->createEmpty();

$userModel = $codecraft->create('app/Models/User.php', [
    'namespace' => 'App\\Models',
    'name' => 'User',
    'extends' => 'Authenticatable',
    'implements' => ['MustVerifyEmail'],
    'properties' => [
        [
            'name' => 'fillable',
            'visibility' => 'protected',
            'type' => 'array',
            'default' => "['name', 'email', 'password']"
        ]
    ],
    'methods' => [
        [
            'name' => 'posts',
            'visibility' => 'public',
            'returnType' => 'HasMany',
            'body' => 'return $this->hasMany(Post::class);'
        ]
    ]
]);

$fastApiApp = $codecraft->create('app/main.py', [
    'type' => 'fastapi',
    'title' => 'User Management API',
    'description' => 'RESTful API for user management',
    'version' => '1.0.0',
    'endpoints' => [
        [
            'path' => '/users',
            'method' => 'GET',
            'function' => 'get_users',
            'return_type' => 'List[User]',
            'body' => 'return user_service.get_all_users()'
        ],
        [
            'path' => '/users',
            'method' => 'POST',
            'function' => 'create_user',
            'params' => ['user: UserCreate'],
            'return_type' => 'User',
            'body' => 'return user_service.create_user(user)'
        ]
    ]
]);

$reactComponent = $codecraft->create('src/components/UserCard.jsx', [
    'type' => 'component',
    'name' => 'UserCard',
    'props' => ['user', 'onEdit', 'onDelete'],
    'hooks' => ['useState', 'useEffect'],
    'functional' => true
]);

$buttonStyles = $codecraft->create('src/styles/button.css', [
    'type' => 'component',
    'name' => 'button',
    'elements' => [
        'text' => [
            'color' => 'white',
            'font-weight' => 'bold'
        ],
        'icon' => [
            'margin-right' => '0.5rem'
        ]
    ],
    'states' => [
        'primary' => ['background-color' => '#007bff'],
        'disabled' => ['opacity' => '0.5']
    ]
]);

$packageJson = $codecraft->create('package.json', [
    'type' => 'package',
    'name' => 'my-awesome-app',
    'version' => '1.0.0',
    'description' => 'An awesome web application',
    'dependencies' => [
        'react' => '^18.2.0',
        'express' => '^4.18.0'
    ],
    'scripts' => [
        'dev' => 'vite',
        'build' => 'vite build',
        'test' => 'jest'
    ]
]);

// Generate content only
$content = $codecraft->create('app/Models/User.php', $options);

// Generate and write to file
$success = $codecraft->write('app/Models/User.php', $options);

// Edit and return content
$newContent = $codecraft->edit('app/Models/User.php', $modifications);

// Edit and save file
$success = $codecraft->editFile('app/Models/User.php', $modifications);

// Analyze file structure (AST-based for PHP)
$analysis = $codecraft->analyze('app/Models/User.php');

echo "Classes found: " . count($analysis['classes']);
echo "Methods found: " . count($analysis['classes'][0]['methods']);
echo "Namespace: " . $analysis['namespace'];

// Validate syntax
$isValid = $codecraft->validate('app/Models/User.php');

// Check support
$canHandle = $codecraft->supports('php');
$extensions = $codecraft->getSupportedExtensions();

// Get help for specific file type
$help = $codecraft->getHelp('php');

// Multiple modifications at once
$codecraft->editFile('app/Models/User.php', [
    ['type' => 'add_property', 'property' => ['name' => 'email', 'type' => 'string']],
    ['type' => 'add_method', 'method' => ['name' => 'getEmail', 'body' => 'return $this->email;']],
    ['type' => 'replace_method', 'method_name' => 'getName', 'method' => [...]]
]);

// Edit JSON files with modifications
$codecraft->editFile('config.json', [
    ['type' => 'set', 'path' => 'database.host', 'value' => 'localhost'],
    ['type' => 'set', 'path' => 'app.name', 'value' => 'MyApp'],
    ['type' => 'append', 'path' => 'features', 'value' => 'notifications'],
    ['type' => 'merge', 'data' => ['new_setting' => 'value']]
]);

// Simple path-based operations that AI can easily understand
$codecraft->write('app/Models/Post.php', [
    'namespace' => 'App\\Models',
    'name' => 'Post',
    'extends' => 'Model'
]);

// Get help for any file type
$help = $codecraft->getHelp('jsx');

// Check what's supported
$extensions = $codecraft->getSupportedExtensions();

// Validate before creating
if ($codecraft->supports('py')) {
    $codecraft->write('app/services/user_service.py', $options);
}

use rnr1721\CodeCraft\Interfaces\FileAdapterInterface;

class RustAdapter implements FileAdapterInterface
{
    public function getName(): string
    {
        return 'rust';
    }
    
    public function getSupportedExtensions(): array
    {
        return ['rs'];
    }
    
    public function create(string $filePath, array $options = []): string
    {
        // Implementation for creating Rust files
    }
    
    public function edit(string $filePath, array $modifications): string
    {
        // Implementation for editing Rust files
    }
    
    public function analyze(string $filePath): array
    {
        // Implementation for analyzing Rust files
    }
    
    public function validate(string $content): bool
    {
        // Implementation for validating Rust syntax
    }
    
    public function getCapabilities(): array
    {
        return [
            'create_struct' => true,
            'create_trait' => true,
            'create_enum' => true
        ];
    }
    
    public function getVersion(): string
    {
        return '1.0.0';
    }
    
    public function getHelp(): array
    {
        return [
            'description' => 'Rust adapter for structs, traits, and enums',
            'supported_types' => [
                'struct' => 'Create Rust structs',
                'trait' => 'Create Rust traits',
                'enum' => 'Create Rust enums'
            ],
            'examples' => [
                // ... comprehensive examples
            ],
            'options' => [
                // ... available options
            ]
        ];
    }
}

// Register your adapter
$codecraft->registerAdapter(new RustAdapter());

// In an Artisan command
use rnr1721\CodeCraft\Interfaces\CodeCraftInterface;

class MakeAdvancedModelCommand extends Command
{
    protected $signature = 'make:advanced-model {name}';
    
    public function handle(CodeCraftInterface $codecraft)
    {
        $name = $this->argument('name');
        $path = "app/Models/{$name}.php";
        
        $success = $codecraft->write($path, [
            'namespace' => 'App\\Models',
            'name' => $name,
            'extends' => 'Model',
            'properties' => [
                ['name' => 'fillable', 'visibility' => 'protected', 'type' => 'array']
            ]
        ]);
        
        if ($success) {
            $this->info("Model {$name} created successfully!");
        }
    }
}

// In any Laravel class (Controller, Service, etc.)
use rnr1721\CodeCraft\Interfaces\CodeCraftInterface;

class CodeGeneratorService
{
    public function __construct(
        private CodeCraftInterface $codecraft
    ) {}
    
    public function generateUserModel(): bool
    {
        return $this->codecraft->write('app/Models/User.php', [
            'namespace' => 'App\\Models',
            'name' => 'User',
            'extends' => 'Model'
        ]);
    }
}