PageRenderTime 60ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 1ms

/oiclient/data/symfony/plugins/sfPropelPlugin/lib/propel/builder/SfObjectBuilder.php

http://openirudi.googlecode.com/
PHP | 372 lines | 297 code | 53 blank | 22 comment | 31 complexity | 97ebac271d79c48b6aadb068ed77e68e MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0
  1. <?php
  2. require_once 'propel/engine/builder/om/php5/PHP5ComplexObjectBuilder.php';
  3. /*
  4. * This file is part of the symfony package.
  5. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * @package symfony
  12. * @subpackage propel
  13. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  14. * @version SVN: $Id: SfObjectBuilder.php 9534 2008-06-11 08:22:45Z fabien $
  15. */
  16. class SfObjectBuilder extends PHP5ComplexObjectBuilder
  17. {
  18. public function build()
  19. {
  20. $objectCode = parent::build();
  21. if (!DataModelBuilder::getBuildProperty('builderAddComments'))
  22. {
  23. $objectCode = sfToolkit::stripComments($objectCode);
  24. }
  25. if (!DataModelBuilder::getBuildProperty('builderAddIncludes'))
  26. {
  27. //remove all inline includes: object classes include the peers
  28. $objectCode = preg_replace("/include_once\s*.*Base.*Peer\.php.*\s*/", "", $objectCode);
  29. }
  30. return $objectCode;
  31. }
  32. protected function addIncludes(&$script)
  33. {
  34. if (!DataModelBuilder::getBuildProperty('builderAddIncludes'))
  35. {
  36. return;
  37. }
  38. parent::addIncludes($script);
  39. // include the i18n classes if needed
  40. if ($this->getTable()->getAttribute('isI18N'))
  41. {
  42. $relatedTable = $this->getDatabase()->getTable($this->getTable()->getAttribute('i18nTable'));
  43. $script .= '
  44. require_once \''.$this->getFilePath($this->getStubObjectBuilder()->getPackage().'.'.$relatedTable->getPhpName().'Peer').'\';
  45. require_once \''.$this->getFilePath($this->getStubObjectBuilder()->getPackage().'.'.$relatedTable->getPhpName()).'\';
  46. ';
  47. }
  48. }
  49. protected function addClassBody(&$script)
  50. {
  51. parent::addClassBody($script);
  52. if ($this->getTable()->getAttribute('isI18N'))
  53. {
  54. if (count($this->getTable()->getPrimaryKey()) > 1)
  55. {
  56. throw new Exception('i18n support only works with a single primary key');
  57. }
  58. $this->addCultureAccessorMethod($script);
  59. $this->addCultureMutatorMethod($script);
  60. $this->addI18nMethods($script);
  61. }
  62. if (DataModelBuilder::getBuildProperty('builderAddBehaviors'))
  63. {
  64. $this->addCall($script);
  65. }
  66. }
  67. protected function addCall(&$script)
  68. {
  69. $script .= "
  70. public function __call(\$method, \$arguments)
  71. {
  72. if (!\$callable = sfMixer::getCallable('{$this->getClassname()}:'.\$method))
  73. {
  74. throw new sfException(sprintf('Call to undefined method {$this->getClassname()}::%s', \$method));
  75. }
  76. array_unshift(\$arguments, \$this);
  77. return call_user_func_array(\$callable, \$arguments);
  78. }
  79. ";
  80. }
  81. protected function addAttributes(&$script)
  82. {
  83. parent::addAttributes($script);
  84. if ($this->getTable()->getAttribute('isI18N'))
  85. {
  86. $script .= '
  87. /**
  88. * The value for the culture field.
  89. * @var string
  90. */
  91. protected $culture;
  92. ';
  93. }
  94. }
  95. protected function addCultureAccessorMethod(&$script)
  96. {
  97. $script .= '
  98. public function getCulture()
  99. {
  100. return $this->culture;
  101. }
  102. ';
  103. }
  104. protected function addCultureMutatorMethod(&$script)
  105. {
  106. $script .= '
  107. public function setCulture($culture)
  108. {
  109. $this->culture = $culture;
  110. }
  111. ';
  112. }
  113. protected function addI18nMethods(&$script)
  114. {
  115. $table = $this->getTable();
  116. $pks = $table->getPrimaryKey();
  117. $pk = $pks[0]->getPhpName();
  118. foreach ($table->getReferrers() as $fk)
  119. {
  120. $tblFK = $fk->getTable();
  121. if ($tblFK->getName() == $table->getAttribute('i18nTable'))
  122. {
  123. $className = $tblFK->getPhpName();
  124. $culture = '';
  125. $culture_peername = '';
  126. foreach ($tblFK->getColumns() as $col)
  127. {
  128. if (("true" === strtolower($col->getAttribute('isCulture'))))
  129. {
  130. $culture = $col->getPhpName();
  131. $culture_peername = PeerBuilder::getColumnName($col, $className);
  132. }
  133. }
  134. foreach ($tblFK->getColumns() as $col)
  135. {
  136. if ($col->isPrimaryKey()) continue;
  137. $script .= '
  138. public function get'.$col->getPhpName().'($culture = null)
  139. {
  140. return $this->getCurrent'.$className.'($culture)->get'.$col->getPhpName().'();
  141. }
  142. public function set'.$col->getPhpName().'($value, $culture = null)
  143. {
  144. $this->getCurrent'.$className.'($culture)->set'.$col->getPhpName().'($value);
  145. }
  146. ';
  147. }
  148. $script .= '
  149. protected $current_i18n = array();
  150. public function getCurrent'.$className.'($culture = null)
  151. {
  152. if (is_null($culture))
  153. {
  154. $culture = is_null($this->culture) ? sfPropel::getDefaultCulture() : $this->culture;
  155. }
  156. if (!isset($this->current_i18n[$culture]))
  157. {
  158. $obj = '.$className.'Peer::retrieveByPK($this->get'.$pk.'(), $culture);
  159. if ($obj)
  160. {
  161. $this->set'.$className.'ForCulture($obj, $culture);
  162. }
  163. else
  164. {
  165. $this->set'.$className.'ForCulture(new '.$className.'(), $culture);
  166. $this->current_i18n[$culture]->set'.$culture.'($culture);
  167. }
  168. }
  169. return $this->current_i18n[$culture];
  170. }
  171. public function set'.$className.'ForCulture($object, $culture)
  172. {
  173. $this->current_i18n[$culture] = $object;
  174. $this->add'.$className.'($object);
  175. }
  176. ';
  177. }
  178. }
  179. }
  180. protected function addDoSave(&$script)
  181. {
  182. $tmp = '';
  183. parent::addDoSave($tmp);
  184. // add autosave to i18n object even if the base object is not changed
  185. $tmp = preg_replace_callback('#(\$this\->(.+?)\->isModified\(\))#', array($this, 'i18nDoSaveCallback'), $tmp);
  186. $script .= $tmp;
  187. }
  188. private function i18nDoSaveCallback($matches)
  189. {
  190. $value = $matches[1];
  191. // get the related class to see if it is a i18n one
  192. $table = $this->getTable();
  193. $column = null;
  194. foreach ($table->getForeignKeys() as $fk)
  195. {
  196. if ($matches[2] == $this->getFKVarName($fk))
  197. {
  198. $column = $fk;
  199. break;
  200. }
  201. }
  202. $foreign_table = $this->getDatabase()->getTable($fk->getForeignTableName());
  203. if ($foreign_table->getAttribute('isI18N'))
  204. {
  205. $foreign_tables_i18n_table = $this->getDatabase()->getTable($foreign_table->getAttribute('i18nTable'));
  206. $value .= ' || ($this->'.$matches[2].'->getCulture() && $this->'.$matches[2].'->getCurrent'.$foreign_tables_i18n_table->getPhpName().'()->isModified())';
  207. }
  208. return $value;
  209. }
  210. protected function addDelete(&$script)
  211. {
  212. $tmp = '';
  213. parent::addDelete($tmp);
  214. if (DataModelBuilder::getBuildProperty('builderAddBehaviors'))
  215. {
  216. // add sfMixer call
  217. $pre_mixer_script = "
  218. foreach (sfMixer::getCallables('{$this->getClassname()}:delete:pre') as \$callable)
  219. {
  220. \$ret = call_user_func(\$callable, \$this, \$con);
  221. if (\$ret)
  222. {
  223. return;
  224. }
  225. }
  226. ";
  227. $post_mixer_script = "
  228. foreach (sfMixer::getCallables('{$this->getClassname()}:delete:post') as \$callable)
  229. {
  230. call_user_func(\$callable, \$this, \$con);
  231. }
  232. ";
  233. $tmp = preg_replace('/{/', '{'.$pre_mixer_script, $tmp, 1);
  234. $tmp = preg_replace('/}\s*$/', $post_mixer_script.' }', $tmp);
  235. }
  236. // update current script
  237. $script .= $tmp;
  238. }
  239. protected function addSave(&$script)
  240. {
  241. $tmp = '';
  242. parent::addSave($tmp);
  243. // add support for created_(at|on) and updated_(at|on) columns
  244. $date_script = '';
  245. $updated = false;
  246. $created = false;
  247. foreach ($this->getTable()->getColumns() as $col)
  248. {
  249. $clo = strtolower($col->getName());
  250. if (!$updated && in_array($clo, array('updated_at', 'updated_on')))
  251. {
  252. $updated = true;
  253. $date_script .= "
  254. if (\$this->isModified() && !\$this->isColumnModified(".$this->getColumnConstant($col)."))
  255. {
  256. \$this->set".$col->getPhpName()."(time());
  257. }
  258. ";
  259. }
  260. else if (!$created && in_array($clo, array('created_at', 'created_on')))
  261. {
  262. $created = true;
  263. $date_script .= "
  264. if (\$this->isNew() && !\$this->isColumnModified(".$this->getColumnConstant($col)."))
  265. {
  266. \$this->set".$col->getPhpName()."(time());
  267. }
  268. ";
  269. }
  270. }
  271. $tmp = preg_replace('/{/', '{'.$date_script, $tmp, 1);
  272. if (DataModelBuilder::getBuildProperty('builderAddBehaviors'))
  273. {
  274. // add sfMixer call
  275. $pre_mixer_script = "
  276. foreach (sfMixer::getCallables('{$this->getClassname()}:save:pre') as \$callable)
  277. {
  278. \$affectedRows = call_user_func(\$callable, \$this, \$con);
  279. if (is_int(\$affectedRows))
  280. {
  281. return \$affectedRows;
  282. }
  283. }
  284. ";
  285. $post_mixer_script = <<<EOF
  286. foreach (sfMixer::getCallables('{$this->getClassname()}:save:post') as \$callable)
  287. {
  288. call_user_func(\$callable, \$this, \$con, \$affectedRows);
  289. }
  290. EOF;
  291. $tmp = preg_replace('/{/', '{'.$pre_mixer_script, $tmp, 1);
  292. $tmp = preg_replace('/(\$con\->commit\(\);)/', '$1'.$post_mixer_script, $tmp);
  293. }
  294. // update current script
  295. $script .= $tmp;
  296. }
  297. protected function addClassClose(&$script)
  298. {
  299. parent::addClassClose($script);
  300. $behaviors = $this->getTable()->getAttribute('behaviors');
  301. if ($behaviors)
  302. {
  303. $behavior_file_name = 'Base'.$this->getTable()->getPhpName().'Behaviors';
  304. $behavior_file_path = $this->getFilePath($this->getStubObjectBuilder()->getPackage().'.om.'.$behavior_file_name);
  305. $behavior_include_script = <<<EOF
  306. if (ProjectConfiguration::getActive() instanceof sfApplicationConfiguration)
  307. {
  308. include_once '%s';
  309. }
  310. EOF;
  311. $script .= sprintf($behavior_include_script, $behavior_file_path);
  312. }
  313. }
  314. }