EncodingShell.php 3.5 KB

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