Drupal investigation

BaseMemcacheProfilerStorage.php 8.0KB

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