Example #1
0
 /**
  * @see models_ClassGenerator_Defaults_Interface::createMethodLoad
  */
 public function createMethodLoad()
 {
     $resultMethod = new Zend_CodeGenerator_Php_Method();
     $resultMethod->setName('load');
     $resultMethod->setStatic(true);
     $resultMethod->setVisibility(Zend_CodeGenerator_Php_Property::VISIBILITY_PUBLIC);
     //---------------------- Parameters ---------------------
     $paramZendDbObj = new Zend_CodeGenerator_Php_Parameter();
     $paramZendDbObj->setName('zendDbObj');
     $paramZendDbObj->setType('Zend_Db_Adapter_Abstract');
     $resultMethod->setParameter($paramZendDbObj);
     //Pkeys
     $pkeyAttributes = $this->getPersistenceInformation()->getPkeyAttributes();
     if (!empty($pkeyAttributes)) {
         foreach ($pkeyAttributes as $pkeyAttribute) {
             /* @var $pkeyAttribute Zend_CodeGenerator_Php_Property */
             $paramPkey = new Zend_CodeGenerator_Php_Parameter();
             $paramPkey->setName('par' . ucfirst($pkeyAttribute->getName()));
             $paramPkey->setType(Model_ClassGenerator_PersistenceInformation::getType($pkeyAttribute));
             $resultMethod->setParameter($paramPkey);
         }
     }
     //--------------------------------------------------------
     //Pkeys
     $body = 'if(!isset(' . $this->_provideInstancesKeySelectorString() . ')){' . "\n";
     $docblock = new Zend_CodeGenerator_Php_Docblock();
     $docblock->setShortDescription('Loads the entry from the database.');
     $resultMethod->setDocblock($docblock);
     $body .= "\t" . '$table = self::' . $this->_provideTableConstant() . ';' . "\n";
     //TODO - Konstante!
     $body .= "\t" . '$select = $this->zendDbObj->select($table, \'*\');' . "\n";
     $pkeyAttributes = $this->getPersistenceInformation()->getPkeyAttributes();
     if (!empty($pkeyAttributes)) {
         foreach ($pkeyAttributes as $pkeyAttribute) {
             /* @var $pkeyAttribute Zend_CodeGenerator_Php_Property */
             $body .= "\t" . '$select->where(self::' . $this->_provideTableConstant() . '.\'.\'.self::' . $this->_provideColumnConstant($pkeyAttribute) . '.\'=?\', $this->' . $pkeyAttribute->getName() . ');' . "\n\n";
         }
     }
     $body .= "\t" . 'self::$instance' . $keysString . ' = new self($parZendDbObj);' . "\n";
     $body .= '}' . "\n\n";
     $body .= 'return self::$instance' . $keysString . ';';
     $resultMethod->setBody($body);
     $this->getClass()->setMethod($resultMethod);
 }
Example #2
0
    /**
     * @group ZF-6444
     */
    public function testMethodWithStaticModifierIsEmitted()
    {
        $codeGen = new Zend_CodeGenerator_Php_Method();
        $codeGen->setName('foo');
        $codeGen->setParameters(array(array('name' => 'one')));
        $codeGen->setStatic(true);
        $expected = <<<EOS
    public static function foo(\$one)
    {
    }

EOS;
        $this->assertEquals($expected, $codeGen->generate());
    }
