Pop Code Component
Switch branches/tags
Nothing to show
Clone or download
Latest commit 034b81c Jun 22, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Add environment tag Jun 22, 2018
tests Add environment tag Jun 22, 2018
.gitattributes Update version, add gitattributes Jul 9, 2015
.gitignore Start working on the unit tests Jun 12, 2015
.travis.yml First commit v3 Oct 16, 2016
LICENSE.TXT Update license & copyright Jan 29, 2018
README.md Update README Feb 22, 2017
composer.json Update license Jan 29, 2018
phpunit.xml Start working on the unit tests Jun 12, 2015

README.md

pop-code

Build Status Coverage Status

OVERVIEW

pop-code provides the ability to dynamically generate PHP code on the fly as well as parse and modify existing PHP code.

pop-code is a component of the Pop PHP Framework.

INSTALL

Install pop-code using Composer.

composer require popphp/pop-code

BASIC USAGE

Create a class with a property and a method

use Pop\Code\Generator;

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

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

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

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

// Save the class file
$class->save();

// Or, you can echo out the contents of the code directly
echo $class;
The newly created class will look like:
<?php
/**
 * @namespace
 */
namespace MyApp;

class MyClass
{

    /**
     * @var   string
     */
    protected $foo = 'bar';


    /**
     * This is the method to set foo.
     */
    public function setFoo($foo)
    {
        $this->foo = $foo;

    }

}

Parse an existing class and add a method to it

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.

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

$class = new Reflection('MyApp\MyClass');

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

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

// Echo out the code
echo $reflect->generator();
The modified class will look like:
<?php
/**
 * @namespace
 */
namespace MyApp;

class MyClass implements
{

    /**
     *
     * @var   string
     */
    protected $foo = 'bar';


    /**
     */
    public function setFoo($foo)
    {
        $this->foo = $foo;
    }


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

    }

}

As you can see, the new method was appended to the class.