public function addSuccess($param) { if ($param['resourceId'] && $param['model']) { $nameExploded = explode('_', $param['resourceId']); $nameExploded = array_map('ucfirst', $nameExploded); $controllerName = $nameExploded[count($nameExploded) - 1]; unset($nameExploded[count($nameExploded) - 1]); $ds = DIRECTORY_SEPARATOR; $path = APPLICATION_PATH . $ds . 'modules' . $ds . $this->getRequest()->getModuleName() . $ds . 'controllers' . (($pathAdd = implode($ds, $nameExploded)) ? $ds . $pathAdd : ''); $fileName = $controllerName . 'Controller.php'; $controllerName = ucfirst($this->getRequest()->getModuleName()) . '_' . (($filePrefix = implode('_', $nameExploded)) ? $filePrefix . '_' : '') . $controllerName . 'Controller'; $class_file = new Zend_CodeGenerator_Php_Class(array('name' => $controllerName, 'extendedclass' => 'Z_Admin_Controller_Datacontrol_Abstract')); Z_Fs::create_file($path . $ds . $fileName, "<?\n" . $class_file->generate()); } }
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(); } }
/** * Генерирует класс модели * @param string $className * Название класса (без префикса) * @param string $tableName * Название таблицы в БД * @param array $params * Параметры для переопределения настроек по умолчанию * @return string */ public static function generate($className, $tableName = NULL, $params = array()) { if (strpos($className, 'z_') === 0 && Z_Auth::getInstance()->getUser()->getRole() == 'root') { $path_prefix = APPLICATION_PATH . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'library' . DIRECTORY_SEPARATOR . 'Z' . DIRECTORY_SEPARATOR . 'Model'; self::$_classPrefix = isset($params['prefixz']) ? $params['prefixz'] : self::$_classPrefixZ; } else { $path_prefix = APPLICATION_PATH . DIRECTORY_SEPARATOR . 'models'; self::$_classPrefix = isset($params['prefix']) ? $params['prefix'] : self::$_classPrefix; } if ($tableName == NULL) { $tableName = strtolower($className); } $className = explode('_', $className); $className = array_map('ucfirst', $className); $path = $className; unset($path[count($path) - 1]); $path = implode(DIRECTORY_SEPARATOR, $path); $filename = $className[count($className) - 1] . '.php'; $className = implode('_', $className); $filepath = $path_prefix . DIRECTORY_SEPARATOR . $path; $generator = new Zend_CodeGenerator_Php_Class(); $generator->setName(self::$_classPrefix . $className)->setExtendedClass(self::$_extendedClass)->setProperty(array('name' => '_name', 'visibility' => 'protected', 'defaultValue' => $tableName)); Z_Fs::create_file($filepath . DIRECTORY_SEPARATOR . $filename, "<?\n" . $generator->generate()); }
/** * @group ZF-9602 */ public function testSetextendedclassShouldNotIgnoreNonEmptyClassnameOnGenerate() { $codeGenClass = new Zend_CodeGenerator_Php_Class(); $codeGenClass->setName('MyClass')->setExtendedClass('ParentClass'); $expected = <<<CODE class MyClass extends ParentClass { } CODE; $this->assertEquals($expected, $codeGenClass->generate()); }
public function testToString() { $codeGenClass = new Zend_CodeGenerator_Php_Class(array('abstract' => true, 'name' => 'SampleClass', 'extendedClass' => 'ExtendedClassName', 'implementedInterfaces' => array('Iterator', 'Traversable'), 'properties' => array(array('name' => 'foo'), array('name' => 'bar')), 'methods' => array(array('name' => 'baz')))); $expectedOutput = <<<EOS abstract class SampleClass extends ExtendedClassName implements Iterator, Traversable { public \$foo = null; public \$bar = null; public function baz() { } } EOS; $output = $codeGenClass->generate(); $this->assertEquals($expectedOutput, $output, $output); }
/** * @group ZF-11513 */ public function testAllowsClassConstantToHaveSameNameAsClassProperty() { $const = new Zend_CodeGenerator_Php_Property(); $const->setName('name')->setDefaultValue('constant')->setConst(true); $property = new Zend_CodeGenerator_Php_Property(); $property->setName('name')->setDefaultValue('property'); $codeGenClass = new Zend_CodeGenerator_Php_Class(); $codeGenClass->setName('My_Class')->setProperties(array($const, $property)); $expected = <<<CODE class My_Class { const name = 'constant'; public \$name = 'property'; } CODE; $this->assertEquals($expected, $codeGenClass->generate()); }
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; }
function createModel($namespace, $class, $location) { $columns = $this->getColumnsProperty('COLUMN_NAME', $this->getTableForClass($class)); $relationships = array(); $methods = array(); foreach ($columns as $col_name) { if (strstr($col_name, "_id")) { $class_name = substr($col_name, 0, -3); $table_name = $this->getTableForClass($class_name); // Todo: Get a proper pluralisation library if ($this->tableExists($table_name)) { $methods[] = new \Zend_CodeGenerator_Php_Method(array('name' => $class_name, 'body' => 'return $this->belongs_to(\'' . ucfirst($class_name) . '\', \'' . $col_name . '\');')); $relationships[] = $class_name; } } } $cols = new \Zend_CodeGenerator_Php_Property(array("name" => "columns", "static" => true, "defaultValue" => $this->getColumnsProperty('COLUMN_NAME', $class))); $links = new \Zend_CodeGenerator_Php_Property(array("name" => "links", "static" => true, "defaultValue" => $relationships)); $table_name = new \Zend_CodeGenerator_Php_Property(array("name" => "_table", "static" => true, "defaultValue" => $this->getTableForClass($class))); $cls = new \Zend_CodeGenerator_Php_Class(array('name' => ucfirst($class), 'extendedClass' => '\\Model', 'properties' => array($cols, $table_name, $links), 'methods' => $methods)); file_put_contents($location, "<?php namespace {$namespace};\n" . $cls->generate()); include $location; return $class; }