Example #3
0
 function gen($package)
 {
     ini_set('display_errors', 1);
     $xml = App_Model_Config::getConfigFilePath($package);
     //   APPLICATION_PATH . '/configs/' . $package . '-model-config.xml';
     $configFileName = basename($xml);
     $data = $config = new Zend_Config_Xml($xml, 'production');
     $classList = $data->classes;
     $project = $data->project;
     $createPackageFolder = true;
     $destinationFolder = $data->destinationDirectory;
     if ('application' == $project) {
         $createPackageFolder = false;
         $destinationFolder = APPLICATION_PATH . "/" . $destinationFolder;
     } elseif ('global' == $project) {
         $createPackageFolder = true;
         $destinationFolder = GLOBAL_PROJECT_PATH . "/" . $destinationFolder;
     } else {
         $createPackageFolder = true;
         $destinationFolder = realpath(APPLICATION_PATH . "/../library");
     }
     // die($destinationFolder);
     $bodyConstruct = '';
     foreach ($classList as $modelName => $attr) {
         $class = new Zend_CodeGenerator_Php_Class();
         //$class->isAbstract();
         $class->setAbstract(true);
         $class2 = new Zend_CodeGenerator_Php_Class();
         $docblock = new Zend_CodeGenerator_Php_Docblock(array('shortDescription' => $modelName, 'longDescription' => 'This is a class generated with Zend_CodeGenerator.', 'tags' => array(array('name' => 'uses', 'description' => $package . '_Model_Abstract'), array('name' => 'package', 'description' => $package), array('name' => 'subpackage', 'description' => 'Model'), array('name' => 'version', 'description' => '$Rev:$'), array('name' => 'update', 'description' => date('d/m/Y')), array('name' => 'license', 'description' => 'licensed By Patiwat Wisedsukol patiwat.wi@gmail.com'))));
         //$docblock2 = new Zend_CodeGenerator_Php_Docblock();
         echo "create ", $modelName, "<br/>";
         $class->setName($modelName . "_Abstract");
         $class2->setName($modelName);
         if ('' == trim($attr->extendedClass)) {
             $class->setExtendedClass($package . '_Model_Abstract');
         } else {
             $class->setExtendedClass($attr->extendedClass);
         }
         $class2->setExtendedClass($modelName . '_Abstract');
         $Prop = array();
         $Methods = array();
         $PropertyData = array();
         $columsToPropsList = array();
         $propsToColumsList = array();
         foreach ($attr->prop as $prop) {
             $name = $prop->name;
             $Prop[] = array('name' => "_" . $name, 'visibility' => 'protected', 'defaultValue' => null);
             $pdata = array();
             $PropertyData[$name] = $this->process_data_array($prop);
             $columsToPropsList[$prop->column] = $prop->name;
             $propsToColumsList[$prop->name] = $prop->column;
             $Methods[] = $name;
         }
         // print_r($PropertyData);
         // exit();
         $PropertyDataString = $this->gen_array($PropertyData);
         //$p = new Zend_CodeGenerator_Php_Property_DefaultValue(array('value'=>$PropertyDataString ,'type'=>Zend_CodeGenerator_Php_Property_DefaultValue::TYPE_ARRAY));
         $Prop[] = array('name' => "propertyData", 'visibility' => 'public', 'defaultValue' => $PropertyDataString);
         if (isset($attr->config)) {
             $configDataString = $this->gen_array($attr->config->toArray());
         } else {
             $configDataString = null;
         }
         $Prop[] = array('name' => "CONFIG_FILE_NAME", 'visibility' => 'public', 'const' => true, 'defaultValue' => $configFileName);
         $Prop[] = array('name' => "COLUMS_TO_PROPS_LIST", 'visibility' => 'public', 'defaultValue' => $this->gen_array($columsToPropsList));
         $Prop[] = array('name' => "PROPS_TO_COLUMS_LIST", 'visibility' => 'public', 'defaultValue' => $this->gen_array($propsToColumsList));
         $Prop[] = array('name' => "configData", 'visibility' => 'public', 'defaultValue' => $configDataString);
         $configSQLString = isset($attr->config->sql) ? (string) $attr->config->sql : '';
         $Prop[] = array('name' => "_baseSQL", 'visibility' => 'public', 'defaultValue' => $configSQLString);
         $class->setDocblock($docblock);
         $class->setProperties($Prop);
         /*	
         $Property = new Zend_CodeGenerator_Php_Property();
         $pv = new Zend_CodeGenerator_Php_Property_DefaultValue($PropertyDataString);
         $pv->setType(Zend_CodeGenerator_Php_Property_DefaultValue::TYPE_ARRAY);
             $Property->setDefaultValue($pv);
              $Property->setName('_propertyData');
             
         
         $class->setProperty($Property);
         */
         $method = new Zend_CodeGenerator_Php_Method();
         $method->setName('__construct');
         $method->setBody("parent::__construct ( \$options, '{$modelName}');");
         $method->setParameters(array(array('name' => 'options', 'type' => 'array', 'defaultValue' => null)));
         $class->setMethod($method);
         foreach ($Methods as $name) {
             $method = new Zend_CodeGenerator_Php_Method();
             $method->setName('set' . strtoupper(substr($name, 0, 1)) . substr($name, 1, strlen($name)));
             $method->setBody(" \n  \n\t\t \$this->_{$name} = \${$name};  \n\n\t\treturn \$this; \n ");
             $method->setParameter(array('name' => $name));
             $docblock = new Zend_CodeGenerator_Php_Docblock(array('shortDescription' => "Set the {$name} property", 'tags' => array(array('name' => 'param', 'description' => "\${$name} the \${$name} to set"), array('name' => 'return', 'description' => $modelName))));
             $method->setDocblock($docblock);
             $class->setMethod($method);
         }
         foreach ($Methods as $name) {
             $method = new Zend_CodeGenerator_Php_Method();
             $method->setName('get' . strtoupper(substr($name, 0, 1)) . substr($name, 1, strlen($name)));
             $method->setBody(" \n  \n\t\treturn  \$this->_{$name}; \n\n\t\t\n\t\t");
             $docblock = new Zend_CodeGenerator_Php_Docblock(array('shortDescription' => "get the {$name} property", 'tags' => array(array('name' => 'return', 'description' => "the {$name}"))));
             $method->setDocblock($docblock);
             $class->setMethod($method);
         }
         $method = new Zend_CodeGenerator_Php_Method();
         $method->setParameter(array('name' => 'id'));
         $method->setStatic(true);
         $method->setName('getObjectByID');
         $method->setBody("\n           \n            \n \$obj = new {$modelName}();\n\t\t    \n \$obj->find(\$id);\n\t\t    \n return \$obj;\n\t\t    ");
         $docblock = new Zend_CodeGenerator_Php_Docblock(array('shortDescription' => "get Singleton  {$modelName}", 'tags' => array(array('name' => 'return', 'description' => "{$modelName}"))));
         $method->setDocblock($docblock);
         $class->setMethod($method);
         ////////////Gen Method Form Config/////////////////
         $tostringCreated = false;
         if (isset($attr->method->name)) {
             $this->addMethod($attr->method, $class);
         } else {
             if (is_array($attr->method)) {
                 foreach ($attr->method as $methodConfig) {
                     $this->addMethod($methodConfig, $class);
                 }
             }
         }
         //ถ้ายังไม่มี __tostrint จะ เพิ่มให้ แต่จะ return ค่าว่าง
         if (false == $this->_isTostringCreated()) {
             $method = new Zend_CodeGenerator_Php_Method();
             $method->setName('__toString');
             $method->setBody("\n\n\t\t    \n return '';\n\t\t    ");
             $class->setMethod($method);
         }
         ///////////////////////END GEN METHOD///////////////////////////////
         $file = new Zend_CodeGenerator_Php_File();
         $file->setClass($class);
         $package = ucfirst(strtolower($package));
         //  Acc/Model/
         if (false == $createPackageFolder) {
             $modelPath = str_replace(ucfirst(strtolower($package)) . "_Model_", "", $modelName);
         } else {
             $modelPath = $modelName;
         }
         $modelPath = str_replace("_", "/", $modelPath);
         //$structure = dirname ( __FILE__ )."/{$modelName}";
         echo $destinationFolder . "/" . $modelPath;
         $structure = realpath($destinationFolder) . "/" . $modelPath;
         //"D:\workspaces\privatework\jiranan\private\library/"."/{$modelName}";
         if (!is_dir($structure)) {
             mkdir($structure, 0777, true);
         }
         $filecontent = $this->filecontentfix($file->generate());
         file_put_contents($structure . "/Abstract.php", $filecontent);
         echo "create [ ", $structure, "/Abstract.php ] complete <br/>";
         // $structure2 =  dirname ( __FILE__ )."/".$modelName.".php";
         $structure2 = realpath($destinationFolder) . "/{$modelPath}.php";
         //"D:\workspaces\privatework\jiranan\private\library/".$modelName.".php";
         //	echo file_exists ($structure2 ==true)? 'true' : 'false';
         echo "<br/>";
         if (file_exists($structure2)) {
             print "The file {$structure2} exists<br/>";
         } else {
             print "The file {$structure2} does not exist<br/>";
             echo "<br/>";
             $file2 = new Zend_CodeGenerator_Php_File();
             $file2->setClass($class2);
             //mkdir($structure, 0777, true);
             file_put_contents($structure2, $this->filecontentfix($file2->generate()));
             echo "create [ ", $structure2, " ] complete <br/>";
         }
         echo "===================================================<br/>";
     }
     // Render the generated file
     //echo $file;
 }
