protected function prepareSwagger()
 {
     $errorTypes = [E_ERROR => 'ERROR', E_WARNING => 'WARNING', E_PARSE => 'PARSE', E_NOTICE => 'NOTICE', E_CORE_ERROR => 'CORE_ERROR', E_CORE_WARNING => 'CORE_WARNING', E_COMPILE_ERROR => 'COMPILE_ERROR', E_COMPILE_WARNING => 'COMPILE_WARNING', E_USER_ERROR => 'ERROR', E_USER_WARNING => 'WARNING', E_USER_NOTICE => 'NOTICE', E_STRICT => 'STRICT', E_RECOVERABLE_ERROR => 'RECOVERABLE_ERROR', E_DEPRECATED => 'DEPRECATED', E_USER_DEPRECATED => 'DEPRECATED'];
     set_error_handler(function ($errno, $errstr, $file, $line) use($errorTypes) {
         if (!(error_reporting() & $errno)) {
             return;
             // This error code is not included in error_reporting
         }
         $type = @$errorTypes[$errno] ?: 'ERROR';
         error_log('[' . $type . '] ' . $errstr . ' in ' . $file . ' on line ' . $line);
         if ($type === 'ERROR') {
             exit($errno);
         }
     });
     set_exception_handler(function ($exception) {
         error_log('[EXCEPTION] ' . $exception->getMessage() . ' in ' . $exception->getFile() . ' on line ' . $exception->getLine());
         exit($exception->getCode() ?: 1);
     });
     Swagger\Logger::getInstance()->log = function ($entry, $type) {
         $type = $type === E_USER_NOTICE ? 'INFO' : 'WARN';
         if ($entry instanceof Exception) {
             $entry = $entry->getMessage();
         }
         error_log('[' . $type . '] ' . $entry . PHP_EOL);
     };
 }
 /**
  * Add routes to the app that generate swagger documentation based on your annotations
  *
  * @param Application $app
  */
 public function boot(Application $app)
 {
     AnnotationRegistry::registerAutoloadNamespace("Swagger\\Annotations", $app["swagger.srcDir"]);
     if ($app["logger"]) {
         $logger = Logger::getInstance();
         $originalLog = $logger->log;
         $logger->log = function ($entry, $type) use($app, $originalLog) {
             $app["logger"]->notice($entry);
             $originalLog($entry, $type);
         };
     }
     $app->get($app["swagger.apiDocPath"], new ResourceListController());
     $app->get("{$app["swagger.apiDocPath"]}/{service}", new ResourceDefinitionController());
 }
 /**
  * Add routes to the app that generate swagger documentation based on your annotations
  *
  * @param Application $app
  */
 public function boot(Application $app)
 {
     // Attach logger
     if ($app['logger']) {
         $logger = Logger::getInstance();
         $originalLog = $logger->log;
         $logger->log = function ($entry, $type) use($app, $originalLog) {
             $app['logger']->notice($entry);
             $originalLog($entry, $type);
         };
     }
     // Register route for docs
     $app->get('/api-docs', new SwaggerController());
 }
示例#4
0
 protected function tearDown()
 {
     $this->assertCount(0, $this->expectedLogMessages, count($this->expectedLogMessages) . ' Swagger\\Logger messages were not triggered');
     Logger::getInstance()->log = $this->originalLogger;
     parent::tearDown();
 }
示例#5
0
<?php

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 {