/src/Propel/Generator/Behavior/I18n/I18nBehaviorObjectBuilderModifier.php

https://github.com/vworldat/Propel2 · PHP · 212 lines · 173 code · 20 blank · 19 comment · 13 complexity · ec735ebb6dd2cabe94317c338aa8021e MD5 · raw file

  1. <?php
  2. /**
  3. * This file is part of the Propel package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. *
  7. * @license MIT License
  8. */
  9. namespace Propel\Generator\Behavior\I18n;
  10. /**
  11. * Allows translation of text columns through transparent one-to-many relationship.
  12. * Modifier for the object builder.
  13. *
  14. * @author François Zaninotto
  15. * @version $Revision$
  16. * @package propel.generator.behavior.i18n
  17. */
  18. class I18nBehaviorObjectBuilderModifier
  19. {
  20. protected $behavior;
  21. protected $table;
  22. protected $builder;
  23. public function __construct($behavior)
  24. {
  25. $this->behavior = $behavior;
  26. $this->table = $behavior->getTable();
  27. }
  28. public function postDelete($builder)
  29. {
  30. $this->builder = $builder;
  31. if (!$builder->getPlatform()->supportsNativeDeleteTrigger() && !$builder->getBuildProperty('emulateForeignKeyConstraints')) {
  32. $i18nTable = $this->behavior->getI18nTable();
  33. return $this->behavior->renderTemplate('objectPostDelete', array(
  34. 'i18nQueryName' => $builder->getNewStubQueryBuilder($i18nTable)->getClassname(),
  35. 'objectClassname' => $builder->getNewStubObjectBuilder($this->behavior->getTable())->getClassname(),
  36. ));
  37. }
  38. }
  39. public function objectAttributes($builder)
  40. {
  41. return $this->behavior->renderTemplate('objectAttributes', array(
  42. 'defaultLocale' => $this->behavior->getDefaultLocale(),
  43. 'objectClassname' => $builder->getNewStubObjectBuilder($this->behavior->getI18nTable())->getClassname(),
  44. ));
  45. }
  46. public function objectClearReferences($builder)
  47. {
  48. return $this->behavior->renderTemplate('objectClearReferences', array(
  49. 'defaultLocale' => $this->behavior->getDefaultLocale(),
  50. ));
  51. }
  52. public function objectMethods($builder)
  53. {
  54. $this->builder = $builder;
  55. $script = '';
  56. $script .= $this->addSetLocale();
  57. $script .= $this->addGetLocale();
  58. if ($alias = $this->behavior->getParameter('locale_alias')) {
  59. $script .= $this->addGetLocaleAlias($alias);
  60. $script .= $this->addSetLocaleAlias($alias);
  61. }
  62. $script .= $this->addGetTranslation();
  63. $script .= $this->addRemoveTranslation();
  64. $script .= $this->addGetCurrentTranslation();
  65. foreach ($this->behavior->getI18nColumns() as $column) {
  66. $script .= $this->addTranslatedColumnGetter($column);
  67. $script .= $this->addTranslatedColumnSetter($column);
  68. }
  69. return $script;
  70. }
  71. protected function addSetLocale()
  72. {
  73. return $this->behavior->renderTemplate('objectSetLocale', array(
  74. 'objectClassname' => $this->builder->getStubObjectBuilder($this->table)->getClassname(),
  75. 'defaultLocale' => $this->behavior->getDefaultLocale(),
  76. ));
  77. }
  78. protected function addGetLocale()
  79. {
  80. return $this->behavior->renderTemplate('objectGetLocale');
  81. }
  82. protected function addSetLocaleAlias($alias)
  83. {
  84. return $this->behavior->renderTemplate('objectSetLocaleAlias', array(
  85. 'objectClassname' => $this->builder->getStubObjectBuilder($this->table)->getClassname(),
  86. 'defaultLocale' => $this->behavior->getDefaultLocale(),
  87. 'alias' => ucfirst($alias),
  88. ));
  89. }
  90. protected function addGetLocaleAlias($alias)
  91. {
  92. return $this->behavior->renderTemplate('objectGetLocaleAlias', array(
  93. 'alias' => ucfirst($alias),
  94. ));
  95. }
  96. protected function addGetTranslation()
  97. {
  98. $i18nTable = $this->behavior->getI18nTable();
  99. $fk = $this->behavior->getI18nForeignKey();
  100. return $this->behavior->renderTemplate('objectGetTranslation', array(
  101. 'i18nTablePhpName' => $this->builder->getNewStubObjectBuilder($i18nTable)->getClassname(),
  102. 'defaultLocale' => $this->behavior->getDefaultLocale(),
  103. 'i18nListVariable' => $this->builder->getRefFKCollVarName($fk),
  104. 'localeColumnName' => $this->behavior->getLocaleColumn()->getPhpName(),
  105. 'i18nQueryName' => $this->builder->getNewStubQueryBuilder($i18nTable)->getClassname(),
  106. 'i18nSetterMethod' => $this->builder->getRefFKPhpNameAffix($fk, $plural = false),
  107. ));
  108. }
  109. protected function addRemoveTranslation()
  110. {
  111. $i18nTable = $this->behavior->getI18nTable();
  112. $fk = $this->behavior->getI18nForeignKey();
  113. return $this->behavior->renderTemplate('objectRemoveTranslation', array(
  114. 'objectClassname' => $this->builder->getStubObjectBuilder($this->table)->getClassname(),
  115. 'defaultLocale' => $this->behavior->getDefaultLocale(),
  116. 'i18nQueryName' => $this->builder->getNewStubQueryBuilder($i18nTable)->getClassname(),
  117. 'i18nCollection' => $this->builder->getRefFKCollVarName($fk),
  118. 'localeColumnName' => $this->behavior->getLocaleColumn()->getPhpName(),
  119. ));
  120. }
  121. protected function addGetCurrentTranslation()
  122. {
  123. return $this->behavior->renderTemplate('objectGetCurrentTranslation', array(
  124. 'i18nTablePhpName' => $this->builder->getNewStubObjectBuilder($this->behavior->getI18nTable())->getClassname(),
  125. ));
  126. }
  127. // FIXME: the connection used by getCurrentTranslation in the generated code
  128. // cannot be specified by the user
  129. protected function addTranslatedColumnGetter(Column $column)
  130. {
  131. $objectBuilder = $this->builder->getNewObjectBuilder($this->behavior->getI18nTable());
  132. $comment = '';
  133. $functionStatement = '';
  134. if ($column->getType() === PropelTypes::DATE || $column->getType() === PropelTypes::TIME || $column->getType() === PropelTypes::TIMESTAMP) {
  135. $objectBuilder->addTemporalAccessorComment($comment, $column);
  136. $objectBuilder->addTemporalAccessorOpen($functionStatement, $column);
  137. } else {
  138. $objectBuilder->addDefaultAccessorComment($comment, $column);
  139. $objectBuilder->addDefaultAccessorOpen($functionStatement, $column);
  140. }
  141. $comment = preg_replace('/^\t/m', '', $comment);
  142. $functionStatement = preg_replace('/^\t/m', '', $functionStatement);
  143. preg_match_all('/\$[a-z]+/i', $functionStatement, $params);
  144. return $this->behavior->renderTemplate('objectTranslatedColumnGetter', array(
  145. 'comment' => $comment,
  146. 'functionStatement' => $functionStatement,
  147. 'columnPhpName' => $column->getPhpName(),
  148. 'params' => implode(', ', $params[0]),
  149. ));
  150. }
  151. // FIXME: the connection used by getCurrentTranslation in the generated code
  152. // cannot be specified by the user
  153. protected function addTranslatedColumnSetter(Column $column)
  154. {
  155. $i18nTablePhpName = $this->builder->getNewStubObjectBuilder($this->behavior->getI18nTable())->getClassname();
  156. $tablePhpName = $this->builder->getStubObjectBuilder()->getClassname();
  157. $objectBuilder = $this->builder->getNewObjectBuilder($this->behavior->getI18nTable());
  158. $comment = '';
  159. $functionStatement = '';
  160. if ($column->getType() === PropelTypes::DATE || $column->getType() === PropelTypes::TIME || $column->getType() === PropelTypes::TIMESTAMP) {
  161. $objectBuilder->addTemporalMutatorComment($comment, $column);
  162. $objectBuilder->addMutatorOpenOpen($functionStatement, $column);
  163. } else {
  164. $objectBuilder->addMutatorComment($comment, $column);
  165. $objectBuilder->addMutatorOpenOpen($functionStatement, $column);
  166. }
  167. $comment = preg_replace('/^\t/m', '', $comment);
  168. $comment = str_replace('@return ' . $i18nTablePhpName, '@return ' . $tablePhpName, $comment);
  169. $functionStatement = preg_replace('/^\t/m', '', $functionStatement);
  170. preg_match_all('/\$[a-z]+/i', $functionStatement, $params);
  171. return $this->behavior->renderTemplate('objectTranslatedColumnSetter', array(
  172. 'comment' => $comment,
  173. 'functionStatement' => $functionStatement,
  174. 'columnPhpName' => $column->getPhpName(),
  175. 'params' => implode(', ', $params[0]),
  176. ));
  177. }
  178. public function objectFilter(&$script, $builder)
  179. {
  180. $i18nTable = $this->behavior->getI18nTable();
  181. $i18nTablePhpName = $this->builder->getNewStubObjectBuilder($i18nTable)->getClassname();
  182. $localeColumnName = $this->behavior->getLocaleColumn()->getPhpName();
  183. $pattern = '/public function add' . $i18nTablePhpName . '.*[\r\n]\s*\{/';
  184. $addition = "
  185. if (\$l && \$locale = \$l->get$localeColumnName()) {
  186. \$this->set$localeColumnName(\$locale);
  187. \$this->currentTranslations[\$locale] = \$l;
  188. }";
  189. $replacement = "\$0$addition";
  190. $script = preg_replace($pattern, $replacement, $script);
  191. }
  192. }