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