ChangedFiles.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\TestFramework\Utility;
  7. use Magento\Framework\App\Utility\Files;
  8. /**
  9. * A helper to gather various changed files
  10. * if INCREMENTAL_BUILD env variable is set by CI build infrastructure, only files changed in the
  11. * branch are gathered, otherwise all files
  12. */
  13. class ChangedFiles
  14. {
  15. /**
  16. * File path with changed files content.
  17. */
  18. const CHANGED_FILES_CONTENT_FILE = '/dev/tests/static/testsuite/Magento/Test/_files/changed_%s_files_content.json';
  19. /**
  20. * Returns array of PHP-files, that use or declare Magento application classes and Magento libs
  21. *
  22. * @param string $changedFilesList
  23. * @param int $fileTypes
  24. * @return array
  25. */
  26. public static function getPhpFiles($changedFilesList, $fileTypes = 0)
  27. {
  28. $fileUtilities = Files::init();
  29. if (isset($_ENV['INCREMENTAL_BUILD'])) {
  30. $phpFiles = [];
  31. foreach (glob($changedFilesList, GLOB_NOSORT) as $listFile) {
  32. $phpFiles = array_merge($phpFiles, file($listFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));
  33. }
  34. array_walk(
  35. $phpFiles,
  36. function (&$file) {
  37. $file = BP . '/' . $file;
  38. }
  39. );
  40. if (!empty($phpFiles)) {
  41. $phpFiles = Files::composeDataSets($phpFiles);
  42. $phpFiles = array_intersect_key($phpFiles, $fileUtilities->getPhpFiles($fileTypes));
  43. }
  44. } else {
  45. $phpFiles = $fileUtilities->getPhpFiles($fileTypes);
  46. }
  47. return $phpFiles;
  48. }
  49. /**
  50. * Get changed content.
  51. *
  52. * @param string $fileName
  53. * @return string
  54. */
  55. public static function getChangedContent($fileName)
  56. {
  57. $data = [];
  58. $extension = self::getFileExtension($fileName);
  59. $fileName = ltrim(str_replace(BP, '', $fileName), DIRECTORY_SEPARATOR);
  60. $changedFilesContentFile = BP . sprintf(self::CHANGED_FILES_CONTENT_FILE, $extension);
  61. if (file_exists($changedFilesContentFile)) {
  62. $changedContent = file_get_contents($changedFilesContentFile);
  63. $data = json_decode($changedContent, true);
  64. }
  65. return isset($data[$fileName]) ? $data[$fileName] : '';
  66. }
  67. /**
  68. * Get file extension.
  69. *
  70. * @param string $fileName
  71. * @return string
  72. */
  73. public static function getFileExtension($fileName)
  74. {
  75. $fileInfo = pathinfo($fileName);
  76. return isset($fileInfo['extension']) ? $fileInfo['extension'] : 'unknown';
  77. }
  78. }