public function getContents() { // Configuring after instantiation $methodUp = new Zend_CodeGenerator_Php_Method(); $methodUp->setName('up')->setBody('// upgrade'); // Configuring after instantiation $methodDown = new Zend_CodeGenerator_Php_Method(); $methodDown->setName('down')->setBody('// degrade'); $class = new Zend_CodeGenerator_Php_Class(); $class->setName('Migration_' . $this->getMigrationName())->setExtendedClass('Core_Migration_Abstract')->setMethod($methodUp)->setMethod($methodDown); $file = new Zend_CodeGenerator_Php_File(); $file->setClass($class)->setFilename($this->getPath()); return $file->generate(); }
protected function _getPopulateMethod() { $metadata = $this->_getMetadata(); $entityVar = $this->_getEntityVarName(); $populate = new Zend_CodeGenerator_Php_Method(); $populate->setName('populate'); $populate->setParameter(array('name' => $entityVar)); $body = ''; $fields = $metadata->fieldNames; $identifierFields = $metadata->getIdentifierFieldNames(); foreach ($fields as $field) { if (!in_array($field, $identifierFields)) { switch ($metadata->getTypeOfField($field)) { default: $body .= '$this->' . $field . '->setValue(${%entityVar}->get' . ucfirst($field) . '());' . PHP_EOL; } } } $populate->setBody($body); return $populate; }
/** * @group ZF-7361 */ public function testHasMethod() { $method = new Zend_CodeGenerator_Php_Method(); $method->setName('methodOne'); $codeGenClass = new Zend_CodeGenerator_Php_Class(); $codeGenClass->setMethod($method); $this->assertTrue($codeGenClass->hasMethod('methodOne')); }
mkdir($module_dir . "/lib/" . $module_name_uc . "/Db", 0777, true); mkdir($module_dir . "/lib/" . $module_name_uc . "/Plugins", 0777, true); mkdir($module_dir . "/models", 0777, true); mkdir($module_dir . "/sql", 0777, true); mkdir($module_dir . "/sql/PDO_MYSQL", 0777, true); mkdir($frontend_theme_dir . "/index", 0777, true); mkdir($admin_theme_dir . "/admin", 0777, true); // MODULE.INI $ini_template = file_get_contents($template_dir . "/module.ini.txt"); $ini_template = str_replace(array("MODULE_NAME_LOWER", "MODULE_NAME_UC"), array($module_name, $module_name_uc), $ini_template); file_put_contents($module_dir . "/module.ini", $ini_template); // INIT METHOD (required in all Rivety controller classes) $init_method = new Zend_CodeGenerator_Php_Method(); $init_method->setName('init')->setBody("\t\tparent::init();"); // FRONTEND INDEX $index_smarty_template = file_get_contents($template_dir . "/index.tpl.txt"); $index_smarty_template = str_replace("MODULE_NAME_UC", $module_name_uc, $index_smarty_template); file_put_contents($frontend_theme_dir . "/index/index.tpl", $index_smarty_template); $index_controller_template = file_get_contents($template_dir . "/IndexController.php.txt"); $index_controller_template = str_replace("MODULE_NAME_UC", $module_name_uc, $index_controller_template); file_put_contents($module_dir . "/controllers/IndexController.php", $index_controller_template); // ADMIN INDEX $admin_index_smarty_template = file_get_contents($template_dir . "/index_admin.tpl.txt"); $admin_index_smarty_template = str_replace("MODULE_NAME_UC", $module_name_uc, $admin_index_smarty_template); file_put_contents($admin_theme_dir . "/admin/index.tpl", $admin_index_smarty_template); $admin_index_controller_template = file_get_contents($template_dir . "/AdminController.php.txt"); $admin_index_controller_template = str_replace("MODULE_NAME_UC", $module_name_uc, $admin_index_controller_template);
$controller_class->setMethod($index_method); // load index template $index_template = file_get_contents($basepath . "/modules/bolts/extras/crudify_templates/index.tpl"); if ($is_admin) { $index_template_replacements = array("OBJECT_NICENAME" => $name, "THEME_GLOBAL_PATH_VAR_NAME" => "admin_theme_global_path", "CREATE_NEW_URL" => "/" . $module_name . "/" . strtolower($name) . "admin/edit", "ROWSET_VAR" => strtolower($name), "THE_ID" => $identity_col, "INDEX_URL" => $index_url); } else { $index_template_replacements = array("OBJECT_NICENAME" => $name, "THEME_GLOBAL_PATH_VAR_NAME" => "theme_global_path", "CREATE_NEW_URL" => "/" . $module_name . "/" . strtolower($name) . "/edit", "ROWSET_VAR" => strtolower($name), "THE_ID" => $identity_col, "INDEX_URL" => $index_url); } $index_template = Bolts_Common::replaceWithArray($index_template, $index_template_replacements); file_put_contents($theme_dir . "/index.tpl", $index_template); // add delete method $delete_method = new Zend_CodeGenerator_Php_Method(); $delete_method_body = file_get_contents($basepath . "/modules/bolts/extras/crudify_templates/deleteAction.txt"); $delete_action_replacements = array("TABLE_OBJECT_VAR" => $table_object_var, "TABLE_CLASSNAME" => $object_name, "ROW_OBJECT_VAR" => $row_object_var, "THE_ID" => $identity_col, "OBJECT_NICENAME" => $name, "ROWSET_VAR" => strtolower($name), "DELETE_URL" => $delete_url, "INDEX_URL" => $index_url); $delete_method_body = Bolts_Common::replaceWithArray($delete_method_body, $delete_action_replacements); $delete_method->setName('deleteAction')->setBody($delete_method_body); $controller_class->setMethod($delete_method); // load delete template $delete_template = file_get_contents($basepath . "/modules/bolts/extras/crudify_templates/delete.tpl"); if ($is_admin) { $delete_template_replacements = array("OBJECT_NICENAME" => $name, "THEME_GLOBAL_PATH_VAR_NAME" => "admin_theme_global_path", "CREATE_NEW_URL" => "/" . $module_name . "/" . strtolower($name) . "admin/edit", "ROWSET_VAR" => strtolower($name), "THE_ID" => $identity_col, "DELETE_URL" => $delete_url, "INDEX_URL" => $index_url); } else { $delete_template_replacements = array("OBJECT_NICENAME" => $name, "THEME_GLOBAL_PATH_VAR_NAME" => "theme_global_path", "CREATE_NEW_URL" => "/" . $module_name . "/" . strtolower($name) . "/edit", "ROWSET_VAR" => strtolower($name), "THE_ID" => $identity_col, "DELETE_URL" => $delete_url, "INDEX_URL" => $index_url); } $delete_template = Bolts_Common::replaceWithArray($delete_template, $delete_template_replacements); file_put_contents($theme_dir . "/delete.tpl", $delete_template); $controller_file = new Zend_CodeGenerator_Php_File(); $controller_file->setClass($controller_class); } // Render the generated files file_put_contents($basepath . "/modules/" . $module_name . "/models/" . $object_name . ".php", $model_file->generate());
/** * Method create's new migration file * * @param string $module Module name * @param null $migrationBody * @param string $label * @param string $desc * @return string Migration name */ public function create($module = null, $migrationBody = null, $label = '', $desc = '') { $path = $this->getMigrationsDirectoryPath($module); list($sec, $msec) = explode(".", microtime(true)); $_migrationName = date('Ymd_His_') . substr($msec, 0, 2); if (!empty($label)) { $_migrationName .= '_' . $label; } // Configuring after instantiation $methodUp = new Zend_CodeGenerator_Php_Method(); $methodUp->setName('up')->setBody('// upgrade'); // Configuring after instantiation $methodDown = new Zend_CodeGenerator_Php_Method(); $methodDown->setName('down')->setBody('// degrade'); //add description if (!empty($desc)) { $methodDesc = new Zend_CodeGenerator_Php_Method(); $methodDesc->setName('getDescription')->setBody("return '" . addslashes($desc) . "'; "); } if ($migrationBody) { if (isset($migrationBody['up'])) { $upBody = ''; foreach ($migrationBody['up'] as $query) { $upBody .= '$this->query(\'' . $query . '\');' . PHP_EOL; } $methodUp->setBody($upBody); } if (isset($migrationBody['down'])) { $downBody = ''; foreach ($migrationBody['down'] as $query) { $downBody .= '$this->query(\'' . $query . '\');' . PHP_EOL; } $methodDown->setBody($downBody); } } $class = new Zend_CodeGenerator_Php_Class(); $className = (null !== $module ? ucfirst($module) . '_' : '') . 'Migration_' . $_migrationName; $class->setName($className)->setExtendedClass('Core_Migration_Abstract')->setMethod($methodUp)->setMethod($methodDown); if (isset($methodDesc)) { $class->setMethod($methodDesc); } $file = new Zend_CodeGenerator_Php_File(); $file->setClass($class)->setFilename($path . '/' . $_migrationName . '.php')->write(); return $_migrationName; }
/** * @see models_ClassGenerator_Defaults_Interface::createMethodToArray */ public function createMethodToArray() { $resultMethod = new Zend_CodeGenerator_Php_Method(); $resultMethod->setName('toArray'); $resultMethod->setVisibility(Zend_CodeGenerator_Php_Property::VISIBILITY_PUBLIC); $body = 'return array(' . "\n"; $columnConstants = $this->_provideSqlColumnConstants(); if (!empty($columnConstants)) { foreach ($columnConstants as $columnConstant => $attribute) { /* @var $catp Zend_CodeGenerator_Php_Property */ //TODO - Später in Konstante! $body .= "\t" . 'self::' . $columnConstant . '\' => $this->' . $attribute->getName() . ",\n"; } } $body .= ');'; $resultMethod->setBody($body); //Comment $docblock = new Zend_CodeGenerator_Php_Docblock(); $docblock->setLongDescription('Returns the array representation of the object using the table column names as keys.'); $resultMethod->setDocblock($docblock); $this->getClass()->setMethod($resultMethod); }
/** * @group ZF-6444 */ public function testMethodWithFinalModifierIsNotEmittedWhenMethodIsAbstract() { $codeGen = new Zend_CodeGenerator_Php_Method(); $codeGen->setName('foo'); $codeGen->setParameters(array(array('name' => 'one'))); $codeGen->setFinal(true); $codeGen->setAbstract(true); $expected = <<<EOS abstract public function foo(\$one) { } EOS; $this->assertEquals($expected, $codeGen->generate()); }
/** * * @param string $pAction * @return string */ public function create_action($pAction = NULL, $pParams = NULL) { $action = $pAction ? $pAction : $this->get_action(); if ($pParams && array_key_exists('view_body', $pParams)) { $view_body = $pParams['view_body']; unset($pParams['view_body']); } else { $view_body = ''; if ($pParams && is_array($pParams) && count($pParams)) { foreach ($pParams as $param) { if (is_array($param)) { list($name, $alias, $default) = $param; $default = trim($default); } elseif (!trim($param)) { continue; } else { $name = $alias = trim($param); $default = NULL; } $name = trim($name); if (!$name) { continue; } ob_start(); ?> $this-><?php echo $name; ?> ; <?php $view_body .= ob_get_clean(); } } } $file = $this->controller_reflection(); $c = $file->getClass($this->controller_class_name()); $aname = "{$action}Action"; $class = Zend_CodeGenerator_Php_Class::fromReflection($c); if (!$class->hasMethod($aname)) { $body = ''; $reflect = ''; if ($pParams && is_array($pParams) && count($pParams)) { ob_start(); foreach ($pParams as $param) { if (!trim($param)) { continue; } elseif (is_array($param)) { list($name, $alias, $default) = $param; $default = trim($default); } else { $name = $alias = trim($param); $default = NULL; } $name = trim($name); if (!$name) { continue; } printf('$%s = $this->_getParam("%s", %s); ', $name, $alias, is_null($default) ? ' NULL ' : "'{$default}'"); ob_start(); printf('$this->view->%s = $%s;', $name, $name); ?> <?php $reflect .= ob_get_clean(); ?> <?php } $body = ob_get_clean() . "\n" . $reflect; } $old = $this->backup_controller(); $method = new Zend_CodeGenerator_Php_Method(); $method->setName($aname)->setBody($body); $class->setMethod($method); $file = new Zend_CodeGenerator_Php_File(); $file->setClass($class); $new_file = preg_replace('~[\\r\\n][\\s]*[\\r\\n]~', "\r", $file->generate()); file_put_contents($this->controller_path(), $new_file); $view_path = $this->view_path($action); if (!file_exists($view_path)) { $dir = dirname($view_path); if (!is_dir($dir)) { mkdir($dir, 0775, TRUE); } file_put_contents($view_path, "<?\n\$this->placeholder('page_title')->set('');\n{$view_body}\n"); } $exec = "diff {$this->_backup_path} {$this->controller_path()} "; $diff = shell_exec($exec); return array('old' => $old, 'new' => $new_file, 'diff' => $diff, 'backup_path' => $this->_backup_path, 'controller_path' => $this->controller_path()); } }
function addMethod($methodConfig, $class) { if ($methodConfig->name == '__toString') { $this->_tostringCreated = true; } $method = new Zend_CodeGenerator_Php_Method(); $method->setName($methodConfig->name); $method->setBody($methodConfig->body); $class->setMethod($method); }