Zend_CodeGenerator_Php_File();
$file = new Zend_CodeGenerator_Php_File(array( 'classes' => array($ class )));
“Only one class is allowed inside a single PHP file.
Placing additional code in the file with the class is allowed, but not welcome. In such files, two blank lines must separate the class and the additional PHP code. ”
$ class = new Zend_CodeGenerator_Php_Class();
$class -> setName('MyClass')
$ class ->setExtendedClass( 'Myclass2' )
$docblock = new Zend_CodeGenerator_Php_Docblock(array(
'shortDescription' => ' ' ,
'longDescription' => ' Zend_CodeGenerator' ,
'tags' => array(
array(
'name' => 'version' ,
);
* This source code was highlighted with Source Code Highlighter .
$vars = array(
array(
'name' => 'name,
'visibility' => 'public' ,
'defaultValue' => '0',
)
array(
'name' => '_table',
'visibility' => 'protected' ,
'defaultValue' => '1',
)
);
$var1 = Zend_CodeGenerator_Php_Parameter(
array(
'name' => ' var ',
'visibility' => 'public' ,
'defaultValue' => '0',
)
);
$var2 = Zend_CodeGenerator_Php_Parameter(
array(
'name' => 'var2',
'visibility' => 'protected' ,
'defaultValue' => '1',
)
);
$vars = array($var1, var2);
$ class = new Zend_CodeGenerator_Php_Class();
$ class -> setName( 'MyClass extends MyClass2' ) -> setProperties($vars) -> setDocblock($docblock);
setMethod()
orsetMethods()
setMethod()
accepts either an instance of the Zend_CodeGenerator_Php_Method class or an array that describes a method, setMethods()
accepts an array that contains method objects, or a set of arrays.$method = Zend_CodeGenerator_Php_Method(array(
'name' => 'myMethod' ,
'body' => 'echo "hello CodeGenerator"' ,
'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
'shortDescription' => ' ' ,
'tags' => array(
new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
'datatype' => 'string|null' ,
)),
),
)),
)),
));
$ class ->setMethod($method);
$ class -> setMethods(array(
array(
'name' => 'myMethod' ,
'parameters' => array(
array( 'name' => 'myVar),
),
' body ' => ' $ var = 'hello world' ; ' . "\n" . ' return $ var ',
' docblock ' => new Zend_CodeGenerator_Php_Docblock(array(
' shortDescription ' => ' ',
' tags ' => array(
new Zend_CodeGenerator_Php_Docblock_Tag_Param(array(
' paramName ' => ' bar ',
' datatype ' => ' string '
)),
new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
' datatype ' => ' string ',
)),
),
)),
)
$code = $file->generate();
file_put_contents( 'file.php' , $code);
<?php
class FormGenerator
{
/**
* mysql
* - input type
* */
protected $_values = array(
'text' => 'Textarea' ,
'int' => 'Text' ,
'varchar' => 'Text' ,
'tinyint' => 'CheckBox' ,
'date' => 'Text' ,
'float' => 'Text' ,
'mediumint' => 'Text' ,
);
/**
*
* - Zend_Db_Table
* */
public function generateForm(Zend_Db_Table $table)
{
//
$tableInfo = $table->info();
//
$className = 'Form_' . ucfirst($tableInfo[ 'name' ]);
//
$folder = $_SERVER[ 'DOCUMENT_ROOT' ] . '/application/models/Form/' ;
//
$formClass = new Zend_CodeGenerator_Php_Class();
$formClass->setName($className);
$formClass->setExtendedClass( 'Zend_Form' );
// init
$initMethodBody = 'parent::init();
$helper = new Zend_View_Helper_Url();
' . "\n\t\t" ;
// generateElement()
foreach ($tableInfo[ 'metadata' ] as $field){
Zend_Debug::dump($field);
$initMethodBody .= $ this ->generateElement($field[ 'COLUMN_NAME' ], $field[ 'DATA_TYPE' ]);
}
// submit
$initMethodBody .= $ this ->generateSumitElement($tableInfo[ 'name' ]);
$initMethod = new Zend_CodeGenerator_Php_Method(
array(
'name' => 'init' ,
'body' =>$initMethodBody
));
//
$formClass->setMethod($initMethod);
//
$file = new Zend_CodeGenerator_Php_File(array(
'classes' => array($formClass)
));
//
$code = $file->generate();
//
if (!file_exists($folder)) {
mkdir($folder);
}
//
file_put_contents($folder.ucfirst($tableInfo[ 'name' ]) . '.php' , $code);
}
/**
*
* - mysql
*
* */
public function generateElement($name, $type)
{
$element = '$' . $name . ' = new Zend_Form_Element_' . $ this ->_values[$type]. "(\n\t\t"
. "'$name'," . " \n\t\t"
. "array("
. "'label' => '$name')); \n\t\t" ;
// Digits
if ($type == 'int' || $type == 'tinyint' || $type == 'mediumint' ) {
$element .= '$' . $name . '->addValidator("Digits", true);' . "\n\t\t" ;
}
// StripTags
if ($ this ->_values[$type] == 'Textarea' || $ this ->_values[$type] == 'Text' ){
$element .= '$' . $name . '->addFilter("StripTags");' . "\n\t\t" ;
}
// StringTrim
$element .= '$' . $name . '->addFilter("StringTrim");' . "\n\t\t"
. '$this->addElement($' . $name . ');' . "\n\n\t\t" ;
return $element;
}
/**
* submit
*/
public function generateSumitElement($name)
{
$element = '$submit_' . $name . ' = new Zend_Form_Element_Submit('
. "'submit_$name'" . "); \n\t\t"
. '$this->addElement($submit_' . $name . ');' ;
return $element;
}
}
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/57609/
All Articles