承接 ride/lib-generator 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ride/lib-generator

最新稳定版本:1.1.2

Composer 安装命令:

composer require ride/lib-generator

包简介

Code generator library of the Ride framework

README 文档

README

Code generator library of the PHP Ride framework.

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.

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.

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.

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.

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.

Code Sample

Check this code sample to see some possibilities of this library:

<?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:

<?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.

composer require ride/lib-generator

统计信息

  • 总下载量: 4.89k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 6
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 8
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-06-03