Exemple #1
0
 public function insertControl($data)
 {
     $ok = true;
     $name = ucfirst(strtolower($data['name']));
     $controller = new Zend_CodeGenerator_Php_Class();
     $controller->setName($name . 'Controller');
     if ($data['parent']) {
         $controller->setExtendedClass($data['parent']);
     }
     if ($data['action']) {
         $ml = explode(',', str_replace(array(' '), array(''), trim($data['action'])));
         $act = array();
         foreach ($ml as $el) {
             $el = strtolower($el);
             $act[] = array('name' => $el . 'Action');
         }
         if ($act) {
             $controller->setMethods($act);
         }
     }
     $doc['tags'] = array();
     if ($data['zk_title']) {
         $doc['tags'][] = array('name' => 'zk_title', 'description' => $data['zk_title']);
     }
     if (!$data['zk_routable']) {
         $doc['tags'][] = array('name' => 'zk_routable', 'description' => 0);
     }
     if (!$data['zk_config']) {
         $doc['tags'][] = array('name' => 'zk_config', 'description' => 0);
     }
     if ($data['zk_routes']) {
         $doc['tags'][] = array('name' => 'zk_routes', 'description' => $data['zk_routes']);
     }
     if ($doc['tags']) {
         $controller->setDocblock(new Zend_CodeGenerator_Php_Docblock($doc));
     }
     $c = '<?php' . "\n\n" . $controller->generate();
     $ok = file_put_contents(APPLICATION_PATH . '/controllers/' . $name . 'Controller.php', $c);
     if ($ok) {
         @chmod(APPLICATION_PATH . '/controllers/' . $name . 'Controller.php', 0777);
     }
     return $ok;
 }
 /**
  * Method for generate the file form.
  * 
  * @param string $table
  * @param string $schema
  * @param array $column
  * @return string
  */
 public function generateFile($table, $schema, $columns)
 {
     try {
         $className = $fileName = $formName = $this->adjustsName($table);
         if (is_null($this->getNamespace())) {
             $className = $this->getClassPrefix() . $className;
         }
         $extendedClass = 'Zend_Form';
         if (!is_null($this->getNamespace())) {
             $extendedClass = $this->getNamespaceCharacter() . $extendedClass;
         }
         $class = new \Zend_CodeGenerator_Php_Class();
         $class->setExtendedClass($extendedClass);
         $class->setName($className);
         $class->setDocblock(new \Zend_CodeGenerator_Php_Docblock(array('shortDescription' => $className . ' form file.')));
         $constructBody = 'parent::__construct($options);' . PHP_EOL . PHP_EOL;
         $constructBody .= '$this->setName(\'frm' . $formName . '\');' . PHP_EOL;
         $constructBody .= '$this->setMethod(\'post\');' . PHP_EOL . PHP_EOL;
         $this->resetConstructParameters();
         foreach ($columns as $columnName => $params) {
             if ($params['primaryKey'] === true && $this->getGeneratePrimaryKeys() === false) {
                 continue;
             }
             $elementName = $this->adjustsName($columnName, true);
             $constructBody .= $this->createContentOfElementColumn($elementName, $columnName, $params);
         }
         $constructBody .= $this->createContentSubmit();
         $this->addConstructorParameters(array('name' => 'options', 'defaultValue' => null));
         $class->setMethods(array(array('name' => '__construct', 'parameters' => $this->getConstructorParameters(), 'body' => $constructBody)));
         $code = $class->generate();
         if (!is_null($this->getNamespace())) {
             $code = 'namespace ' . $this->getNamespace() . ';' . PHP_EOL . PHP_EOL . $code;
         }
         $code = '<?php' . PHP_EOL . PHP_EOL . $code;
         file_put_contents($this->getFileDestination() . '/' . $fileName . $this->getFileExtension(), $code);
         return 'Generated file ' . $fileName . $this->getFileExtension() . '...' . PHP_EOL;
     } catch (Exception $e) {
         return $e->getMessage();
     }
 }
