PageRenderTime 33ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/propel/propel1/generator/lib/behavior/i18n/I18nBehaviorObjectBuilderModifier.php

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