tonic icon indicating copy to clipboard operation
tonic copied to clipboard

Generic OPTIONS handler

Open kingschnulli opened this issue 11 years ago • 2 comments

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

kingschnulli avatar Feb 13 '14 13:02 kingschnulli

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.

peej avatar Apr 02 '14 11:04 peej

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?

justechn avatar Sep 07 '15 02:09 justechn