Example #4
0
 /**
  * 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);
 }
 public function table_file()
 {
     $cs = $this->create_sql();
     $create_method = new Zend_CodeGenerator_Php_Method(array('name' => 'create_table', 'visibility' => 'public', 'static' => TRUE, 'body' => "\$this->getInstance()->getAdapter()->query(\"{$cs}\");"));
     $create_method->setStatic(TRUE);
     $init_method = new Zend_CodeGenerator_Php_Method(array('name' => 'init', 'visibility' => 'protected', 'body' => '     $create_method->setStatic(TRUE);'));
     $id_prop = new Zend_CodeGenerator_Php_Property(array('name' => '_id_field', 'defaultValue' => $this->get_id_field(), 'visibility' => 'protected'));
     $name_prop = new Zend_CodeGenerator_Php_Property(array('name' => '_name', 'defaultValue' => $this->get_table_name(), 'visibility' => 'protected'));
     $class = new Zend_CodeGenerator_Php_Class(array('name' => $this->get_table_class_name(), 'extendedClass' => 'Zupal_Table_Abstract', 'methods' => array($create_method), 'properties' => array($id_prop, $name_prop)));
     if ($this->get_database_name()) {
         $const = new Zend_CodeGenerator_Php_Method(array('name' => '__construct', 'body' => '        parent::__construct(array("db" => Zupal_Module_Manager::getInstance()->database("' . $this->get_database_name() . '")));'));
         $class->setMethod($const);
     }
     $file = new Zend_CodeGenerator_Php_File(array('classes' => array($class)));
     return $file;
 }