<commit_before><?php

namespace RREST\Provider;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response as HttpFoundationResponse;
use RREST\Response;
use Silex\Application;

/**
 * Silex provider.
 */
class Silex implements ProviderInterface
{
    /**
     * @var Request
     */
    protected $request;

    /**
     * @var Application
     */
    protected $app;

    /**
     * @param Application $app
     */
    public function __construct(Application $app)
    {
        $this->app = $app;
        $this->request = Request::createFromGlobals();
    }

    /**
     * {@inheritdoc}
     */
    public function addRoute($routePath, $method, $controllerClassName, $actionMethodName, Response $response, \Closure $init)
    {
        $controller = $this->app->match(
            $routePath,
            $controllerClassName.'::'.$actionMethodName
        )
        ->method(strtoupper($method))
        //define a response configured
        ->value('response', $response)
        ->before(function(Request $request) use ($init) {
            $this->request = $request;
            $init();
        });
    }

    /**
     * {@inheritdoc}
     */
    public function applyCORS($origin = '*', $methods = 'GET,POST,PUT,DELETE,OPTIONS', $headers = '')
    {
        $this->app->before(function (Request $request) use ($origin, $methods, $headers) {
            if ($request->getMethod() === 'OPTIONS') {
                $response = new HttpFoundationResponse();
                $response->headers->set("Access-Control-Allow-Origin",$origin);
                $response->headers->set("Access-Control-Allow-Methods",$methods);
                $response->headers->set("Access-Control-Allow-Headers",$headers);
                $response->setStatusCode(200);
                return $response;
            }
        }, Application::EARLY_EVENT);

        return true;
    }

    /**
     * {@inheritdoc}
     */
    public function getHTTPProtocol()
    {
        return $this->request->getScheme();
    }

    /**
     * {@inheritdoc}
     */
    public function getHTTPParameterValue($key, $type)
    {
        $parameterBags = ['query', 'request', 'attributes'];
        // Search in all Silex Request parameters
        foreach ($parameterBags as $parameterBag) {
            $requestParam = $this->request->{$parameterBag};
            if ($requestParam->has($key)) {
                return $requestParam->get($key);
            }
        }

        return null;
    }

    /**
     * {@inheritdoc}
     */
    public function setHTTPParameterValue($key, $value)
    {
        $parameterBags = ['query', 'request', 'attributes'];
        // Search in Silex Request parameters
        foreach ($parameterBags as $parameterBag) {
            $requestParam = $this->request->{$parameterBag};
            if ($requestParam->has($key)) {
                $requestParam->set($key, $value);
                //stop searching parameter when finding one
                break;
            }
        }
    }

    /**
     * {@inheritdoc}
     */
    public function getHTTPPayloadBodyValue()
    {
        return $this->request->getContent();
    }

    /**
     * {@inheritdoc}
     */
    public function setHTTPPayloadBodyValue($payloadBodyJSON)
    {
        $this->request->request->replace((array) $payloadBodyJSON);
    }

    /**
     * {@inheritdoc}
     */
    public function getHTTPHeaderContentType()
    {
        return $this->request->headers->get('Content-Type');
    }

    /**
     * {@inheritdoc}
     */
    public function getHTTPHeaderAccept()
    {
        //Accept is empty in header, a bug?
        //return $this->request->headers->get('Accept');
        return $this->request->server->get('HTTP_ACCEPT');
    }

    /**
     * {@inheritdoc}
     */
    public function getResponse($content = '', $statusCode = 200, $headers = array())<commit_msg>fix: enhance Silex provider to unserialize the request payload body<commit_after><?php

namespace RREST\Provider;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response as HttpFoundationResponse;
use RREST\Response;
use Silex\Application;

/**
 * Silex provider.
 */
class Silex implements ProviderInterface
{
    /**
     * @var Request
     */
    protected $request;

    /**
     * @var Application
     */
    protected $app;

    /**
     * @param Application $app
     */
    public function __construct(Application $app)
    {
        $this->app = $app;
        $this->request = Request::createFromGlobals();
    }

    /**
     * {@inheritdoc}
     */
    public function addRoute($routePath, $method, $controllerClassName, $actionMethodName, Response $response, \Closure $init)
    {
        $controller = $this->app->match(
            $routePath,
            $controllerClassName.'::'.$actionMethodName
        )
        ->method(strtoupper($method))
        //define a response configured
        ->value('response', $response)
        ->before(function(Request $request) use ($init) {
            $this->request = $request;
            $init();
        });
    }

    /**
     * {@inheritdoc}
     */
    public function applyCORS($origin = '*', $methods = 'GET,POST,PUT,DELETE,OPTIONS', $headers = '')
    {
        $this->app->before(function (Request $request) use ($origin, $methods, $headers) {
            if ($request->getMethod() === 'OPTIONS') {
                $response = new HttpFoundationResponse();
                $response->headers->set("Access-Control-Allow-Origin",$origin);
                $response->headers->set("Access-Control-Allow-Methods",$methods);
                $response->headers->set("Access-Control-Allow-Headers",$headers);
                $response->setStatusCode(200);
                return $response;
            }
        }, Application::EARLY_EVENT);

        return true;
    }

    /**
     * {@inheritdoc}
     */
    public function getHTTPProtocol()
    {
        return $this->request->getScheme();
    }

    /**
     * {@inheritdoc}
     */
    public function getHTTPParameterValue($key, $type)
    {
        $parameterBags = ['query', 'request', 'attributes'];
        // Search in all Silex Request parameters
        foreach ($parameterBags as $parameterBag) {
            $requestParam = $this->request->{$parameterBag};
            if ($requestParam->has($key)) {
                return $requestParam->get($key);
            }
        }

        return null;
    }

    /**
     * {@inheritdoc}
     */
    public function setHTTPParameterValue($key, $value)
    {
        $parameterBags = ['query', 'request', 'attributes'];
        // Search in Silex Request parameters
        foreach ($parameterBags as $parameterBag) {
            $requestParam = $this->request->{$parameterBag};
            if ($requestParam->has($key)) {
                $requestParam->set($key, $value);
                //stop searching parameter when finding one
                break;
            }
        }
    }

    /**
     * {@inheritdoc}
     */
    public function getHTTPPayloadBodyValue()
    {
        return $this->request->getContent();
    }

    /**
     * {@inheritdoc}
     */
    public function setHTTPPayloadBodyValue($payloadBodyJSON)
    {
        //no better way to setContent of the Request
        $this->request->initialize(
            $this->request->query->all(),
            $this->request->request->all(),
            $this->request->attributes->all(),
            $this->request->cookies->all(),
            $this->request->files->all(),
            $this->request->server->all(),
            $payloadBodyJSON
        );
    }

    /**
     * {@inheritdoc}
     */
    public function getHTTPHeaderContentType()
    {
        return $this->request->headers->get('Content-Type');
    }

    /**
     * {@inheritdoc}
     */
    public function getHTTPHeaderAccept()
    {
        //Accept is empty in header, a bug?
        //return $this->request->headers->get('Accept');
        return $this->request->server->get('HTTP_ACCEPT');
    }

    /**
     * {@inheritdoc}
     */
    public function getResponse($content = '', $statusCode = 200, $headers = array())
{'fraction_non_alphanumeric': 0.14967568229102926, 'fraction_numerical': 0.0014686084934524537, 'mean_word_length': 2.056095736724009, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 104, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '884173', 'n_tokens_mistral': 2259, 'n_tokens_neox': 2210, 'n_words': 424}