EncodingShell.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. App::uses('AppShell', 'Console/Command');
  3. /**
  4. * Detect encoding or find invalid files (starting with BOM)
  5. *
  6. * @author Mark Scherer
  7. * @license http://opensource.org/licenses/mit-license.php MIT
  8. */
  9. class EncodingShell extends AppShell {
  10. /**
  11. * Files that need to be processed.
  12. *
  13. * @var array
  14. */
  15. protected $_found = [];
  16. /**
  17. * ConvertShell::folder()
  18. *
  19. * @return void
  20. */
  21. public function folder() {
  22. $folder = APP;
  23. if (!empty($this->args)) {
  24. $folder = array_shift($this->args);
  25. $folder = realpath($folder);
  26. }
  27. if (empty($folder)) {
  28. return $this->error('Invalid dir', 'No valid dir given (either absolute or relative to APP)');
  29. }
  30. $this->out('Searching folder:');
  31. $this->out($folder, 2);
  32. $extensions = $this->params['ext'];
  33. if (!$extensions) {
  34. $extensions = 'php';
  35. }
  36. $this->_detect($folder, $extensions);
  37. $this->out('Found: ' . count($this->_found));
  38. if ($this->params['verbose']) {
  39. foreach ($this->_found as $file) {
  40. $this->out(' - ' . str_replace(APP, '/', $file));
  41. }
  42. }
  43. $in = '';
  44. if ($this->_found) {
  45. $in = $this->in('Correct those files?', ['y', 'n'], 'n');
  46. }
  47. if ($in === 'y') {
  48. if (empty($this->params['dry-run'])) {
  49. foreach ($this->_found as $file) {
  50. $content = file_get_contents($file);
  51. $content = trim($content, b"\xEF\xBB\xBF");
  52. file_put_contents($file, $content);
  53. }
  54. }
  55. $this->out('Corrections applied');
  56. }
  57. $this->out('Done!');
  58. }
  59. /**
  60. * EncodingShell::_detect()
  61. *
  62. * @param string $path
  63. * @param array $extensions
  64. * @param array $excludes
  65. * @return void
  66. */
  67. protected function _detect($path, $extensions, $excludes = []) {
  68. $Iterator = new RegexIterator(
  69. new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)),
  70. '/^.+\.(' . $extensions . ')$/i',
  71. RegexIterator::MATCH
  72. );
  73. foreach ($Iterator as $path) {
  74. $fullPath = $path->__toString();
  75. $continue = false;
  76. foreach ($excludes as $exclude) {
  77. if (strpos($fullPath, $exclude) === 0) {
  78. $continue = true;
  79. break;
  80. }
  81. }
  82. if ($continue) {
  83. continue;
  84. }
  85. if ($path->isDir()) {
  86. continue;
  87. }
  88. if (strpos($fullPath, DS . '.') !== false) {
  89. continue;
  90. }
  91. if (!empty($this->params['verbose'])) {
  92. $this->out('Probing file: ' . str_replace(APP, '/', $fullPath));
  93. }
  94. $content = file_get_contents($fullPath);
  95. if (strpos($content, b"\xEF\xBB\xBF") === 0) {
  96. $this->_found[] = $fullPath;
  97. }
  98. }
  99. }
  100. /**
  101. * Get the option parser.
  102. *
  103. * @return ConsoleOptionParser
  104. */
  105. public function getOptionParser() {
  106. $subcommandParser = [
  107. 'options' => [
  108. 'ext' => [
  109. 'short' => 'e',
  110. 'help' => 'Specify extensions [php|txt|...] - defaults to [php].',
  111. 'default' => '',
  112. ],
  113. 'dry-run' => [
  114. 'short' => 'd',
  115. 'help' => 'Dry run the command, no files will actually be modified. Should be combined with verbose.',
  116. 'boolean' => true
  117. ],
  118. 'exclude' => [
  119. 'short' => 'x',
  120. 'help' => 'Exclude the following files',
  121. 'boolean' => true,
  122. 'default' => ''
  123. ]
  124. ]
  125. ];
  126. return parent::getOptionParser()
  127. ->description(sprintf('The %sShell finds BOM files and can correct them.', $this->name))
  128. ->addSubcommand('folder', [
  129. 'help' => 'Search and correct folder recursivly.',
  130. 'parser' => $subcommandParser
  131. ]);
  132. }
  133. }