cvs: pear /DB_DataObject/DataObject Generator.php
| From: | Alan Knowles | Date: | Thu, 30 Mar 2006 03:12:52 +0000 |
| Subject: | cvs: pear /DB_DataObject/DataObject Generator.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-39402@lists.php.net to get a copy of this message | ||
alan_k Thu Mar 30 03:12:52 2006 UTC
Modified files:
/pear/DB_DataObject/DataObject Generator.php
Log:
add methods getClassNameFromTableName(), getFileNameFromTableName(),
getMethodNameFromColumnName(), which allow you to set up your own mapping system by overriding them
in your extended generator.. it however needs some support in dataobjects so currently flagged as
EXPERIMENTAL
http://cvs.php.net/viewcvs.cgi/pear/DB_DataObject/DataObject/Generator.php?r1=1.113&r2=1.114&diff_format=u
Index: pear/DB_DataObject/DataObject/Generator.php
diff -u pear/DB_DataObject/DataObject/Generator.php:1.113
pear/DB_DataObject/DataObject/Generator.php:1.114
--- pear/DB_DataObject/DataObject/Generator.php:1.113 Fri Mar 3 07:03:25 2006
+++ pear/DB_DataObject/DataObject/Generator.php Thu Mar 30 03:12:52 2006
@@ -15,7 +15,7 @@
* @author Alan Knowles <alan@akbkhome.com>
* @copyright 1997-2006 The PHP Group
* @license http://www.php.net/license/3_0.txt
PHP License 3.0
- * @version CVS: $Id: Generator.php,v 1.113 2006/03/03 07:03:25 alan_k Exp $
+ * @version CVS: $Id: Generator.php,v 1.114 2006/03/30 03:12:52 alan_k Exp $
* @link http://pear.php.net/package/DB_DataObject
*/
@@ -549,45 +549,95 @@
}
- /*
- * building the class files
- * for each of the tables output a file!
- */
- function generateClasses()
+ /**
+ * Convert a table name into a class name -> override this if you want a different mapping
+ *
+ * @access public
+ * @return string class name;
+ */
+
+
+ function getClassNameFromTableName($table)
+ {
+ $options = &PEAR::getStaticProperty('DB_DataObject','options');
+ $class_prefix = $options['class_prefix'];
+ return
$class_prefix.preg_replace('/[^A-Z0-9]/i','_',ucfirst(trim($this->table)));
+ }
+
+
+ /**
+ * Convert a table name into a file name -> override this if you want a different mapping
+ *
+ * @access public
+ * @return string file name;
+ */
+
+
+ function getFileNameFromTableName($table)
{
- //echo "Generating Class files: \n";
$options = &PEAR::getStaticProperty('DB_DataObject','options');
$base = $options['class_location'];
if (strpos($base,'%s') !== false) {
$base = dirname($base);
}
-
-
if (!file_exists($base)) {
require_once 'System.php';
System::mkdir(array('-p',$base));
}
- $class_prefix = $options['class_prefix'];
+ if (strpos($options['class_location'],'%s') !== false) {
+ $outfilename = sprintf($options['class_location'],
+ preg_replace('/[^A-Z0-9]/i','_',ucfirst($this->table)));
+ } else {
+ $outfilename =
"{$base}/".preg_replace('/[^A-Z0-9]/i','_',ucfirst($this->table)).".php";
+ }
+ return $outfilename;
+
+ }
+
+
+ /**
+ * Convert a column name into a method name (usually prefixed by get/set/validateXXXXX)
+ *
+ * @access public
+ * @return string method name;
+ */
+
+
+ function getMethodNameFromColumnName($col)
+ {
+ return ucfirst($col);
+ }
+
+
+
+
+ /*
+ * building the class files
+ * for each of the tables output a file!
+ */
+ function generateClasses()
+ {
+ //echo "Generating Class files: \n";
+ $options = &PEAR::getStaticProperty('DB_DataObject','options');
+
+
if ($extends = @$options['extends']) {
$this->_extends = $extends;
$this->_extendsFile = $options['extends_location'];
}
foreach($this->tables as $this->table) {
- $this->table = trim($this->table);
- $this->classname =
$class_prefix.preg_replace('/[^A-Z0-9]/i','_',ucfirst($this->table));
+ $this->table = trim($this->table);
+ $this->classname = $this->getClassNameFromTableName($this->table);
$i = '';
+ $outfilename = $this->getFileNameFromTableName($this->table);
- if (strpos($options['class_location'],'%s') !== false) {
- $outfilename = sprintf($options['class_location'],
preg_replace('/[^A-Z0-9]/i','_',ucfirst($this->table)));
- } else {
- $outfilename =
"{$base}/".preg_replace('/[^A-Z0-9]/i','_',ucfirst($this->table)).".php";
- }
$oldcontents = '';
if (file_exists($outfilename)) {
// file_get_contents???
$oldcontents = implode('',file($outfilename));
}
+
$out = $this->_generateClassTable($oldcontents);
$this->debug( "writing $this->classname\n");
$fh = fopen($outfilename, "w");
@@ -751,7 +801,7 @@
if (!strlen(trim($t->name))) {
continue;
}
- $validate_fname = 'validate' . ucfirst(strtolower($t->name));
+ $validate_fname = 'validate' .
$this->getMethodNameFromColumnName($t->name);
// dont re-add it..
if (preg_match('/\s+function\s+' . $validate_fname . '\s*\(/i',
$input)) {
continue;
@@ -862,11 +912,8 @@
$this->_extends = $extends;
$this->_extendsFile = $options['extends_location'];
}
+ $classname = $this->classname = $this->getClassNameFromTableName($this->table);
-
-
- $classname = $this->classname =
$class_prefix.preg_replace('/[^A-Z0-9]/i','_',ucfirst(trim($this->table)));
-
$out = $this->_generateClassTable();
//echo $out;
eval('?>'.$out);
@@ -958,7 +1005,7 @@
foreach ($defs = $defs as $t) {
// build mehtod name
- $methodName = 'get' . ucfirst($t->name);
+ $methodName = 'get' . $this->getMethodNameFromColumnName($t->name);
if (!strlen(trim($t->name)) ||
preg_match("/function[\s]+[&]?$methodName\(/i", $input)) {
continue;
@@ -1011,7 +1058,7 @@
foreach ($defs = $defs as $t) {
// build mehtod name
- $methodName = 'set' . ucfirst($t->name);
+ $methodName = 'set' . $this->getMethodNameFromColumnName($t->name);
if (!strlen(trim($t->name)) ||
preg_match("/function[\s]+[&]?$methodName\(/i", $input)) {
continue;