PdoProfilerStorage.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel\Profiler;
  11. @trigger_error('The '.__NAMESPACE__.'\PdoProfilerStorage class is deprecated since Symfony 2.8 and will be removed in 3.0. Use FileProfilerStorage instead.', E_USER_DEPRECATED);
  12. /**
  13. * Base PDO storage for profiling information in a PDO database.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. * @author Jan Schumann <js@schumann-it.com>
  17. *
  18. * @deprecated Deprecated since Symfony 2.8, to be removed in Symfony 3.0.
  19. * Use {@link FileProfilerStorage} instead.
  20. */
  21. abstract class PdoProfilerStorage implements ProfilerStorageInterface
  22. {
  23. protected $dsn;
  24. protected $username;
  25. protected $password;
  26. protected $lifetime;
  27. protected $db;
  28. /**
  29. * @param string $dsn A data source name
  30. * @param string $username The username for the database
  31. * @param string $password The password for the database
  32. * @param int $lifetime The lifetime to use for the purge
  33. */
  34. public function __construct($dsn, $username = '', $password = '', $lifetime = 86400)
  35. {
  36. $this->dsn = $dsn;
  37. $this->username = $username;
  38. $this->password = $password;
  39. $this->lifetime = (int) $lifetime;
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function find($ip, $url, $limit, $method, $start = null, $end = null)
  45. {
  46. if (null === $start) {
  47. $start = 0;
  48. }
  49. if (null === $end) {
  50. $end = time();
  51. }
  52. list($criteria, $args) = $this->buildCriteria($ip, $url, $start, $end, $limit, $method);
  53. $criteria = $criteria ? 'WHERE '.implode(' AND ', $criteria) : '';
  54. $db = $this->initDb();
  55. $tokens = $this->fetch($db, 'SELECT token, ip, method, url, time, parent, status_code FROM sf_profiler_data '.$criteria.' ORDER BY time DESC LIMIT '.((int) $limit), $args);
  56. $this->close($db);
  57. return $tokens;
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function read($token)
  63. {
  64. $db = $this->initDb();
  65. $args = array(':token' => $token);
  66. $data = $this->fetch($db, 'SELECT data, parent, ip, method, url, time FROM sf_profiler_data WHERE token = :token LIMIT 1', $args);
  67. $this->close($db);
  68. if (isset($data[0]['data'])) {
  69. return $this->createProfileFromData($token, $data[0]);
  70. }
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function write(Profile $profile)
  76. {
  77. $db = $this->initDb();
  78. $args = array(
  79. ':token' => $profile->getToken(),
  80. ':parent' => $profile->getParentToken(),
  81. ':data' => base64_encode(serialize($profile->getCollectors())),
  82. ':ip' => $profile->getIp(),
  83. ':method' => $profile->getMethod(),
  84. ':url' => $profile->getUrl(),
  85. ':time' => $profile->getTime(),
  86. ':created_at' => time(),
  87. ':status_code' => $profile->getStatusCode(),
  88. );
  89. try {
  90. if ($this->has($profile->getToken())) {
  91. $this->exec($db, 'UPDATE sf_profiler_data SET parent = :parent, data = :data, ip = :ip, method = :method, url = :url, time = :time, created_at = :created_at, status_code = :status_code WHERE token = :token', $args);
  92. } else {
  93. $this->exec($db, 'INSERT INTO sf_profiler_data (token, parent, data, ip, method, url, time, created_at, status_code) VALUES (:token, :parent, :data, :ip, :method, :url, :time, :created_at, :status_code)', $args);
  94. }
  95. $this->cleanup();
  96. $status = true;
  97. } catch (\Exception $e) {
  98. $status = false;
  99. }
  100. $this->close($db);
  101. return $status;
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. public function purge()
  107. {
  108. $db = $this->initDb();
  109. $this->exec($db, 'DELETE FROM sf_profiler_data');
  110. $this->close($db);
  111. }
  112. /**
  113. * Build SQL criteria to fetch records by ip and url.
  114. *
  115. * @param string $ip The IP
  116. * @param string $url The URL
  117. * @param string $start The start period to search from
  118. * @param string $end The end period to search to
  119. * @param string $limit The maximum number of tokens to return
  120. * @param string $method The request method
  121. *
  122. * @return array An array with (criteria, args)
  123. */
  124. abstract protected function buildCriteria($ip, $url, $start, $end, $limit, $method);
  125. /**
  126. * Initializes the database.
  127. *
  128. * @throws \RuntimeException When the requested database driver is not installed
  129. */
  130. abstract protected function initDb();
  131. protected function cleanup()
  132. {
  133. $db = $this->initDb();
  134. $this->exec($db, 'DELETE FROM sf_profiler_data WHERE created_at < :time', array(':time' => time() - $this->lifetime));
  135. $this->close($db);
  136. }
  137. protected function exec($db, $query, array $args = array())
  138. {
  139. $stmt = $this->prepareStatement($db, $query);
  140. foreach ($args as $arg => $val) {
  141. $stmt->bindValue($arg, $val, \is_int($val) ? \PDO::PARAM_INT : \PDO::PARAM_STR);
  142. }
  143. $success = $stmt->execute();
  144. if (!$success) {
  145. throw new \RuntimeException(sprintf('Error executing query "%s"', $query));
  146. }
  147. }
  148. protected function prepareStatement($db, $query)
  149. {
  150. try {
  151. $stmt = $db->prepare($query);
  152. } catch (\Exception $e) {
  153. $stmt = false;
  154. }
  155. if (false === $stmt) {
  156. throw new \RuntimeException('The database cannot successfully prepare the statement');
  157. }
  158. return $stmt;
  159. }
  160. protected function fetch($db, $query, array $args = array())
  161. {
  162. $stmt = $this->prepareStatement($db, $query);
  163. foreach ($args as $arg => $val) {
  164. $stmt->bindValue($arg, $val, \is_int($val) ? \PDO::PARAM_INT : \PDO::PARAM_STR);
  165. }
  166. $stmt->execute();
  167. return $stmt->fetchAll(\PDO::FETCH_ASSOC);
  168. }
  169. protected function close($db)
  170. {
  171. }
  172. protected function createProfileFromData($token, $data, $parent = null)
  173. {
  174. $profile = new Profile($token);
  175. $profile->setIp($data['ip']);
  176. $profile->setMethod($data['method']);
  177. $profile->setUrl($data['url']);
  178. $profile->setTime($data['time']);
  179. $profile->setCollectors(unserialize(base64_decode($data['data'])));
  180. if (!$parent && !empty($data['parent'])) {
  181. $parent = $this->read($data['parent']);
  182. }
  183. if ($parent) {
  184. $profile->setParent($parent);
  185. }
  186. $profile->setChildren($this->readChildren($token, $profile));
  187. return $profile;
  188. }
  189. /**
  190. * Reads the child profiles for the given token.
  191. *
  192. * @param string $token The parent token
  193. * @param string $parent The parent instance
  194. *
  195. * @return Profile[] An array of Profile instance
  196. */
  197. protected function readChildren($token, $parent)
  198. {
  199. $db = $this->initDb();
  200. $data = $this->fetch($db, 'SELECT token, data, ip, method, url, time FROM sf_profiler_data WHERE parent = :token', array(':token' => $token));
  201. $this->close($db);
  202. if (!$data) {
  203. return array();
  204. }
  205. $profiles = array();
  206. foreach ($data as $d) {
  207. $profiles[] = $this->createProfileFromData($d['token'], $d, $parent);
  208. }
  209. return $profiles;
  210. }
  211. /**
  212. * Returns whether data for the given token already exists in storage.
  213. *
  214. * @param string $token The profile token
  215. *
  216. * @return string
  217. */
  218. protected function has($token)
  219. {
  220. $db = $this->initDb();
  221. $tokenExists = $this->fetch($db, 'SELECT 1 FROM sf_profiler_data WHERE token = :token LIMIT 1', array(':token' => $token));
  222. $this->close($db);
  223. return !empty($tokenExists);
  224. }
  225. }