| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?php
- App::uses('AppShell', 'Console/Command');
- App::uses('Folder', 'Utility');
- class WhitespaceShell extends AppShell {
- public $autoCorrectAll = false;
- # each report: [0] => found, [1] => corrected
- public $report = array('leading'=>array(0, 0), 'trailing'=>array(0, 0));
- public function find() {
- $App = new Folder(APP);
- $files = $App->findRecursive('.*\.php');
- $this->out("Checking *.php in ".APP);
- $folders = array();
- foreach ($files as $file) {
- $error = '';
- $action = '';
- $c = file_get_contents($file);
- if (preg_match('/^[\n\r|\n\r|\n|\r|\s]+\<\?php/', $c)) {
- $error = 'leading';
- }
- if (preg_match('/\?\>[\n\r|\n\r|\n|\r|\s]+$/', $c)) {
- $error = 'trailing';
- }
- if (empty($error)) {
- continue;
- }
- $this->report[$error][0]++;
- $this->out('');
- $this->out('contains '.$error.' whitespaces: '.$this->shortPath($file));
- if (!$this->autoCorrectAll) {
- $dirname = dirname($file);
- if (in_array($dirname, $folders)) {
- $action = 'y';
- }
- while (empty($action)) {
- //TODO: [r]!
- $action = $this->in(__('Remove? [y]/[n], [a] for all in this folder, [r] for all below, [*] for all files(!), [q] to quit'), array('y','n','r','a','q','*'), 'q');
- }
- } else {
- $action = 'y';
- }
- if ($action === '*') {
- $action = 'y';
- $this->autoCorrectAll = true;
- } elseif ($action === 'a') {
- $action = 'y';
- $folders[] = $dirname;
- $this->out('All: '.$dirname);
- }
- if ($action === 'q') {
- die('Abort... Done');
- } elseif ($action === 'y') {
- if ($error === 'leading') {
- $res = preg_replace('/^[\n\r|\n\r|\n|\r|\s]+\<\?php/', '<?php', $c);
- } else { //trailing
- $res = preg_replace('/\?\>[\n\r|\n\r|\n|\r|\s]+$/', '?>', $c);
- }
- file_put_contents($file, $res);
- $this->report[$error][1]++;
- $this->out('fixed '.$error.' whitespaces: '.$this->shortPath($file));
- }
- }
- # report
- $this->out('--------');
- $this->out('found '.$this->report['leading'][0].' leading, '.$this->report['trailing'][0].' trailing ws');
- $this->out('fixed '.$this->report['leading'][1].' leading, '.$this->report['trailing'][1].' trailing ws');
- }
- /**
- * whitespaces at the end of the file
- */
- public function eof() {
- if (!empty($this->args[0])) {
- $folder = realpath($this->args[0]);
- } else {
- $folder = APP;
- }
- $App = new Folder($folder);
- $this->out("Checking *.php in ".APP);
- $files = $App->findRecursive('.*\.php');
- $this->out('Found '. count($files) . ' files.');
- $action = $this->in(__('Continue? [y]/[n]'), array('y', 'n'), 'n');
- if ($action === 'n') {
- $this->error('Aborted');
- }
- foreach ($files as $file) {
- $content = $store = file_get_contents($file);
- $newline = PHP_EOL;
- $x = substr_count($content, "\r\n");
- if ($x > 0) {
- $newline = "\r\n";
- } else {
- $newline = "\n";
- }
- # add one new line at the end
- $content = trim($content) . $newline;
- if ($content !== $store) {
- file_put_contents($file, $content);
- }
- }
- }
- public function getOptionParser() {
- $subcommandParser = array(
- 'options' => array(
- 'ext' => array(
- 'short' => 'e',
- 'help' => __d('cake_console', 'Specify extensions [php|txt|...]'),
- 'default' => '',
- ),
- 'dry-run'=> array(
- 'short' => 'd',
- 'help' => __d('cake_console', 'Dry run the clear command, no files will actually be deleted. Should be combined with verbose!'),
- 'boolean' => true
- ),
- 'plugin'=> array(
- 'short' => 'p',
- 'help' => __d('cake_console', 'Plugin'),
- 'default' => '',
- ),
- )
- );
- return parent::getOptionParser()
- ->description(__d('cake_console', 'The Whitespace Shell removes uncessary/wrong whitespaces.
- Either provide a path as first argument, use -p PluginName or run it as it is for the complete APP dir.'))
- ->addSubcommand('find', array(
- 'help' => __d('cake_console', 'Detect any leading/trailing whitespaces'),
- 'parser' => $subcommandParser
- ))
- ->addSubcommand('eof', array(
- 'help' => __d('cake_console', 'Fix whitespaces at the end of PHP files (a single newline as per coding standards)'),
- 'parser' => $subcommandParser
- ));
- }
- }
|