PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/backend-blogtamsu/vendor/maximebf/debugbar/src/DebugBar/DebugBar.php

https://gitlab.com/ntphuc/FoodyBackend
PHP | 468 lines | 251 code | 57 blank | 160 comment | 26 complexity | 2f2e2c6348369f082aa4969a5784cfb5 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the DebugBar package.
  4. *
  5. * (c) 2013 Maxime Bouroumeau-Fuseau
  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 DebugBar;
  11. use ArrayAccess;
  12. use DebugBar\DataCollector\DataCollectorInterface;
  13. use DebugBar\Storage\StorageInterface;
  14. /**
  15. * Main DebugBar object
  16. *
  17. * Manages data collectors. DebugBar provides an array-like access
  18. * to collectors by name.
  19. *
  20. * <code>
  21. * $debugbar = new DebugBar();
  22. * $debugbar->addCollector(new DataCollector\MessagesCollector());
  23. * $debugbar['messages']->addMessage("foobar");
  24. * </code>
  25. */
  26. class DebugBar implements ArrayAccess
  27. {
  28. public static $useOpenHandlerWhenSendingDataHeaders = false;
  29. protected $collectors = array();
  30. protected $data;
  31. protected $jsRenderer;
  32. protected $requestIdGenerator;
  33. protected $requestId;
  34. protected $storage;
  35. protected $httpDriver;
  36. protected $stackSessionNamespace = 'PHPDEBUGBAR_STACK_DATA';
  37. protected $stackAlwaysUseSessionStorage = false;
  38. /**
  39. * Adds a data collector
  40. *
  41. * @param DataCollectorInterface $collector
  42. *
  43. * @throws DebugBarException
  44. * @return $this
  45. */
  46. public function addCollector(DataCollectorInterface $collector)
  47. {
  48. if ($collector->getName() === '__meta') {
  49. throw new DebugBarException("'__meta' is a reserved name and cannot be used as a collector name");
  50. }
  51. if (isset($this->collectors[$collector->getName()])) {
  52. throw new DebugBarException("'{$collector->getName()}' is already a registered collector");
  53. }
  54. $this->collectors[$collector->getName()] = $collector;
  55. return $this;
  56. }
  57. /**
  58. * Checks if a data collector has been added
  59. *
  60. * @param string $name
  61. * @return boolean
  62. */
  63. public function hasCollector($name)
  64. {
  65. return isset($this->collectors[$name]);
  66. }
  67. /**
  68. * Returns a data collector
  69. *
  70. * @param string $name
  71. * @return DataCollectorInterface
  72. */
  73. public function getCollector($name)
  74. {
  75. if (!isset($this->collectors[$name])) {
  76. throw new DebugBarException("'$name' is not a registered collector");
  77. }
  78. return $this->collectors[$name];
  79. }
  80. /**
  81. * Returns an array of all data collectors
  82. *
  83. * @return array[DataCollectorInterface]
  84. */
  85. public function getCollectors()
  86. {
  87. return $this->collectors;
  88. }
  89. /**
  90. * Sets the request id generator
  91. *
  92. * @param RequestIdGeneratorInterface $generator
  93. */
  94. public function setRequestIdGenerator(RequestIdGeneratorInterface $generator)
  95. {
  96. $this->requestIdGenerator = $generator;
  97. return $this;
  98. }
  99. /**
  100. * @return RequestIdGeneratorInterface
  101. */
  102. public function getRequestIdGenerator()
  103. {
  104. if ($this->requestIdGenerator === null) {
  105. $this->requestIdGenerator = new RequestIdGenerator();
  106. }
  107. return $this->requestIdGenerator;
  108. }
  109. /**
  110. * Returns the id of the current request
  111. *
  112. * @return string
  113. */
  114. public function getCurrentRequestId()
  115. {
  116. if ($this->requestId === null) {
  117. $this->requestId = $this->getRequestIdGenerator()->generate();
  118. }
  119. return $this->requestId;
  120. }
  121. /**
  122. * Sets the storage backend to use to store the collected data
  123. *
  124. * @param StorageInterface $storage
  125. */
  126. public function setStorage(StorageInterface $storage = null)
  127. {
  128. $this->storage = $storage;
  129. return $this;
  130. }
  131. /**
  132. * @return StorageInterface
  133. */
  134. public function getStorage()
  135. {
  136. return $this->storage;
  137. }
  138. /**
  139. * Checks if the data will be persisted
  140. *
  141. * @return boolean
  142. */
  143. public function isDataPersisted()
  144. {
  145. return $this->storage !== null;
  146. }
  147. /**
  148. * Sets the HTTP driver
  149. *
  150. * @param HttpDriverInterface $driver
  151. */
  152. public function setHttpDriver(HttpDriverInterface $driver)
  153. {
  154. $this->httpDriver = $driver;
  155. return $this;
  156. }
  157. /**
  158. * Returns the HTTP driver
  159. *
  160. * If no http driver where defined, a PhpHttpDriver is automatically created
  161. *
  162. * @return HttpDriverInterface
  163. */
  164. public function getHttpDriver()
  165. {
  166. if ($this->httpDriver === null) {
  167. $this->httpDriver = new PhpHttpDriver();
  168. }
  169. return $this->httpDriver;
  170. }
  171. /**
  172. * Collects the data from the collectors
  173. *
  174. * @return array
  175. */
  176. public function collect()
  177. {
  178. $this->data = array(
  179. '__meta' => array(
  180. 'id' => $this->getCurrentRequestId(),
  181. 'datetime' => date('Y-m-d H:i:s'),
  182. 'utime' => microtime(true),
  183. 'method' => isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : null,
  184. 'uri' => isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : null,
  185. 'ip' => isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null
  186. )
  187. );
  188. foreach ($this->collectors as $name => $collector) {
  189. $this->data[$name] = $collector->collect();
  190. }
  191. // Remove all invalid (non UTF-8) characters
  192. array_walk_recursive($this->data, function (&$item) {
  193. if (is_string($item) && !mb_check_encoding($item, 'UTF-8')) {
  194. $item = mb_convert_encoding($item, 'UTF-8', 'UTF-8');
  195. }
  196. });
  197. if ($this->storage !== null) {
  198. $this->storage->save($this->getCurrentRequestId(), $this->data);
  199. }
  200. return $this->data;
  201. }
  202. /**
  203. * Returns collected data
  204. *
  205. * Will collect the data if none have been collected yet
  206. *
  207. * @return array
  208. */
  209. public function getData()
  210. {
  211. if ($this->data === null) {
  212. $this->collect();
  213. }
  214. return $this->data;
  215. }
  216. /**
  217. * Returns an array of HTTP headers containing the data
  218. *
  219. * @param string $headerName
  220. * @param integer $maxHeaderLength
  221. * @return array
  222. */
  223. public function getDataAsHeaders($headerName = 'phpdebugbar', $maxHeaderLength = 4096, $maxTotalHeaderLength = 250000)
  224. {
  225. $data = rawurlencode(json_encode(array(
  226. 'id' => $this->getCurrentRequestId(),
  227. 'data' => $this->getData()
  228. )));
  229. if (strlen($data) > $maxTotalHeaderLength) {
  230. $data = rawurlencode(json_encode(array(
  231. 'error' => 'Maximum header size exceeded'
  232. )));
  233. }
  234. $chunks = array();
  235. while (strlen($data) > $maxHeaderLength) {
  236. $chunks[] = substr($data, 0, $maxHeaderLength);
  237. $data = substr($data, $maxHeaderLength);
  238. }
  239. $chunks[] = $data;
  240. $headers = array();
  241. for ($i = 0, $c = count($chunks); $i < $c; $i++) {
  242. $name = $headerName . ($i > 0 ? "-$i" : '');
  243. $headers[$name] = $chunks[$i];
  244. }
  245. return $headers;
  246. }
  247. /**
  248. * Sends the data through the HTTP headers
  249. *
  250. * @param bool $useOpenHandler
  251. * @param string $headerName
  252. * @param integer $maxHeaderLength
  253. */
  254. public function sendDataInHeaders($useOpenHandler = null, $headerName = 'phpdebugbar', $maxHeaderLength = 4096)
  255. {
  256. if ($useOpenHandler === null) {
  257. $useOpenHandler = self::$useOpenHandlerWhenSendingDataHeaders;
  258. }
  259. if ($useOpenHandler && $this->storage !== null) {
  260. $this->getData();
  261. $headerName .= '-id';
  262. $headers = array($headerName => $this->getCurrentRequestId());
  263. } else {
  264. $headers = $this->getDataAsHeaders($headerName, $maxHeaderLength);
  265. }
  266. $this->getHttpDriver()->setHeaders($headers);
  267. return $this;
  268. }
  269. /**
  270. * Stacks the data in the session for later rendering
  271. */
  272. public function stackData()
  273. {
  274. $http = $this->initStackSession();
  275. $data = null;
  276. if (!$this->isDataPersisted() || $this->stackAlwaysUseSessionStorage) {
  277. $data = $this->getData();
  278. } elseif ($this->data === null) {
  279. $this->collect();
  280. }
  281. $stack = $http->getSessionValue($this->stackSessionNamespace);
  282. $stack[$this->getCurrentRequestId()] = $data;
  283. $http->setSessionValue($this->stackSessionNamespace, $stack);
  284. return $this;
  285. }
  286. /**
  287. * Checks if there is stacked data in the session
  288. *
  289. * @return boolean
  290. */
  291. public function hasStackedData()
  292. {
  293. try {
  294. $http = $this->initStackSession();
  295. } catch (DebugBarException $e) {
  296. return false;
  297. }
  298. return count($http->getSessionValue($this->stackSessionNamespace)) > 0;
  299. }
  300. /**
  301. * Returns the data stacked in the session
  302. *
  303. * @param boolean $delete Whether to delete the data in the session
  304. * @return array
  305. */
  306. public function getStackedData($delete = true)
  307. {
  308. $http = $this->initStackSession();
  309. $stackedData = $http->getSessionValue($this->stackSessionNamespace);
  310. if ($delete) {
  311. $http->deleteSessionValue($this->stackSessionNamespace);
  312. }
  313. $datasets = array();
  314. if ($this->isDataPersisted() && !$this->stackAlwaysUseSessionStorage) {
  315. foreach ($stackedData as $id => $data) {
  316. $datasets[$id] = $this->getStorage()->get($id);
  317. }
  318. } else {
  319. $datasets = $stackedData;
  320. }
  321. return $datasets;
  322. }
  323. /**
  324. * Sets the key to use in the $_SESSION array
  325. *
  326. * @param string $ns
  327. */
  328. public function setStackDataSessionNamespace($ns)
  329. {
  330. $this->stackSessionNamespace = $ns;
  331. return $this;
  332. }
  333. /**
  334. * Returns the key used in the $_SESSION array
  335. *
  336. * @return string
  337. */
  338. public function getStackDataSessionNamespace()
  339. {
  340. return $this->stackSessionNamespace;
  341. }
  342. /**
  343. * Sets whether to only use the session to store stacked data even
  344. * if a storage is enabled
  345. *
  346. * @param boolean $enabled
  347. */
  348. public function setStackAlwaysUseSessionStorage($enabled = true)
  349. {
  350. $this->stackAlwaysUseSessionStorage = $enabled;
  351. return $this;
  352. }
  353. /**
  354. * Checks if the session is always used to store stacked data
  355. * even if a storage is enabled
  356. *
  357. * @return boolean
  358. */
  359. public function isStackAlwaysUseSessionStorage()
  360. {
  361. return $this->stackAlwaysUseSessionStorage;
  362. }
  363. /**
  364. * Initializes the session for stacked data
  365. *
  366. * @return HttpDriverInterface
  367. */
  368. protected function initStackSession()
  369. {
  370. $http = $this->getHttpDriver();
  371. if (!$http->isSessionStarted()) {
  372. throw new DebugBarException("Session must be started before using stack data in the debug bar");
  373. }
  374. if (!$http->hasSessionValue($this->stackSessionNamespace)) {
  375. $http->setSessionValue($this->stackSessionNamespace, array());
  376. }
  377. return $http;
  378. }
  379. /**
  380. * Returns a JavascriptRenderer for this instance
  381. *
  382. * @param string $baseUrl
  383. * @param string $basePathng
  384. * @return JavascriptRenderer
  385. */
  386. public function getJavascriptRenderer($baseUrl = null, $basePath = null)
  387. {
  388. if ($this->jsRenderer === null) {
  389. $this->jsRenderer = new JavascriptRenderer($this, $baseUrl, $basePath);
  390. }
  391. return $this->jsRenderer;
  392. }
  393. // --------------------------------------------
  394. // ArrayAccess implementation
  395. public function offsetSet($key, $value)
  396. {
  397. throw new DebugBarException("DebugBar[] is read-only");
  398. }
  399. public function offsetGet($key)
  400. {
  401. return $this->getCollector($key);
  402. }
  403. public function offsetExists($key)
  404. {
  405. return $this->hasCollector($key);
  406. }
  407. public function offsetUnset($key)
  408. {
  409. throw new DebugBarException("DebugBar[] is read-only");
  410. }
  411. }