Created
March 22, 2011 09:24
-
-
Save kristm/880978 to your computer and use it in GitHub Desktop.
php script for generating zend framework models. http://blog.dottystylecreative.com/metaprogramming-in-php/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/Applications/MAMP/bin/php5.2/bin/php | |
<?php | |
class dzt{ | |
public $host = 'localhost'; | |
public $user = 'dbuser'; | |
public $pw = 'dbpassword'; | |
public $db = 'dbname'; | |
public function main($params){ | |
if(count($params) < 3) die('Usage: dzt.php <path> <table> [type]'.PHP_EOL); | |
$path = $params[1]; | |
$data = $params[2]; | |
$method = 'make'.(isset($params[3]) ? ucfirst($params[3]) : 'Model'); | |
if(!method_exists($this,$method)) | |
die('Invalid parameter'.PHP_EOL); | |
dzt::$method($path,$data); | |
} | |
public function makeModel($path,$table){ | |
$con = dzt::connect(); | |
$data = dzt::describe($table); | |
dzt::close($con); | |
$is_new = (file_exists($path)) ? false : true; | |
if(!$is_new){ | |
$fp = fopen($path,'r'); | |
while(!feof($fp)){ | |
$head .= fgets($fp); | |
if(preg_match('/\{/',$head,$matches) >= 1) | |
break; | |
} | |
fclose($fp); | |
}else{ | |
flock($fp,LOCK_EX); | |
$head = $this->classHead($table,'model'); | |
} | |
$fp = fopen($path,'w'); | |
if($fp){ | |
flock($fp,LOCK_EX); | |
fwrite($fp,$head); | |
foreach($data as $type=>$value) | |
fwrite($fp,$this->generateProps($value)); | |
fwrite($fp,PHP_EOL.PHP_EOL); | |
foreach($data as $type=>$value) | |
fwrite($fp,$this->generateMethods($value,$type)); | |
// close off class definition | |
fwrite($fp,'}'.PHP_EOL); | |
flock($fp,LOCK_UN); | |
echo "model created".PHP_EOL; | |
return true; | |
} | |
fclose($fp); | |
} | |
public function makeController($path,$controller){ | |
#$is_new = (file_exists($path)) ? false : true; | |
$fp = fopen($path,'w+'); | |
#if($is_new){ | |
flock($fp,LOCK_EX); | |
fwrite($fp,$this->classHead($controller,'controller')); | |
fwrite($fp,$this->classDef('init' )); | |
#fwrite($fp,$this->classDef('__call', '$method, $args','protected')); | |
fwrite($fp,$this->classDef('indexAction')); | |
fwrite($fp,'}'.PHP_EOL); | |
flock($fp,LOCK_UN); | |
#} | |
fclose($fp); | |
echo "controller created".PHP_EOL; | |
return true; | |
} | |
public function connect(){ | |
$con = mysql_connect($this->host,$this->user,$this->pw); | |
if(!$con){ | |
die('could not connect: '.mysql_error()); | |
} | |
return $con; | |
} | |
public function close($con){ | |
echo 'closing db '.PHP_EOL; | |
mysql_close($con); | |
} | |
public function describe($table){ | |
$db_ok = mysql_select_db($this->db); | |
if(!$db_ok) die('cant use db : '.$table.PHP_EOL); | |
$users_info = 'describe '.$table; | |
$describe = mysql_query($users_info); | |
$table_info = array(); | |
if(!$describe) die('invalid query'.mysql_error().PHP_EOL); | |
$i=0; | |
while($row=mysql_fetch_array($describe)){ | |
extract($row); | |
$colname = preg_match('/[a-zA-Z]*([\_][a-zA-Z]*)?/',$row[0],$matches); | |
$table_info[substr($row['Type'],0,3).$i++] = strtolower($matches[0]); | |
} | |
return $table_info; | |
} | |
protected function generateProps($propname){ | |
return sprintf("\tprotected \$_%s;\n",$propname); | |
} | |
protected function generateMethods($value,$type){ | |
$method = $this->getter($value); | |
$method .= $this->setter($value,substr($type,0,3)); | |
return $method; | |
} | |
protected function getter($value){ | |
$method = sprintf("\tpublic function get%s()\n\t{\n",ucwords($value)); | |
$method .= sprintf("\t\treturn \$this->_%s;\n\t}\n\n",$value); | |
return $method; | |
} | |
protected function setter($value,$type){ | |
$method = sprintf("\tpublic function set%s(\$%s)\n\t{\n",ucwords($value),$value); | |
switch($type){ | |
case 'int': | |
$method .= sprintf("\t\t\$this->_%s = (int) \$%s;\n",$value,$value); | |
break; | |
case 'dec': | |
$method .= sprintf("\t\t\$this->_%s = (float) \$%s;\n",$value,$value); | |
break; | |
case 'var': | |
$method .= sprintf("\t\t\$this->_%s = (string) \$%s;\n",$value,$value); | |
break; | |
case 'dat': | |
$method .= sprintf("\t\t\$this->_%s = (string) \$%s;\n",$value,$value); | |
break; | |
case 'tin': | |
$method .= sprintf("\t\t\$this->_%s = (bool) \$%s;\n",$value,$value); | |
break; | |
default: | |
break; | |
} | |
$method .= sprintf("\t\treturn \$this;\n"); | |
$method .= sprintf("\t}\n\n"); | |
return $method; | |
} | |
protected function classHead($name,$type='model'){ | |
switch($type){ | |
case 'controller': | |
return sprintf("<?php\nclass %sController extends Zend_Controller_Action{\n",ucfirst(strtolower($name))); | |
break; | |
case 'model': | |
default: | |
return sprintf("<?php\nclass %s{\n",ucfirst(strtolower($name))); | |
break; | |
} | |
} | |
protected function classDef($method,$params,$access='public'){ | |
$def = sprintf("\n\t%s function %s",$access,$method); | |
$def .= sprintf("("); | |
$def .= (isset($params)) ? $params : ""; | |
$def .= sprintf(")\n\t{\n"); | |
$def .= sprintf("\t}\n"); | |
return $def; | |
} | |
} | |
$rs = new dzt(); | |
$rs->main($argv); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment