Advertisement
Guest User

Untitled

a guest
Jan 30th, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Ormigo\Bundle\TranslationBundle\Translation\Dumper;
  4.  
  5. use Ormigo\Bundle\TranslationBundle\Translation\Loader\PdoLoader;
  6.  
  7. use Symfony\Component\Translation\Dumper\DumperInterface;
  8. use Symfony\Component\Translation\MessageCatalogue;
  9.  
  10. class PdoDumper extends PdoLoader implements DumperInterface
  11. {
  12. protected $insertStatement;
  13. protected $updateStatement;
  14. protected $selectStatement;
  15.  
  16. /**
  17. * Dumps the message catalogue.
  18. *
  19. * @param MessageCatalogue $messages The message catalogue
  20. * @param array $options Options that are used by the dumper
  21. */
  22. public function dump(MessageCatalogue $messages, $options = array())
  23. {
  24. $this->con->beginTransaction();
  25.  
  26. $insertStmt = $this->getInsertStatement();
  27. $updateStmt = $this->getUpdateStatement();
  28. $selectStmt = $this->getSelectStatement();
  29.  
  30. $now = strtotime('now');
  31.  
  32. $locale = $messages->getLocale();
  33. foreach ($messages->getDomains() as $eachDomain) {
  34. foreach ($messages->all($eachDomain) as $eachKey => $eachTranslation) {
  35. $selectStmt->bindValue(':locale', $locale);
  36. $selectStmt->bindValue(':domain', $eachDomain);
  37. $selectStmt->bindValue(':key', $eachKey);
  38.  
  39. if (false === $selectStmt->execute()) {
  40. throw new \RuntimeException('Could not fetch translation data from database.');
  41. }
  42.  
  43. $currentTranslation = null;
  44.  
  45. $selectStmt->bindColumn('translation', $currentTranslation);
  46. $selectStmt->fetch();
  47.  
  48. $dumpStmt = null;
  49. if (null === $currentTranslation) {
  50. $dumpStmt = $insertStmt;
  51. } else {
  52. if ($currentTranslation === (string) $eachTranslation) {
  53. continue;
  54. }
  55.  
  56. $dumpStmt = $updateStmt;
  57. }
  58.  
  59.  
  60. $dumpStmt->bindValue(':key', $eachKey);
  61. $dumpStmt->bindValue(':translation', (string) $eachTranslation);
  62. $dumpStmt->bindValue(':locale', $locale);
  63. $dumpStmt->bindValue(':domain', $eachDomain);
  64. $dumpStmt->bindValue(':updated_at', $now, \PDO::PARAM_INT);
  65.  
  66. $dumpStmt->execute();
  67. }
  68. }
  69.  
  70. if (!$this->con->commit()) {
  71. $this->con->rollBack();
  72.  
  73. throw new \RuntimeException(sprintf('An error occurred while committing the transaction. [%s: %s]', $this->con->errorCode(), $this->con->errorInfo()));
  74. }
  75. }
  76.  
  77. protected function getInsertStatement()
  78. {
  79. if ($this->insertStatement instanceof \PDOStatement) {
  80. return $this->insertStatement;
  81. }
  82.  
  83. $sql = vsprintf('INSERT INTO `%s` (`%s`, `%s`, `%s`, `%s`, `%s`) VALUES (:key, :translation, :locale, :domain, :updated_at)', array(
  84. // INSERT INTO ..
  85. $this->getTablename(),
  86. // ( .. )
  87. $this->getColumnname('key'),
  88. $this->getColumnname('translation'),
  89. $this->getColumnname('locale'),
  90. $this->getColumnname('domain'),
  91. $this->getColumnname('updated_at'),
  92. ));
  93.  
  94. $stmt = $this->getConnection()->prepare($sql);
  95.  
  96. $this->insertStatement = $stmt;
  97.  
  98. return $stmt;
  99. }
  100.  
  101. protected function getUpdateStatement()
  102. {
  103. if ($this->updateStatement instanceof \PDOStatement) {
  104. return $this->updateStatement;
  105. }
  106.  
  107. $sql = vsprintf('UPDATE `%s` SET `%s` = :translation, `%s` = :updated_at WHERE `%s` = :key AND `%s` = :locale AND `%s` = :domain', array(
  108. // UPDATE ..
  109. $this->getTablename(),
  110. // SET ( .. )
  111. $this->getColumnname('translation'),
  112. $this->getColumnname('updated_at'),
  113. // WHERE ..
  114. $this->getColumnname('key'),
  115. $this->getColumnname('locale'),
  116. $this->getColumnname('domain'),
  117. ));
  118.  
  119. $stmt = $this->getConnection()->prepare($sql);
  120. $stmt->setFetchMode(\PDO::FETCH_ASSOC);
  121.  
  122. $this->updateStatement = $stmt;
  123.  
  124. return $stmt;
  125. }
  126.  
  127. protected function getSelectStatement()
  128. {
  129. if ($this->selectStatement instanceof \PDOStatement) {
  130. return $this->selectStatement;
  131. }
  132.  
  133. $sql = vsprintf('SELECT `%s` AS `translation` FROM `%s` WHERE `%s` = :locale AND `%s` = :domain AND `%s` = :key', array(
  134. // SELECT ..
  135. $this->getColumnname('translation'),
  136. // FROM ..
  137. $this->getTablename(),
  138. // WHERE ..
  139. $this->getColumnname('locale'),
  140. $this->getColumnname('domain'),
  141. $this->getColumnname('key'),
  142. ));
  143.  
  144. $stmt = $this->getConnection()->prepare($sql);
  145. $stmt->setFetchMode(\PDO::FETCH_COLUMN, 0);
  146.  
  147. $this->selectStatement = $stmt;
  148.  
  149. return $stmt;
  150. }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement