| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- <?php
- App::uses('AppShell', 'Console/Command');
- /**
- * Detect encoding or find invalid files (starting with BOM)
- *
- * @cakephp 2.x
- * @author Mark Scherer
- * @license MIT
- * 2013-07-09 ms
- */
- class EncodingShell extends AppShell {
- /**
- * Files that need to be processed.
- *
- * @var array
- */
- protected $_found = array();
- /**
- * @return void
- */
- public function startup() {
- parent::startup();
- }
- /**
- * ConvertShell::folder()
- *
- * @return void
- */
- public function folder() {
- $folder = APP;
- if (!empty($this->args)) {
- $folder = array_shift($this->args);
- $folder = realpath($folder);
- }
- if (empty($folder)) {
- $this->error('Invalid dir', 'No valid dir given (either absolute or relative to APP)');
- }
- $this->out('Searching folder:');
- $this->out($folder, 2);
- $extensions = $this->params['ext'];
- if (!$extensions) {
- $extensions = 'php';
- }
- $this->_detect($folder, $extensions);
- $this->out('Found: ' . count($this->_found));
- if ($this->params['verbose']) {
- foreach ($this->_found as $file) {
- $this->out(' - ' . str_replace(APP, '/', $file));
- }
- }
- $in = '';
- if ($this->_found) {
- $in = $this->in('Correct those files?', array('y', 'n'), 'n');
- }
- if ($in === 'y') {
- if (empty($this->params['dry-run'])) {
- foreach ($this->_found as $file) {
- $content = file_get_contents($file);
- $content = trim($content, b"\xEF\xBB\xBF");
- file_put_contents($file, $content);
- }
- }
- $this->out('Corrections applied');
- }
- $this->out('Done!');
- }
- /**
- * EncodingShell::_detect()
- *
- * @param string $path
- * @param array $extensions
- * @param array $excludes
- * @return void
- */
- public function _detect($path, $extensions, $excludes = array()) {
- $Iterator = new RegexIterator(
- new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)),
- '/^.+\.(' . $extensions . ')$/i',
- RegexIterator::MATCH
- );
- foreach ($Iterator as $path) {
- $fullPath = $path->__toString();
- $continue = false;
- foreach ($excludes as $exclude) {
- if (strpos($fullPath, $exclude) === 0) {
- $continue = true;
- break;
- }
- }
- if ($continue) {
- continue;
- }
- if ($path->isDir()) {
- continue;
- }
- if (strpos($fullPath, DS . '.') !== false) {
- continue;
- }
- if (!empty($this->params['verbose'])) {
- $this->out('Probing file: ' . str_replace(APP, '/', $fullPath));
- }
- $content = file_get_contents($fullPath);
- if (strpos($content, b"\xEF\xBB\xBF") === 0) {
- $this->_found[] = $fullPath;
- }
- }
- }
- /**
- * Get the option parser.
- *
- * @return ConsoleOptionParser
- */
- public function getOptionParser() {
- $subcommandParser = array(
- 'options' => array(
- 'ext' => array(
- 'short' => 'e',
- 'help' => __d('cake_console', 'Specify extensions [php|txt|...] - defaults to [php].'),
- 'default' => '',
- ),
- 'dry-run'=> array(
- 'short' => 'd',
- 'help' => __d('cake_console', 'Dry run the command, no files will actually be modified. Should be combined with verbose.'),
- 'boolean' => true
- ),
- 'exclude'=> array(
- 'short' => 'x',
- 'help' => __d('cake_console', 'exclude the following files'),
- 'boolean' => true,
- 'default' => ''
- )
- )
- );
- return parent::getOptionParser()
- ->description(__d('cake_console', 'The %sShell finds BOM files and can correct them.', $this->name))
- ->addSubcommand('folder', array(
- 'help' => __d('cake_console', 'Search and correct folder recursivly.'),
- 'parser' => $subcommandParser
- ));
- }
- }
|