PHP code example of draw / open-api-bundle

1. Go to this page and download the library: Download draw/open-api-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

draw / open-api-bundle example snippets


 namespace App\Listener;

use Draw\Bundle\OpenApiBundle\Response\Serialization;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;

class VersionListener implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return [
             //It must be after reading __template attribute but before the serializer listener pass
            KernelEvents::VIEW => ['onKernelView', 31] 
        ];
    }

    public function onKernelView(ViewEvent $event)
    {
        $request = $event->getRequest();
        $pathInfo = $request->getPathInfo();

        $sections = explode('/', $pathInfo, 4);

        if(!isset($sections[2])) {
            return;
        }

        $version = trim($sections[2], 'v');

        if($sections[2] != ('v' . $version)) {
            return;
        }

        $view = $request->attributes->get('_draw_open_api_serialization', new Serialization([]));

        if($view instanceof Serialization && $view->getSerializerVersion() === null) {
            $view->setSerializerVersion($version);
        }

        $request->attributes->set('_draw_open_api_serialization', $view);
    }
}

/**
 * @OpenApi\Tag("Acme")
 */
public function defaultAction()
{
   //...
}

/**
 * @OpenApi\Operation(
 *     operationId="default",
 *     tags={"Acme"}
 * )
 */
public function defaultAction()
{
   //...
}

/**
 * @OpenApi\QueryParameter(name="param1")
 *
 * @param string $param1
 */
public function createAction($param1 = 'default')
{
   //...
}