PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/gulliver/thirdparty/propel-generator/classes/propel/engine/builder/om/php5/PHP5MapBuilderBuilder.php

https://bitbucket.org/ferOnti/processmaker
PHP | 301 lines | 156 code | 27 blank | 118 comment | 19 complexity | f5bf2a68375ca8bcd66384ce6c5d14b4 MD5 | raw file
  1. <?php
  2. /*
  3. * $Id: PHP5MapBuilderBuilder.php 562 2007-02-01 02:17:24Z hans $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://propel.phpdb.org>.
  20. */
  21. require_once 'propel/engine/builder/om/OMBuilder.php';
  22. /**
  23. * Generates the PHP5 map builder class for user object model (OM).
  24. *
  25. * This class replaces the MapBuilder.tpl, with the intent of being easier for users
  26. * to customize (through extending & overriding).
  27. *
  28. * @author Hans Lellelid <hans@xmpl.org>
  29. * @package propel.engine.builder.om.php5
  30. */
  31. class PHP5MapBuilderBuilder extends OMBuilder {
  32. /**
  33. * Gets the package for the map builder classes.
  34. * @return string
  35. */
  36. public function getPackage()
  37. {
  38. return parent::getPackage() . '.map';
  39. }
  40. /**
  41. * Returns the name of the current class being built.
  42. * @return string
  43. */
  44. public function getClassname()
  45. {
  46. return $this->getTable()->getPhpName() . 'MapBuilder';
  47. }
  48. /**
  49. * Adds the include() statements for files that this class depends on or utilizes.
  50. * @param string &$script The script will be modified in this method.
  51. */
  52. protected function addIncludes(&$script)
  53. {
  54. $script .= "
  55. require_once 'propel/map/MapBuilder.php';
  56. include_once 'creole/CreoleTypes.php';
  57. ";
  58. } // addIncludes()
  59. /**
  60. * Adds class phpdoc comment and openning of class.
  61. * @param string &$script The script will be modified in this method.
  62. */
  63. protected function addClassOpen(&$script)
  64. {
  65. $table = $this->getTable();
  66. $script .= "
  67. /**
  68. * This class adds structure of '".$table->getName()."' table to '".$table->getDatabase()->getName()."' DatabaseMap object.
  69. *
  70. *";
  71. if ($this->getBuildProperty('addTimeStamp')) {
  72. $now = strftime('%c');
  73. $script .= "
  74. * This class was autogenerated by Propel on:
  75. *
  76. * $now
  77. *";
  78. }
  79. $script .= "
  80. *
  81. * These statically-built map classes are used by Propel to do runtime db structure discovery.
  82. * For example, the createSelectSql() method checks the type of a given column used in an
  83. * ORDER BY clause to know whether it needs to apply SQL to make the ORDER BY case-insensitive
  84. * (i.e. if it's a text column type).
  85. *
  86. * @package workflow.".$this->getPackage()."
  87. */
  88. class ".$this->getClassname()."
  89. {
  90. ";
  91. }
  92. /**
  93. * Specifies the methods that are added as part of the map builder class.
  94. * This can be overridden by subclasses that wish to add more methods.
  95. * @see ObjectBuilder::addClassBody()
  96. */
  97. protected function addClassBody(&$script)
  98. {
  99. $this->addConstants($script);
  100. $this->addAttributes($script);
  101. $this->addIsBuilt($script);
  102. $this->addGetDatabaseMap($script);
  103. $this->addDoBuild($script);
  104. }
  105. /**
  106. * Adds any constants needed for this MapBuilder class.
  107. * @param string &$script The script will be modified in this method.
  108. */
  109. protected function addConstants(&$script)
  110. {
  111. $script .= "
  112. /**
  113. * The (dot-path) name of this class
  114. */
  115. const CLASS_NAME = '".$this->getClasspath()."';
  116. ";
  117. }
  118. /**
  119. * Adds any attributes needed for this MapBuilder class.
  120. * @param string &$script The script will be modified in this method.
  121. */
  122. protected function addAttributes(&$script)
  123. {
  124. $script .= "
  125. /**
  126. * The database map.
  127. */
  128. private \$dbMap;
  129. ";
  130. }
  131. /**
  132. * Closes class.
  133. * @param string &$script The script will be modified in this method.
  134. */
  135. protected function addClassClose(&$script)
  136. {
  137. $script .= "
  138. } // " . $this->getClassname() . "
  139. ";
  140. }
  141. /**
  142. * Adds the method that indicates whether this map has already been built.
  143. * @param string &$script The script will be modified in this method.
  144. */
  145. protected function addIsBuilt(&$script)
  146. {
  147. $script .= "
  148. /**
  149. * Tells us if this DatabaseMapBuilder is built so that we
  150. * don't have to re-build it every time.
  151. *
  152. * @return boolean true if this DatabaseMapBuilder is built, false otherwise.
  153. */
  154. public function isBuilt()
  155. {
  156. return (\$this->dbMap !== null);
  157. }
  158. ";
  159. }
  160. /**
  161. * Adds the DatabaseMap accessor method.
  162. * @param string &$script The script will be modified in this method.
  163. */
  164. protected function addGetDatabaseMap(&$script)
  165. {
  166. $script .= "
  167. /**
  168. * Gets the databasemap this map builder built.
  169. *
  170. * @return the databasemap
  171. */
  172. public function getDatabaseMap()
  173. {
  174. return \$this->dbMap;
  175. }
  176. ";
  177. }
  178. /**
  179. * Adds the main doBuild() method to the map builder class.
  180. * @param string &$script The script will be modified in this method.
  181. */
  182. protected function addDoBuild(&$script)
  183. {
  184. $table = $this->getTable();
  185. $platform = $this->getPlatform();
  186. $script .= "
  187. /**
  188. * The doBuild() method builds the DatabaseMap
  189. *
  190. * @return void
  191. * @throws PropelException
  192. */
  193. public function doBuild()
  194. {
  195. \$this->dbMap = Propel::getDatabaseMap('".$table->getDatabase()->getName()."');
  196. \$tMap = \$this->dbMap->addTable('".$table->getName()."');
  197. \$tMap->setPhpName('".$table->getPhpName()."');
  198. ";
  199. if ($table->getIdMethod() == "native") {
  200. $script .= "
  201. \$tMap->setUseIdGenerator(true);
  202. ";
  203. } else {
  204. $script .= "
  205. \$tMap->setUseIdGenerator(false);
  206. ";
  207. }
  208. if ($table->getIdMethodParameters()) {
  209. $params = $table->getIdMethodParameters();
  210. $imp = $params[0];
  211. $script .= "
  212. \$tMap->setPrimaryKeyMethodInfo('".$imp->getValue()."');
  213. ";
  214. } elseif ($table->getIdMethod() == "native" && ($platform->getNativeIdMethod() == Platform::SEQUENCE)) {
  215. $script .= "
  216. \$tMap->setPrimaryKeyMethodInfo('".$table->getSequenceName()."');
  217. ";
  218. } elseif ($table->getIdMethod() == "native" && ($platform->getNativeIdMethod() == Platform::SEQUENCE)) {
  219. $script .= "
  220. \$tMap->setPrimaryKeyMethodInfo('".$table->getName()."');
  221. ";
  222. }
  223. // Add columns to map
  224. foreach ($table->getColumns() as $col) {
  225. $tfc=$table->getPhpName();
  226. $cfc=$col->getPhpName();
  227. $cup=strtoupper($col->getName());
  228. if (!$col->getSize()) {
  229. $size = "null";
  230. } else {
  231. $size = $col->getSize();
  232. }
  233. if($col->isPrimaryKey()) {
  234. if($col->isForeignKey()) {
  235. $script .= "
  236. \$tMap->addForeignPrimaryKey('$cup', '$cfc', '".$col->getPhpType()."' , CreoleTypes::".$col->getType().", '".$col->getRelatedTableName()."', '".strtoupper($col->getRelatedColumnName())."', ".($col->isNotNull() ? 'true' : 'false').", ".$size.");
  237. ";
  238. } else {
  239. $script .= "
  240. \$tMap->addPrimaryKey('$cup', '$cfc', '".$col->getPhpType()."', CreoleTypes::".$col->getType().", ".var_export($col->isNotNull(), true).", ".$size.");
  241. ";
  242. }
  243. } else {
  244. if($col->isForeignKey()) {
  245. $script .= "
  246. \$tMap->addForeignKey('$cup', '$cfc', '".$col->getPhpType()."', CreoleTypes::".$col->getType().", '".$col->getRelatedTableName()."', '".strtoupper($col->getRelatedColumnName())."', ".($col->isNotNull() ? 'true' : 'false').", ".$size.");
  247. ";
  248. } else {
  249. $script .= "
  250. \$tMap->addColumn('$cup', '$cfc', '".$col->getPhpType()."', CreoleTypes::".$col->getType().", ".var_export($col->isNotNull(), true).", ".$size.");
  251. ";
  252. }
  253. } // if col-is prim key
  254. } // foreach
  255. foreach($table->getValidators() as $val) {
  256. $col = $val->getColumn();
  257. $cup = strtoupper($col->getName());
  258. foreach($val->getRules() as $rule) {
  259. if ($val->getTranslate() !== Validator::TRANSLATE_NONE) {
  260. $script .= "
  261. \$tMap->addValidator('$cup', '".$rule->getName()."', '".$rule->getClass()."', '".$rule->getValue()."', ".$val->getTranslate()."('".str_replace("'", "\'", $rule->getMessage())."'));
  262. ";
  263. } else {
  264. $script .= "
  265. \$tMap->addValidator('$cup', '".$rule->getName()."', '".$rule->getClass()."', '".$rule->getValue()."', '".str_replace("'", "\'", $rule->getMessage())."');
  266. ";
  267. } // if ($rule->getTranslation() ...
  268. } // foreach rule
  269. } // foreach validator
  270. $script .= "
  271. } // doBuild()
  272. ";
  273. }
  274. } // PHP5ExtensionPeerBuilder