Recommend this page to a friend! |
Download |
Info | Documentation | Files | Install with Composer | Download | Reputation | Support forum | Blog | Links |
Ratings | Unique User Downloads | Download Rankings | ||||
73% | Total: 429 | All time: 6,329 This week: 38 |
Version | License | PHP version | Categories | |||
php-generator 1.0.13 | MIT/X Consortium ... | 5.3.3 | PHP 5, Libraries, Code Generation |
Description | Author | |||
This package can generate PHP code elements programatically. Innovation Award
|
This directory contains the components that are on top of element as component can contain elements and eases generation of more complex elements.
Using one of these components, you can generate the content of more complex element:
$class = new PhpClassComponent('Foo', true, 'stdClass');
$class
->addAnnotationBlock('@var string')
->addConstant('FOO', 'theValue')
->addAnnotationBlock('@var string')
->addConstant('BAR', 'theOtherValue')
->addAnnotationBlock(new PhpAnnotationElement('var', 'int'))
->addProperty('bar', 1)
->addAnnotationBlock(new PhpAnnotationElement('var', 'bool'))
->addPropertyElement(new PhpPropertyElement('sample', true))
->addAnnotationBlock([
new PhpAnnotationElement(PhpAnnotationElement::NO_NAME, 'This method is very useful'),
new PhpAnnotationElement('date', '2012-03-01'),
'@return mixed'
])
->addMethod('getMyValue', [
new PhpFunctionParameterElement('asString', true),
'unusedParameter'
])
->addAnnotationBlock([
new PhpAnnotationElement(PhpAnnotationElement::NO_NAME, 'This method is very useless'),
new PhpAnnotationElement('date', '2012-03-01'),
'@return void'
])
->addMethod('uselessMethod', [
new PhpFunctionParameterElement('uselessParameter', null),
'unusedParameter'
]);
echo $class->toString();
displays
abstract class Foo extends stdClass
{
/
* @var string
*/
const FOO = 'theValue';
/
* @var string
*/
const BAR = 'theOtherValue';
/
* @var int
*/
public $bar = 1;
/
* @var bool
*/
public $sample = true;
/
* This method is very useful
* @date 2012-03-01
* @return mixed
*/
public function getMyValue($asString = true, $unusedParameter)
{
}
/
* This method is very useless
* @date 2012-03-01
* @return void
*/
public function uselessMethod($uselessParameter = null, $unusedParameter)
{
}
}
$file = new PhpFileComponent('Foo');
$class = new PhpClassComponent('Foo', true, 'stdClass');
$class
->addAnnotationBlock('@var string')
->addConstant('FOO', 'theValue')
->addAnnotationBlock('@var string')
->addConstant('BAR', 'theOtherValue')
->addAnnotationBlock(new PhpAnnotationElement('var', 'int'))
->addProperty('bar', 1)
->addAnnotationBlock(new PhpAnnotationElement('var', 'bool'))
->addPropertyElement(new PhpPropertyElement('sample', true))
->addAnnotationBlock([
new PhpAnnotationElement(PhpAnnotationElement::NO_NAME, 'This method is very useful'),
new PhpAnnotationElement('date', '2012-03-01'),
'@return mixed'
])
->addMethod('getMyValue', [
new PhpFunctionParameterElement('asString', true),
'unusedParameter'
])
->addAnnotationBlock([
new PhpAnnotationElement(PhpAnnotationElement::NO_NAME, 'This method is very useless'),
new PhpAnnotationElement('date', '2012-03-01'),
'@return void'
])
->addMethod('uselessMethod', [
new PhpFunctionParameterElement('uselessParameter', null),
'unusedParameter'
]);
$file
->setDeclare(PhpDeclare::DIRECTIVE_STRICT_TYPES, 1)
->setNamespace('My\\Testing\\NamespaceName')
->addUse('My\\Testing\\ParentNamespace\\Model')
->addUse('My\\Testing\\ParentNamespace\\Repository')
->addUse('My\\Testing\\ParentNamespace\\Generator')
->addUse('My\\Testing\\ParentNamespace\\Foo', 'FooType')
->addClassComponent($class);
echo $file->toString();
displays
<?php
declare(strict_types=1);
namespace My\Testing\NamespaceName;
use My\Testing\ParentNamespace\Model;
use My\Testing\ParentNamespace\Repository;
use My\Testing\ParentNamespace\Generator;
use My\Testing\ParentNamespace\Foo as FooType;
abstract class Foo extends stdClass
{
/
* @var string
*/
const FOO = 'theValue';
/
* @var string
*/
const BAR = 'theOtherValue';
/
* @var int
*/
public $bar = 1;
/
* @var bool
*/
public $sample = true;
/
* This method is very useful
* @date 2012-03-01
* @return mixed
*/
public function getMyValue($asString = true, $unusedParameter)
{
}
/
* This method is very useless
* @date 2012-03-01
* @return void
*/
public function uselessMethod($uselessParameter = null, $unusedParameter)
{
}
}
> PhpGenerator helps to generate PHP source code
Even if this project is yet another PHP source code generator, its main goal is to provide a consistent PHP source code generator for the PackageGenerator project. Nevertheless, it also aims to be used for any PHP source code generation process as it generates standard PHP code.
Rest assured that it is not tweaked for the purpose of the PackageGenerator project.
This project contains two main features:
Thanks to the Docker image of phpfarm, tests can be run locally under any PHP version using the cli: - php-7.4
First of all, you need to create your container which you can do using docker-compose by running the below command line from the root directory of the project:
$ docker-compose up -d --build
You then have a container named php_generator
in which you can run composer
commands and php cli
commands such as:
# install deps in container (using update ensure it does use the composer.lock file if there is any)
$ docker exec -it php_generator php-7.4 /usr/bin/composer update
# run tests in container
$ docker exec -it php_generator php-7.4 -dmemory_limit=-1 vendor/bin/phpunit
If you have a question, feel free to create an issue.
The MIT License (MIT). Please see License File for more information.
Files (63) |
File | Role | Description | ||
---|---|---|---|---|
.docker (1 file) | ||||
src (2 directories) | ||||
tests (1 file, 3 directories) | ||||
.editorconfig | Data | Auxiliary data | ||
.php-cs-fixer.php | Example | Example script | ||
CHANGELOG.md | Data | Auxiliary data | ||
composer.json | Data | Auxiliary data | ||
docker-compose.yml | Data | Auxiliary data | ||
LICENSE | Lic. | License text | ||
phpunit.xml.dist | Data | Auxiliary data | ||
README.md | Doc. | Documentation | ||
UPGRADE-3.0.md | Data | Auxiliary data | ||
UPGRADE-4.0.md | Data | Auxiliary data |
Files (63) | / | src | / | Component |
File | Role | Description |
---|---|---|
AbstractComponent.php | Class | Class source |
GenerateableInterface.php | Class | Class source |
PhpClass.php | Class | Class source |
PhpFile.php | Class | Class source |
PhpInterface.php | Class | Class source |
README.md | Doc. | Documentation |
Files (63) | / | src | / | Element |
File | Role | Description |
---|---|---|
AbstractElement.php | Class | Class source |
AccessRestrictedElementInterface.php | Class | Class source |
AccessRestrictedElementTrait.php | Class | Class source |
AssignedValueElementInterface.php | Class | Class source |
AssignedValueElementTrait.php | Class | Class source |
GenerateableInterface.php | Class | Class source |
PhpAnnotation.php | Class | Class source |
PhpAnnotationBlock.php | Class | Class source |
PhpClass.php | Class | Class source |
PhpConstant.php | Class | Class source |
PhpDeclare.php | Class | Class source |
PhpFile.php | Class | Class source |
PhpFunction.php | Class | Class source |
PhpFunctionParameter.php | Class | Class source |
PhpInterface.php | Class | Class source |
PhpMethod.php | Class | Class source |
PhpProperty.php | Class | Class source |
PhpVariable.php | Class | Class source |
README.md | Doc. | Documentation |
TypeHintedElementInterface.php | Class | Class source |
TypeHintedElementTrait.php | Class | Class source |
Files (63) | / | tests |
File | Role | Description | ||
---|---|---|---|---|
Component (4 files) | ||||
Element (12 files) | ||||
resources (8 files) | ||||
TestCase.php | Test | Unit test script |
Files (63) | / | tests | / | Component |
File | Role | Description |
---|---|---|
AbstractComponent.php | Test | Unit test script |
PhpClassTest.php | Test | Unit test script |
PhpFileTest.php | Test | Unit test script |
PhpInterfaceTest.php | Test | Unit test script |
Files (63) | / | tests | / | Element |
File | Role | Description |
---|---|---|
PhpAnnotationBlockTest.php | Test | Unit test script |
PhpAnnotationTest.php | Test | Unit test script |
PhpClassTest.php | Test | Unit test script |
PhpConstantTest.php | Test | Unit test script |
PhpDeclareTest.php | Class | Class source |
PhpFileTest.php | Test | Unit test script |
PhpFunctionParameterTest.php | Test | Unit test script |
PhpFunctionTest.php | Test | Unit test script |
PhpInterfaceTest.php | Test | Unit test script |
PhpMethodTest.php | Test | Unit test script |
PhpPropertyTest.php | Test | Unit test script |
PhpVariableTest.php | Test | Unit test script |
Files (63) | / | tests | / | resources |
File | Role | Description |
---|---|---|
PhpClassTest_SimpleToString.php | Test | Unit test script |
PhpClassTest_Simpl...rnTypePerMethod.php | Aux. | Auxiliary script |
PhpClassTest_Simpl...rnTypePerMethod.php | Aux. | Auxiliary script |
PhpFileTest_SimpleClassToString.php | Test | Unit test script |
PhpFileTest_Simple...gWithReturnType.php | Class | Class source |
PhpFileTest_SimpleInterfaceToString.php | Test | Unit test script |
PhpInterfaceTest_SimpleToString.php | Test | Unit test script |
PhpInterfaceTest_S...gWithReturnType.php | Aux. | Auxiliary script |
The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page. |
Install with Composer |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
User Ratings | ||||||||||||||||||||||||||||||
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.