BaseMemcacheProfilerStorage.php 7.9 KB

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