Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/carono/codegen
Динамическая генерация классов в php
https://github.com/carono/codegen
codegen php
Last synced: about 5 hours ago
JSON representation
Динамическая генерация классов в php
- Host: GitHub
- URL: https://github.com/carono/codegen
- Owner: carono
- Created: 2016-08-23T18:20:09.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2022-10-05T09:06:42.000Z (about 2 years ago)
- Last Synced: 2024-10-12T23:29:57.145Z (26 days ago)
- Topics: codegen, php
- Language: PHP
- Homepage:
- Size: 10.7 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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