Пример #1
0
 public function fire()
 {
     $projectPaths = $this->realpaths($this->paths);
     $excludePaths = $this->realpaths($this->exclude);
     $outputPath = head((array) $this->output) . DIRECTORY_SEPARATOR;
     $swagger = new \Swagger\Swagger($projectPaths, $excludePaths);
     $resourceList = $swagger->getResourceList(['output' => 'array', 'suffix' => $this->suffix, 'apiVersion' => $this->default_api_version, 'swaggerVersion' => $this->default_swagger_version, 'template' => $this->api_doc_template]);
     $resourceOptions = ['output' => 'json', 'defaultSwaggerVersion' => $resourceList['swaggerVersion'], 'defaultBasePath' => $this->default_base_path];
     if (isset($resourceList['apiVersion'])) {
         $resourceOptions['defaultApiVersion'] = $resourceList['apiVersion'];
     }
     $resourceName = false;
     $output = [];
     foreach ($swagger->getResourceNames() as $resourceName) {
         $json = $swagger->getResource($resourceName, $resourceOptions);
         $resourceName = str_replace(DIRECTORY_SEPARATOR, '-', ltrim($resourceName, DIRECTORY_SEPARATOR));
         $output[$resourceName] = $json;
     }
     if (!$output) {
         throw new SwaggerException('no valid resources found');
     }
     if (file_exists($outputPath) && !is_dir($outputPath)) {
         throw new SwaggerException(sprintf('[%s] is not a directory', $outputPath));
     } elseif (!file_exists($outputPath) && !mkdir($outputPath, 0755, true)) {
         throw new SwaggerException(sprintf('[%s] is not writeable', $outputPath));
     }
     if (!file_exists($outputPath . '/.gitignore')) {
         file_put_contents($outputPath . '/.gitignore', "*\n!.gitignore");
     }
     $filename = $outputPath . 'api-docs.json';
     if (file_put_contents($filename, \Swagger\Swagger::jsonEncode($resourceList, true))) {
         $this->logger('Created ' . $filename);
     }
     foreach ($output as $name => $json) {
         $name = str_replace(DIRECTORY_SEPARATOR, '-', ltrim($name, DIRECTORY_SEPARATOR));
         $filename = $outputPath . $name . '.json';
         $this->logger('Created ' . $filename);
         file_put_contents($filename, $json);
     }
     $this->logger('');
 }
Пример #2
0
 $excludePaths = array();
 // Possible options and their default values.
 $options = array('url' => null, 'default-base-path' => null, 'default-api-version' => null, 'default-swagger-version' => '1.2', 'version' => false, 'suffix' => '.{format}', 'help' => false, 'debug' => false);
 \Swagger\Logger::getInstance()->log = function ($entry, $type) {
     $type = $type === E_USER_NOTICE ? 'INFO' : 'WARN';
     if ($entry instanceof Exception) {
         $entry = $entry->getMessage();
     }
     echo '[', $type, '] ', $entry, PHP_EOL;
 };
 $swagger = new \Swagger\Swagger($projectPaths, $excludePaths);
 $resourceListOptions = array('output' => 'json', 'suffix' => $options['suffix'], 'basePath' => $options['url'], 'apiVersion' => $options['default-api-version'], 'swaggerVersion' => $options['default-swagger-version']);
 $resourceOptions = array('output' => 'json', 'defaultBasePath' => $options['default-base-path'], 'defaultApiVersion' => $options['default-api-version'], 'defaultSwaggerVersion' => $options['default-swagger-version']);
 $resourceName = false;
 $output = array();
 foreach ($swagger->getResourceNames() as $resourceName) {
     $json = $swagger->getResource($resourceName, $resourceOptions);
     $resourceName = str_replace(DIRECTORY_SEPARATOR, '-', ltrim($resourceName, DIRECTORY_SEPARATOR));
     $output[$resourceName] = $json;
 }
 if ($output) {
     if (file_exists($outputPath) && !is_dir($outputPath)) {
         throw new RuntimeException(sprintf('[%s] is not a directory', $outputPath));
     } else {
         if (!file_exists($outputPath) && !mkdir($outputPath, 0755, true)) {
             throw new RuntimeException(sprintf('[%s] is not writeable', $outputPath));
         }
     }
     $filename = $outputPath . 'api-docs.json';
     if (file_put_contents($filename, $swagger->getResourceList($resourceListOptions))) {
         echo 'Created ', $filename, PHP_EOL;