You are here

class DatabaseProfilerStorage in Devel 4.x

Same name and namespace in other branches
  1. 8.3 webprofiler/src/Profiler/DatabaseProfilerStorage.php \Drupal\webprofiler\Profiler\DatabaseProfilerStorage
  2. 8 webprofiler/src/Profiler/DatabaseProfilerStorage.php \Drupal\webprofiler\Profiler\DatabaseProfilerStorage
  3. 8.2 webprofiler/src/Profiler/DatabaseProfilerStorage.php \Drupal\webprofiler\Profiler\DatabaseProfilerStorage

Implements a profiler storage using the DBTNG query api.

Hierarchy

  • class \Drupal\webprofiler\Profiler\DatabaseProfilerStorage implements \Symfony\Component\HttpKernel\Profiler\ProfilerStorageInterface

Expanded class hierarchy of DatabaseProfilerStorage

1 string reference to 'DatabaseProfilerStorage'
webprofiler.services.yml in webprofiler/webprofiler.services.yml
webprofiler/webprofiler.services.yml
1 service uses DatabaseProfilerStorage
profiler.database_storage in webprofiler/webprofiler.services.yml
Drupal\webprofiler\Profiler\DatabaseProfilerStorage

File

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

Namespace

Drupal\webprofiler\Profiler
View source
class DatabaseProfilerStorage implements ProfilerStorageInterface {

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * Constructs a new DatabaseProfilerStorage instance.
   *
   * @param \Drupal\Core\Database\Connection $database
   *   The database connection.
   */
  public function __construct(Connection $database) {
    $this->database = $database;
  }

  /**
   * {@inheritdoc}
   */
  public function find($ip, $url, $limit, $method, $start = NULL, $end = NULL) : array {
    $select = $this->database
      ->select('webprofiler', 'wp', [
      'fetch' => \PDO::FETCH_ASSOC,
    ]);
    if (NULL === $start) {
      $start = 0;
    }
    if (NULL === $end) {
      $end = time();
    }
    if ($ip = preg_replace('/[^\\d\\.]/', '', $ip)) {
      $select
        ->condition('ip', '%' . $this->database
        ->escapeLike($ip) . '%', 'LIKE');
    }
    if ($url) {
      $select
        ->condition('url', '%' . $this->database
        ->escapeLike(addcslashes($url, '%_\\')) . '%', 'LIKE');
    }
    if ($method) {
      $select
        ->condition('method', $method);
    }
    if (!empty($start)) {
      $select
        ->condition('time', $start, '>=');
    }
    if (!empty($end)) {
      $select
        ->condition('time', $end, '<=');
    }
    $select
      ->fields('wp', [
      'token',
      'ip',
      'method',
      'url',
      'time',
      'parent',
      'status_code',
    ]);
    $select
      ->orderBy('time', 'DESC');
    $select
      ->range(0, $limit);
    return $select
      ->execute()
      ->fetchAllAssoc('token');
  }

  /**
   * {@inheritdoc}
   */
  public function read($token) : ?Profile {
    $record = $this->database
      ->select('webprofiler', 'w')
      ->fields('w')
      ->condition('token', $token)
      ->execute()
      ->fetch();
    if (isset($record->data)) {
      return $this
        ->createProfileFromData($token, $record);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function write(Profile $profile) : bool {
    $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;
  }

  /**
   * {@inheritdoc}
   */
  public function purge() {
    $this->database
      ->truncate('webprofiler')
      ->execute();
  }

  /**
   * @param string $token
   * @param $data
   *
   * @return \Symfony\Component\HttpKernel\Profiler\Profile
   */
  private function createProfileFromData($token, $data) {
    $profile = new Profile($token);
    $profile
      ->setIp($data->ip);
    $profile
      ->setMethod($data->method);
    $profile
      ->setUrl($data->url);
    $profile
      ->setTime($data->time);
    $profile
      ->setCollectors(unserialize(base64_decode($data->data)));
    $profile
      ->setStatusCode($data->status_code);
    return $profile;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DatabaseProfilerStorage::$database protected property The database connection.
DatabaseProfilerStorage::createProfileFromData private function
DatabaseProfilerStorage::find public function
DatabaseProfilerStorage::purge public function
DatabaseProfilerStorage::read public function
DatabaseProfilerStorage::write public function
DatabaseProfilerStorage::__construct public function Constructs a new DatabaseProfilerStorage instance.