<commit_before><?php declare(strict_types=1);

/*
 * This file is part of chrisguitarguy/request-id-bundle

 * Copyright (c) <NAME> <http://christopherdavis.me>
 *
 * For full copyright information see the LICENSE file distributed
 * with this source code.
 *
 * @license     http://opensource.org/licenses/MIT MIT
 */

namespace Chrisguitarguy\RequestId\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Chrisguitarguy\RequestId\RequestIdGenerator;
use Chrisguitarguy\RequestId\RequestIdStorage;

/**
 * Listens for requests and responses and sets up the request ID on each.
 *
 * @since   1.0
 */
final class RequestIdListener implements EventSubscriberInterface
{
    /**
     * The header to inspect for the incoming request ID.
     *
     * @var string
     */
    private $requestHeader;

    /**
     * The header that will contain the request ID in the response.
     *
     * @var string
     */
    private $responseHeader;

    /**
     * Trust the value from the request? Or generate?
     *
     * @var boolean
     */
    private $trustRequest;

    /**
     * The request ID storage, used to store the ID from the request or a
     * newly generated ID.
     *
     * @var RequestIdStorage
     */
    private $idStorage;

    /**
     * Used to generate a request ID if one isn't present.
     *
     * @var RequestIdGenerator
     */
    private $idGenerator;

    public function __construct($reqHeader, $respHeader, $trustReq, RequestIdStorage $storage, RequestIdGenerator $generator)
    {
        $this->requestHeader = $reqHeader;
        $this->responseHeader = $reqHeader;
        $this->trustRequest = $trustReq;
        $this->idStorage = $storage;
        $this->idGenerator = $generator;
    }

    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents() : array
    {
        return [
            KernelEvents::REQUEST   => ['onRequest', 100],
            KernelEvents::RESPONSE  => ['onResponse', -100],
        ];
    }

    public function onRequest(GetResponseEvent $event) : void
    {
        if (!$event->isMasterRequest()) {
            return;
        }

        $req = $event->getRequest();

        // always give the incoming request priority. If it has the ID in
        // its headers already put that into our ID storage.
        if ($this->trustRequest && ($id = $req->headers->get($this->requestHeader))) {
            $this->idStorage->setRequestId($id);
            return;
        }

        // similarly, if the request ID storage already has an ID set we
        // don't need to do anything other than put it into the request headers
        if ($id = $this->idStorage->getRequestId()) {
            $req->headers->set($this->requestHeader, $id);
            return;
        }

        $id = $this->idGenerator->generate();
        $req->headers->set($this->requestHeader, $id);<commit_msg>Increase priority of response listener so it runs before profiler<commit_after><?php declare(strict_types=1);

/*
 * This file is part of chrisguitarguy/request-id-bundle

 * Copyright (c) <NAME> <http://christopherdavis.me>
 *
 * For full copyright information see the LICENSE file distributed
 * with this source code.
 *
 * @license     http://opensource.org/licenses/MIT MIT
 */

namespace Chrisguitarguy\RequestId\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Chrisguitarguy\RequestId\RequestIdGenerator;
use Chrisguitarguy\RequestId\RequestIdStorage;

/**
 * Listens for requests and responses and sets up the request ID on each.
 *
 * @since   1.0
 */
final class RequestIdListener implements EventSubscriberInterface
{
    /**
     * The header to inspect for the incoming request ID.
     *
     * @var string
     */
    private $requestHeader;

    /**
     * The header that will contain the request ID in the response.
     *
     * @var string
     */
    private $responseHeader;

    /**
     * Trust the value from the request? Or generate?
     *
     * @var boolean
     */
    private $trustRequest;

    /**
     * The request ID storage, used to store the ID from the request or a
     * newly generated ID.
     *
     * @var RequestIdStorage
     */
    private $idStorage;

    /**
     * Used to generate a request ID if one isn't present.
     *
     * @var RequestIdGenerator
     */
    private $idGenerator;

    public function __construct($reqHeader, $respHeader, $trustReq, RequestIdStorage $storage, RequestIdGenerator $generator)
    {
        $this->requestHeader = $reqHeader;
        $this->responseHeader = $reqHeader;
        $this->trustRequest = $trustReq;
        $this->idStorage = $storage;
        $this->idGenerator = $generator;
    }

    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents() : array
    {
        return [
            KernelEvents::REQUEST   => ['onRequest', 100],
            KernelEvents::RESPONSE  => ['onResponse', -99],
        ];
    }

    public function onRequest(GetResponseEvent $event) : void
    {
        if (!$event->isMasterRequest()) {
            return;
        }

        $req = $event->getRequest();

        // always give the incoming request priority. If it has the ID in
        // its headers already put that into our ID storage.
        if ($this->trustRequest && ($id = $req->headers->get($this->requestHeader))) {
            $this->idStorage->setRequestId($id);
            return;
        }

        // similarly, if the request ID storage already has an ID set we
        // don't need to do anything other than put it into the request headers
        if ($id = $this->idStorage->getRequestId()) {
            $req->headers->set($this->requestHeader, $id);
            return;
        }

        $id = $this->idGenerator->generate();
        $req->headers->set($this->requestHeader, $id);
{'fraction_non_alphanumeric': 0.11123686337914308, 'fraction_numerical': 0.00274858528698464, 'mean_word_length': 2.514772727272727, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 57, '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': '809161', 'n_tokens_mistral': 1734, 'n_tokens_neox': 1681, 'n_words': 490}