Skip to content

Commit

Permalink
Configurable File Patterns added
Browse files Browse the repository at this point in the history
  • Loading branch information
ktamas77 authored and bfanger committed Nov 30, 2018
1 parent 5d96b7c commit 5a697be
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
12 changes: 11 additions & 1 deletion bin/openapi
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ $options = [
'output' => false,
'format' => 'auto',
'exclude' => [],
'pattern' => '*.php',
'bootstrap' => false,
'help' => false,
'debug' => false,
Expand All @@ -22,6 +23,7 @@ $options = [
$aliases = [
'o' => 'output',
'e' => 'exclude',
'n' => 'pattern',
'b' => 'bootstrap',
'h' => 'help',
'd' => 'debug',
Expand All @@ -32,6 +34,7 @@ $needsArgument = [
'output',
'format',
'exclude',
'pattern',
'bootstrap',
'processor',
];
Expand Down Expand Up @@ -103,6 +106,8 @@ Options:
ex: --output openapi.yaml
--exclude (-e) Exclude path(s).
ex: --exclude vendor,library/Zend
--pattern (-n) Pattern of files to scan.
ex: --pattern "*.php" or --pattern "/\.(phps|php)$/"
--bootstrap (-b) Bootstrap a php file for defining constants, etc.
ex: --bootstrap config/constants.php
--processor Register an additional processor.
Expand Down Expand Up @@ -210,6 +215,11 @@ if ($options['exclude']) {
}
}

$pattern = "*.php";
if ($options['pattern']) {
$pattern = $options['pattern'];
}

foreach ($options["processor"] as $processor) {
$class = '\OpenApi\Processors\\'.$processor;
if (class_exists($class)) {
Expand All @@ -220,7 +230,7 @@ foreach ($options["processor"] as $processor) {
Analysis::registerProcessor($processor);
}

$openapi = OpenApi\scan($paths, ['exclude' => $exclude]);
$openapi = OpenApi\scan($paths, ['exclude' => $exclude, 'pattern' => $pattern]);

if ($exit !== 0) {
error_log('');
Expand Down
9 changes: 7 additions & 2 deletions src/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,22 @@ private static function removePrefix($str, $prefix)
*
* @param string|array|Finder $directory The directory(s) or filename(s)
* @param null|string|array $exclude The directory(s) or filename(s) to exclude (as absolute or relative paths)
* @param null|string $pattern The pattern of the files to scan
* @throws InvalidArgumentException
*/
public static function finder($directory, $exclude = null)
public static function finder($directory, $exclude = null, $pattern = null)
{
if ($directory instanceof Finder) {
return $directory;
} else {
$finder = new Finder();
$finder->sortByName();
}
$finder->files()->followLinks()->name('*.php');
if ($pattern === null) {
$pattern = '*.php';
}

$finder->files()->followLinks()->name($pattern);
if (is_string($directory)) {
if (is_file($directory)) { // Scan a single file?
$finder->append([$directory]);
Expand Down
4 changes: 3 additions & 1 deletion src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* @param string|array|Finder $directory The directory(s) or filename(s)
* @param array $options
* exclude: string|array $exclude The directory(s) or filename(s) to exclude (as absolute or relative paths)
* pattern: string $pattern File pattern(s) to scan (default: *.php)
* analyser: defaults to StaticAnalyser
* analysis: defaults to a new Analysis
* processors: defaults to the registered processors in Analysis
Expand All @@ -34,9 +35,10 @@ function scan($directory, $options = [])
$analysis = array_key_exists('analysis', $options) ? $options['analysis'] : new Analysis();
$processors = array_key_exists('processors', $options) ? $options['processors'] : Analysis::processors();
$exclude = array_key_exists('exclude', $options) ? $options['exclude'] : null;
$pattern = array_key_exists('pattern', $options) ? $options['pattern'] : null;

// Crawl directory and parse all files
$finder = Util::finder($directory, $exclude);
$finder = Util::finder($directory, $exclude, $pattern);
foreach ($finder as $file) {
$analysis->addAnalysis($analyser->fromFile($file->getPathname()));
}
Expand Down

0 comments on commit 5a697be

Please sign in to comment.