Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/carono/codegen

Динамическая генерация классов в php
https://github.com/carono/codegen

codegen php

Last synced: about 5 hours ago
JSON representation

Динамическая генерация классов в php

Awesome Lists containing this project

README

        

[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/carono/codegen/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/carono/codegen/?branch=master)
[![Latest Stable Version](https://poser.pugx.org/carono/codegen/v/stable)](https://packagist.org/packages/carono/codegen)
[![Total Downloads](https://poser.pugx.org/carono/codegen/downloads)](https://packagist.org/packages/carono/codegen)
[![License](https://poser.pugx.org/carono/codegen/license)](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