public function testDocblockGetterAndSetter() { $d = new \Zend\CodeGenerator\Php\PhpDocblock(); $this->_method->setDocblock($d); $this->assertTrue($d === $this->_method->getDocblock()); }
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; }
/** * setMethod() * * @param array|Zend_CodeGenerator_Php_Method $method * @return Zend_CodeGenerator_Php_Class */ public function setMethod($method) { if (is_array($method)) { $method = new Zend_CodeGenerator_Php_Method($method); $methodName = $method->getName(); } elseif ($method instanceof Zend_CodeGenerator_Php_Method) { $methodName = $method->getName(); } else { require_once 'Zend/CodeGenerator/Php/Exception.php'; throw new Zend_CodeGenerator_Php_Exception('setMethod() expects either an array of method options or an instance of Zend_CodeGenerator_Php_Method'); } if (isset($this->_methods[$methodName])) { require_once 'Zend/CodeGenerator/Php/Exception.php'; throw new Zend_CodeGenerator_Php_Exception('A method by name ' . $methodName . ' already exists in this class.'); } $this->_methods[$methodName] = $method; return $this; }
/** * @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", 0777, true); 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");
$index_method_body = file_get_contents($basepath . "/modules/bolts/extras/crudify_templates/indexAction.txt"); $index_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), "INDEX_URL" => $index_url); $index_method_body = Bolts_Common::replaceWithArray($index_method_body, $index_action_replacements); $index_method->setName('indexAction')->setBody($index_method_body); $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();
/** * 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-7205 */ public function testMethodCanHaveDocblock() { $codeGenProperty = new Zend_CodeGenerator_Php_Method(array('name' => 'someFoo', 'static' => true, 'visibility' => 'protected', 'docblock' => '@var string $someVal This is some val')); $expected = <<<EOS /** * @var string \$someVal This is some val */ protected static function someFoo() { } EOS; $this->assertEquals($expected, $codeGenProperty->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()); } }
/** * Append method to class * * @param string $className * @param string $methodName * @param string $append * @param array $params * @param null|array $flags * @param string $ignoreRegex * * @return bool|Zend_CodeGenerator_Php_Class */ public static function appendMethod($className, $methodName, $append, array $params, $flags = null, $ignoreRegex = null) { // Get class that method belogs to $class = self::get($className); // Get existing method (if any) $method = $class->getMethod($methodName); $body = ''; // If method already exists, set existing body that is to be appended to and check ignoreRegex if ($method) { $body = $method->getBody() . "\n"; // Trim indentation to avoid recursion if ($indent = preg_match('/^(\\s*)/', $body, $match)) { $indent = $match[1]; $body = preg_replace('/^' . $indent . '/m', '', $body); } // Check if ignoreregex matches and if so skip append if ($ignoreRegex != null and preg_match($ignoreRegex, $body)) { return false; } } // Append body to method $body .= $append; // Generate method anew (only the body is inherited) $method = new Zend_CodeGenerator_Php_Method(array('name' => $methodName)); // Set params (if any) if (!empty($params)) { foreach ($params as $param) { $method->setParameter($param); } } // Append body to method structure $method->setBody($body); // Check for flags if (is_array($flags)) { if (in_array('static', $flags)) { // Method is static $method->setStatic(true); } } // Append method to class $class->setMethod($method); // Save class return self::save($class); }
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); }
public function table_file() { $cs = $this->create_sql(); $create_method = new Zend_CodeGenerator_Php_Method(array('name' => 'create_table', 'visibility' => 'public', 'static' => TRUE, 'body' => "\$this->getInstance()->getAdapter()->query(\"{$cs}\");")); $create_method->setStatic(TRUE); $init_method = new Zend_CodeGenerator_Php_Method(array('name' => 'init', 'visibility' => 'protected', 'body' => ' $create_method->setStatic(TRUE);')); $id_prop = new Zend_CodeGenerator_Php_Property(array('name' => '_id_field', 'defaultValue' => $this->get_id_field(), 'visibility' => 'protected')); $name_prop = new Zend_CodeGenerator_Php_Property(array('name' => '_name', 'defaultValue' => $this->get_table_name(), 'visibility' => 'protected')); $class = new Zend_CodeGenerator_Php_Class(array('name' => $this->get_table_class_name(), 'extendedClass' => 'Zupal_Table_Abstract', 'methods' => array($create_method), 'properties' => array($id_prop, $name_prop))); if ($this->get_database_name()) { $const = new Zend_CodeGenerator_Php_Method(array('name' => '__construct', 'body' => ' parent::__construct(array("db" => Zupal_Module_Manager::getInstance()->database("' . $this->get_database_name() . '")));')); $class->setMethod($const); } $file = new Zend_CodeGenerator_Php_File(array('classes' => array($class))); return $file; }