2017 © Pedro Peláez
 

library pop-code

Pop Code Component for Pop PHP Framework

image

popphp/pop-code

Pop Code Component for Pop PHP Framework

  • Friday, June 22, 2018
  • by nicksagona
  • Repository
  • 1 Watchers
  • 3 Stars
  • 1,050 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 11 Versions
  • 6 % Grown

The README.md

pop-code

Build Status Coverage Status, (*1)

Join the chat at https://discord.gg/TZjgT74U7E, (*2)

Overview

pop-code provides the ability to dynamically generate PHP code on the fly as well as parse and modify existing PHP code., (*3)

pop-code is a component of the Pop PHP Framework., (*4)

Top, (*5)

Install

Install pop-code using Composer., (*6)

composer require popphp/pop-code

Or, require it in your composer.json file, (*7)

"require": {
    "popphp/pop-view" : "^5.0.4"
}

Top, (*8)

Quickstart

Create a simple function

In this example, a function is created and rendered to a string:, (*9)

use Pop\Code\Generator;

$function = new Generator\FunctionGenerator('sayHello');
$function->addArgument('name', 'null', 'string');
$function->setBody("echo 'Hello ' . \$name;");
$function->addReturnType('void');
$function->setDesc('This is the first function');

echo $function;
/**
 * This is the first function
 * 
 * @param  string|null  $name
 * @return void
 */
function sayHello(string|null $name = null): void
{
    echo 'Hello ' . $name;
}

Create a simple class

In this example, a class is created and saved to a file:, (*10)

use Pop\Code\Generator;

// Create the class object and give it a namespace
$class = new Generator\ClassGenerator('MyClass');
$class->setNamespace(new Generator\NamespaceGenerator('MyApp'));

// Create a new protected property with a default value
$prop = new Generator\PropertyGenerator('foo', 'string', null, 'protected');

// Create a method and give it an argument, body and docblock description
$method = new Generator\MethodGenerator('setFoo', 'public');
$method->addArgument('foo', null, 'string')
    ->setBody('$this->foo = $foo;')
    ->addReturnType('void')
    ->setDesc('This is the method to set foo.');

// Add the property and the method to the class code object
$class->addProperty($prop);
$class->addMethod($method);

// Save the class to a file
$code = new Generator($class);
$code->writeToFile('MuClass.php');

The contents of the file will be:, (*11)

<?php

/**
 * @namespace 
 */
namespace MyApp;

class MyClass
{

    /**
     * @var   string
     */
    protected string|null $foo = null;

    /**
     * This is the method to set foo.
     * 
     * @param  string  $foo
     * @return void
     */
    public function setFoo(string $foo): void
    {
        $this->foo = $foo;
    }

}

Top, (*12)

Generate Code

There are a number of individual code generators available to manage the creation and output of various types of code blocks. Code generators are available for the following type of code:, (*13)

  • Classes
  • Interfaces
  • Traits
  • Methods
  • Functions
  • Constants
  • Properties
  • Namespaces
  • Docblocks
  • Bodies (general blocks of code)

Create a file with some functions

use Pop\Code\Generator;

$function1 = new Generator\FunctionGenerator('sayHello');
$function1->addArgument('name', null, 'string')
    ->setBody("echo 'Hello ' . \$name;")
    ->setDesc('This is the first function')
    ->addReturnType('void');

$function2 = new Generator\FunctionGenerator('sayGoodbye');
$function2->addArgument('name', null, 'string')
    ->setBody("echo 'Goodbye ' . \$name;")
    ->setDesc('This is the second function')
    ->addReturnType('void');

$code = new Generator();
$code->addCodeObjects([$function1, $function2]);
$code->writeToFile('functions.php');

The above code will produce a file called functions.php with the following code in it:, (*14)

<?php

/**
 * This is the first function
 * 
 * @param  string  $name
 * @return void
 */
function sayHello(string $name): void
{
    echo 'Hello ' . $name;
}

/**
 * This is the second function
 * 
 * @param  string  $name
 * @return void
 */
function sayGoodbye(string $name): void
{
    echo 'Goodbye ' . $name;
}

Top, (*15)

Parse Code

This pop-code component also provides the ability to parse existing code, which is useful to obtain information about the code or to even modify and save new code., (*16)

