class DatabaseProfilerStorage in Devel 4.x
Same name and namespace in other branches
- 8.3 webprofiler/src/Profiler/DatabaseProfilerStorage.php \Drupal\webprofiler\Profiler\DatabaseProfilerStorage
- 8 webprofiler/src/Profiler/DatabaseProfilerStorage.php \Drupal\webprofiler\Profiler\DatabaseProfilerStorage
- 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
File
- webprofiler/
src/ Profiler/ DatabaseProfilerStorage.php, line 12
Namespace
Drupal\webprofiler\ProfilerView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DatabaseProfilerStorage:: |
protected | property | The database connection. | |
DatabaseProfilerStorage:: |
private | function | ||
DatabaseProfilerStorage:: |
public | function | ||
DatabaseProfilerStorage:: |
public | function | ||
DatabaseProfilerStorage:: |
public | function | ||
DatabaseProfilerStorage:: |
public | function | ||
DatabaseProfilerStorage:: |
public | function | Constructs a new DatabaseProfilerStorage instance. |