2017 © Pedro Peláez
 

library lib-generator

Code generator library of the Ride framework

image

ride/lib-generator

Code generator library of the Ride framework

  • Thursday, September 8, 2016
  • by ride-user
  • Repository
  • 8 Watchers
  • 1 Stars
  • 2,952 Installations
  • PHP
  • 6 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 10 Versions
  • 2 % Grown

The README.md

Ride: Code Generator Library

Code generator library of the PHP Ride framework., (*1)

What's In This Library

CodeGenerator

The CodeGenerator interface is the main interface of this library. You can use it to create classes, methods, properties and variables. After setting up you class, you can pass it to the generateClass method to get the PHP code., (*2)

CodeClass

The CodeClass interface is the representation of a class or interface. You can add constants, properties and methods to it. When done, you can pass it to the generateClass method of a CodeGenerator to get the PHP code., (*3)

CodeMethod

The CodeMethod interface is the representation of a class method. You can add arguments to it, set whether it's abstract or static, set the source, ... When the method is defined, you can add it to a CodeClass instance., (*4)

CodeProperty

The CodeProperty interface is the representation of a class property. It extends from the CodeVariable interface and can thus be used as a variable as well. The difference between the two is that a property has a scope., (*5)

CodeVariable

The CodeVariable interface is the representation of a variable. It can be used for a class constant, a method argument or a return value., (*6)

Code Sample

Check this code sample to see some possibilities of this library:, (*7)

<?php

use ride\library\generator\CodeGenerator;

function generateCode(CodeGenerator $generator) {
    // create a constant
    $constant = $generator->createVariable('MY_CONSTANT', 'string');
    $constant->setDefaultValue('constant');
    $constant->setDescription('Dummy constant');

    // create a property
    $isActiveProperty = $generator->createProperty('isActive', 'boolean', 'private');
    $isActiveProperty->setDefaultValue(false);
    $isActiveProperty->setDescription('Flag to see if this instance is active');

    // create needed variables
    $dataVariable = $generator->createVariable('data', 'vendor\\library\\data\\DataContainer');
    $dataVariable->setDescription('Data container to check');

    $isValidVariable = $generator->createVariable('isValid', 'boolean');
    $isValidVariable->setDescription('Flag to see if the provided data is valid');

    // create methods
    $isActiveMethod = $generator->createMethod('isActive');
    $isActiveMethod->setDescription('Checks if this instance is active');
    $isActiveMethod->setSource('return $this->isActive;');
    $isActiveMethod->setReturnValue($isActiveProperty);

    $setIsActiveMethod = $generator->createMethod('setIsActive', array($isActiveProperty));
    $setIsActiveMethod->setDescription('Sets the active state of this instance');
    $setIsActiveMethod->setSource('$this->isActive = $isActive;');

    $checkDataMethod = $generator->createMethod('checkData', array($dataVariable));
    $checkDataMethod->setDescription('Checks the provided data');
    $checkDataMethod->setSource('return $data->isValid();');
    $checkDataMethod->setReturnValue($isValidVariable);

    // join it all together in a class
    $class = $generator->createClass('vendor\\library\\MyClass', null, array('vendor\\library\\MyInterface'));
    $class->addConstant($constant);
    $class->addProperty($isActiveProperty);
    $class->addMethod($isActiveMethod);
    $class->addMethod($setIsActiveMethod);
    $class->addMethod($checkDataMethod);

    // generate the PHP code
    return $this->generator->generateClass($class);
}

This function will generate the following PHP code:, (*8)

<?php

namespace vendor\library;

use vendor\library\data\DataContainer;

class MyClass implements MyInterface {

    /**
     * Dummy constant
     * @var string
     */
    const MY_CONSTANT = 'constant';

    /**
     * Flag to see if this instance is active
     * @var boolean
     */
    private $isActive = false;

    /**
     * Checks if this instance is active
     * @return boolean Flag to see if this instance is active
     */
    public function isActive() {
        return $this->isActive;
    }

    /**
     * Sets the active state of this instance
     * @param boolean $isActive Flag to see if this instance is active
     * @return null
     */
    public function setIsActive($isActive = false) {
        $this->isActive = $isActive;
    }

    /**
     * Checks the provided data
     * @param \vendor\library\data\DataContainer $data Data container to check
     * @return boolean Flag to see if the provided data is valid
     */
    public function checkData(DataContainer $data) {
        return $data->isValid();
    }

}

Installation

You can use Composer to install this library., (*9)

composer require ride/lib-generator

The Versions

08/09 2016

dev-master

9999999-dev

Code generator library of the Ride framework

  Sources   Download

MIT

by Joris Vandeweerd

08/09 2016

dev-develop

dev-develop

Code generator library of the Ride framework

  Sources   Download

MIT

by Joris Vandeweerd

08/09 2016

1.1.1

1.1.1.0

Code generator library of the Ride framework

  Sources   Download

MIT

by Joris Vandeweerd

23/06 2016

1.1.0

1.1.0.0

Code generator library of the Ride framework

  Sources   Download

MIT

by Joris Vandeweerd

22/06 2016

1.0.0

1.0.0.0

Code generator library of the Ride framework

  Sources   Download

MIT

by Joris Vandeweerd

18/05 2016

0.1.4

0.1.4.0

Code generator library of the Ride framework

  Sources   Download

MIT

by Joris Vandeweerd

14/03 2015

0.1.3

0.1.3.0

Code generator library of the Ride framework

  Sources   Download

MIT

by Joris Vandeweerd

08/08 2014

0.1.2

0.1.2.0

Code generator library of the Ride framework

  Sources   Download

MIT

by Joris Vandeweerd

06/06 2014

0.1.1

0.1.1.0

Code generator library of the Ride framework

  Sources   Download

MIT

by Joris Vandeweerd

03/06 2014

0.1.0

0.1.0.0

Code generator library of the Ride framework

  Sources   Download

MIT

by Joris Vandeweerd