You are here

public function DatabaseProfilerStorage::write in Devel 8

Same name and namespace in other branches
  1. 8.3 webprofiler/src/Profiler/DatabaseProfilerStorage.php \Drupal\webprofiler\Profiler\DatabaseProfilerStorage::write()
  2. 8.2 webprofiler/src/Profiler/DatabaseProfilerStorage.php \Drupal\webprofiler\Profiler\DatabaseProfilerStorage::write()
  3. 4.x webprofiler/src/Profiler/DatabaseProfilerStorage.php \Drupal\webprofiler\Profiler\DatabaseProfilerStorage::write()

File

webprofiler/src/Profiler/DatabaseProfilerStorage.php, line 97

Class

DatabaseProfilerStorage
Implements a profiler storage using the DBTNG query api.

Namespace

Drupal\webprofiler\Profiler

Code

public function write(Profile $profile) {
  $args = [
    'token' => $profile
      ->getToken(),
    'parent' => $profile
      ->getParentToken(),
    'data' => base64_encode(serialize($profile
      ->getCollectors())),
    'ip' => $profile
      ->getIp(),
    'method' => $profile
      ->getMethod(),
    'url' => $profile
      ->getUrl(),
    'time' => $profile
      ->getTime(),
    'created_at' => time(),
    'status_code' => $profile
      ->getStatusCode(),
  ];
  try {
    $query = $this->database
      ->select('webprofiler', 'w')
      ->fields('w', [
      'token',
    ]);
    $query
      ->condition('token', $profile
      ->getToken());
    $count = $query
      ->countQuery()
      ->execute()
      ->fetchAssoc();
    if ($count['expression']) {
      $this->database
        ->update('webprofiler')
        ->fields($args)
        ->condition('token', $profile
        ->getToken())
        ->execute();
    }
    else {
      $this->database
        ->insert('webprofiler')
        ->fields($args)
        ->execute();
    }
    $status = TRUE;
  } catch (\Exception $e) {
    $status = FALSE;
  }
  return $status;
}