Author: Nick Sagona, III (dev@nolainteractive.com)
Inheritance: implements Pop\Code\Generator\GeneratorInterface
Example #1
0
 /**
  * Get properties
  *
  * @return void
  */
 protected function getClassProperties()
 {
     // Detect and set properties
     $properties = $this->getDefaultProperties();
     if (count($properties) > 0) {
         foreach ($properties as $name => $value) {
             $property = $this->getProperty($name);
             $visibility = 'public';
             if ($property->isPublic()) {
                 $visibility = 'public';
             } else {
                 if ($property->isProtected()) {
                     $visibility = 'protected';
                 } else {
                     if ($property->isPrivate()) {
                         $visibility = 'private';
                     }
                 }
             }
             $doc = $property->getDocComment();
             if (null !== $doc && strpos($doc, '/*') !== false) {
                 $docblock = Generator\DocblockGenerator::parse($doc);
                 $desc = $docblock->getDesc();
                 $type = $docblock->getTag('var');
             } else {
                 $type = strtolower(gettype($value));
                 $desc = null;
             }
             if (is_array($value)) {
                 $formattedValue = count($value) == 0 ? null : $value;
             } else {
                 $formattedValue = $value;
             }
             $prop = new Generator\PropertyGenerator($property->getName(), $type, $formattedValue, $visibility);
             $prop->setStatic($property->isStatic());
             $prop->setDesc($desc);
             $this->generator->code()->addProperty($prop);
         }
     }
 }
Example #2
0
<?php

require_once '../../bootstrap.php';
use Pop\Code;
try {
    // Create the code generator object
    $code = new Code\Generator('MyClass.php', Code\Generator::CREATE_CLASS);
    $code->setDocblock(new Code\Generator\DocblockGenerator('This is my test class file'))->getDocblock()->setTag('category', 'Pop')->setTag('package', 'Pop_Code')->setTag('author', 'Joe Author');
    // Create namespace object
    $ns = new Code\Generator\NamespaceGenerator('Some\\Other');
    $ns->setUse('Some\\Other\\Thing')->setUse('Some\\Other\\Blah', 'B')->setUse('Some\\Other\\Another');
    // Create property object
    $prop = new Code\Generator\PropertyGenerator('_testProp', 'string', 'test', 'protected');
    $prop->setDesc('This is a test property');
    // Create a method object
    $method = new Code\Generator\MethodGenerator('__construct');
    $method->setDesc('This is a test method')->setBody("// Let's get some stuff to happen here." . PHP_EOL . "\$blah = 'Sounds like a good idea';")->appendToBody("echo \$blah;", false)->addArgument('test', "null", "Pop\\Filter\\String")->addArgument('other', "array()", 'array');
    // Add code pieces to the code file
    $code->setNamespace($ns);
    $code->code()->setDocblock(new Code\Generator\DocblockGenerator('This is my test class'))->getDocblock()->setTag('category', 'Pop')->setTag('package', 'Pop_Code')->setTag('author', 'Joe Author');
    $code->code()->addProperty($prop);
    $code->code()->addMethod($method);
    // Render and output the code
    $code->output();
} catch (\Exception $e) {
    echo $e->getMessage() . PHP_EOL . PHP_EOL;
}
Example #3
0
 public function testRender()
 {
     $p = PropertyGenerator::factory('testProp', 'array', array(0, 1, 2));
     $this->assertContains('array', $p->render(true));
     $p = PropertyGenerator::factory('testProp', 'array', array('prop1' => 1, 'prop2' => 2));
     $this->assertContains('array', $p->render(true));
     $p = PropertyGenerator::factory('testProp', 'int', 0);
     $this->assertContains('int', $p->render(true));
     $p = PropertyGenerator::factory('testProp', 'boolean', true);
     $this->assertContains('boolean', $p->render(true));
     $p = PropertyGenerator::factory('testProp', 'string', 0, 'const');
     $this->assertContains('const', $p->render(true));
     $p = PropertyGenerator::factory('testProp', 'string', 123);
     $p = PropertyGenerator::factory('testProp', 'array');
     $p->setStatic(true);
     $this->assertTrue($p->isStatic());
     $codeStr = (string) $p;
     $code = $p->render(true);
     ob_start();
     $p->render();
     $output = ob_get_clean();
     $this->assertContains('static', $code);
     $this->assertContains('static', $codeStr);
     $this->assertContains('static', $output);
 }