EncodingShell.php 3.4 KB

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