Exemple #3
0
    /**
     * 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 testMethodAccessors()
 {
     $codeGenClass = new Zend_CodeGenerator_Php_Class();
     $codeGenClass->setMethods(array(array('name' => 'methodOne'), new Zend_CodeGenerator_Php_Method(array('name' => 'methodTwo'))));
     $methods = $codeGenClass->getMethods();
     $this->assertEquals(count($methods), 2);
     $this->isInstanceOf(current($methods), 'Zend_CodeGenerator_Php_Method');
     $method = $codeGenClass->getMethod('methodOne');
     $this->isInstanceOf($method, 'Zend_CodeGenerator_Php_Method');
     $this->assertEquals($method->getName(), 'methodOne');
     // add a new property
     $codeGenClass->setMethod(array('name' => 'methodThree'));
     $this->assertEquals(count($codeGenClass->getMethods()), 3);
 }
 /**
  * getContents()
  *
  * @return string
  */
 public function getContents()
 {
     $filter = new Zend_Filter_Word_DashToCamelCase();
     $className = $filter->filter($this->_projectProviderName) . 'Provider';
     $class = new Zend_CodeGenerator_Php_Class(array('name' => $className, 'extendedClass' => 'Zend_Tool_Project_Provider_Abstract'));
     $methods = array();
     foreach ($this->_actionNames as $actionName) {
         $methods[] = new Zend_CodeGenerator_Php_Method(array('name' => $actionName, 'body' => '        /** @todo Implementation */'));
     }
     if ($methods) {
         $class->setMethods($methods);
     }
     $codeGenFile = new Zend_CodeGenerator_Php_File(array('requiredFiles' => array('Zend/Tool/Project/Provider/Abstract.php', 'Zend/Tool/Project/Provider/Exception.php'), 'classes' => array($class)));
     return $codeGenFile->generate();
 }
Exemple #6
0
    public function insertControl($data)
    {
        $ok = true;
        $name = ucfirst($data['parentid']);
        $model = new Zend_CodeGenerator_Php_Class();
        $model->setName('Default_Model_' . $name);
        if ($data['parent']) {
            $model->setExtendedClass($data['parent']);
        }
        $prop = array();
        if ($data['table']) {
            $prop[] = array('name' => '_name', 'visibility' => 'protected', 'defaultValue' => trim($data['table']));
        }
        if ($data['multilang']) {
            $ml = explode(',', str_replace(array(' '), array(''), trim($data['multilang'])));
            $prop[] = array('name' => '_multilang_field', 'visibility' => 'protected', 'defaultValue' => $ml);
        }
        if ($prop) {
            $model->setProperties($prop);
        }
        if ($data['method']) {
            $met = array();
            foreach ($data['method'] as $el) {
                $mn = $mb = '';
                $mp = array();
                switch ($el) {
                    case 'list':
                        $mn = 'fetchList';
                        $mb = 'return $this->fetchAll();';
                        break;
                    case 'list_join':
                        $mn = 'fetchList';
                        $mb = '$m = new Default_Model_Temp();
$s = $this->getAdapter()->select()
	->from(array(\'i\' => $this->info(\'name\')))
	->join(array(\'m\' => $m->info(\'name\')), \'i.parentid = m.id\', array(
		\'temp\' => \'o.title\'
	))
	->group(\'i.id\')
	->order(\'i.orderid\');
return $this->fetchAll($s);';
                        break;
                    case 'card':
                        $mn = 'fetchCard';
                        $mp = array(array('name' => 'id'));
                        $mb = 'return $this->fetchRow(array(\'`stitle` = ?\' => $id));';
                        break;
                    case 'card_join':
                        $mn = 'fetchCard';
                        $mp = array(array('name' => 'id'));
                        $mb = '$m = new Default_Model_Temp();
$s = $this->getAdapter()->select()
	->from(array(\'i\' => $this->info(\'name\')))
	->join(array(\'m\' => $m->info(\'name\')), \'i.parentid = m.id\', array(
		\'temp\' => \'o.title\'
	))
	->group(\'i.id\')
	->where(\'`stitle` = ?\', $id);
return $this->fetchRow($s);';
                        break;
                    case 'idtitle':
                        $mn = 'fetchIdTitle';
                        $mb = 'return $this->fetchPairs(\'id\', \'title\', null, \'orderid\');';
                        break;
                }
                if ($mn) {
                    $met[] = array('name' => $mn, 'body' => $mb, 'parameters' => $mp);
                }
            }
            if ($met) {
                $model->setMethods($met);
            }
        }
        $c = '<?php' . "\n\n" . $model->generate();
        $ok = file_put_contents(APPLICATION_PATH . '/models/' . $name . '.php', $c);
        if ($ok) {
            @chmod(APPLICATION_PATH . '/models/' . $name . '.php', 0777);
        }
        if ($data['table'] && $data['table_create']) {
            $n = substr($data['table_create'], 3);
            $model = new Default_Model_Page();
            switch (substr($data['table_create'], 0, 3)) {
                case '_e_':
                    $model->getAdapter()->query('CREATE TABLE `' . $data['table'] . '` LIKE `' . $n . '`');
                    break;
                case '_t_':
                    $c = file_get_contents(APPLICATION_PATH . '/../library/Zkernel/Other/Template/Db/' . $n);
                    if ($c) {
                        $c = str_replace(array('%name%'), array($data['table']), $c);
                        $model->getAdapter()->query($c);
                    }
                    break;
            }
        }
        return $ok;
    }