Comparer.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Package\Comparer;
  12. /**
  13. * class Comparer
  14. *
  15. * @author Hector Prats <hectorpratsortega@gmail.com>
  16. */
  17. class Comparer
  18. {
  19. private $source;
  20. private $update;
  21. private $changed;
  22. public function setSource($source)
  23. {
  24. $this->source = $source;
  25. }
  26. public function setUpdate($update)
  27. {
  28. $this->update = $update;
  29. }
  30. public function getChanged($toString = false, $explicated = false)
  31. {
  32. $changed = $this->changed;
  33. if (!count($changed)) {
  34. return false;
  35. }
  36. if ($explicated) {
  37. foreach ($changed as $sectionKey => $itemSection) {
  38. foreach ($itemSection as $itemKey => $item) {
  39. $changed[$sectionKey][$itemKey] = $item.' ('.$sectionKey.')';
  40. }
  41. }
  42. }
  43. if ($toString) {
  44. foreach ($changed as $sectionKey => $itemSection) {
  45. foreach ($itemSection as $itemKey => $item) {
  46. $changed['string'][] = $item."\r\n";
  47. }
  48. }
  49. $changed = implode("\r\n", $changed['string']);
  50. }
  51. return $changed;
  52. }
  53. public function doCompare()
  54. {
  55. $source = array();
  56. $destination = array();
  57. $this->changed = array();
  58. $currentDirectory = getcwd();
  59. chdir($this->source);
  60. $source = $this->doTree('.', $source);
  61. if (!is_array($source)) {
  62. return;
  63. }
  64. chdir($currentDirectory);
  65. chdir($this->update);
  66. $destination = $this->doTree('.', $destination);
  67. if (!is_array($destination)) {
  68. exit;
  69. }
  70. chdir($currentDirectory);
  71. foreach ($source as $dir => $value) {
  72. foreach ($value as $file => $hash) {
  73. if (isset($destination[$dir][$file])) {
  74. if ($hash !== $destination[$dir][$file]) {
  75. $this->changed['changed'][] = $dir.'/'.$file;
  76. }
  77. } else {
  78. $this->changed['removed'][] = $dir.'/'.$file;
  79. }
  80. }
  81. }
  82. foreach ($destination as $dir => $value) {
  83. foreach ($value as $file => $hash) {
  84. if (!isset($source[$dir][$file])) {
  85. $this->changed['added'][] = $dir.'/'.$file;
  86. }
  87. }
  88. }
  89. }
  90. private function doTree($dir, &$array)
  91. {
  92. if ($dh = opendir($dir)) {
  93. while ($file = readdir($dh)) {
  94. if ($file !== '.' && $file !== '..') {
  95. if (is_dir($dir.'/'.$file)) {
  96. if (!count($array)) {
  97. $array[0] = 'Temp';
  98. }
  99. if (!$this->doTree($dir.'/'.$file, $array)) {
  100. return false;
  101. }
  102. } else {
  103. if (filesize($dir.'/'.$file)) {
  104. set_time_limit(30);
  105. $array[$dir][$file] = md5_file($dir.'/'.$file);
  106. }
  107. }
  108. }
  109. }
  110. if (count($array) > 1 && isset($array['0'])) {
  111. unset($array['0']);
  112. }
  113. return $array;
  114. }
  115. return false;
  116. }
  117. }