/plugins/sfPropelORMPlugin/lib/vendor/propel/generator/lib/behavior/archivable/ArchivableBehavior.php

https://github.com/crew-cr/Crew · PHP · 197 lines · 147 code · 19 blank · 31 comment · 23 complexity · 5a59d1346c9459298a303ed278400e66 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. require_once dirname(__FILE__) . '/ArchivableBehaviorObjectBuilderModifier.php';
  10. require_once dirname(__FILE__) . '/ArchivableBehaviorQueryBuilderModifier.php';
  11. /**
  12. * Keeps tracks of an ActiveRecord object, even after deletion
  13. *
  14. * @author Francois Zaninotto
  15. * @version $Revision$
  16. * @package propel.generator.archivable
  17. */
  18. class ArchivableBehavior extends Behavior
  19. {
  20. // default parameters value
  21. protected $parameters = array(
  22. 'archive_table' => '',
  23. 'archive_class' => '',
  24. 'log_archived_at' => 'true',
  25. 'archived_at_column' => 'archived_at',
  26. 'archive_on_insert' => 'false',
  27. 'archive_on_update' => 'false',
  28. 'archive_on_delete' => 'true',
  29. );
  30. protected $archiveTable;
  31. protected $objectBuilderModifier;
  32. protected $queryBuilderModifier;
  33. public function modifyDatabase()
  34. {
  35. foreach ($this->getDatabase()->getTables() as $table) {
  36. if ($table->hasBehavior($this->getName())) {
  37. // don't add the same behavior twice
  38. continue;
  39. }
  40. if (property_exists($table, 'isArchiveTable')) {
  41. // don't add the behavior to archive tables
  42. continue;
  43. }
  44. $b = clone $this;
  45. $table->addBehavior($b);
  46. }
  47. }
  48. public function modifyTable()
  49. {
  50. if ($this->getParameter('archive_class') && $this->getParameter('archive_table')) {
  51. throw new InvalidArgumentException('Please set only one of the two parameters "archive_class" and "archive_table".');
  52. }
  53. if (!$this->getParameter('archive_class')) {
  54. $this->addArchiveTable();
  55. }
  56. }
  57. protected function addArchiveTable()
  58. {
  59. $table = $this->getTable();
  60. $database = $table->getDatabase();
  61. $archiveTableName = $this->getParameter('archive_table') ? $this->getParameter('archive_table') : ($this->getTable()->getName() . '_archive');
  62. if (!$database->hasTable($archiveTableName)) {
  63. // create the version table
  64. $archiveTable = $database->addTable(array(
  65. 'name' => $archiveTableName,
  66. 'package' => $table->getPackage(),
  67. 'schema' => $table->getSchema(),
  68. 'namespace' => $table->getNamespace() ? '\\' . $table->getNamespace() : null,
  69. ));
  70. $archiveTable->isArchiveTable = true;
  71. // copy all the columns
  72. foreach ($table->getColumns() as $column) {
  73. $columnInArchiveTable = clone $column;
  74. if ($columnInArchiveTable->hasReferrers()) {
  75. $columnInArchiveTable->clearReferrers();
  76. }
  77. if ($columnInArchiveTable->isAutoincrement()) {
  78. $columnInArchiveTable->setAutoIncrement(false);
  79. }
  80. $archiveTable->addColumn($columnInArchiveTable);
  81. }
  82. // add archived_at column
  83. if ($this->getParameter('log_archived_at') == 'true') {
  84. $archiveTable->addColumn(array(
  85. 'name' => $this->getParameter('archived_at_column'),
  86. 'type' => 'TIMESTAMP'
  87. ));
  88. }
  89. // do not copy foreign keys
  90. // copy the indices
  91. foreach ($table->getIndices() as $index) {
  92. $copiedIndex = clone $index;
  93. $copiedIndex->setName('');
  94. $archiveTable->addIndex($copiedIndex);
  95. }
  96. // copy unique indices to indices
  97. // see https://github.com/propelorm/Propel/issues/175 for details
  98. foreach ($table->getUnices() as $unique) {
  99. $index = new Index();
  100. foreach ($unique->getColumns() as $columnName) {
  101. if ($size = $unique->getColumnSize($columnName)) {
  102. $index->addColumn(array('name' => $columnName, 'size' => $size));
  103. } else {
  104. $index->addColumn(array('name' => $columnName));
  105. }
  106. }
  107. $archiveTable->addIndex($index);
  108. }
  109. // every behavior adding a table should re-execute database behaviors
  110. foreach ($database->getBehaviors() as $behavior) {
  111. $behavior->modifyDatabase();
  112. }
  113. $this->archiveTable = $archiveTable;
  114. } else {
  115. $this->archiveTable = $database->getTable($archiveTableName);
  116. }
  117. }
  118. /**
  119. * @return Table
  120. */
  121. public function getArchiveTable()
  122. {
  123. return $this->archiveTable;
  124. }
  125. public function getArchiveTablePhpName($builder)
  126. {
  127. if ($this->hasArchiveClass()) {
  128. return $this->getParameter('archive_class');
  129. }
  130. return $builder->getNewStubObjectBuilder($this->getArchiveTable())->getClassname();
  131. }
  132. public function getArchiveTableQueryName($builder)
  133. {
  134. if ($this->hasArchiveClass()) {
  135. return $this->getParameter('archive_class') . 'Query';
  136. }
  137. return $builder->getNewStubQueryBuilder($this->getArchiveTable())->getClassname();
  138. }
  139. public function hasArchiveClass()
  140. {
  141. return $this->getParameter('archive_class') != '';
  142. }
  143. /**
  144. * @return Column
  145. */
  146. public function getArchivedAtColumn()
  147. {
  148. if ($this->getParameter('log_archived_at') == 'true') {
  149. return $this->getTable()->getColumn($this->getParameter('archived_at_column'));
  150. }
  151. }
  152. public function isArchiveOnInsert()
  153. {
  154. return $this->getParameter('archive_on_insert') == 'true';
  155. }
  156. public function isArchiveOnUpdate()
  157. {
  158. return $this->getParameter('archive_on_update') == 'true';
  159. }
  160. public function isArchiveOnDelete()
  161. {
  162. return $this->getParameter('archive_on_delete') == 'true';
  163. }
  164. public function getObjectBuilderModifier()
  165. {
  166. if (is_null($this->objectBuilderModifier)) {
  167. $this->objectBuilderModifier = new ArchivableBehaviorObjectBuilderModifier($this);
  168. }
  169. return $this->objectBuilderModifier;
  170. }
  171. public function getQueryBuilderModifier()
  172. {
  173. if (is_null($this->queryBuilderModifier)) {
  174. $this->queryBuilderModifier = new ArchivableBehaviorQueryBuilderModifier($this);
  175. }
  176. return $this->queryBuilderModifier;
  177. }
  178. }