Version 1
[yaffs-website] / web / modules / contrib / devel / webprofiler / src / Profiler / DatabaseProfilerStorage.php
1 <?php
2
3 namespace Drupal\webprofiler\Profiler;
4
5 use Drupal\Core\Database\Connection;
6 use Symfony\Component\HttpKernel\Profiler\Profile;
7 use Symfony\Component\HttpKernel\Profiler\ProfilerStorageInterface;
8
9 /**
10  * Implements a profiler storage using the DBTNG query api.
11  */
12 class DatabaseProfilerStorage implements ProfilerStorageInterface {
13
14   /**
15    * The database connection.
16    *
17    * @var \Drupal\Core\Database\Connection
18    */
19   protected $database;
20
21   /**
22    * Constructs a new DatabaseProfilerStorage instance.
23    *
24    * @param \Drupal\Core\Database\Connection $database
25    *   The database connection.
26    */
27   function __construct(Connection $database) {
28     $this->database = $database;
29   }
30
31   /**
32    * {@inheritdoc}
33    */
34   public function find($ip, $url, $limit, $method, $start = NULL, $end = NULL) {
35     $select = $this->database->select('webprofiler', 'wp', ['fetch' => \PDO::FETCH_ASSOC]);
36
37     if (NULL === $start) {
38       $start = 0;
39     }
40
41     if (NULL === $end) {
42       $end = time();
43     }
44
45     if ($ip = preg_replace('/[^\d\.]/', '', $ip)) {
46       $select->condition('ip', '%' . $this->database->escapeLike($ip) . '%', 'LIKE');
47     }
48
49     if ($url) {
50       $select->condition('url', '%' . $this->database->escapeLike(addcslashes($url, '%_\\')) . '%', 'LIKE');
51     }
52
53     if ($method) {
54       $select->condition('method', $method);
55     }
56
57     if (!empty($start)) {
58       $select->condition('time', $start, '>=');
59     }
60
61     if (!empty($end)) {
62       $select->condition('time', $end, '<=');
63     }
64
65     $select->fields('wp', [
66       'token',
67       'ip',
68       'method',
69       'url',
70       'time',
71       'parent',
72       'status_code'
73     ]);
74     $select->orderBy('time', 'DESC');
75     $select->range(0, $limit);
76     return $select->execute()
77       ->fetchAllAssoc('token');
78   }
79
80   /**
81    * {@inheritdoc}
82    */
83   public function read($token) {
84     $record = $this->database->select('webprofiler', 'w')
85       ->fields('w')
86       ->condition('token', $token)
87       ->execute()
88       ->fetch();
89     if (isset($record->data)) {
90       return $this->createProfileFromData($token, $record);
91     }
92   }
93
94   /**
95    * {@inheritdoc}
96    */
97   public function write(Profile $profile) {
98     $args = [
99       'token' => $profile->getToken(),
100       'parent' => $profile->getParentToken(),
101       'data' => base64_encode(serialize($profile->getCollectors())),
102       'ip' => $profile->getIp(),
103       'method' => $profile->getMethod(),
104       'url' => $profile->getUrl(),
105       'time' => $profile->getTime(),
106       'created_at' => time(),
107       'status_code' => $profile->getStatusCode(),
108     ];
109
110     try {
111       $query = $this->database->select('webprofiler', 'w')
112         ->fields('w', ['token']);
113       $query->condition('token', $profile->getToken());
114       $count = $query->countQuery()->execute()->fetchAssoc();
115
116       if ($count['expression']) {
117         $this->database->update('webprofiler')
118           ->fields($args)
119           ->condition('token', $profile->getToken())
120           ->execute();
121       }
122       else {
123         $this->database->insert('webprofiler')->fields($args)->execute();
124       }
125
126       $status = TRUE;
127     } catch (\Exception $e) {
128       $status = FALSE;
129     }
130
131     return $status;
132   }
133
134   /**
135    * {@inheritdoc}
136    */
137   public function purge() {
138     $this->database->truncate('webprofiler')->execute();
139   }
140
141   /**
142    * @param string $token
143    * @param $data
144    *
145    * @return Profile
146    */
147   private function createProfileFromData($token, $data) {
148     $profile = new Profile($token);
149     $profile->setIp($data->ip);
150     $profile->setMethod($data->method);
151     $profile->setUrl($data->url);
152     $profile->setTime($data->time);
153     $profile->setCollectors(unserialize(base64_decode($data->data)));
154     $profile->setStatusCode($data->status_code);
155
156     return $profile;
157   }
158 }