EncodingShell.php 3.5 KB

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