/** * Install the project class files * * @param \Pop\Config $install * @param string $installDir * @return void */ public static function install($install, $installDir) { // Create the project class file $projectCls = new Generator($install->project->base . '/module/' . $install->project->name . '/src/' . $install->project->name . '/Project.php', Generator::CREATE_CLASS); // Set namespace $ns = new NamespaceGenerator($install->project->name); $ns->setUse('Pop\\Project\\Project', 'P'); // Create 'run' method $run = new MethodGenerator('run'); $run->setDesc('Add any project specific code to this method for run-time use here.'); $run->appendToBody('parent::run();', false); $run->getDocblock()->setReturn('void'); // Finalize the project config file and save it $projectCls->setNamespace($ns); $projectCls->code()->setParent('P')->addMethod($run); $projectCls->save(); $input = self::installWeb(); // Install any web config and controller files if ($input != 'n') { if (file_exists(__DIR__ . '/Web/index.php')) { $index = new Generator(__DIR__ . '/Web/index.php'); $contents = $index->read() . '// Run the project' . PHP_EOL . 'try {' . PHP_EOL . ' $project->run();' . PHP_EOL . '} catch (\\Exception $e) {' . PHP_EOL . ' echo $e->getMessage();' . PHP_EOL . '}' . PHP_EOL; file_put_contents($install->project->docroot . '/index.php', $contents); } if ($input == 'a') { if (file_exists(__DIR__ . '/Web/ht.access')) { copy(__DIR__ . '/Web/ht.access', $install->project->docroot . '/.htaccess'); } } else { if ($input == 'i') { if (file_exists(__DIR__ . '/Web/web.config')) { copy(__DIR__ . '/Web/web.config', $install->project->docroot . '/web.config'); } } else { echo \Pop\I18n\I18n::factory()->__('You will have to install your web server rewrite configuration manually.') . PHP_EOL; } } } }
<?php require_once '../../bootstrap.php'; use Pop\Code; try { $reflect = new Code\Reflection('Pop\\Compress\\Zlib'); $code = $reflect->generator(); // Create a method object to add to the class $method = new Code\Generator\MethodGenerator('someNewMethod'); $method->setDesc('This is a new 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'); $code->code()->addMethod($method); $code->output(); } catch (\Exception $e) { echo $e->getMessage() . PHP_EOL . PHP_EOL; }
/** * Create the controller class files * * @param array $controllers * @param array $base * @param string $depth * @param \Pop\Code\Generator $controllerCls * @return void */ public static function createControllers($controllers, $base = null, $depth = null, $controllerCls = null) { foreach ($controllers as $key => $value) { $level = strpos($key, '/') !== false ? $depth . $key : null; if (null !== $level) { if ($level != '/') { $l = substr($level, 1); if (strpos($l, '/') !== false) { $l = substr($l, 0, strpos($l, '/')); } $ns = '\\' . ucfirst($l); $l = '/' . ucfirst($l); $lView = $level; } else { $ns = null; $l = null; $lView = null; } // Check to make sure an 'index' method is defined for the top-level controller if (substr_count($level, '/') == 1 && !array_key_exists('index', $value)) { echo "The 'index' method of the top level controller '{$key}' is not defined." . PHP_EOL; exit(0); } $viewPath = $base['view'] . ($level != '/' ? $level : null); $relativeViewPath = strpos($base['src'] . $l, 'Controller/') !== false ? '/../../../../view' . $lView : '/../../../view' . $lView; $srcPath = $base['src'] . $l; $namespace = $base['namespace'] . $ns; if (array_key_exists('index', $value) && (null === $l || strtolower($key) == strtolower($l))) { $ctrlFile = $base['src'] . $l . '/IndexController.php'; $parent = 'C'; } else { if (array_key_exists('index', $value) && strtolower($key) != strtolower($l)) { $ctrlFile = $base['src'] . $l . '/' . ucfirst(substr($key, 1)) . 'Controller.php'; $parent = 'C'; } else { $ctrlFile = $base['src'] . $l . '/' . ucfirst(substr($key, 1)) . 'Controller.php'; $parent = 'IndexController'; } } if (!file_exists($viewPath)) { mkdir($viewPath); } if (!file_exists($srcPath)) { mkdir($srcPath); } if (null === $controllerCls || $controllerCls->getFullpath() != $ctrlFile) { $controllerCls = new Generator($ctrlFile, Generator::CREATE_CLASS); // Set namespace $ns = new NamespaceGenerator($namespace); $ns->setUses(array('Pop\\Http\\Response', 'Pop\\Http\\Request', array('Pop\\Mvc\\Controller', 'C'), 'Pop\\Mvc\\View', 'Pop\\Project\\Project')); // Create the constructor $construct = new MethodGenerator('__construct'); $construct->setDesc('Constructor method to instantiate the controller object'); $construct->addArguments(array(array('name' => 'request', 'value' => 'null', 'type' => 'Request'), array('name' => 'response', 'value' => 'null', 'type' => 'Response'), array('name' => 'project', 'value' => 'null', 'type' => 'Project'), array('name' => 'viewPath', 'value' => 'null', 'type' => 'string'))); if ($parent == 'C') { $construct->appendToBody("if (null === \$viewPath) {")->appendToBody(" \$viewPath = __DIR__ . '{$relativeViewPath}';")->appendToBody("}" . PHP_EOL); } if ($level != '/') { $construct->appendToBody("if (null === \$request) {")->appendToBody(" \$request = new Request(null, '{$level}');")->appendToBody("}" . PHP_EOL); } $construct->appendToBody("parent::__construct(\$request, \$response, \$project, \$viewPath);", false); $construct->getDocblock()->setReturn('self'); $controllerCls->setNamespace($ns); $controllerCls->code()->setParent($parent)->addMethod($construct); } } if (is_array($value)) { self::createControllers($value, $base, $level, $controllerCls); } else { // Copy view files over $viewPath = $base['view'] . ($depth != '/' ? $depth : null); if (!file_exists($viewPath)) { mkdir($viewPath); } $viewFile = $base['installDir'] . '/view' . ($depth != '/' ? $depth : null) . '/' . $value; $viewFileCopy = $base['view'] . ($depth != '/' ? $depth : null) . '/' . $value; if (file_exists($viewFile)) { copy($viewFile, $viewFileCopy); } // Create action methods $method = new MethodGenerator($key); $method->setDesc('The \'' . $key . '()\' method.'); $code = $key == 'error' ? '404' : null; if ($controllerCls->code()->getParent() != 'C') { $vp = substr($depth, strrpos($depth, '/') + 1) . '/' . $value; } else { $vp = $value; } $method->appendToBody("\$this->view = View::factory(\$this->viewPath . '/{$vp}');"); $method->appendToBody("\$this->send(" . $code . ");", false); $method->getDocblock()->setReturn('void'); $controllerCls->code()->addMethod($method); } } $controllerCls->save(); }
public function testRender() { $m = MethodGenerator::factory('testMethod'); $m->setBody('some body code', true); $m->appendToBody('some more body code'); $m->appendToBody('even more body code', false); $m->addArgument('testVar', 123, 'int'); $m->addParameter('oneMoreTestVar', 789, 'int'); $codeStr = (string) $m; $code = $m->render(true); ob_start(); $m->render(); $output = ob_get_clean(); $this->assertContains('function testMethod($testVar = 123, $oneMoreTestVar = 789)', $output); $this->assertContains('function testMethod($testVar = 123, $oneMoreTestVar = 789)', $code); $this->assertContains('function testMethod($testVar = 123, $oneMoreTestVar = 789)', $codeStr); }
/** * Get methods * * @return void */ protected function getClassMethods() { // Detect and set methods $methods = $this->getMethods(); if (count($methods) > 0) { foreach ($methods as $value) { $methodExport = \ReflectionMethod::export($value->class, $value->name, true); // Get the method docblock if (strpos($methodExport, '/*') !== false && strpos($methodExport, '*/') !== false) { $docBlock = substr($methodExport, strpos($methodExport, '/*')); $docBlock = substr($docBlock, 0, strpos($methodExport, '*/') + 2); } else { $docBlock = null; } $method = $this->getMethod($value->name); $visibility = 'public'; if ($method->isPublic()) { $visibility = 'public'; } else { if ($method->isProtected()) { $visibility = 'protected'; } else { if ($method->isPrivate()) { $visibility = 'private'; } } } $mthd = new Generator\MethodGenerator($value->name, $visibility, $method->isStatic()); if (null !== $docBlock && strpos($docBlock, '/*') !== false) { $mthd->setDocblock(Generator\DocblockGenerator::parse($docBlock, $mthd->getIndent())); } $mthd->setFinal($method->isFinal())->setAbstract($method->isAbstract()); // Get the method parameters if (stripos($methodExport, 'Parameter') !== false) { $matches = array(); preg_match_all('/Parameter \\#(.*)\\]/m', $methodExport, $matches); if (isset($matches[0][0])) { foreach ($matches[0] as $param) { $name = null; $value = null; $type = null; // Get name $name = substr($param, strpos($param, '$')); $name = trim(substr($name, 0, strpos($name, ' '))); // Get value if (strpos($param, '=') !== false) { $value = trim(substr($param, strpos($param, '=') + 1)); $value = trim(substr($value, 0, strpos($value, ' '))); $value = str_replace('NULL', 'null', $value); } // Get type $type = substr($param, strpos($param, '>') + 1); $type = trim(substr($type, 0, strpos($type, '$'))); if ($type == '') { $type = null; } $mthd->addArgument($name, $value, $type); } } } // Get method body if (strpos($methodExport, '@@') !== false && file_exists($this->getFilename())) { $lineNums = substr($methodExport, strpos($methodExport, '@@ ') + 3); $start = trim(substr($lineNums, strpos($lineNums, ' '))); $end = substr($start, strpos($start, ' - ') + 3); $start = substr($start, 0, strpos($start, ' ')); $end = (int) $end; if (is_numeric($start) && is_numeric($end)) { $classLines = file($this->getFilename()); $body = null; $start = $start + 1; $end = $end - 1; for ($i = $start; $i < $end; $i++) { if (isset($classLines[$i])) { if (substr($classLines[$i], 0, 8) == ' ') { $body .= substr($classLines[$i], 8); } else { if (substr($classLines[$i], 0, 4) == ' ') { $body .= substr($classLines[$i], 4); } else { if (substr($classLines[$i], 0, 2) == "\t\t") { $body .= substr($classLines[$i], 2); } else { if (substr($classLines[$i], 0, 1) == "\t") { $body .= substr($classLines[$i], 1); } else { $body .= $classLines[$i]; } } } } } } $mthd->setBody(rtrim($body), false); } } $this->generator->code()->addMethod($mthd); } } }
/** * Install the form class files * * @param \Pop\Config $install * @return void */ public static function install($install) { echo \Pop\I18n\I18n::factory()->__('Creating form class files...') . PHP_EOL; // Create form class folder $formDir = $install->project->base . '/module/' . $install->project->name . '/src/' . $install->project->name . '/Form'; if (!file_exists($formDir)) { mkdir($formDir); } $forms = $install->forms->asArray(); foreach ($forms as $name => $form) { $formName = ucfirst(\Pop\Filter\String::underscoreToCamelcase($name)); // Define namespace $ns = new NamespaceGenerator($install->project->name . '\\Form'); $ns->setUse('Pop\\Form\\Form')->setUse('Pop\\Form\\Element')->setUse('Pop\\Validator'); // Create the constructor $construct = new MethodGenerator('__construct'); $construct->setDesc('Constructor method to instantiate the form object'); $construct->getDocblock()->setReturn('self'); $construct->addArguments(array(array('name' => 'action', 'value' => 'null', 'type' => 'string'), array('name' => 'method', 'value' => "'post'", 'type' => 'string'), array('name' => 'fields', 'value' => 'null', 'type' => 'array'), array('name' => 'indent', 'value' => 'null', 'type' => 'string'))); // Create the init values array within the constructor if (is_array($form) && count($form) > 0) { $construct->appendToBody("\$this->initFieldsValues = array ("); $i = 0; foreach ($form as $name => $field) { $i++; $construct->appendToBody(" '" . $name . "' => array ("); $j = 0; foreach ($field as $key => $value) { $j++; $comma = $j < count($field) ? ',' : null; if ($key == 'validators') { $val = null; if (is_array($value)) { $val = 'array(' . PHP_EOL; foreach ($value as $v) { $val .= ' new Validator\\' . $v . ',' . PHP_EOL; } $val .= ' )'; } else { $val = 'new Validator\\' . $value; } $construct->appendToBody(" '{$key}' => {$val}{$comma}"); } else { if ($key == 'value' || $key == 'marked' || $key == 'attributes' || $key == 'error') { $val = var_export($value, true); $val = str_replace(PHP_EOL, PHP_EOL . ' ', $val); if (strpos($val, 'Select::') !== false) { $val = 'Element\\' . str_replace("'", '', $val); } $construct->appendToBody(" '{$key}' => {$val}{$comma}"); } else { if (is_bool($value)) { $val = $value ? 'true' : 'false'; } else { $val = "'" . $value . "'"; } $construct->appendToBody(" '{$key}' => {$val}{$comma}"); } } } $end = $i < count($form) ? ' ),' : ' )'; $construct->appendToBody($end); } $construct->appendToBody(");"); } $construct->appendToBody("parent::__construct(\$action, \$method, \$fields, \$indent);"); // Create and save form class file $formCls = new Generator($formDir . '/' . $formName . '.php', Generator::CREATE_CLASS); $formCls->setNamespace($ns); $formCls->code()->setParent('Form')->addMethod($construct); $formCls->save(); } }
<?php require_once '../../bootstrap.php'; use Pop\Code; try { // Create the code generator object $code = new Code\Generator('../tmp/MyInterface.php', Code\Generator::CREATE_INTERFACE); $code->setDocblock(new Code\Generator\DocblockGenerator('This is my test interface 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 a method object $method = new Code\Generator\MethodGenerator('testMethod'); $method->setDesc('This is a test method')->addArgument('test', "null", '\\Pop\\Filter\\String')->addArgument('other', "array()", 'array'); // Create another method object $method2 = new Code\Generator\MethodGenerator('anotherMethod'); $method2->setDesc('This is another test method')->addArgument('someParam', "array()", 'array'); // Add code pieces to the code file $code->setNamespace($ns); $code->code()->setDocblock(new Code\Generator\DocblockGenerator('This is my test interface'))->getDocblock()->setTag('category', 'Pop')->setTag('package', 'Pop_Code')->setTag('author', 'Joe Author'); $code->code()->addMethod($method)->addMethod($method2); // Render and save the interface $code->save(); echo 'Interface saved.'; } catch (\Exception $e) { echo $e->getMessage() . PHP_EOL . PHP_EOL; }