Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[DoctrineBridge] Refactored the query sanitization in the collector

The original parameters are kept whenever possible to allow using them
again to explain the query.
  • Loading branch information...
commit fb009c834dd9445de57760073040b4590e5e8a43 1 parent 318b454
Christophe Coevoet stof authored

Showing 1 changed file with 49 additions and 23 deletions. Show diff stats Hide diff stats

  1. +49 23 DataCollector/DoctrineDataCollector.php
72 DataCollector/DoctrineDataCollector.php
@@ -13,6 +13,7 @@
13 13
14 14 use Doctrine\Common\Persistence\ManagerRegistry;
15 15 use Doctrine\DBAL\Logging\DebugStack;
  16 +use Doctrine\DBAL\Types\Type;
16 17 use Symfony\Component\HttpKernel\DataCollector\DataCollector;
17 18 use Symfony\Component\HttpFoundation\Request;
18 19 use Symfony\Component\HttpFoundation\Response;
@@ -54,7 +55,7 @@ public function collect(Request $request, Response $response, \Exception $except
54 55 {
55 56 $queries = array();
56 57 foreach ($this->loggers as $name => $logger) {
57   - $queries[$name] = $this->sanitizeQueries($logger->queries);
  58 + $queries[$name] = $this->sanitizeQueries($name, $logger->queries);
58 59 }
59 60
60 61 $this->data = array(
@@ -104,48 +105,73 @@ public function getName()
104 105 return 'db';
105 106 }
106 107
107   - private function sanitizeQueries($queries)
  108 + private function sanitizeQueries($connectionName, $queries)
108 109 {
109 110 foreach ($queries as $i => $query) {
110   - foreach ((array) $query['params'] as $j => $param) {
111   - $queries[$i]['params'][$j] = $this->varToString($param);
112   - }
  111 + $queries[$i] = $this->sanitizeQuery($connectionName, $query);
113 112 }
114 113
115 114 return $queries;
116 115 }
117 116
118   - private function varToString($var)
  117 + private function sanitizeQuery($connectionName, $query)
  118 + {
  119 + $query['explainable'] = true;
  120 + $query['params'] = (array) $query['params'];
  121 + foreach ($query['params'] as $j => &$param) {
  122 + if (isset($query['types'][$j])) {
  123 + // Transform the param according to the type
  124 + $type = $query['types'][$j];
  125 + if (is_string($type)) {
  126 + $type = Type::getType($type);
  127 + }
  128 + if ($type instanceof Type) {
  129 + $query['types'][$j] = $type->getBindingType();
  130 + $param = $type->convertToDatabaseValue($param, $this->registry->getConnection($connectionName)->getDatabasePlatform());
  131 + }
  132 + }
  133 +
  134 + list($param, $explainable) = $this->sanitizeParam($param);
  135 + if (!$explainable) {
  136 + $query['explainable'] = false;
  137 + }
  138 + }
  139 +
  140 + return $query;
  141 + }
  142 +
  143 + /**
  144 + * Sanitizes a param.
  145 + *
  146 + * The return value is an array with the sanitized value and a boolean
  147 + * indicating if the original value was kept (allowing to use the sanitized
  148 + * value to explain the query).
  149 + *
  150 + * @param mixed $var
  151 + * @return array
  152 + */
  153 + private function sanitizeParam($var)
119 154 {
120 155 if (is_object($var)) {
121   - return sprintf('Object(%s)', get_class($var));
  156 + return array(sprintf('Object(%s)', get_class($var)), false);
122 157 }
123 158
124 159 if (is_array($var)) {
125 160 $a = array();
  161 + $original = true;
126 162 foreach ($var as $k => $v) {
127   - $a[] = sprintf('%s => %s', $k, $this->varToString($v));
  163 + list($value, $orig) = $this->sanitizeParam($v);
  164 + $original = $original && $orig;
  165 + $a[$k] = $value;
128 166 }
129 167
130   - return sprintf("Array(%s)", implode(', ', $a));
  168 + return array($a, $original);
131 169 }
132 170
133 171 if (is_resource($var)) {
134   - return sprintf('Resource(%s)', get_resource_type($var));
135   - }
136   -
137   - if (null === $var) {
138   - return 'null';
139   - }
140   -
141   - if (false === $var) {
142   - return 'false';
143   - }
144   -
145   - if (true === $var) {
146   - return 'true';
  172 + return array(sprintf('Resource(%s)', get_resource_type($var)), false);
147 173 }
148 174
149   - return (string) $var;
  175 + return array($var, true);
150 176 }
151 177 }

0 comments on commit fb009c8

Please sign in to comment.
Something went wrong with that request. Please try again.