/app/code/local/Expressdecorcom/Commercebug/Helper/Corescan.php

https://github.com/expressdecor/Expressdecor · PHP · 185 lines · 165 code · 17 blank · 3 comment · 14 complexity · 1cbeacecba0ef228e95a1cb86eb968ce MD5 · raw file

  1. <?php
  2. class Expressdecorcom_Commercebug_Helper_Corescan extends Mage_Core_Helper_Abstract
  3. {
  4. protected $_helperDiff;
  5. public function getAppRoot()
  6. {
  7. return Mage::getRoot();
  8. }
  9. protected function _getDiffHelper()
  10. {
  11. if(!$this->_helperDiff)
  12. {
  13. $this->_helperDiff = Mage::helper('commercebug/diff');
  14. }
  15. return $this->_helperDiff;
  16. }
  17. protected function _runDiffForRow($i,$diffs)
  18. {
  19. $helper_diff = $this->_getDiffHelper();
  20. $path = $this->getAppRoot() . '/../' . $i['file'];
  21. $hash_filesystem = self::hashWithRemovedLineendings($path);
  22. $diff = new Varien_Object();
  23. $diff->file = $i['file'];
  24. if($hash_filesystem != $i['hash'])
  25. {
  26. if(file_exists($path))
  27. {
  28. $diff->output = $helper_diff->diff(
  29. preg_split('{[\r\n]}',$i['contents'],null,PREG_SPLIT_NO_EMPTY),
  30. preg_split('{[\r\n]}',file_get_contents($path),null,PREG_SPLIT_NO_EMPTY)
  31. );
  32. }
  33. else
  34. {
  35. $diff->output = 'Could not find ' . $path;
  36. }
  37. $diffs[] = $diff;
  38. }
  39. }
  40. public function diffSnapshot($snapshot_name)
  41. {
  42. $snapshot_name_id = Mage::getModel('commercebug/snapshot_name')
  43. ->getCollection()->addFieldToFilter('snapshot_name', $snapshot_name)
  44. ->getFirstItem()->getSnapshotNameId();
  45. if(!$snapshot_name_id)
  46. {
  47. throw new Exception("No Snapshot found [" . strip_tags($snapshot_name) . "]");
  48. }
  49. $q = Mage::getModel('commercebug/snapshot')
  50. ->getCollection()
  51. ->addFieldToFilter('snapshot_name_id',$snapshot_name_id)
  52. ->getSelect();
  53. $diffs = new ArrayObject();
  54. $c=0;
  55. foreach($q->query() as $row)
  56. {
  57. $this->_runDiffForRow($row,$diffs);
  58. $c++;
  59. Mage::Log("Compared Filed " . $row['file']);
  60. // var_dump($c);
  61. // flush();
  62. }
  63. $results = new Varien_Object();
  64. $results->setNumberOfFilesScanned($c);
  65. $results->setDiffs($diffs);
  66. return $results;
  67. }
  68. public function getPathsToScan()
  69. {
  70. return array(
  71. realpath($this->getAppRoot() . '/../lib'),
  72. realpath($this->getAppRoot() . '/code/core'),
  73. );
  74. }
  75. public function createSnapshotFromFiles($name,$lib,$core,$root=false)
  76. {
  77. $name = strip_tags($name);
  78. $files = $this->makeFileList(array($lib, $core));
  79. return $this->createSnapshot($name,$files,$root);
  80. }
  81. public function createSnapshotFromBase($name,$root=false)
  82. {
  83. $name = strip_tags($name);
  84. $files = $this->makeFileList($this->getPathsToScan());
  85. return $this->createSnapshot($name,$files,$root);
  86. }
  87. protected function getSnapshotNameId($name)
  88. {
  89. $snapshot_name_id = Mage::getModel('commercebug/snapshot_name')->getCollection()
  90. ->addFieldToFilter('snapshot_name', $name)->getFirstItem()->getSnapshotNameId();
  91. if(!$snapshot_name_id)
  92. {
  93. return Mage::getModel('commercebug/snapshot_name')
  94. ->setSnapshotName($name)
  95. ->save()
  96. ->getSnapshotNameId();
  97. }
  98. return $snapshot_name_id;
  99. }
  100. protected function createSnapshot($name,$files,$root)
  101. {
  102. $name = strip_tags($name);
  103. $snapshot_name_id = $this->getSnapshotNameId($name);
  104. Mage::getModel('commercebug/snapshot')->deleteSnapshots($snapshot_name_id);
  105. // $files = $this->makeFileList();
  106. foreach($files as $file)
  107. {
  108. if(!is_dir($file) && file_exists($file))
  109. {
  110. Mage::Log("Adding File: " . $file);
  111. $snapshot = Mage::getModel('commercebug/snapshot');
  112. $snapshot->setSnapshotNameId($snapshot_name_id)
  113. ->setHash(self::hashWithRemovedLineendings($file))
  114. ->setFile($this->stripPathPrefix($file,$root))
  115. ->setContents(preg_replace('%[\r\n]{1,2}$%',"\n",file_get_contents($file)))
  116. ->save();
  117. }
  118. else
  119. {
  120. Mage::Log("Skipped Adding File: " . $file);
  121. }
  122. }
  123. $result = new stdClass();
  124. $result->number_added = count($files);
  125. return $result;
  126. }
  127. protected function stripPathPrefix($file,$root)
  128. {
  129. $root = $root ? $root : ($this->getAppRoot() . '/../');
  130. return str_replace(realpath($root),'',$file);
  131. }
  132. protected function makeFileList($paths_to_scan)
  133. {
  134. $parts = array();
  135. foreach($paths_to_scan as $dir)
  136. {
  137. $tmp = $this->recursiveGlob($dir);
  138. $parts = array_merge($parts, $tmp);
  139. }
  140. return $parts;
  141. }
  142. static protected function hashWithRemovedLineendings($file)
  143. {
  144. if(file_exists($file))
  145. {
  146. return md5(preg_replace('%[\r\n]%','',file_get_contents($file)));
  147. }
  148. return md5('NOFILE');
  149. }
  150. static protected function recursiveGlob($path, $parts=false)
  151. {
  152. $parts = $parts ? $parts : array();
  153. $path .= '/*';
  154. foreach(glob($path) as $file)
  155. {
  156. if($file == '.' || $file == '..')
  157. {
  158. continue;
  159. }
  160. $parts[] = $file;
  161. if(is_dir($file))
  162. {
  163. $parts = self::recursiveGlob($file, $parts);
  164. }
  165. }
  166. return $parts;
  167. }
  168. }