/** * Generate the action * * This is a gigantic method used to generate the actual Action code. It * uses the properties, description, name, and routes passed to it. * * This method uses the Zend_CodeGenerator_Php_* family to identify and create the * new files. * * @param string $name The name of the action * @param array $properties An array of properties related to an action * @param string $description A description for an action. Default false. * @param string $route The custom route for that action. Default false. * * @return string A generated file. */ private function generateAction($name, $properties, $description = false, $route = false) { $docblock = new Zend_CodeGenerator_Php_Docblock(array('shortDescription' => 'Required parameters', 'tags' => array(new Zend_CodeGenerator_Php_Docblock_Tag(array('name' => 'var', 'datatype' => 'array', 'description' => 'An array of required parameters.'))))); $class = new Zend_CodeGenerator_Php_Class(); $class->setName('Action_' . $name); $class->setExtendedClass('Frapi_Action'); $class->setImplementedInterfaces(array('Frapi_Action_Interface')); $tags = array(array('name' => 'link', 'description' => 'http://getfrapi.com'), array('name' => 'author', 'description' => 'Frapi <*****@*****.**>')); if ($route !== false) { $tags[] = array('name' => 'link', 'description' => $route); } $classDocblock = new Zend_CodeGenerator_Php_Docblock(array('shortDescription' => 'Action ' . $name . ' ', 'longDescription' => $description !== false ? $description : 'A class generated by Frapi', 'tags' => $tags)); $class->setDocblock($classDocblock); $class->setProperties(array(array('name' => 'requiredParams', 'visibility' => 'protected', 'defaultValue' => $properties, 'docblock' => $docblock), array('name' => 'data', 'visibility' => 'private', 'defaultValue' => array(), 'docblock' => new Zend_CodeGenerator_Php_Docblock(array('shortDescription' => 'The data container to use in toArray()', 'tags' => array(new Zend_CodeGenerator_Php_Docblock_Tag(array('name' => 'var', 'datatype' => 'array', 'description' => 'A container of data to fill and return in toArray()')))))))); $methods = array(); $docblock = new Zend_CodeGenerator_Php_Docblock(array('shortDescription' => 'To Array', 'longDescription' => "This method returns the value found in the database \n" . 'into an associative array.', 'tags' => array(new Zend_CodeGenerator_Php_Docblock_Tag_Return(array('datatype' => 'array'))))); $toArrayBody = ' ' . "\n"; if (!empty($properties)) { foreach ($properties as $p) { $toArrayBody .= '$this->data[\'' . $p . '\'] = ' . '$this->getParam(\'' . $p . '\', self::TYPE_OUTPUT);' . "\n"; } } $toArrayBody .= 'return $this->data;'; $methods[] = new Zend_CodeGenerator_Php_Method(array('name' => 'toArray', 'body' => $toArrayBody, 'docblock' => $docblock)); $executeActionBody = ''; if (!empty($properties)) { $executeActionBody = ' $valid = $this->hasRequiredParameters($this->requiredParams); if ($valid instanceof Frapi_Error) { return $valid; }'; } $executeActionBody .= "\n\n" . 'return $this->toArray();'; $docblockArray = array('shortDescription' => '', 'longDescription' => '', 'tags' => array(new Zend_CodeGenerator_Php_Docblock_Tag_Return(array('datatype' => 'array')))); $docblock = new Zend_CodeGenerator_Php_Docblock(array()); $docblockArray['shortDescription'] = 'Default Call Method'; $docblockArray['longDescription'] = 'This method is called when no specific ' . 'request handler has been found'; $methods[] = new Zend_CodeGenerator_Php_Method(array('name' => 'executeAction', 'body' => $executeActionBody, 'docblock' => $docblockArray)); $docblockArray['shortDescription'] = 'Get Request Handler'; $docblockArray['longDescription'] = 'This method is called when a request is a GET'; $methods[] = new Zend_CodeGenerator_Php_Method(array('name' => 'executeGet', 'body' => $executeActionBody, 'docblock' => $docblockArray)); $docblockArray['shortDescription'] = 'Post Request Handler'; $docblockArray['longDescription'] = 'This method is called when a request is a POST'; $methods[] = new Zend_CodeGenerator_Php_Method(array('name' => 'executePost', 'body' => $executeActionBody, 'docblock' => $docblockArray)); $docblockArray['shortDescription'] = 'Put Request Handler'; $docblockArray['longDescription'] = 'This method is called when a request is a PUT'; $methods[] = new Zend_CodeGenerator_Php_Method(array('name' => 'executePut', 'body' => $executeActionBody, 'docblock' => $docblockArray)); $docblockArray['shortDescription'] = 'Delete Request Handler'; $docblockArray['longDescription'] = 'This method is called when a request is a DELETE'; $methods[] = new Zend_CodeGenerator_Php_Method(array('name' => 'executeDelete', 'body' => $executeActionBody, 'docblock' => $docblockArray)); $docblockArray['shortDescription'] = 'Head Request Handler'; $docblockArray['longDescription'] = 'This method is called when a request is a HEAD'; $methods[] = new Zend_CodeGenerator_Php_Method(array('name' => 'executeHead', 'body' => $executeActionBody, 'docblock' => $docblockArray)); $class->setMethods($methods); $file = new Zend_CodeGenerator_Php_File(); $file->setClass($class); return $file->generate(); }
public function testImplementedInterfacesAccessors() { $codeGenClass = new Zend_CodeGenerator_Php_Class(); $codeGenClass->setImplementedInterfaces(array('Class1', 'Class2')); $this->assertEquals($codeGenClass->getImplementedInterfaces(), array('Class1', 'Class2')); }