Valid point. I have modified the script, and move some things around. Best regards, Levi
Kornel Lesiński wrote: > On 20-07-2009 at 04:03:32 Levi Stanley <l...@eneservices.com> wrote: > >> Patch Adds: >> >> + I added force parsing every time the script is ran. >> + Added an include option, so you are now able to include your >> modifiers, prefilters, etc. >> + Now using getopt > > Thanks! > > Custom includes is a good idea. > > However I don't like that getopt() ignores "loose" arguments. This > makes the tool harder to use, and gives surprising results in case like: > > phptal_lint.php -p foo*.php > > shell will expand this to: > > phptal_lint.php -p foo1.php foo2.php > > > and getopt() will cause foo2.php to be silently ignored. > > >
--- phptal_lint.sh 2009-07-10 20:23:46.000000000 -0400 +++ phptal_lint.php 2009-07-20 07:56:49.124942853 -0400 @@ -1,7 +1,7 @@ #!/usr/bin/env php <?php - -try { +try +{ $myphptal = dirname(__FILE__).'/../classes/PHPTAL.php'; if (file_exists($myphptal)) { @@ -11,46 +11,43 @@ { require_once "PHPTAL.php"; } - - if (!defined('PHPTAL_VERSION')) { + + if (!defined('PHPTAL_VERSION')) + { throw new Exception("Your PHPTAL installation is broken or too new for this tool"); } echo "PHPTAL Lint 1.0 (PHPTAL ",PHPTAL_VERSION,")\n"; - if (!empty($_SERVER['REQUEST_URI'])) { + if (! empty($_SERVER['REQUEST_URI'])) + { throw new Exception("Please use this tool from command line"); } - $paths = array(); - $custom_extensions = array(); - if (isset($_SERVER['argv']) && count($_SERVER['argv']) > 1) + $options = extended_getopt('i,e'); + print_r($options); + + if (isset($options['i'])) { - $arguments = $_SERVER['argv']; - for($i=1; $i < count($arguments); $i++) - { - if ($arguments[$i] === '-e') - { - if ($i < count($arguments)-1) - { - $custom_extensions = array_merge($custom_extensions,preg_split('/[\s,.]+/',$arguments[$i+1])); - $i++; - } - } - else - { - $paths[] = $arguments[$i]; - } - } + include_path($options['i']); + } + + if (isset($options['e'])) + { + $custom_extensions = array_merge($custom_extensions, preg_split('/[\s,.]+/', $options['e'][0])); + } + + if (isset($options['--filenames--'])) + { + $paths = ($options['--filenames--']); } + if (!count($paths)) - { - echo "Usage: phptal_lint.php [-e extensions] file_or_directory_to_check ...\n"; - echo " -e comma-separated list of extensions\n"; - echo " Use 'phptal_lint.php .' to scan current directory\n\n"; + { + usage(); exit(1); } @@ -59,10 +56,10 @@ if ($custom_extensions) { $lint->acceptExtensions($custom_extensions); - echo "Using *.",implode(', *.',$custom_extensions),"\n"; + echo "Using *.",implode(', *.', $custom_extensions),"\n"; } - - foreach($paths as $arg) + + foreach ($paths as $arg) { if (is_dir($arg)) { @@ -76,19 +73,22 @@ echo "\n\n"; echo "Checked {$lint->checked} file(s)."; - if ($lint->skipped) echo " Skipped {$lint->skipped} non-template file(s)."; + if ($lint->skipped) + echo " Skipped {$lint->skipped} non-template file(s)."; echo "\n"; - if (!$custom_extensions && count($lint->skipped_filenames)) { - echo "Skipped file(s): ",implode(', ',array_keys($lint->skipped_filenames)),".\n"; + if (!$custom_extensions && count($lint->skipped_filenames)) + { + echo "Skipped file(s): ",implode(', ', array_keys($lint->skipped_filenames)),".\n"; } if (count($lint->errors)) { echo "Found ",count($lint->errors)," error(s):\n"; $last_dir = NULL; - foreach($lint->errors as $errinfo) + foreach ($lint->errors as $errinfo) { - if ($errinfo[0] !== $last_dir) { + if ($errinfo[0] !== $last_dir) + { echo "In ",$errinfo[0],":\n"; $last_dir = $errinfo[0]; } @@ -106,11 +106,66 @@ } catch(Exception $e) { - fwrite(STDOUT,$e->getMessage()."\n"); + fwrite(STDOUT, $e->getMessage()."\n"); $errcode = $e->getCode(); exit($errcode ? $errcode : 1); } +function usage() { + echo "Usage: phptal_lint.php [-e extensions] [-i php_file_or_directory] file_or_directory_to_check ...\n"; + echo " -e comma-separated list of extensions\n"; + echo " -i phptales file/include file, or directory\n"; + echo " Use 'phptal_lint.php .' to scan current directory\n\n"; +} + +function extended_getopt($string) { + function prefix_a_switch(&$value, $key) { + $value = "-$value"; + } + $options = explode(',', $string); + array_walk($options, 'prefix_a_switch'); + + $results = array(); + for ($i = 1; $i < count($_SERVER['argv']); $i++) + { + if (in_array($_SERVER['argv'][$i], $options)) + { + $results[substr($_SERVER['argv'][$i], 1)][] = $_SERVER['argv'][++$i]; + } + else if (substr($_SERVER['argv'][$i], 0, 1) == '-') + { + echo "{$_SERVER['argv'][$i]} is not a valid options\n\n"; + usage(); + exit; + } + else + { + $results['--filenames--'][] = $_SERVER['argv'][$i]; + } + } + return $results; +} + +function include_path($tales) { + foreach ($tales as $path) + { + if (is_dir($path)) + { + foreach ( new DirectoryIterator($path) as $file) + { + if (preg_match('/\.php$/', "$path/$file") && is_file("$path/$file")) + { + include_once ("$path/$file"); + } + } + } + else if (preg_match('/\.php$/', $path) && is_file($path)) + { + include_once ("$path"); + } + } +} + class PHPTAL_Lint { private $ignore_pattern = '/^\.|\.(?i:php|inc|jpe?g|gif|png|mo|po|txt|orig|rej|xsl|xsd|sh|in|ini|conf|css|js|py|pdf|swf|csv|ico|jar|htc)$|^Makefile|^[A-Z]+$/'; private $accept_pattern = '/\.(?:xml|x?html|zpt|phptal|tal|tpl)$/i'; @@ -120,50 +175,51 @@ public $skipped = 0; public $checked = 0; - function acceptExtensions(array $ext) - { - $this->accept_pattern = '/\.(?:'.implode('|',$ext).')$/i'; + function acceptExtensions(array $ext) { + $this->accept_pattern = '/\.(?:'.implode('|', $ext).')$/i'; } - function scan($path) - { - foreach(new DirectoryIterator($path) as $entry) + function scan($path) { + foreach ( new DirectoryIterator($path) as $entry) { $filename = $entry->getFilename(); - if ($filename === '.' || $filename === '..') { + if ($filename === '.' || $filename === '..') + { continue; } - if (preg_match($this->ignore_pattern,$filename)) { + if (preg_match($this->ignore_pattern, $filename)) + { $this->skipped++; continue; } - - if ($entry->isDir()) { + + if ($entry->isDir()) + { echo '.'; - $this->scan($path . DIRECTORY_SEPARATOR . $filename); + $this->scan($path.DIRECTORY_SEPARATOR.$filename); continue; } - - if (!preg_match($this->accept_pattern,$filename)) + + if (!preg_match($this->accept_pattern, $filename)) { $this->skipped++; $this->skipped_filenames[$filename] = true; continue; - } + } - $this->testFile($path . DIRECTORY_SEPARATOR . $filename); + $this->testFile($path.DIRECTORY_SEPARATOR.$filename); } } - function testFile($fullpath) - { + function testFile($fullpath) { try - { + { $this->checked++; $phptal = new PHPTAL($fullpath); - $phptal->prepare(); + $phptal->setForceReparse(true); + $phptal->prepare(); echo '.'; } catch(PHPTAL_UnknownModifierException $e) @@ -173,7 +229,7 @@ catch(Exception $e) { echo 'E'; - $this->errors[] = array(dirname($fullpath),basename($fullpath), $e->getMessage(), $e->getLine()); + $this->errors[] = array(dirname($fullpath), basename($fullpath), $e->getMessage(), $e->getLine()); } } }
_______________________________________________ PHPTAL mailing list PHPTAL@lists.motion-twin.com http://lists.motion-twin.com/mailman/listinfo/phptal