Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic OPTIONS handler #179

Open
kingschnulli opened this issue Feb 13, 2014 · 2 comments
Open

Generic OPTIONS handler #179

kingschnulli opened this issue Feb 13, 2014 · 2 comments

Comments

@kingschnulli
Copy link

I just fiddled around with tonic a little, I came up with this solution for a generic OPTIONS request handler that will describe the service:

/**
    * @description Describe the service
    * @method OPTIONS
    * @json
    * @priority 5
    * @return Tonic\Response
    */
public function describeService(){

        $r = new ReflectionClass($this);

        $aMethods = $r->getMethods();

        $aResult = array();

        foreach ($aMethods as $oMethod) {
            if($oMethod->isPublic()){

                $sDocComment = $oMethod->getDocComment();
                $aAnnotations = $this->parseDocComment($sDocComment);

                $sHttpMethod = $aAnnotations["@method"][0][0];
                $aRawParams = $aAnnotations["@param"];

                $aParams = array();

                if($aRawParams){
                    foreach ($aRawParams as $aParam) {
                        $sType = array_shift($aParam);
                        $sName = str_replace('$', '', array_shift($aParam));
                        $sDescription = implode(" ", $aParam);

                        $aParams[$sName] = array(
                            "type" => $sType,
                            "description" => $sDescription
                        );
                    }    
                }

                if($sHttpMethod){
                    $aResult[$sHttpMethod] = array(
                        "description" => implode(" ", $aAnnotations["@description"][0]),
                        "parameters"  => $aParams
                    );    
                }
            }


        }

        return new Response(200, $aResult);

    }

I needed to copy the Application::parseDocComment as this is not a public method to my service. With a little rework this might make it into the core - what do you think?

Currently the description needs to be marked with @description annotation, I was just too lazy to write the parser code for that.

This will output what was described as a good practice here:

http://zacstewart.com/2012/04/14/http-options-method.html

@peej
Copy link
Owner

peej commented Apr 2, 2014

This is definitely something that is useful and should be possible, although I'm not sure it should be part of the core as the output format requirements will be different for different people.

Exposing the Resource metadata so that responses can be generated based upon it is something that I'm working on as part of the phpsepc2 branch. Take a look over there, it should get merged in soon.

@justechn
Copy link

justechn commented Sep 7, 2015

I am running into this while implementing my rest api. Chrome is requesting OPTIONS, and it does not find anything (405 Method Not Allowed). I did not see any mention of this in any of the instructions on the front page. What is the preferred solution? Do I have to write an @method OPTIONS for every url? If so, what is the appropriate response to an OPTIONS request?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants