group.inc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * @file
  4. * Definition for a migration group.
  5. */
  6. class MigrateGroup {
  7. /**
  8. * The name of the group - used to identify it in drush commands.
  9. *
  10. * @var string
  11. */
  12. protected $name;
  13. public function getName() {
  14. return $this->name;
  15. }
  16. /**
  17. * List of groups this group is dependent on.
  18. *
  19. * @var array
  20. */
  21. protected $dependencies = array();
  22. public function getDependencies() {
  23. return $this->dependencies;
  24. }
  25. /**
  26. * The central list of all known groups, keyed by group name.
  27. *
  28. * @var array
  29. */
  30. static protected $groupList = array();
  31. static public function groups() {
  32. $groups = array();
  33. $dependent_groups = array();
  34. $required_groups = array();
  35. foreach (self::$groupList as $name => $group) {
  36. $dependencies = $group->getDependencies();
  37. if (count($dependencies) > 0) {
  38. // Set groups with dependencies aside for reordering
  39. $dependent_groups[$name] = $group;
  40. $required_groups += $dependencies;
  41. }
  42. else {
  43. // No dependencies, just add
  44. $groups[$name] = $group;
  45. }
  46. }
  47. $iterations = 0;
  48. while (count($dependent_groups) > 0) {
  49. if ($iterations++ > 20) {
  50. $group_names = implode(',', array_keys($dependent_groups));
  51. throw new MigrateException(t('Failure to sort migration list - most likely due ' .
  52. 'to circular dependencies involving groups !group_names',
  53. array('!group_names' => $group_names)));
  54. }
  55. foreach ($dependent_groups as $name => $group) {
  56. $ready = TRUE;
  57. // Scan all the dependencies for this group and make sure they're all
  58. // in the final list
  59. foreach ($group->getDependencies() as $dependency) {
  60. if (!isset($groups[$dependency])) {
  61. $ready = FALSE;
  62. break;
  63. }
  64. }
  65. if ($ready) {
  66. // Yes they are! Move this group to the final list
  67. $groups[$name] = $group;
  68. unset($dependent_groups[$name]);
  69. }
  70. }
  71. }
  72. return $groups;
  73. }
  74. /**
  75. * Basic constructor.
  76. *
  77. * @param string $name
  78. * Group name.
  79. *
  80. * @param array $dependencies
  81. * List of dependent groups.
  82. */
  83. public function __construct($name, $dependencies = array()) {
  84. $this->name = $name;
  85. $this->dependencies = $dependencies;
  86. }
  87. /**
  88. * Retrieve (creating if necessary) an instance of the named group.
  89. *
  90. * @param string $name
  91. * Group name.
  92. *
  93. * @param array $dependencies
  94. * List of dependent groups.
  95. */
  96. static public function getInstance($name, $dependencies = array()) {
  97. if (empty(self::$groupList[$name])) {
  98. self::$groupList[$name] = new MigrateGroup($name, $dependencies);
  99. }
  100. return self::$groupList[$name];
  101. }
  102. }