BaseMemcacheProfilerStorage.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. /**
  12. * Base Memcache storage for profiling information in a Memcache.
  13. *
  14. * @author Andrej Hudec <pulzarraider@gmail.com>
  15. */
  16. abstract class BaseMemcacheProfilerStorage implements ProfilerStorageInterface
  17. {
  18. const TOKEN_PREFIX = 'sf_profiler_';
  19. protected $dsn;
  20. protected $lifetime;
  21. /**
  22. * Constructor.
  23. *
  24. * @param string $dsn A data source name
  25. * @param string $username
  26. * @param string $password
  27. * @param int $lifetime The lifetime to use for the purge
  28. */
  29. public function __construct($dsn, $username = '', $password = '', $lifetime = 86400)
  30. {
  31. $this->dsn = $dsn;
  32. $this->lifetime = (int) $lifetime;
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function find($ip, $url, $limit, $method, $start = null, $end = null)
  38. {
  39. $indexName = $this->getIndexName();
  40. $indexContent = $this->getValue($indexName);
  41. if (!$indexContent) {
  42. return array();
  43. }
  44. $profileList = explode("\n", $indexContent);
  45. $result = array();
  46. foreach ($profileList as $item) {
  47. if ($limit === 0) {
  48. break;
  49. }
  50. if ($item=='') {
  51. continue;
  52. }
  53. list($itemToken, $itemIp, $itemMethod, $itemUrl, $itemTime, $itemParent) = explode("\t", $item, 6);
  54. $itemTime = (int) $itemTime;
  55. if ($ip && false === strpos($itemIp, $ip) || $url && false === strpos($itemUrl, $url) || $method && false === strpos($itemMethod, $method)) {
  56. continue;
  57. }
  58. if (!empty($start) && $itemTime < $start) {
  59. continue;
  60. }
  61. if (!empty($end) && $itemTime > $end) {
  62. continue;
  63. }
  64. $result[$itemToken] = array(
  65. 'token' => $itemToken,
  66. 'ip' => $itemIp,
  67. 'method' => $itemMethod,
  68. 'url' => $itemUrl,
  69. 'time' => $itemTime,
  70. 'parent' => $itemParent,
  71. );
  72. --$limit;
  73. }
  74. usort($result, function($a, $b) {
  75. if ($a['time'] === $b['time']) {
  76. return 0;
  77. }
  78. return $a['time'] > $b['time'] ? -1 : 1;
  79. });
  80. return $result;
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function purge()
  86. {
  87. // delete only items from index
  88. $indexName = $this->getIndexName();
  89. $indexContent = $this->getValue($indexName);
  90. if (!$indexContent) {
  91. return false;
  92. }
  93. $profileList = explode("\n", $indexContent);
  94. foreach ($profileList as $item) {
  95. if ($item == '') {
  96. continue;
  97. }
  98. if (false !== $pos = strpos($item, "\t")) {
  99. $this->delete($this->getItemName(substr($item, 0, $pos)));
  100. }
  101. }
  102. return $this->delete($indexName);
  103. }
  104. /**
  105. * {@inheritdoc}
  106. */
  107. public function read($token)
  108. {
  109. if (empty($token)) {
  110. return false;
  111. }
  112. $profile = $this->getValue($this->getItemName($token));
  113. if (false !== $profile) {
  114. $profile = $this->createProfileFromData($token, $profile);
  115. }
  116. return $profile;
  117. }
  118. /**
  119. * {@inheritdoc}
  120. */
  121. public function write(Profile $profile)
  122. {
  123. $data = array(
  124. 'token' => $profile->getToken(),
  125. 'parent' => $profile->getParentToken(),
  126. 'children' => array_map(function ($p) { return $p->getToken(); }, $profile->getChildren()),
  127. 'data' => $profile->getCollectors(),
  128. 'ip' => $profile->getIp(),
  129. 'method' => $profile->getMethod(),
  130. 'url' => $profile->getUrl(),
  131. 'time' => $profile->getTime(),
  132. );
  133. $profileIndexed = false !== $this->getValue($this->getItemName($profile->getToken()));
  134. if ($this->setValue($this->getItemName($profile->getToken()), $data, $this->lifetime)) {
  135. if (!$profileIndexed) {
  136. // Add to index
  137. $indexName = $this->getIndexName();
  138. $indexRow = implode("\t", array(
  139. $profile->getToken(),
  140. $profile->getIp(),
  141. $profile->getMethod(),
  142. $profile->getUrl(),
  143. $profile->getTime(),
  144. $profile->getParentToken(),
  145. ))."\n";
  146. return $this->appendValue($indexName, $indexRow, $this->lifetime);
  147. }
  148. return true;
  149. }
  150. return false;
  151. }
  152. /**
  153. * Retrieve item from the memcache server
  154. *
  155. * @param string $key
  156. *
  157. * @return mixed
  158. */
  159. abstract protected function getValue($key);
  160. /**
  161. * Store an item on the memcache server under the specified key
  162. *
  163. * @param string $key
  164. * @param mixed $value
  165. * @param int $expiration
  166. *
  167. * @return boolean
  168. */
  169. abstract protected function setValue($key, $value, $expiration = 0);
  170. /**
  171. * Delete item from the memcache server
  172. *
  173. * @param string $key
  174. *
  175. * @return boolean
  176. */
  177. abstract protected function delete($key);
  178. /**
  179. * Append data to an existing item on the memcache server
  180. * @param string $key
  181. * @param string $value
  182. * @param int $expiration
  183. *
  184. * @return boolean
  185. */
  186. abstract protected function appendValue($key, $value, $expiration = 0);
  187. private function createProfileFromData($token, $data, $parent = null)
  188. {
  189. $profile = new Profile($token);
  190. $profile->setIp($data['ip']);
  191. $profile->setMethod($data['method']);
  192. $profile->setUrl($data['url']);
  193. $profile->setTime($data['time']);
  194. $profile->setCollectors($data['data']);
  195. if (!$parent && $data['parent']) {
  196. $parent = $this->read($data['parent']);
  197. }
  198. if ($parent) {
  199. $profile->setParent($parent);
  200. }
  201. foreach ($data['children'] as $token) {
  202. if (!$token) {
  203. continue;
  204. }
  205. if (!$childProfileData = $this->getValue($this->getItemName($token))) {
  206. continue;
  207. }
  208. $profile->addChild($this->createProfileFromData($token, $childProfileData, $profile));
  209. }
  210. return $profile;
  211. }
  212. /**
  213. * Get item name
  214. *
  215. * @param string $token
  216. *
  217. * @return string
  218. */
  219. private function getItemName($token)
  220. {
  221. $name = self::TOKEN_PREFIX.$token;
  222. if ($this->isItemNameValid($name)) {
  223. return $name;
  224. }
  225. return false;
  226. }
  227. /**
  228. * Get name of index
  229. *
  230. * @return string
  231. */
  232. private function getIndexName()
  233. {
  234. $name = self::TOKEN_PREFIX.'index';
  235. if ($this->isItemNameValid($name)) {
  236. return $name;
  237. }
  238. return false;
  239. }
  240. private function isItemNameValid($name)
  241. {
  242. $length = strlen($name);
  243. if ($length > 250) {
  244. throw new \RuntimeException(sprintf('The memcache item key "%s" is too long (%s bytes). Allowed maximum size is 250 bytes.', $name, $length));
  245. }
  246. return true;
  247. }
  248. }