生成自动文档SlimFramework

时间:2015-03-29 16:40:50

标签: php reflection slim

我有一个简单的RESTful API,它已经使用Slim Framework构建。

代码示例:

$app->group('/settings','authenticate','is_admin',function () use($app){
    $app->get('/gear', function () use ($app) {
        ...
    });

    $app->group('/users',function() use($app){
        $app->get('/', function () use ($app) {...});
        $app->post('/', function () use ($app) {...});
        $app->put('/:id', function ($id) use ($app) {...});
        $app->delete('/:id', function () use ($app) {...});
    });
});

现在,我需要构建一个简单的页面,列出我的所有路由组作为'资源'并在每个组下嵌套路由,以便我可以为每个路由组/方法添加某种文本文档,稍后我将在数据库或文件存储中保留这些文档。

总之,我的问题是:

  1. 有类似的东西已经存在吗?
  2. 如何在Slim Framework中列出所有路由和路由组?
  3. 我目前的Hack:

    $app->get('/', function () use ($app) {
        $data=[];
        //$router->routes is a protected variable, so i had to change it to public, same for $r->methods and pattern
        foreach($app->router->routes as $r){
            $x = explode('/',$r->pattern);
            $x = $x[1];
            $data[$x][$r->methods[0]][]=$r->pattern."[".implode(',',$r->middleware)."]";
        }
        print_r($data);
    });
    

    我给出了这样的结果:

    Array
    (
        [auth] => Array
            (
                [GET] => Array
                    (
                        [0] => /auth/me[authenticate]
                        [1] => /auth/logout[]
                    )
    
                [POST] => Array
                    (
                        [0] => /auth/login[]
                    )
    
                [DELETE] => Array
                    (
                        [0] => /auth/logout[]
                    )
    
            )
    
        [settings] => Array
            (
                [GET] => Array
                    (
                        [0] => /settings/classes[authenticate,is_admin]
                        [1] => /[authenticate,is_admin]
                    )
    
                [POST] => Array
                    (
                        [0] => /settings/grades/:id/class[authenticate,is_admin]
                        [1] => /settings/grades/:id/subject[authenticate,is_admin]
                        [2] => /settings/departments/:id/grade[authenticate,is_admin]
                        [3] => /settings/subjects/:id/skills[authenticate,is_admin]
                    )
    
            )
    

1 个答案:

答案 0 :(得分:0)

所以我想要做到这一点它有一些限制,但它会让你在那里的一部分。

我使用了苗条的中间件并创建了自己的中间件。 http://docs.slimframework.com/middleware/overview/

然后每次使用路线时,我都会得到请求模式和方法。如果是get,我会使用phps反射函数来获取变量类型整数或字符串。如果它是一个帖子我会得到请求正文解析它并获得变量类型。然后我会将这些保存到一个json文件(每个路由一个文件)半按照swagger-ui规范。然后我会将swagger-ui指向另一个php脚本,它会查看所有json文件的目录,然后我将它放在一起,我将一个json文件放在swagger-ui json规范之后,我将这个json回显给swagger-ui 。

这种方法有很多不足之处,比如对所有内容的描述,但它让我们在那里......这就行了。

`

class MyMiddleware extends \Slim\Middleware
{
public $myRoute;
    public function call()
    {
                //The Slim application
                $app = $this->app;
                $this->app->hook('slim.before.dispatch', array($this, 'onBeforeDispatch'));
                //The Environment object
                $env = $app->environment;
                //The Request object
                $req = $app->request;
                $method = $req->getMethod();
                $return['operations'] = array('method'=> $req->getMethod(),"summary" =>'',"type"=>'',"nickname"=>'',"parameters"=>array());
                if($method == "POST"){
                        $parameters = json_decode($env->offsetGet('slim.input'));
                        $return['operations']['parameters'] = $parameters;
                }
                //The Response object
                $res = $app->response;
                $body = $res->getBody();
                $this->return = $return;
                //call the next middleware
                $this->next->call();
    }
         public function onBeforeDispatch()
    {
        $route = $this->app->router()->getCurrentRoute();
        $this->myRoute = $route;
         $return['path'] = $route->getPattern();
                $return = $this->return;
                if($return['operations']['method'] == "GET"){
                        $refFunc = new ReflectionFunction($route->callable);
                        foreach ($refFunc->getParameters()  as $refParameter) {
                                $return['operations']['parameters'] = array();
                                $return['operations']['parameters']['name'] = $refParameter->name;
                                $return['operations']['parameters']['required'] = $refParameter->isOptional();
                        }
                }
                echo json_encode($return);
    }
}
$app->add(new MyMiddleware);

`无论如何我希望它有所帮助。