PageRenderTime 23ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/ntphuc/FoodyBackend
PHP | 307 lines | 198 code | 39 blank | 70 comment | 18 complexity | 7ef58f5ad950c971441c44f8b5c230d2 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\Bridge;
  11. use DebugBar\DataCollector\AssetProvider;
  12. use DebugBar\DataCollector\DataCollector;
  13. use DebugBar\DataCollector\Renderable;
  14. use Monolog\Handler\TestHandler;
  15. use Monolog\Logger;
  16. use Propel\Runtime\Connection\ConnectionInterface;
  17. use Propel\Runtime\Connection\ProfilerConnectionWrapper;
  18. use Propel\Runtime\Propel;
  19. use Psr\Log\LogLevel;
  20. use Psr\Log\LoggerInterface;
  21. /**
  22. * A Propel logger which acts as a data collector
  23. *
  24. * http://propelorm.org/
  25. *
  26. * Will log queries and display them using the SQLQueries widget.
  27. *
  28. * Example:
  29. * <code>
  30. * $debugbar->addCollector(new \DebugBar\Bridge\Propel2Collector(\Propel\Runtime\Propel::getServiceContainer()->getReadConnection()));
  31. * </code>
  32. */
  33. class Propel2Collector extends DataCollector implements Renderable, AssetProvider
  34. {
  35. /**
  36. * @var null|TestHandler
  37. */
  38. protected $handler = null;
  39. /**
  40. * @var null|Logger
  41. */
  42. protected $logger = null;
  43. /**
  44. * @var array
  45. */
  46. protected $config = array();
  47. /**
  48. * @var array
  49. */
  50. protected $errors = array();
  51. /**
  52. * @var int
  53. */
  54. protected $queryCount = 0;
  55. /**
  56. * @param ConnectionInterface $connection Propel connection
  57. */
  58. public function __construct(
  59. ConnectionInterface $connection,
  60. array $logMethods = array(
  61. 'beginTransaction',
  62. 'commit',
  63. 'rollBack',
  64. 'forceRollBack',
  65. 'exec',
  66. 'query',
  67. 'execute'
  68. )
  69. ) {
  70. if ($connection instanceof ProfilerConnectionWrapper) {
  71. $connection->setLogMethods($logMethods);
  72. $this->config = $connection->getProfiler()->getConfiguration();
  73. $this->handler = new TestHandler();
  74. if ($connection->getLogger() instanceof Logger) {
  75. $this->logger = $connection->getLogger();
  76. $this->logger->pushHandler($this->handler);
  77. } else {
  78. $this->errors[] = 'Supported only monolog logger';
  79. }
  80. } else {
  81. $this->errors[] = 'You need set ProfilerConnectionWrapper';
  82. }
  83. }
  84. /**
  85. * @return TestHandler|null
  86. */
  87. public function getHandler()
  88. {
  89. return $this->handler;
  90. }
  91. /**
  92. * @return array
  93. */
  94. public function getConfig()
  95. {
  96. return $this->config;
  97. }
  98. /**
  99. * @return Logger|null
  100. */
  101. public function getLogger()
  102. {
  103. return $this->logger;
  104. }
  105. /**
  106. * @return LoggerInterface
  107. */
  108. protected function getDefaultLogger()
  109. {
  110. return Propel::getServiceContainer()->getLogger();
  111. }
  112. /**
  113. * @return int
  114. */
  115. protected function getQueryCount()
  116. {
  117. return $this->queryCount;
  118. }
  119. /**
  120. * @param array $records
  121. * @param array $config
  122. * @return array
  123. */
  124. protected function getStatements($records, $config)
  125. {
  126. $statements = array();
  127. foreach ($records as $record) {
  128. $duration = null;
  129. $memory = null;
  130. $isSuccess = ( LogLevel::INFO === strtolower($record['level_name']) );
  131. $detailsCount = count($config['details']);
  132. $parameters = explode($config['outerGlue'], $record['message'], $detailsCount + 1);
  133. if (count($parameters) === ($detailsCount + 1)) {
  134. $parameters = array_map('trim', $parameters);
  135. $_details = array();
  136. foreach (array_splice($parameters, 0, $detailsCount) as $string) {
  137. list($key, $value) = array_map('trim', explode($config['innerGlue'], $string, 2));
  138. $_details[$key] = $value;
  139. }
  140. $details = array();
  141. foreach ($config['details'] as $key => $detail) {
  142. if (isset($_details[$detail['name']])) {
  143. $value = $_details[$detail['name']];
  144. if ('time' === $key) {
  145. if (substr_count($value, 'ms')) {
  146. $value = (float)$value / 1000;
  147. } else {
  148. $value = (float)$value;
  149. }
  150. } else {
  151. $suffixes = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
  152. $suffix = substr($value, -2);
  153. $i = array_search($suffix, $suffixes, true);
  154. $i = (false === $i) ? 0 : $i;
  155. $value = ((float)$value) * pow(1024, $i);
  156. }
  157. $details[$key] = $value;
  158. }
  159. }
  160. if (isset($details['time'])) {
  161. $duration = $details['time'];
  162. }
  163. if (isset($details['memDelta'])) {
  164. $memory = $details['memDelta'];
  165. }
  166. $message = end($parameters);
  167. if ($isSuccess) {
  168. $this->queryCount++;
  169. }
  170. } else {
  171. $message = $record['message'];
  172. }
  173. $statement = array(
  174. 'sql' => $message,
  175. 'is_success' => $isSuccess,
  176. 'duration' => $duration,
  177. 'duration_str' => $this->getDataFormatter()->formatDuration($duration),
  178. 'memory' => $memory,
  179. 'memory_str' => $this->getDataFormatter()->formatBytes($memory),
  180. );
  181. if (false === $isSuccess) {
  182. $statement['sql'] = '';
  183. $statement['error_code'] = $record['level'];
  184. $statement['error_message'] = $message;
  185. }
  186. $statements[] = $statement;
  187. }
  188. return $statements;
  189. }
  190. /**
  191. * @return array
  192. */
  193. public function collect()
  194. {
  195. if (count($this->errors)) {
  196. return array(
  197. 'statements' => array_map(function ($message) {
  198. return array('sql' => '', 'is_success' => false, 'error_code' => 500, 'error_message' => $message);
  199. }, $this->errors),
  200. 'nb_statements' => 0,
  201. 'nb_failed_statements' => count($this->errors),
  202. );
  203. }
  204. if ($this->getHandler() === null) {
  205. return array();
  206. }
  207. $statements = $this->getStatements($this->getHandler()->getRecords(), $this->getConfig());
  208. $failedStatement = count(array_filter($statements, function ($statement) {
  209. return false === $statement['is_success'];
  210. }));
  211. $accumulatedDuration = array_reduce($statements, function ($accumulatedDuration, $statement) {
  212. $time = isset($statement['duration']) ? $statement['duration'] : 0;
  213. return $accumulatedDuration += $time;
  214. });
  215. $memoryUsage = array_reduce($statements, function ($memoryUsage, $statement) {
  216. $time = isset($statement['memory']) ? $statement['memory'] : 0;
  217. return $memoryUsage += $time;
  218. });
  219. return array(
  220. 'nb_statements' => $this->getQueryCount(),
  221. 'nb_failed_statements' => $failedStatement,
  222. 'accumulated_duration' => $accumulatedDuration,
  223. 'accumulated_duration_str' => $this->getDataFormatter()->formatDuration($accumulatedDuration),
  224. 'memory_usage' => $memoryUsage,
  225. 'memory_usage_str' => $this->getDataFormatter()->formatBytes($memoryUsage),
  226. 'statements' => $statements
  227. );
  228. }
  229. /**
  230. * @return string
  231. */
  232. public function getName()
  233. {
  234. $additionalName = '';
  235. if ($this->getLogger() !== $this->getDefaultLogger()) {
  236. $additionalName = ' ('.$this->getLogger()->getName().')';
  237. }
  238. return 'propel2'.$additionalName;
  239. }
  240. /**
  241. * @return array
  242. */
  243. public function getWidgets()
  244. {
  245. return array(
  246. $this->getName() => array(
  247. 'icon' => 'bolt',
  248. 'widget' => 'PhpDebugBar.Widgets.SQLQueriesWidget',
  249. 'map' => $this->getName(),
  250. 'default' => '[]'
  251. ),
  252. $this->getName().':badge' => array(
  253. 'map' => $this->getName().'.nb_statements',
  254. 'default' => 0
  255. ),
  256. );
  257. }
  258. /**
  259. * @return array
  260. */
  261. public function getAssets()
  262. {
  263. return array(
  264. 'css' => 'widgets/sqlqueries/widget.css',
  265. 'js' => 'widgets/sqlqueries/widget.js'
  266. );
  267. }
  268. }