Exemple #1
0
 function api($name)
 {
     $swagger = new \Swagger\Swagger(dirname(__DIR__));
     header("Content-Type: application/json");
     $result = $swagger->getResource('/' . $name, array('output' => 'json'));
     $result = get_object_vars(json_decode($result));
     $result = str_replace('{baseUrl}', "http" . (!empty($_SERVER['HTTPS']) ? "s" : "") . "://" . $_SERVER['SERVER_NAME'] . $this->getBasePath(), $result);
     echo json_encode($result);
 }
Exemple #2
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('');
 }
Exemple #3
0
 public function call()
 {
     //The Slim application
     $app = $this->app;
     // Swager Documentation
     $app->group($this->path, function () use($app) {
         $app->get('/', function () use($app) {
             $swagger = new \Swagger\Swagger($this->scanPath);
             $resourceList = $swagger->getResourceList();
             echo json_encode($resourceList);
         });
         $app->get('/:path', function ($path) use($app) {
             $swagger = new \Swagger\Swagger($this->scanPath);
             $resourceList = $swagger->getResourceList();
             if (!empty($path)) {
                 echo $swagger->getResource('/' . $path, $this->options);
             } else {
                 echo json_encode($resourceList);
             }
         });
     });
     // Call next middleware
     $this->next->call();
 }
Exemple #4
0
try {
    $project_root = realpath(__DIR__ . '/../../');
    require_once $project_root . '/vendor/autoload.php';
    $outputPath = realpath(__DIR__ . '/json/');
    $projectPaths = array($project_root . '/server/library/Cms/Request/', $project_root . '/server/library/Cms/Response/', $project_root . '/server/application/controllers/');
    $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));
 /**
  *
  * @param type $files
  * @param type $dirs
  * @return type
  */
 public function getSwagger($files, $dirs = array())
 {
     $dirs = array_merge($this->dirs, $dirs);
     $Statements = array();
     foreach ($files as $file) {
         switch (pathinfo($file, PATHINFO_EXTENSION)) {
             case 'php':
                 $fileStatements = $this->parsePhpFile($file, $dirs);
                 break;
             case 'txt':
                 $fileStatements = $this->parseTextFile($file, $dirs);
                 break;
             default:
                 $fileStatements = $this->parseText($file, $dirs);
                 break;
         }
         $Statements = array_merge($Statements, $fileStatements);
     }
     $Swagger = new Swagger\Swagger($this->host, $this->basePath);
     $stack = array($Swagger);
     /* @var Swagger\AbstractObject[] $stack */
     foreach ($Statements as $Statement) {
         $top = end($stack);
         do {
             $result = $top->handleCommand($Statement->command, $Statement->data);
             if ($result) {
                 if ($result !== $top) {
                     // Remove all similar classes from array first!
                     $classname = get_class($result);
                     $stack = array_filter($stack, function ($class) use($classname) {
                         return !is_a($class, $classname);
                     });
                     $stack[] = $result;
                 }
             } else {
                 $top = prev($stack);
             }
         } while (!$result && $top);
         if (!$result && !$top) {
             $messages = array('Unsupported or unknown command: ' . $Statement);
             if (!empty($this->Paths)) {
                 end($this->Paths);
                 $messages[] = 'Current endpoint is ' . key($this->Paths);
             }
             $stacktrace = array();
             foreach ($stack as $object) {
                 $stacktrace[] = (string) $object;
             }
             $messages[] = join(", \n", $stacktrace);
             throw new \SwaggerGen\Exception(join('. ', $messages));
         }
     }
     return $Swagger->toArray();
 }
Exemple #6
0
 public function resourceAction()
 {
     $swagger = new \Swagger\Swagger(__DIR__);
     $resource = $swagger->getResource('/' . $this->dispatcher->getParam('id'));
     return $this->response->setJsonContent($resource);
 }