PageRenderTime 30ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/vendor/symfony/lib/plugins/sfPropelPlugin/lib/builder/SfObjectBuilder.php

https://github.com/Kazuhiro-Murota/OpenPNE3
PHP | 392 lines | 314 code | 56 blank | 22 comment | 31 complexity | d917901ae84d6236d821f2e046477277 MD5 | raw file
  1. <?php
  2. require_once 'propel/engine/builder/om/php5/PHP5ObjectBuilder.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 14378 2008-12-29 20:04:39Z Kris.Wallsmith $
  15. */
  16. class SfObjectBuilder extends PHP5ObjectBuilder
  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 \''.ClassTools::getFilePath($this->getStubObjectBuilder()->getPackage().'.', $relatedTable->getPhpName().'Peer').'\';
  45. require_once \''.ClassTools::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. /**
  99. * Returns the culture.
  100. *
  101. * @return string The culture
  102. */
  103. public function getCulture()
  104. {
  105. return $this->culture;
  106. }
  107. ';
  108. }
  109. protected function addCultureMutatorMethod(&$script)
  110. {
  111. $script .= '
  112. /**
  113. * Sets the culture.
  114. *
  115. * @param string $culture The culture to set
  116. *
  117. * @return void
  118. */
  119. public function setCulture($culture)
  120. {
  121. $this->culture = $culture;
  122. }
  123. ';
  124. }
  125. protected function addI18nMethods(&$script)
  126. {
  127. $table = $this->getTable();
  128. $pks = $table->getPrimaryKey();
  129. $pk = $pks[0]->getPhpName();
  130. foreach ($table->getReferrers() as $fk)
  131. {
  132. $tblFK = $fk->getTable();
  133. if ($tblFK->getName() == $table->getAttribute('i18nTable'))
  134. {
  135. $className = $tblFK->getPhpName();
  136. $culture = '';
  137. $culture_peername = '';
  138. foreach ($tblFK->getColumns() as $col)
  139. {
  140. if (("true" === strtolower($col->getAttribute('isCulture'))))
  141. {
  142. $culture = $col->getPhpName();
  143. $culture_peername = PeerBuilder::getColumnName($col, $className);
  144. }
  145. }
  146. foreach ($tblFK->getColumns() as $col)
  147. {
  148. if ($col->isPrimaryKey()) continue;
  149. $script .= '
  150. public function get'.$col->getPhpName().'($culture = null)
  151. {
  152. return $this->getCurrent'.$className.'($culture)->get'.$col->getPhpName().'();
  153. }
  154. public function set'.$col->getPhpName().'($value, $culture = null)
  155. {
  156. $this->getCurrent'.$className.'($culture)->set'.$col->getPhpName().'($value);
  157. }
  158. ';
  159. }
  160. $script .= '
  161. protected $current_i18n = array();
  162. public function getCurrent'.$className.'($culture = null)
  163. {
  164. if (is_null($culture))
  165. {
  166. $culture = is_null($this->culture) ? sfPropel::getDefaultCulture() : $this->culture;
  167. }
  168. if (!isset($this->current_i18n[$culture]))
  169. {
  170. $obj = '.$className.'Peer::retrieveByPK($this->get'.$pk.'(), $culture);
  171. if ($obj)
  172. {
  173. $this->set'.$className.'ForCulture($obj, $culture);
  174. }
  175. else
  176. {
  177. $this->set'.$className.'ForCulture(new '.$className.'(), $culture);
  178. $this->current_i18n[$culture]->set'.$culture.'($culture);
  179. }
  180. }
  181. return $this->current_i18n[$culture];
  182. }
  183. public function set'.$className.'ForCulture($object, $culture)
  184. {
  185. $this->current_i18n[$culture] = $object;
  186. $this->add'.$className.'($object);
  187. }
  188. ';
  189. }
  190. }
  191. }
  192. protected function addDoSave(&$script)
  193. {
  194. $tmp = '';
  195. parent::addDoSave($tmp);
  196. // add autosave to i18n object even if the base object is not changed
  197. $tmp = preg_replace_callback('#(\$this\->(.+?)\->isModified\(\))#', array($this, 'i18nDoSaveCallback'), $tmp);
  198. $script .= $tmp;
  199. }
  200. private function i18nDoSaveCallback($matches)
  201. {
  202. $value = $matches[1];
  203. // get the related class to see if it is a i18n one
  204. $table = $this->getTable();
  205. $column = null;
  206. foreach ($table->getForeignKeys() as $fk)
  207. {
  208. if ($matches[2] == $this->getFKVarName($fk))
  209. {
  210. $column = $fk;
  211. break;
  212. }
  213. }
  214. $foreign_table = $this->getDatabase()->getTable($fk->getForeignTableName());
  215. if ($foreign_table->getAttribute('isI18N'))
  216. {
  217. $foreign_tables_i18n_table = $this->getDatabase()->getTable($foreign_table->getAttribute('i18nTable'));
  218. $value .= ' || ($this->'.$matches[2].'->getCulture() && $this->'.$matches[2].'->getCurrent'.$foreign_tables_i18n_table->getPhpName().'()->isModified())';
  219. }
  220. return $value;
  221. }
  222. protected function addDelete(&$script)
  223. {
  224. $tmp = '';
  225. parent::addDelete($tmp);
  226. if (DataModelBuilder::getBuildProperty('builderAddBehaviors'))
  227. {
  228. // add sfMixer call
  229. $pre_mixer_script = "
  230. foreach (sfMixer::getCallables('{$this->getClassname()}:delete:pre') as \$callable)
  231. {
  232. \$ret = call_user_func(\$callable, \$this, \$con);
  233. if (\$ret)
  234. {
  235. return;
  236. }
  237. }
  238. ";
  239. $post_mixer_script = "
  240. foreach (sfMixer::getCallables('{$this->getClassname()}:delete:post') as \$callable)
  241. {
  242. call_user_func(\$callable, \$this, \$con);
  243. }
  244. ";
  245. $tmp = preg_replace('/{/', '{'.$pre_mixer_script, $tmp, 1);
  246. $tmp = preg_replace('/}\s*$/', $post_mixer_script.' }', $tmp);
  247. }
  248. // update current script
  249. $script .= $tmp;
  250. }
  251. protected function addSave(&$script)
  252. {
  253. $tmp = '';
  254. parent::addSave($tmp);
  255. // add support for created_(at|on) and updated_(at|on) columns
  256. $date_script = '';
  257. $updated = false;
  258. $created = false;
  259. foreach ($this->getTable()->getColumns() as $col)
  260. {
  261. $clo = strtolower($col->getName());
  262. if (!$updated && in_array($clo, array('updated_at', 'updated_on')))
  263. {
  264. $updated = true;
  265. $date_script .= "
  266. if (\$this->isModified() && !\$this->isColumnModified(".$this->getColumnConstant($col)."))
  267. {
  268. \$this->set".$col->getPhpName()."(time());
  269. }
  270. ";
  271. }
  272. else if (!$created && in_array($clo, array('created_at', 'created_on')))
  273. {
  274. $created = true;
  275. $date_script .= "
  276. if (\$this->isNew() && !\$this->isColumnModified(".$this->getColumnConstant($col)."))
  277. {
  278. \$this->set".$col->getPhpName()."(time());
  279. }
  280. ";
  281. }
  282. }
  283. $tmp = preg_replace('/{/', '{'.$date_script, $tmp, 1);
  284. if (DataModelBuilder::getBuildProperty('builderAddBehaviors'))
  285. {
  286. // add sfMixer call
  287. $pre_mixer_script = "
  288. foreach (sfMixer::getCallables('{$this->getClassname()}:save:pre') as \$callable)
  289. {
  290. \$affectedRows = call_user_func(\$callable, \$this, \$con);
  291. if (is_int(\$affectedRows))
  292. {
  293. return \$affectedRows;
  294. }
  295. }
  296. ";
  297. $post_mixer_script = <<<EOF
  298. foreach (sfMixer::getCallables('{$this->getClassname()}:save:post') as \$callable)
  299. {
  300. call_user_func(\$callable, \$this, \$con, \$affectedRows);
  301. }
  302. EOF;
  303. $tmp = preg_replace('/{/', '{'.$pre_mixer_script, $tmp, 1);
  304. $tmp = preg_replace('/(\$con\->commit\(\);)/', '$1'.$post_mixer_script, $tmp);
  305. }
  306. // update current script
  307. $script .= $tmp;
  308. }
  309. protected function addClassClose(&$script)
  310. {
  311. parent::addClassClose($script);
  312. $behaviors = $this->getTable()->getAttribute('behaviors');
  313. if ($behaviors)
  314. {
  315. $behavior_file_name = 'Base'.$this->getTable()->getPhpName().'Behaviors';
  316. $behavior_file_path = ClassTools::getFilePath($this->getStubObjectBuilder()->getPackage().'.om', $behavior_file_name);
  317. $behavior_include_script = <<<EOF
  318. if (sfProjectConfiguration::getActive() instanceof sfApplicationConfiguration)
  319. {
  320. include_once '%s';
  321. }
  322. EOF;
  323. $script .= sprintf($behavior_include_script, $behavior_file_path);
  324. }
  325. }
  326. protected function addConstants(&$script)
  327. {
  328. $script .= "\n const PEER = '".$this->getPeerClassName()."';\n";
  329. }
  330. }