1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org) |
4
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) |
5
|
|
|
* |
6
|
|
|
* Licensed under The MIT License |
7
|
|
|
* Redistributions of files must retain the above copyright notice. |
8
|
|
|
* |
9
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) |
10
|
|
|
* @link http://cakephp.org CakePHP(tm) Project |
11
|
|
|
* @since DebugKit 1.3 |
12
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License |
13
|
|
|
*/ |
14
|
|
|
namespace DebugKit\Shell; |
15
|
|
|
|
16
|
|
|
use Cake\Console\Shell; |
17
|
|
|
use Cake\Filesystem\Folder; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Whitespace shell. Helps find and trim whitespace from files. |
21
|
|
|
* |
22
|
|
|
* Based on jperras' shell found at http://bin.cakephp.org/view/626544881 |
23
|
|
|
* |
24
|
|
|
* @since DebugKit 1.3 |
25
|
|
|
*/ |
26
|
|
|
class WhitespaceShell extends Shell |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Will check files for whitespace and notify you |
31
|
|
|
* of any files containing leading or trailing whitespace. |
32
|
|
|
* |
33
|
|
|
* @return void |
34
|
|
|
*/ |
35
|
|
|
public function main() |
36
|
|
|
{ |
37
|
|
|
$path = APP; |
38
|
|
View Code Duplication |
if (!empty($this->params['path']) && strpos($this->params['path'], '/') === 0) { |
39
|
|
|
$path = $this->params['path']; |
40
|
|
|
} elseif (!empty($this->params['path'])) { |
41
|
|
|
$path .= $this->params['path']; |
42
|
|
|
} |
43
|
|
|
$folder = new Folder($path); |
44
|
|
|
|
45
|
|
|
$r = $folder->findRecursive('.*\.php'); |
46
|
|
|
$this->out("Checking *.php in " . $path); |
47
|
|
|
foreach ($r as $file) { |
48
|
|
|
$c = file_get_contents($file); |
49
|
|
|
if (preg_match('/^[\n\r|\n\r|\n|\r|\s]+\<\?php/', $c)) { |
50
|
|
|
$this->out('!!!contains leading whitespaces: ' . $this->shortPath($file)); |
51
|
|
|
} |
52
|
|
|
if (preg_match('/\?\>[\n\r|\n\r|\n|\r|\s]+$/', $c)) { |
53
|
|
|
$this->out('!!!contains trailing whitespaces: ' . $this->shortPath($file)); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Much like main() except files are modified. Be sure to have |
60
|
|
|
* backups or use version control. |
61
|
|
|
* |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
|
|
public function trim() |
65
|
|
|
{ |
66
|
|
|
$path = APP; |
67
|
|
View Code Duplication |
if (!empty($this->params['path']) && strpos($this->params['path'], '/') === 0) { |
68
|
|
|
$path = $this->params['path']; |
69
|
|
|
} elseif (!empty($this->params['path'])) { |
70
|
|
|
$path .= $this->params['path']; |
71
|
|
|
} |
72
|
|
|
$folder = new Folder($path); |
73
|
|
|
|
74
|
|
|
$r = $folder->findRecursive('.*\.php'); |
75
|
|
|
$this->out("Checking *.php in " . $path); |
76
|
|
|
foreach ($r as $file) { |
77
|
|
|
$c = file_get_contents($file); |
78
|
|
|
if (preg_match('/^[\n\r|\n\r|\n|\r|\s]+\<\?php/', $c) || preg_match('/\?\>[\n\r|\n\r|\n|\r|\s]+$/', $c)) { |
79
|
|
|
$this->out('trimming' . $this->shortPath($file)); |
80
|
|
|
$c = preg_replace('/^[\n\r|\n\r|\n|\r|\s]+\<\?php/', '<?php', $c); |
81
|
|
|
$c = preg_replace('/\?\>[\n\r|\n\r|\n|\r|\s]+$/', '?>', $c); |
82
|
|
|
file_put_contents($file, $c); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* get the option parser |
89
|
|
|
* |
90
|
|
|
* @return ConsoleOptionParser |
91
|
|
|
*/ |
92
|
|
|
public function getOptionParser() |
93
|
|
|
{ |
94
|
|
|
$parser = parent::getOptionParser(); |
95
|
|
|
return $parser->addOption('path', [ |
96
|
|
|
'short' => 'p', |
97
|
|
|
'help' => __d('cake_console', 'Absolute path or relative to APP.') |
98
|
|
|
]); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|