Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
12345 anikendra 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
App::uses('Folder', 'Utility');
15
 
16
/**
17
 * Whitespace shell. Helps find and trim whitespace from files.
18
 *
19
 * Based on jperras' shell found at http://bin.cakephp.org/view/626544881
20
 *
21
 * @since         DebugKit 1.3
22
 */
23
class WhitespaceShell extends Shell {
24
 
25
/**
26
 * Will check files for whitespace and notify you
27
 * of any files containing leading or trailing whitespace.
28
 *
29
 * @return void
30
 */
31
	public function main() {
32
		$path = APP;
33
		if (!empty($this->params['path']) && strpos($this->params['path'], '/') === 0) {
34
			$path = $this->params['path'];
35
		} elseif (!empty($this->params['path'])) {
36
			$path .= $this->params['path'];
37
		}
38
		$folder = new Folder($path);
39
 
40
		$r = $folder->findRecursive('.*\.php');
41
		$this->out("Checking *.php in " . $path);
42
		foreach ($r as $file) {
43
			$c = file_get_contents($file);
44
			if (preg_match('/^[\n\r|\n\r|\n|\r|\s]+\<\?php/', $c)) {
45
				$this->out('!!!contains leading whitespaces: ' . $this->shortPath($file));
46
			}
47
			if (preg_match('/\?\>[\n\r|\n\r|\n|\r|\s]+$/', $c)) {
48
				$this->out('!!!contains trailing whitespaces: ' . $this->shortPath($file));
49
			}
50
		}
51
	}
52
 
53
/**
54
 * Much like main() except files are modified. Be sure to have
55
 * backups or use version control.
56
 *
57
 * @return void
58
 */
59
	public function trim() {
60
		$path = APP;
61
		if (!empty($this->params['path']) && strpos($this->params['path'], '/') === 0) {
62
			$path = $this->params['path'];
63
		} elseif (!empty($this->params['path'])) {
64
			$path .= $this->params['path'];
65
		}
66
		$folder = new Folder($path);
67
 
68
		$r = $folder->findRecursive('.*\.php');
69
		$this->out("Checking *.php in " . $path);
70
		foreach ($r as $file) {
71
			$c = file_get_contents($file);
72
			if (preg_match('/^[\n\r|\n\r|\n|\r|\s]+\<\?php/', $c) || preg_match('/\?\>[\n\r|\n\r|\n|\r|\s]+$/', $c)) {
73
				$this->out('trimming' . $this->shortPath($file));
74
				$c = preg_replace('/^[\n\r|\n\r|\n|\r|\s]+\<\?php/', '<?php', $c);
75
				$c = preg_replace('/\?\>[\n\r|\n\r|\n|\r|\s]+$/', '?>', $c);
76
				file_put_contents($file, $c);
77
			}
78
		}
79
	}
80
 
81
/**
82
 * get the option parser
83
 *
84
 * @return ConsoleOptionParser
85
 */
86
	public function getOptionParser() {
87
		$parser = parent::getOptionParser();
88
		return $parser->addOption('path', array(
89
			'short' => 'p',
90
			'help' => __d('cake_console', 'Absolute path or relative to APP.')
91
		));
92
	}
93
}