Base for a static organization website

WhitespaceShell.php 2.7KB

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