Skip to content

Commit

Permalink
修改文档生成方式并增加更多的配置空间。
Browse files Browse the repository at this point in the history
latrell committed Jul 10, 2014
1 parent 1fb54f7 commit 32ccaca
Showing 4 changed files with 145 additions and 28 deletions.
116 changes: 116 additions & 0 deletions src/Latrell/Swagger/Swagger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php
namespace Latrell\Swagger;

class Swagger
{

protected $options;

public function __construct($options = array())
{
$this->options = $options;
}

public function fire()
{
$projectPaths = $this->realpaths($this->paths);
$excludePaths = $this->realpaths($this->exclude);
$outputPath = head($this->realpaths(base_path($this->output)));

$swagger = new \Swagger\Swagger($projectPaths, $excludePaths);

$resourceList = $swagger->getResourceList(array(
'output' => 'array',
'suffix' => $this->suffix,
'apiVersion' => $this->default_api_version,
'swaggerVersion' => $this->default_swagger_version,
'template' => $this->api_doc_template
));
$resourceOptions = array(
'output' => 'json',
'defaultSwaggerVersion' => $resourceList['swaggerVersion'],
'defaultBasePath' => $this->default_base_path
);
if (isset($resourceList['apiVersion'])) {
$resourceOptions['defaultApiVersion'] = $resourceList['apiVersion'];
}

$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) {
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));
}

$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('');
}

protected function logger()
{
// echo join('', func_get_args()), PHP_EOL;
}

/**
* 检查并转换路径为绝对路径。
*
* @param array $paths
* @throws SwaggerException
* @return array
*/
protected function realpaths($paths)
{
if (is_string($paths)) {
$paths = array(
$paths
);
}
if (! is_array($paths)) {
return array();
}
foreach ($paths as $i => $path) {
$paths[$i] = realpath($path);
if ($paths[$i] === false) {
$paths[$i] = realpath(base_path($path));
if ($paths[$i] === false) {
throw new SwaggerException("Path \"{$path}\" not found");
}
}
$paths[$i] .= DIRECTORY_SEPARATOR;
}
return $paths;
}

public function __set($key, $value)
{
$this->options[$key] = $value;
}

public function __get($key)
{
return $this->options[$key];
}

public function __isset($key)
{
return isset($this->options[$key]);
}
}
32 changes: 16 additions & 16 deletions src/Latrell/Swagger/SwaggerServiceProvider.php
Original file line number Diff line number Diff line change
@@ -32,24 +32,24 @@ public function boot()
));

if (Config::get('app.debug')) {
$appdir = app_path();
$docdir = base_path(Config::get('swagger::docs-dir'));
// $swagger = realpath(dirname(__DIR__) . '/../../') . '/vendor/zircote/swagger-php/swagger.phar';
$swagger = base_path('vendor/zircote/swagger-php/swagger.phar');
exec(sprintf('php "%s" "%s" -o "%s"', $swagger, $appdir, $docdir), $output, $return_var);
if ($return_var) {
throw new SwaggerException(join("\n", $output));
}
$errors = [];
foreach ($output as $line) {
if (preg_match('/^\[\w+\]/', $line, $matchs)) {
$errors[] = $line;
}
}
if ($errors) {
throw new SwaggerException(join("\n", $errors));

$swagger = new Swagger();

$swagger->paths = Config::get('swagger::paths');
$swagger->exclude = Config::get('swagger::exclude');
$swagger->output = Config::get('swagger::output');
$swagger->suffix = Config::get('swagger::suffix');
$swagger->default_api_version = Config::get('swagger::default-api-version');
$swagger->default_swagger_version = Config::get('swagger::default-swagger-version');
$swagger->api_doc_template = Config::get('swagger::api-doc-template');
$swagger->default_base_path = Config::get('swagger::default-base-path');

if (is_null($swagger->default_base_path)) {
$swagger->default_base_path = Config::get('app.url');
}

$swagger->fire();

require __DIR__ . '/../../routes.php';
}
}
10 changes: 9 additions & 1 deletion src/config/config.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
<?php
return array(
'prefix' => 'api-docs',
'docs-dir' => 'docs',

'paths' => 'app',
'output' => 'docs',
'exclude' => null,
'default-base-path' => null,
'default-api-version' => null,
'default-swagger-version' => null,
'api-doc-template' => null,
'suffix' => '.{format}',

'title' => 'Swagger UI'
);
15 changes: 4 additions & 11 deletions src/controllers/SwaggerController.php
Original file line number Diff line number Diff line change
@@ -15,20 +15,13 @@ public function getIndex()
return View::make('swagger::index');
}

public function getDocs($page = 'index.php')
public function getDocs($page = 'api-docs.json')
{
$path = base_path(Config::get('swagger::docs-dir') . '/' . $page);
$path = base_path(Config::get('swagger::output') . '/' . $page);
if (! file_exists($path)) {
App::abort(404);
}
$content = '';
if (array_get(pathinfo($page), 'extension') === 'php') {
ob_start();
require $path;
$content = ob_get_clean();
} else {
$content = file_get_contents($path);
}
return Response::make($content);//->header('Content-Type', 'application/json');
$content = file_get_contents($path);
return Response::make($content)->header('Content-Type', 'application/json');
}
}

0 comments on commit 32ccaca

Please sign in to comment.