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; }
/** * 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); }
public function testMethodBodyGetterAndSetter() { $this->_method->setBody('Foo'); $this->assertEquals('Foo', $this->_method->getBody()); }
/** * 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); }