PHP Classes

PHP Code Generator: Generate PHP code elements programatically

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 73%Total: 429 All time: 6,329 This week: 38Up
Version License PHP version Categories
php-generator 1.0.13MIT/X Consortium ...5.3.3PHP 5, Libraries, Code Generation
Description 

Author

This package can generate PHP code elements programatically.

It provides a set of classes that can generate PHP code elements of different types like PHP files, classes, interfaces, functions, function patrameters, variables and annotations.

Another set of classes can generate more complex components made of base PHP code elements that are passed to these classes as object parameters.

Innovation Award
PHP Programming Innovation award nominee
September 2015
Number 8


Prize: One copy of the Zend Studio
One common way to generate code in PHP or any other language is to use templates and replace variable values.

This package provide an alternative approach that consists in classes of objects that represent each type of construct of the PHP language, like classes, interfaces, functions, variables, etc..

It can define the PHP code you want to generate by combining language element objects in a hierarchic way.

Manuel Lemos
Picture of WsdlToPhp
  Performance   Level  
Name: WsdlToPhp <contact>
Classes: 9 packages by
Country: France France
Age: 41
All time rank: 73731 in France France
Week rank: 206 Up8 in France France Up
Innovation award
Innovation award
Nominee: 5x

Winner: 1x

Documentation

PhpGenerator Component

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:

  • complete php file
  • class: classic, abstract, interface

Main features

Generate a complete class

$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)
    {
    }
}

Generate a complete PHP file with a class

$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)
    {
    }
}


Details

PhpGenerator, a Real PHP source code generator

> PhpGenerator helps to generate PHP source code

License Latest Stable Version TeamCity build status Scrutinizer Code Quality Code Coverage Total Downloads StyleCI SymfonyInsight

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.

Main features

This project contains two main features:

  • Element: generate basic elements
  • Component: generate structured complex elements

Testing using Docker

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

FAQ

If you have a question, feel free to create an issue.

License

The MIT License (MIT). Please see License File for more information.


  Files folder image Files (63)  
File Role Description
Files folder image.docker (1 file)
Files folder imagesrc (2 directories)
Files folder imagetests (1 file, 3 directories)
Accessible without login Plain text file .editorconfig Data Auxiliary data
Accessible without login Plain text file .php-cs-fixer.php Example Example script
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file docker-compose.yml Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml.dist Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file UPGRADE-3.0.md Data Auxiliary data
Accessible without login Plain text file UPGRADE-4.0.md Data Auxiliary data

  Files folder image Files (63)  /  .docker  
File Role Description
  Accessible without login Plain text file Dockerfile Data Auxiliary data

  Files folder image Files (63)  /  src  
File Role Description
Files folder imageComponent (6 files)
Files folder imageElement (21 files)

  Files folder image Files (63)  /  src  /  Component  
File Role Description
  Plain text file AbstractComponent.php Class Class source
  Plain text file GenerateableInterface.php Class Class source
  Plain text file PhpClass.php Class Class source
  Plain text file PhpFile.php Class Class source
  Plain text file PhpInterface.php Class Class source
  Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (63)  /  src  /  Element  
File Role Description
  Plain text file AbstractElement.php Class Class source
  Plain text file AccessRestrictedElementInterface.php Class Class source
  Plain text file AccessRestrictedElementTrait.php Class Class source
  Plain text file AssignedValueElementInterface.php Class Class source
  Plain text file AssignedValueElementTrait.php Class Class source
  Plain text file GenerateableInterface.php Class Class source
  Plain text file PhpAnnotation.php Class Class source
  Plain text file PhpAnnotationBlock.php Class Class source
  Plain text file PhpClass.php Class Class source
  Plain text file PhpConstant.php Class Class source
  Plain text file PhpDeclare.php Class Class source
  Plain text file PhpFile.php Class Class source
  Plain text file PhpFunction.php Class Class source
  Plain text file PhpFunctionParameter.php Class Class source
  Plain text file PhpInterface.php Class Class source
  Plain text file PhpMethod.php Class Class source
  Plain text file PhpProperty.php Class Class source
  Plain text file PhpVariable.php Class Class source
  Accessible without login Plain text file README.md Doc. Documentation
  Plain text file TypeHintedElementInterface.php Class Class source
  Plain text file TypeHintedElementTrait.php Class Class source

  Files folder image Files (63)  /  tests  
File Role Description
Files folder imageComponent (4 files)
Files folder imageElement (12 files)
Files folder imageresources (8 files)
  Accessible without login Plain text file TestCase.php Test Unit test script

  Files folder image Files (63)  /  tests  /  Component  
File Role Description
  Accessible without login Plain text file AbstractComponent.php Test Unit test script
  Accessible without login Plain text file PhpClassTest.php Test Unit test script
  Accessible without login Plain text file PhpFileTest.php Test Unit test script
  Accessible without login Plain text file PhpInterfaceTest.php Test Unit test script

  Files folder image Files (63)  /  tests  /  Element  
File Role Description
  Accessible without login Plain text file PhpAnnotationBlockTest.php Test Unit test script
  Accessible without login Plain text file PhpAnnotationTest.php Test Unit test script
  Accessible without login Plain text file PhpClassTest.php Test Unit test script
  Accessible without login Plain text file PhpConstantTest.php Test Unit test script
  Plain text file PhpDeclareTest.php Class Class source
  Accessible without login Plain text file PhpFileTest.php Test Unit test script
  Accessible without login Plain text file PhpFunctionParameterTest.php Test Unit test script
  Accessible without login Plain text file PhpFunctionTest.php Test Unit test script
  Accessible without login Plain text file PhpInterfaceTest.php Test Unit test script
  Accessible without login Plain text file PhpMethodTest.php Test Unit test script
  Accessible without login Plain text file PhpPropertyTest.php Test Unit test script
  Accessible without login Plain text file PhpVariableTest.php Test Unit test script

  Files folder image Files (63)  /  tests  /  resources  
File Role Description
  Accessible without login Plain text file PhpClassTest_SimpleToString.php Test Unit test script
  Accessible without login Plain text file PhpClassTest_Simpl...rnTypePerMethod.php Aux. Auxiliary script
  Accessible without login Plain text file PhpClassTest_Simpl...rnTypePerMethod.php Aux. Auxiliary script
  Accessible without login Plain text file PhpFileTest_SimpleClassToString.php Test Unit test script
  Plain text file PhpFileTest_Simple...gWithReturnType.php Class Class source
  Accessible without login Plain text file PhpFileTest_SimpleInterfaceToString.php Test Unit test script
  Accessible without login Plain text file PhpInterfaceTest_SimpleToString.php Test Unit test script
  Accessible without login Plain text file 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 Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:429
This week:0
All time:6,329
This week:38Up
 User Ratings  
 
 All time
Utility:93%StarStarStarStarStar
Consistency:93%StarStarStarStarStar
Documentation:81%StarStarStarStarStar
Examples:-
Tests:87%StarStarStarStarStar
Videos:-
Overall:73%StarStarStarStar
Rank:143