You are here

public function RequestDataCollector::collect in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/http-kernel/DataCollector/RequestDataCollector.php \Symfony\Component\HttpKernel\DataCollector\RequestDataCollector::collect()

Collects data for the given Request and Response.

Parameters

Request $request A Request instance:

Response $response A Response instance:

\Exception $exception An Exception instance:

Overrides DataCollectorInterface::collect

File

vendor/symfony/http-kernel/DataCollector/RequestDataCollector.php, line 40

Class

RequestDataCollector
RequestDataCollector.

Namespace

Symfony\Component\HttpKernel\DataCollector

Code

public function collect(Request $request, Response $response, \Exception $exception = null) {
  $responseHeaders = $response->headers
    ->all();
  $cookies = array();
  foreach ($response->headers
    ->getCookies() as $cookie) {
    $cookies[] = $this
      ->getCookieHeader($cookie
      ->getName(), $cookie
      ->getValue(), $cookie
      ->getExpiresTime(), $cookie
      ->getPath(), $cookie
      ->getDomain(), $cookie
      ->isSecure(), $cookie
      ->isHttpOnly());
  }
  if (count($cookies) > 0) {
    $responseHeaders['Set-Cookie'] = $cookies;
  }

  // attributes are serialized and as they can be anything, they need to be converted to strings.
  $attributes = array();
  foreach ($request->attributes
    ->all() as $key => $value) {
    if ('_route' === $key && is_object($value)) {
      $attributes[$key] = $this
        ->varToString($value
        ->getPath());
    }
    elseif ('_route_params' === $key) {

      // we need to keep route params as an array (see getRouteParams())
      foreach ($value as $k => $v) {
        $value[$k] = $this
          ->varToString($v);
      }
      $attributes[$key] = $value;
    }
    else {
      $attributes[$key] = $this
        ->varToString($value);
    }
  }
  $content = null;
  try {
    $content = $request
      ->getContent();
  } catch (\LogicException $e) {

    // the user already got the request content as a resource
    $content = false;
  }
  $sessionMetadata = array();
  $sessionAttributes = array();
  $flashes = array();
  if ($request
    ->hasSession()) {
    $session = $request
      ->getSession();
    if ($session
      ->isStarted()) {
      $sessionMetadata['Created'] = date(DATE_RFC822, $session
        ->getMetadataBag()
        ->getCreated());
      $sessionMetadata['Last used'] = date(DATE_RFC822, $session
        ->getMetadataBag()
        ->getLastUsed());
      $sessionMetadata['Lifetime'] = $session
        ->getMetadataBag()
        ->getLifetime();
      $sessionAttributes = $session
        ->all();
      $flashes = $session
        ->getFlashBag()
        ->peekAll();
    }
  }
  $statusCode = $response
    ->getStatusCode();
  $this->data = array(
    'format' => $request
      ->getRequestFormat(),
    'content' => $content,
    'content_type' => $response->headers
      ->get('Content-Type', 'text/html'),
    'status_text' => isset(Response::$statusTexts[$statusCode]) ? Response::$statusTexts[$statusCode] : '',
    'status_code' => $statusCode,
    'request_query' => $request->query
      ->all(),
    'request_request' => $request->request
      ->all(),
    'request_headers' => $request->headers
      ->all(),
    'request_server' => $request->server
      ->all(),
    'request_cookies' => $request->cookies
      ->all(),
    'request_attributes' => $attributes,
    'response_headers' => $responseHeaders,
    'session_metadata' => $sessionMetadata,
    'session_attributes' => $sessionAttributes,
    'flashes' => $flashes,
    'path_info' => $request
      ->getPathInfo(),
    'controller' => 'n/a',
    'locale' => $request
      ->getLocale(),
  );
  if (isset($this->data['request_headers']['php-auth-pw'])) {
    $this->data['request_headers']['php-auth-pw'] = '******';
  }
  if (isset($this->data['request_server']['PHP_AUTH_PW'])) {
    $this->data['request_server']['PHP_AUTH_PW'] = '******';
  }
  if (isset($this->data['request_request']['_password'])) {
    $this->data['request_request']['_password'] = '******';
  }
  if (isset($this->controllers[$request])) {
    $controller = $this->controllers[$request];
    if (is_array($controller)) {
      try {
        $r = new \ReflectionMethod($controller[0], $controller[1]);
        $this->data['controller'] = array(
          'class' => is_object($controller[0]) ? get_class($controller[0]) : $controller[0],
          'method' => $controller[1],
          'file' => $r
            ->getFileName(),
          'line' => $r
            ->getStartLine(),
        );
      } catch (\ReflectionException $e) {
        if (is_callable($controller)) {

          // using __call or  __callStatic
          $this->data['controller'] = array(
            'class' => is_object($controller[0]) ? get_class($controller[0]) : $controller[0],
            'method' => $controller[1],
            'file' => 'n/a',
            'line' => 'n/a',
          );
        }
      }
    }
    elseif ($controller instanceof \Closure) {
      $r = new \ReflectionFunction($controller);
      $this->data['controller'] = array(
        'class' => $r
          ->getName(),
        'method' => null,
        'file' => $r
          ->getFileName(),
        'line' => $r
          ->getStartLine(),
      );
    }
    elseif (is_object($controller)) {
      $r = new \ReflectionClass($controller);
      $this->data['controller'] = array(
        'class' => $r
          ->getName(),
        'method' => null,
        'file' => $r
          ->getFileName(),
        'line' => $r
          ->getStartLine(),
      );
    }
    else {
      $this->data['controller'] = (string) $controller ?: 'n/a';
    }
    unset($this->controllers[$request]);
  }
}