From ef5cbfe40f457fcddd4ae5a9a0a034c311d7c73f Mon Sep 17 00:00:00 2001 From: m8rge Date: Sun, 19 Apr 2015 20:47:55 +0500 Subject: [PATCH] improve cli interface --- composer.json | 3 +- composer.lock | 178 +++++++++++++++++++++++++++++++++++++++++++++++++- index.php | 173 +++++++++++++++++++++++++----------------------- 3 files changed, 271 insertions(+), 83 deletions(-) diff --git a/composer.json b/composer.json index 6185a4e..9cc88fe 100644 --- a/composer.json +++ b/composer.json @@ -10,6 +10,7 @@ } ], "require": { - "twig/twig": "*" + "twig/twig": "*", + "mnapoli/silly": "dev-master" } } diff --git a/composer.lock b/composer.lock index 3560e19..dad8924 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,180 @@ "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "90a0da17a7b9dfa910c4c83cac459f5a", + "hash": "c8d04ef0bc83578eb0a8b6872c789024", "packages": [ + { + "name": "container-interop/container-interop", + "version": "1.1.0", + "source": { + "type": "git", + "url": "https://github.com/container-interop/container-interop.git", + "reference": "fc08354828f8fd3245f77a66b9e23a6bca48297e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/container-interop/container-interop/zipball/fc08354828f8fd3245f77a66b9e23a6bca48297e", + "reference": "fc08354828f8fd3245f77a66b9e23a6bca48297e", + "shasum": "" + }, + "type": "library", + "autoload": { + "psr-4": { + "Interop\\Container\\": "src/Interop/Container/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Promoting the interoperability of container objects (DIC, SL, etc.)", + "time": "2014-12-30 15:22:37" + }, + { + "name": "mnapoli/invoker", + "version": "0.2.1", + "source": { + "type": "git", + "url": "https://github.com/mnapoli/Invoker.git", + "reference": "ca2c470d68335fa28be1a2b6e89c7d0792c690fb" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/mnapoli/Invoker/zipball/ca2c470d68335fa28be1a2b6e89c7d0792c690fb", + "reference": "ca2c470d68335fa28be1a2b6e89c7d0792c690fb", + "shasum": "" + }, + "require": { + "container-interop/container-interop": "~1.1" + }, + "require-dev": { + "athletic/athletic": "~0.1.8", + "phpunit/phpunit": "~4.5" + }, + "type": "library", + "autoload": { + "psr-4": { + "Invoker\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Generic and extensible callable invoker", + "homepage": "https://github.com/mnapoli/Invoker", + "keywords": [ + "callable", + "dependency", + "dependency-injection", + "injection", + "invoke", + "invoker" + ], + "time": "2015-04-01 07:51:10" + }, + { + "name": "mnapoli/silly", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/mnapoli/silly.git", + "reference": "72ae27e3ac6544092f2652b4471c31d0628f3a59" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/mnapoli/silly/zipball/72ae27e3ac6544092f2652b4471c31d0628f3a59", + "reference": "72ae27e3ac6544092f2652b4471c31d0628f3a59", + "shasum": "" + }, + "require": { + "container-interop/container-interop": "~1.0", + "mnapoli/invoker": "~0.2.1", + "php": ">=5.5", + "symfony/console": "~2.6" + }, + "require-dev": { + "mnapoli/phpunit-easymock": "~0.1.0", + "phpunit/phpunit": "~4.5" + }, + "type": "library", + "autoload": { + "psr-4": { + "Silly\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Silly CLI micro-framework based on Symfony Console", + "keywords": [ + "cli", + "console", + "framework", + "micro-framework", + "silly" + ], + "time": "2015-04-12 01:43:47" + }, + { + "name": "symfony/console", + "version": "2.8.x-dev", + "target-dir": "Symfony/Component/Console", + "source": { + "type": "git", + "url": "https://github.com/symfony/Console.git", + "reference": "32f19477d488649a77227d57a7f5775b17cb336b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/Console/zipball/32f19477d488649a77227d57a7f5775b17cb336b", + "reference": "32f19477d488649a77227d57a7f5775b17cb336b", + "shasum": "" + }, + "require": { + "php": ">=5.3.9" + }, + "require-dev": { + "psr/log": "~1.0", + "symfony/event-dispatcher": "~2.1|~3.0.0", + "symfony/phpunit-bridge": "~2.7|~3.0.0", + "symfony/process": "~2.1|~3.0.0" + }, + "suggest": { + "psr/log": "For using the console logger", + "symfony/event-dispatcher": "", + "symfony/process": "" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.8-dev" + } + }, + "autoload": { + "psr-0": { + "Symfony\\Component\\Console\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Symfony Community", + "homepage": "http://symfony.com/contributors" + }, + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + } + ], + "description": "Symfony Console Component", + "homepage": "http://symfony.com", + "time": "2015-04-11 08:55:16" + }, { "name": "twig/twig", "version": "dev-master", @@ -67,7 +239,9 @@ "packages-dev": [], "aliases": [], "minimum-stability": "dev", - "stability-flags": [], + "stability-flags": { + "mnapoli/silly": 20 + }, "prefer-stable": false, "prefer-lowest": false, "platform": [], diff --git a/index.php b/index.php index 9f1fe8c..d702205 100644 --- a/index.php +++ b/index.php @@ -3,90 +3,103 @@ use e96\swagger\Parameters; use e96\swagger\Schema; use e96\swagger\Swagger; +use Symfony\Component\Console\Output\OutputInterface; /** @var \Composer\Autoload\ClassLoader $autoLoader */ $autoLoader = require_once 'vendor/autoload.php'; $autoLoader->addPsr4('e96\\', __DIR__); -$config = json_decode(file_get_contents('php://stdin'), true); -if ($config['swagger'] != '2.0') { - die('swagger version must be 2.0'); -} -Swagger::$root = $config; -$swagger = new Swagger($config); +$app = new Silly\Application(); -$loader = new Twig_Loader_Filesystem(__DIR__); -$twig = new Twig_Environment($loader); -$twig->addFilter(new Twig_Filter('newSchema', function ($data) { - return new Schema($data); -})); -$twig->addFilter(new Twig_Filter('newParameters', function ($data) { - return new Parameters($data); -})); -$twig->addFilter(new Twig_Filter('textStatus', function ($data) { - static $statusTexts = [ - 100 => 'Continue', - 101 => 'Switching Protocols', - 102 => 'Processing', - 200 => 'OK', - 201 => 'Created', - 202 => 'Accepted', - 203 => 'Non-Authoritative Information', - 204 => 'No Content', - 205 => 'Reset Content', - 206 => 'Partial Content', - 207 => 'Multi-Status', - 208 => 'Already Reported', - 226 => 'IM Used', - 300 => 'Multiple Choices', - 301 => 'Moved Permanently', - 302 => 'Found', - 303 => 'See Other', - 304 => 'Not Modified', - 305 => 'Use Proxy', - 307 => 'Temporary Redirect', - 308 => 'Permanent Redirect', - 400 => 'Bad Request', - 401 => 'Unauthorized', - 402 => 'Payment Required', - 403 => 'Forbidden', - 404 => 'Not Found', - 405 => 'Method Not Allowed', - 406 => 'Not Acceptable', - 407 => 'Proxy Authentication Required', - 408 => 'Request Timeout', - 409 => 'Conflict', - 410 => 'Gone', - 411 => 'Length Required', - 412 => 'Precondition Failed', - 413 => 'Request Entity Too Large', - 414 => 'Request-URI Too Long', - 415 => 'Unsupported Media Type', - 416 => 'Requested Range Not Satisfiable', - 417 => 'Expectation Failed', - 422 => 'Unprocessable Entity', - 423 => 'Locked', - 424 => 'Failed Dependency', - 425 => 'Reserved for WebDAV advanced collections expired proposal', - 426 => 'Upgrade required', - 428 => 'Precondition Required', - 429 => 'Too Many Requests', - 431 => 'Request Header Fields Too Large', - 500 => 'Internal Server Error', - 501 => 'Not Implemented', - 502 => 'Bad Gateway', - 503 => 'Service Unavailable', - 504 => 'Gateway Timeout', - 505 => 'HTTP Version Not Supported', - 506 => 'Variant Also Negotiates (Experimental)', - 507 => 'Insufficient Storage', - 508 => 'Loop Detected', - 510 => 'Not Extended', - 511 => 'Network Authentication Required', - ]; - return $statusTexts[$data]; -})); +$app->command('convert inputFile outputFile', function($inputFile, $outputFile, OutputInterface $output) { + $config = json_decode(file_get_contents($inputFile), true); + if ($config['swagger'] != '2.0') { + $output->writeln('swagger version must be 2.0'); + return; + } + Swagger::$root = $config; + $swagger = new Swagger($config); -$twig->display('slate.twig', array( - 'api' => $swagger, -)); \ No newline at end of file + $loader = new Twig_Loader_Filesystem(__DIR__); + $twig = new Twig_Environment($loader); + $twig->addFilter(new Twig_Filter('newSchema', function ($data) { + return new Schema($data); + })); + $twig->addFilter(new Twig_Filter('newParameters', function ($data) { + return new Parameters($data); + })); + $twig->addFilter(new Twig_Filter('textStatus', function ($data) { + static $statusTexts = [ + 100 => 'Continue', + 101 => 'Switching Protocols', + 102 => 'Processing', + 200 => 'OK', + 201 => 'Created', + 202 => 'Accepted', + 203 => 'Non-Authoritative Information', + 204 => 'No Content', + 205 => 'Reset Content', + 206 => 'Partial Content', + 207 => 'Multi-Status', + 208 => 'Already Reported', + 226 => 'IM Used', + 300 => 'Multiple Choices', + 301 => 'Moved Permanently', + 302 => 'Found', + 303 => 'See Other', + 304 => 'Not Modified', + 305 => 'Use Proxy', + 307 => 'Temporary Redirect', + 308 => 'Permanent Redirect', + 400 => 'Bad Request', + 401 => 'Unauthorized', + 402 => 'Payment Required', + 403 => 'Forbidden', + 404 => 'Not Found', + 405 => 'Method Not Allowed', + 406 => 'Not Acceptable', + 407 => 'Proxy Authentication Required', + 408 => 'Request Timeout', + 409 => 'Conflict', + 410 => 'Gone', + 411 => 'Length Required', + 412 => 'Precondition Failed', + 413 => 'Request Entity Too Large', + 414 => 'Request-URI Too Long', + 415 => 'Unsupported Media Type', + 416 => 'Requested Range Not Satisfiable', + 417 => 'Expectation Failed', + 422 => 'Unprocessable Entity', + 423 => 'Locked', + 424 => 'Failed Dependency', + 425 => 'Reserved for WebDAV advanced collections expired proposal', + 426 => 'Upgrade required', + 428 => 'Precondition Required', + 429 => 'Too Many Requests', + 431 => 'Request Header Fields Too Large', + 500 => 'Internal Server Error', + 501 => 'Not Implemented', + 502 => 'Bad Gateway', + 503 => 'Service Unavailable', + 504 => 'Gateway Timeout', + 505 => 'HTTP Version Not Supported', + 506 => 'Variant Also Negotiates (Experimental)', + 507 => 'Insufficient Storage', + 508 => 'Loop Detected', + 510 => 'Not Extended', + 511 => 'Network Authentication Required', + ]; + return $statusTexts[$data]; + })); + + $slate = $twig->render('slate.twig', array( + 'api' => $swagger, + )); + + file_put_contents($outputFile, $slate); +})->descriptions('Converts swagger.json to slate markdown file', [ + 'inputFile' => 'swagger.json file path', + 'outputFile' => 'source/index.md file path', +]); + +$app->run(); \ No newline at end of file