https://github.com/carono/codegen
  
  
    Динамическая генерация классов в php 
    https://github.com/carono/codegen
  
codegen php
        Last synced: 7 months ago 
        JSON representation
    
Динамическая генерация классов в php
- Host: GitHub
- URL: https://github.com/carono/codegen
- Owner: carono
- Created: 2016-08-23T18:20:09.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2022-10-05T09:06:42.000Z (about 3 years ago)
- Last Synced: 2025-03-24T21:13:10.893Z (7 months ago)
- Topics: codegen, php
- Language: PHP
- Homepage:
- Size: 10.7 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
- 
            Metadata Files:
            - Readme: README.md
 
Awesome Lists containing this project
README
          [](https://scrutinizer-ci.com/g/carono/codegen/?branch=master)
[](https://packagist.org/packages/carono/codegen)
[](https://packagist.org/packages/carono/codegen)
[](https://packagist.org/packages/carono/codegen)
Установка
=========
`composer require carono/codegen`
Как использовать
================
Создайте класс от `carono\codegen\ClassGenerator`
```php
addParameter('param1');
        $method->addParameter('param2', null);
        $method->addComment('@param mixed $param1');
        $method->addComment('@param mixed|null $param2');
        $method->addComment('@return mixed');
        $method->setStatic();
        $method->addBody('return ?;', [$this->params['value']]);
    }
    protected function phpProperties()
    {
        return ['id' => 100, 'name' => 'myParam'];
    }
    protected function phpDocComments()
    {
        return ['Auto generated class'];
    }
    protected function formExtends()
    {
        return 'ArrayObject';
    }
    protected function classConstants()
    {
        return [
            'const1' => 1,
            'const2' => 2
        ];
    }
    protected function classTraits()
    {
        return ['someTrait'];
    }
    protected function classUses()
    {
        return ['some\Object1', 'baseObject' => 'some2\Object1'];
    }
    protected function classAfterRender()
    {
        $property = $this->phpClass->addProperty('afterRenderProperty');
        $property->setStatic();
        $property->setVisibility('private');
        $property->addComment('Event after render');
        $method = $this->phpClass->addMethod('dynamicMethod' . $this->params['value']);
        $method->setStatic();
        $method->addBody('return ?;', [$this->params['value'] * 2]);
    }
    protected function formOutputPath()
    {
        return __DIR__ . DIRECTORY_SEPARATOR . 'output' . DIRECTORY_SEPARATOR . 'MyClassDemo.php';
    }
    protected function formClassNamespace()
    {
        return 'carono\codegen\tests\Demo';
    }
    protected function formClassName()
    {
        return 'MyClassDemo';
    }
}
```
Создайте экземпляр своего генератора и произведите рендер
```php
render(['value' => 500]);
file_put_contents($demo->output, $content);
```
Итоговый файл
```php