In this example, we use the class that we created above. The reflection object provides you with a code generator object like the one above so that you can add or remove things from the parsed code., (*17)

use Pop\Code\Reflection;
use Pop\Code\Generator;

$class = Reflection::createClass('MyApp\MyClass');

// Create the new method that you want to add to the existing class
$method = new Generator\MethodGenerator('hasFoo', 'public');
$method->addArgument('foo', null, 'string')
    ->setBody('return ($this->foo !== null);')
    ->setDesc('This is the method to see if foo is set.')
    ->addReturnType('bool');

// Access the generator and it's code object to add the new method to it
$class->addMethod($method);

// Echo out the code
$code = new Generator($class);
$code->writeToFile('MyClass.php');

And the modified class will look like, complete with the new hasFoo() method:, (*18)

<?php

/**
 * @namespace 
 */
namespace MyApp;

class MyClass
{

    /**
     * @var   string
     */
    protected string|null $foo = null;

    /**
     * This is the method to set foo.
     * 
     * @param  string  $foo
     * @return void
     */
    public function setFoo(string $foo): void
    {
        $this->foo = $foo;
    }

    /**
     * This is the method to see if foo is set.
     * 
     * @param  string  $foo
     * @return bool
     */
    public function hasFoo(string $foo): bool
    {
        return ($this->foo !== null);
    }

}

Top, (*19)

The Versions

22/06 2018

dev-master

9999999-dev http://www.popphp.org/

Pop Code Component for Pop PHP Framework

  Sources   Download

BSD-3-Clause New BSD

The Requires

  • php >=5.6.0

 

The Development Requires

php reflection code generation pop pop php

22/06 2018

dev-v3-dev

dev-v3-dev http://www.popphp.org/

Pop Code Component for Pop PHP Framework

  Sources   Download

BSD-3-Clause New BSD

The Requires

  • php >=5.6.0

 

The Development Requires

php reflection code generation pop pop php

22/06 2018

3.1.0

3.1.0.0 http://www.popphp.org/

Pop Code Component for Pop PHP Framework

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.6.0

 

The Development Requires

php reflection code generation pop pop php

29/01 2018

v2.x-dev

2.9999999.9999999.9999999-dev http://www.popphp.org/

Pop Code Component for Pop PHP Framework

  Sources   Download

BSD-3-Clause New BSD

The Requires

  • php >=5.4.0

 

The Development Requires

php reflection code generation pop pop php

29/01 2018

3.0.1

3.0.1.0 http://www.popphp.org/

Pop Code Component for Pop PHP Framework

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.6.0

 

The Development Requires

php reflection code generation pop pop php

22/02 2017

3.0.0

3.0.0.0 http://www.popphp.org/

Pop Code Component for Pop PHP Framework

  Sources   Download

New BSD

The Requires

  • php >=5.6.0

 

The Development Requires

php reflection code generation pop pop php

03/10 2016

2.1.1

2.1.1.0 http://www.popphp.org/

Pop Code Component for Pop PHP Framework

  Sources   Download

New BSD

The Requires

  • php >=5.4.0

 

The Development Requires

php reflection code generation pop pop php

01/07 2016

2.1.0

2.1.0.0 http://www.popphp.org/

Pop Code Component for Pop PHP Framework

  Sources   Download

New BSD

The Requires

  • php >=5.4.0

 

The Development Requires

php reflection code generation pop pop php

10/05 2016

2.0.0p2

2.0.0.0-patch2 http://www.popphp.org/

Pop Code Component for Pop PHP Framework

  Sources   Download

New BSD

The Requires

  • php >=5.4.0

 

The Development Requires

php reflection code generation pop pop php

19/02 2016

2.0.0p1

2.0.0.0-patch1 http://www.popphp.org/

Pop Code Component for Pop PHP Framework

  Sources   Download

New BSD

The Requires

  • php >=5.4.0

 

The Development Requires

php reflection code generation pop pop php

16/07 2015

2.0.0

2.0.0.0 http://www.popphp.org/

Pop Code Component for Pop PHP Framework

  Sources   Download

New BSD

The Requires

  • php >=5.4.0

 

The Development Requires

php reflection code generation pop pop php