[ Index ]

PHP Cross Reference of phpBB-3.3.5-deutsch

title

Body

[close]

/vendor/symfony/http-kernel/DataCollector/ -> RequestDataCollector.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <fabien@symfony.com>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Symfony\Component\HttpKernel\DataCollector;
  13  
  14  use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15  use Symfony\Component\HttpFoundation\Cookie;
  16  use Symfony\Component\HttpFoundation\ParameterBag;
  17  use Symfony\Component\HttpFoundation\Request;
  18  use Symfony\Component\HttpFoundation\Response;
  19  use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  20  use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
  21  use Symfony\Component\HttpKernel\KernelEvents;
  22  
  23  /**
  24   * @author Fabien Potencier <fabien@symfony.com>
  25   */
  26  class RequestDataCollector extends DataCollector implements EventSubscriberInterface, LateDataCollectorInterface
  27  {
  28      protected $controllers;
  29  
  30      public function __construct()
  31      {
  32          $this->controllers = new \SplObjectStorage();
  33      }
  34  
  35      /**
  36       * {@inheritdoc}
  37       */
  38      public function collect(Request $request, Response $response, \Exception $exception = null)
  39      {
  40          // attributes are serialized and as they can be anything, they need to be converted to strings.
  41          $attributes = [];
  42          $route = '';
  43          foreach ($request->attributes->all() as $key => $value) {
  44              if ('_route' === $key) {
  45                  $route = \is_object($value) ? $value->getPath() : $value;
  46                  $attributes[$key] = $route;
  47              } else {
  48                  $attributes[$key] = $value;
  49              }
  50          }
  51  
  52          try {
  53              $content = $request->getContent();
  54          } catch (\LogicException $e) {
  55              // the user already got the request content as a resource
  56              $content = false;
  57          }
  58  
  59          $sessionMetadata = [];
  60          $sessionAttributes = [];
  61          $flashes = [];
  62          if ($request->hasSession()) {
  63              $session = $request->getSession();
  64              if ($session->isStarted()) {
  65                  $sessionMetadata['Created'] = date(\DATE_RFC822, $session->getMetadataBag()->getCreated());
  66                  $sessionMetadata['Last used'] = date(\DATE_RFC822, $session->getMetadataBag()->getLastUsed());
  67                  $sessionMetadata['Lifetime'] = $session->getMetadataBag()->getLifetime();
  68                  $sessionAttributes = $session->all();
  69                  $flashes = $session->getFlashBag()->peekAll();
  70              }
  71          }
  72  
  73          $statusCode = $response->getStatusCode();
  74  
  75          $responseCookies = [];
  76          foreach ($response->headers->getCookies() as $cookie) {
  77              $responseCookies[$cookie->getName()] = $cookie;
  78          }
  79  
  80          $this->data = [
  81              'method' => $request->getMethod(),
  82              'format' => $request->getRequestFormat(),
  83              'content' => $content,
  84              'content_type' => $response->headers->get('Content-Type', 'text/html'),
  85              'status_text' => isset(Response::$statusTexts[$statusCode]) ? Response::$statusTexts[$statusCode] : '',
  86              'status_code' => $statusCode,
  87              'request_query' => $request->query->all(),
  88              'request_request' => $request->request->all(),
  89              'request_headers' => $request->headers->all(),
  90              'request_server' => $request->server->all(),
  91              'request_cookies' => $request->cookies->all(),
  92              'request_attributes' => $attributes,
  93              'route' => $route,
  94              'response_headers' => $response->headers->all(),
  95              'response_cookies' => $responseCookies,
  96              'session_metadata' => $sessionMetadata,
  97              'session_attributes' => $sessionAttributes,
  98              'flashes' => $flashes,
  99              'path_info' => $request->getPathInfo(),
 100              'controller' => 'n/a',
 101              'locale' => $request->getLocale(),
 102          ];
 103  
 104          if (isset($this->data['request_headers']['php-auth-pw'])) {
 105              $this->data['request_headers']['php-auth-pw'] = '******';
 106          }
 107  
 108          if (isset($this->data['request_server']['PHP_AUTH_PW'])) {
 109              $this->data['request_server']['PHP_AUTH_PW'] = '******';
 110          }
 111  
 112          if (isset($this->data['request_request']['_password'])) {
 113              $this->data['request_request']['_password'] = '******';
 114          }
 115  
 116          foreach ($this->data as $key => $value) {
 117              if (!\is_array($value)) {
 118                  continue;
 119              }
 120              if ('request_headers' === $key || 'response_headers' === $key) {
 121                  $this->data[$key] = array_map(function ($v) { return isset($v[0]) && !isset($v[1]) ? $v[0] : $v; }, $value);
 122              }
 123          }
 124  
 125          if (isset($this->controllers[$request])) {
 126              $this->data['controller'] = $this->parseController($this->controllers[$request]);
 127              unset($this->controllers[$request]);
 128          }
 129  
 130          if ($request->attributes->has('_redirected') && $redirectCookie = $request->cookies->get('sf_redirect')) {
 131              $this->data['redirect'] = json_decode($redirectCookie, true);
 132  
 133              $response->headers->clearCookie('sf_redirect');
 134          }
 135  
 136          if ($response->isRedirect()) {
 137              $response->headers->setCookie(new Cookie(
 138                  'sf_redirect',
 139                  json_encode([
 140                      'token' => $response->headers->get('x-debug-token'),
 141                      'route' => $request->attributes->get('_route', 'n/a'),
 142                      'method' => $request->getMethod(),
 143                      'controller' => $this->parseController($request->attributes->get('_controller')),
 144                      'status_code' => $statusCode,
 145                      'status_text' => Response::$statusTexts[(int) $statusCode],
 146                  ])
 147              ));
 148          }
 149  
 150          $this->data['identifier'] = $this->data['route'] ?: (\is_array($this->data['controller']) ? $this->data['controller']['class'].'::'.$this->data['controller']['method'].'()' : $this->data['controller']);
 151      }
 152  
 153      public function lateCollect()
 154      {
 155          $this->data = $this->cloneVar($this->data);
 156      }
 157  
 158      public function reset()
 159      {
 160          $this->data = [];
 161          $this->controllers = new \SplObjectStorage();
 162      }
 163  
 164      public function getMethod()
 165      {
 166          return $this->data['method'];
 167      }
 168  
 169      public function getPathInfo()
 170      {
 171          return $this->data['path_info'];
 172      }
 173  
 174      public function getRequestRequest()
 175      {
 176          return new ParameterBag($this->data['request_request']->getValue());
 177      }
 178  
 179      public function getRequestQuery()
 180      {
 181          return new ParameterBag($this->data['request_query']->getValue());
 182      }
 183  
 184      public function getRequestHeaders()
 185      {
 186          return new ParameterBag($this->data['request_headers']->getValue());
 187      }
 188  
 189      public function getRequestServer($raw = false)
 190      {
 191          return new ParameterBag($this->data['request_server']->getValue($raw));
 192      }
 193  
 194      public function getRequestCookies($raw = false)
 195      {
 196          return new ParameterBag($this->data['request_cookies']->getValue($raw));
 197      }
 198  
 199      public function getRequestAttributes()
 200      {
 201          return new ParameterBag($this->data['request_attributes']->getValue());
 202      }
 203  
 204      public function getResponseHeaders()
 205      {
 206          return new ParameterBag($this->data['response_headers']->getValue());
 207      }
 208  
 209      public function getResponseCookies()
 210      {
 211          return new ParameterBag($this->data['response_cookies']->getValue());
 212      }
 213  
 214      public function getSessionMetadata()
 215      {
 216          return $this->data['session_metadata']->getValue();
 217      }
 218  
 219      public function getSessionAttributes()
 220      {
 221          return $this->data['session_attributes']->getValue();
 222      }
 223  
 224      public function getFlashes()
 225      {
 226          return $this->data['flashes']->getValue();
 227      }
 228  
 229      public function getContent()
 230      {
 231          return $this->data['content'];
 232      }
 233  
 234      public function getContentType()
 235      {
 236          return $this->data['content_type'];
 237      }
 238  
 239      public function getStatusText()
 240      {
 241          return $this->data['status_text'];
 242      }
 243  
 244      public function getStatusCode()
 245      {
 246          return $this->data['status_code'];
 247      }
 248  
 249      public function getFormat()
 250      {
 251          return $this->data['format'];
 252      }
 253  
 254      public function getLocale()
 255      {
 256          return $this->data['locale'];
 257      }
 258  
 259      /**
 260       * Gets the route name.
 261       *
 262       * The _route request attributes is automatically set by the Router Matcher.
 263       *
 264       * @return string The route
 265       */
 266      public function getRoute()
 267      {
 268          return $this->data['route'];
 269      }
 270  
 271      public function getIdentifier()
 272      {
 273          return $this->data['identifier'];
 274      }
 275  
 276      /**
 277       * Gets the route parameters.
 278       *
 279       * The _route_params request attributes is automatically set by the RouterListener.
 280       *
 281       * @return array The parameters
 282       */
 283      public function getRouteParams()
 284      {
 285          return isset($this->data['request_attributes']['_route_params']) ? $this->data['request_attributes']['_route_params']->getValue() : [];
 286      }
 287  
 288      /**
 289       * Gets the parsed controller.
 290       *
 291       * @return array|string The controller as a string or array of data
 292       *                      with keys 'class', 'method', 'file' and 'line'
 293       */
 294      public function getController()
 295      {
 296          return $this->data['controller'];
 297      }
 298  
 299      /**
 300       * Gets the previous request attributes.
 301       *
 302       * @return array|bool A legacy array of data from the previous redirection response
 303       *                    or false otherwise
 304       */
 305      public function getRedirect()
 306      {
 307          return isset($this->data['redirect']) ? $this->data['redirect'] : false;
 308      }
 309  
 310      public function onKernelController(FilterControllerEvent $event)
 311      {
 312          $this->controllers[$event->getRequest()] = $event->getController();
 313      }
 314  
 315      public function onKernelResponse(FilterResponseEvent $event)
 316      {
 317          if (!$event->isMasterRequest()) {
 318              return;
 319          }
 320  
 321          if ($event->getRequest()->cookies->has('sf_redirect')) {
 322              $event->getRequest()->attributes->set('_redirected', true);
 323          }
 324      }
 325  
 326      public static function getSubscribedEvents()
 327      {
 328          return [
 329              KernelEvents::CONTROLLER => 'onKernelController',
 330              KernelEvents::RESPONSE => 'onKernelResponse',
 331          ];
 332      }
 333  
 334      /**
 335       * {@inheritdoc}
 336       */
 337      public function getName()
 338      {
 339          return 'request';
 340      }
 341  
 342      /**
 343       * Parse a controller.
 344       *
 345       * @param mixed $controller The controller to parse
 346       *
 347       * @return array|string An array of controller data or a simple string
 348       */
 349      protected function parseController($controller)
 350      {
 351          if (\is_string($controller) && false !== strpos($controller, '::')) {
 352              $controller = explode('::', $controller);
 353          }
 354  
 355          if (\is_array($controller)) {
 356              try {
 357                  $r = new \ReflectionMethod($controller[0], $controller[1]);
 358  
 359                  return [
 360                      'class' => \is_object($controller[0]) ? \get_class($controller[0]) : $controller[0],
 361                      'method' => $controller[1],
 362                      'file' => $r->getFileName(),
 363                      'line' => $r->getStartLine(),
 364                  ];
 365              } catch (\ReflectionException $e) {
 366                  if (\is_callable($controller)) {
 367                      // using __call or  __callStatic
 368                      return [
 369                          'class' => \is_object($controller[0]) ? \get_class($controller[0]) : $controller[0],
 370                          'method' => $controller[1],
 371                          'file' => 'n/a',
 372                          'line' => 'n/a',
 373                      ];
 374                  }
 375              }
 376          }
 377  
 378          if ($controller instanceof \Closure) {
 379              $r = new \ReflectionFunction($controller);
 380  
 381              $controller = [
 382                  'class' => $r->getName(),
 383                  'method' => null,
 384                  'file' => $r->getFileName(),
 385                  'line' => $r->getStartLine(),
 386              ];
 387  
 388              if (false !== strpos($r->name, '{closure}')) {
 389                  return $controller;
 390              }
 391              $controller['method'] = $r->name;
 392  
 393              if ($class = $r->getClosureScopeClass()) {
 394                  $controller['class'] = $class->name;
 395              } else {
 396                  return $r->name;
 397              }
 398  
 399              return $controller;
 400          }
 401  
 402          if (\is_object($controller)) {
 403              $r = new \ReflectionClass($controller);
 404  
 405              return [
 406                  'class' => $r->getName(),
 407                  'method' => null,
 408                  'file' => $r->getFileName(),
 409                  'line' => $r->getStartLine(),
 410              ];
 411          }
 412  
 413          return \is_string($controller) ? $controller : 'n/a';
 414      }
 415  }


Generated: Mon Oct 4 17:42:11 2021 Cross-referenced by PHPXref 0.7.1