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: 430 All time: 6,321 This week: 52Up
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  
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

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:430
This week:0
All time:6,321
This week:52Up
 User Ratings  
 
 All time
Utility:93%StarStarStarStarStar
Consistency:93%StarStarStarStarStar
Documentation:81%StarStarStarStarStar
Examples:-
Tests:87%StarStarStarStarStar
Videos:-
Overall:73%StarStarStarStar
Rank:143