[ Index ] |
|
Code source de Symfony 1.0.0 |
1 <?php 2 3 /* 4 * $Id: DataModelBuilder.php 351 2006-03-15 18:42:34Z hans $ 5 * 6 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 7 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 8 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 9 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 10 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 11 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 12 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 13 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 14 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 15 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 16 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 17 * 18 * This software consists of voluntary contributions made by many individuals 19 * and is licensed under the LGPL. For more information please see 20 * <http://propel.phpdb.org>. 21 */ 22 23 24 /** 25 * This is the base class for any builder class that is using the data model. 26 * 27 * This could be extended by classes that build SQL DDL, PHP classes, configuration 28 * files, input forms, etc. 29 * 30 * This class has a static method to return the correct builder subclass identified by 31 * a given key. Note that in order for this factory method to work, the properties have to have 32 * been loaded first. Usage should look something like this (from within a AbstractProelDataModelTask subclass): 33 * 34 * <code> 35 * DataModelBuilder::setBuildProperties($this->getPropelProperties()); 36 * $builder = DataModelBuilder::builderFactory($table, 'peer'); 37 * // $builder (by default) instanceof PHP5ComplexPeerBuilder 38 * </code> 39 * 40 * @author Hans Lellelid <hans@xmpl.org> 41 * @package propel.engine.builder 42 */ 43 abstract class DataModelBuilder { 44 45 // -------------------------------------------------------------- 46 // Static properties & methods 47 // -------------------------------------------------------------- 48 49 /** 50 * Build properties (after they've been transformed from "propel.some.name" => "someName"). 51 * @var array string[] 52 */ 53 private static $buildProperties = array(); 54 55 /** 56 * Sets the [name transformed] build properties to use. 57 * @param array Property values keyed by [transformed] prop names. 58 */ 59 public static function setBuildProperties($props) 60 { 61 self::$buildProperties = $props; 62 } 63 64 /** 65 * Get a specific [name transformed] build property. 66 * @param string $name 67 * @return string 68 */ 69 public static function getBuildProperty($name) 70 { 71 return isset(self::$buildProperties[$name]) ? self::$buildProperties[$name] : null; 72 } 73 74 /** 75 * Imports and returns the classname of the builder class for specified 'type'. 76 * @param $type The "key" for class to load. 77 * @return string The unqualified classname. 78 */ 79 public static function getBuilderClass($type) 80 { 81 if (empty(self::$buildProperties)) { 82 throw new BuildException("Cannot determine builder class when no build properties have been loaded (hint: Did you call DataModelBuilder::setBuildProperties(\$props) first?)"); 83 } 84 $propname = 'builder' . ucfirst(strtolower($type)) . 'Class'; 85 $classpath = self::getBuildProperty($propname); 86 87 if (empty($classpath)) { 88 throw new BuildException("Unable to find class path for '$propname' property."); 89 } 90 91 // This is a slight hack to workaround camel case inconsistencies for the DDL classes. 92 // Basically, we want to turn ?.?.?.sqliteDDLBuilder into ?.?.?.SqliteDDLBuilder 93 $lastdotpos = strrpos($classpath, '.'); 94 if ($lastdotpos) $classpath{$lastdotpos+1} = strtoupper($classpath{$lastdotpos+1}); 95 else ucfirst($classpath); 96 97 return Phing::import($classpath); 98 } 99 100 /** 101 * Factory method to load a new builder instance based on specified type. 102 * @param Table $table 103 * @param $type The "key" for class to load. 104 * @throws BuildException if specified class cannot be found / loaded. 105 */ 106 public static function builderFactory(Table $table, $type) 107 { 108 $classname = self::getBuilderClass($type); 109 return new $classname($table); 110 } 111 112 /** 113 * Utility function to build a path for use in include()/require() statement. 114 * 115 * Supports two function signatures: 116 * (1) getFilePath($dotPathClass); 117 * (2) getFilePath($dotPathPrefix, $className); 118 * 119 * @param string $path dot-path to class or to package prefix. 120 * @param string $classname class name 121 * @return string 122 */ 123 public static function getFilePath($path, $classname = null, $extension = '.php') 124 { 125 $path = strtr(ltrim($path, '.'), '.', '/'); 126 if ($classname !== null) { 127 if ($path !== "") { $path .= '/'; } 128 return $path . $classname . $extension; 129 } else { 130 return $path . $extension; 131 } 132 } 133 134 // -------------------------------------------------------------- 135 // Non-static properties & methods inherited by subclasses 136 // -------------------------------------------------------------- 137 138 /** 139 * The current table. 140 * @var Table 141 */ 142 private $table; 143 144 /** 145 * An array of warning messages that can be retrieved for display (e.g. as part of phing build process). 146 * @var array string[] 147 */ 148 private $warnings = array(); 149 150 /** 151 * Creates new instance of DataModelBuilder subclass. 152 * @param Table $table The Table which we are using to build [OM, DDL, etc.]. 153 */ 154 public function __construct(Table $table) 155 { 156 $this->table = $table; 157 } 158 159 /** 160 * Returns the Platform class for this table (database). 161 * @return Platform 162 */ 163 protected function getPlatform() 164 { 165 return $this->getTable()->getDatabase()->getPlatform(); 166 } 167 168 /** 169 * Returns the database for current table. 170 * @return Database 171 */ 172 protected function getDatabase() 173 { 174 return $this->getTable()->getDatabase(); 175 } 176 177 /** 178 * Returns the current Table object. 179 * @return Table 180 */ 181 protected function getTable() 182 { 183 return $this->table; 184 } 185 186 /** 187 * Pushes a message onto the stack of warnings. 188 * @param string $msg The warning message. 189 */ 190 protected function warn($msg) 191 { 192 $this->warnings[] = $msg; 193 } 194 195 /** 196 * Gets array of warning messages. 197 * @return array string[] 198 */ 199 public function getWarnings() 200 { 201 return $this->warnings; 202 } 203 204 /** 205 * Wraps call to Platform->quoteIdentifier() with a check to see whether quoting is enabled. 206 * 207 * All subclasses should call this quoteIdentifier() method rather than calling the Platform 208 * method directly. This method is used by both DataSQLBuilder and DDLBuilder, and potentially 209 * in the OM builders also, which is why it is defined in this class. 210 * 211 * @param string $text The text to quote. 212 * @return string Quoted text. 213 */ 214 public function quoteIdentifier($text) 215 { 216 if (!self::getBuildProperty('disableIdentifierQuoting')) { 217 return $this->getPlatform()->quoteIdentifier($text); 218 } 219 return $text; 220 } 221 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Mar 16 22:42:14 2007 | par Balluche grâce à PHPXref 0.7 |