/** * Scan the filesystem for swagger annotations and build swagger-documentation. * * @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) * analyser: defaults to StaticAnalyser * analysis: defaults to a new Analysis * processors: defaults to the registered processors in Analysis * @return Swagger */ function scan($directory, $options = array()) { $analyser = @$options['analyser'] ?: new StaticAnalyser(); $analysis = @$options['analysis'] ?: new Analysis(); $processors = @$options['processors'] ?: Analysis::processors(); $exclude = @$options['exclude'] ?: null; // Crawl directory and parse all files $finder = Util::finder($directory, $exclude); foreach ($finder as $file) { $analysis->addAnalysis($analyser->fromFile($file->getPathname())); } // Post processing $analysis->process($processors); // Validation (Generate notices & warnings) $analysis->validate(); return $analysis->swagger; }
/** * {@inheritDoc} */ public function getServiceConfig() { return array('aliases' => array('service.swagger' => 'Swagger\\Annotations\\Swagger'), 'factories' => array('SwaggerModule\\Options\\ModuleOptions' => function ($serviceManager) { $config = $serviceManager->get('Config'); $config = isset($config['swagger']) ? $config['swagger'] : null; if ($config === null) { throw new RuntimeException('Configuration for SwaggerModule was not found'); } return new SwaggerModuleOptions($config); }, 'Swagger\\Annotations\\Swagger' => function ($serviceManager) { /** @var $options \SwaggerModule\Options\ModuleOptions */ $options = $serviceManager->get('SwaggerModule\\Options\\ModuleOptions'); $analyser = new SwaggerStaticAnalyser(); $analysis = new SwaggerAnalysis(); $processors = SwaggerAnalysis::processors(); // Crawl directory and parse all files $paths = $options->getPaths(); foreach ($paths as $directory) { $finder = SwaggerUtil::finder($directory); foreach ($finder as $file) { $analysis->addAnalysis($analyser->fromFile($file->getPathname())); } } // Post processing $analysis->process($processors); // Validation (Generate notices & warnings) $analysis->validate(); // Pass options to analyzer $resourceOptions = $options->getResourceOptions(); if (!empty($resourceOptions['defaultBasePath'])) { $analysis->swagger->basePath = $resourceOptions['defaultBasePath']; } if (!empty($resourceOptions['defaultHost'])) { $analysis->swagger->host = $resourceOptions['defaultHost']; } if (!empty($resourceOptions['schemes'])) { $analysis->swagger->schemes = $resourceOptions['schemes']; } return $analysis->swagger; })); }
/** * Build a Symfony Finder object that scans the given $directory. * * @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) * @throws InvalidArgumentException */ public static function finder($directory, $exclude = null) { if ($directory instanceof Finder) { return $directory; } else { $finder = new Finder(); $finder->sortByName(); } $finder->files(); if (is_string($directory)) { if (is_file($directory)) { // Scan a single file? $finder->append([$directory]); } else { // Scan a directory $finder->in($directory); } } elseif (is_array($directory)) { foreach ($directory as $path) { if (is_file($path)) { // Scan a file? $finder->append([$path]); } else { $finder->in($path); } } } else { throw new InvalidArgumentException('Unexpected $directory value:' . gettype($directory)); } if ($exclude !== null) { if (is_string($exclude)) { $finder->notPath(Util::getRelativePath($exclude, $directory)); } elseif (is_array($exclude)) { foreach ($exclude as $path) { $finder->notPath(Util::getRelativePath($path, $directory)); } } else { throw new InvalidArgumentException('Unexpected $exclude value:' . gettype($exclude)); } } return $finder; }