Deprecated: Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 58

Deprecated: Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 76
Symfony Profiler

vendor/symfony/doctrine-bridge/DataCollector/DoctrineDataCollector.php line 42

Open in your IDE?
  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\Bridge\Doctrine\DataCollector;
  11. use Doctrine\DBAL\Logging\DebugStack;
  12. use Doctrine\DBAL\Types\ConversionException;
  13. use Doctrine\DBAL\Types\Type;
  14. use Doctrine\Persistence\ManagerRegistry;
  15. use Symfony\Bridge\Doctrine\Middleware\Debug\DebugDataHolder;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\HttpKernel\DataCollector\DataCollector;
  19. use Symfony\Component\VarDumper\Caster\Caster;
  20. use Symfony\Component\VarDumper\Cloner\Stub;
  21. /**
  22.  * DoctrineDataCollector.
  23.  *
  24.  * @author Fabien Potencier <fabien@symfony.com>
  25.  */
  26. class DoctrineDataCollector extends DataCollector
  27. {
  28.     private $registry;
  29.     private $connections;
  30.     private $managers;
  31.     private $debugDataHolder;
  32.     /**
  33.      * @var DebugStack[]
  34.      */
  35.     private $loggers = [];
  36.     public function __construct(ManagerRegistry $registryDebugDataHolder $debugDataHolder null)
  37.     {
  38.         $this->registry $registry;
  39.         $this->connections $registry->getConnectionNames();
  40.         $this->managers $registry->getManagerNames();
  41.         $this->debugDataHolder $debugDataHolder;
  42.     }
  43.     /**
  44.      * Adds the stack logger for a connection.
  45.      */
  46.     public function addLogger(string $nameDebugStack $logger)
  47.     {
  48.         $this->loggers[$name] = $logger;
  49.     }
  50.     /**
  51.      * {@inheritdoc}
  52.      */
  53.     public function collect(Request $requestResponse $response\Throwable $exception null)
  54.     {
  55.         $this->data = [
  56.             'queries' => $this->collectQueries(),
  57.             'connections' => $this->connections,
  58.             'managers' => $this->managers,
  59.         ];
  60.     }
  61.     private function collectQueries(): array
  62.     {
  63.         $queries = [];
  64.         if (null !== $this->debugDataHolder) {
  65.             foreach ($this->debugDataHolder->getData() as $name => $data) {
  66.                 $queries[$name] = $this->sanitizeQueries($name$data);
  67.             }
  68.             return $queries;
  69.         }
  70.         foreach ($this->loggers as $name => $logger) {
  71.             $queries[$name] = $this->sanitizeQueries($name$logger->queries);
  72.         }
  73.         return $queries;
  74.     }
  75.     public function reset()
  76.     {
  77.         $this->data = [];
  78.         if (null !== $this->debugDataHolder) {
  79.             $this->debugDataHolder->reset();
  80.             return;
  81.         }
  82.         foreach ($this->loggers as $logger) {
  83.             $logger->queries = [];
  84.             $logger->currentQuery 0;
  85.         }
  86.     }
  87.     public function getManagers()
  88.     {
  89.         return $this->data['managers'];
  90.     }
  91.     public function getConnections()
  92.     {
  93.         return $this->data['connections'];
  94.     }
  95.     public function getQueryCount()
  96.     {
  97.         return array_sum(array_map('count'$this->data['queries']));
  98.     }
  99.     public function getQueries()
  100.     {
  101.         return $this->data['queries'];
  102.     }
  103.     public function getTime()
  104.     {
  105.         $time 0;
  106.         foreach ($this->data['queries'] as $queries) {
  107.             foreach ($queries as $query) {
  108.                 $time += $query['executionMS'];
  109.             }
  110.         }
  111.         return $time;
  112.     }
  113.     /**
  114.      * {@inheritdoc}
  115.      */
  116.     public function getName()
  117.     {
  118.         return 'db';
  119.     }
  120.     /**
  121.      * {@inheritdoc}
  122.      */
  123.     protected function getCasters()
  124.     {
  125.         return parent::getCasters() + [
  126.             ObjectParameter::class => static function (ObjectParameter $o, array $aStub $s): array {
  127.                 $s->class $o->getClass();
  128.                 $s->value $o->getObject();
  129.                 $r = new \ReflectionClass($o->getClass());
  130.                 if ($f $r->getFileName()) {
  131.                     $s->attr['file'] = $f;
  132.                     $s->attr['line'] = $r->getStartLine();
  133.                 } else {
  134.                     unset($s->attr['file']);
  135.                     unset($s->attr['line']);
  136.                 }
  137.                 if ($error $o->getError()) {
  138.                     return [Caster::PREFIX_VIRTUAL.'⚠' => $error->getMessage()];
  139.                 }
  140.                 if ($o->isStringable()) {
  141.                     return [Caster::PREFIX_VIRTUAL.'__toString()' => (string) $o->getObject()];
  142.                 }
  143.                 return [Caster::PREFIX_VIRTUAL.'⚠' => sprintf('Object of class "%s" could not be converted to string.'$o->getClass())];
  144.             },
  145.         ];
  146.     }
  147.     private function sanitizeQueries(string $connectionName, array $queries): array
  148.     {
  149.         foreach ($queries as $i => $query) {
  150.             $queries[$i] = $this->sanitizeQuery($connectionName$query);
  151.         }
  152.         return $queries;
  153.     }
  154.     private function sanitizeQuery(string $connectionName, array $query): array
  155.     {
  156.         $query['explainable'] = true;
  157.         $query['runnable'] = true;
  158.         if (null === $query['params']) {
  159.             $query['params'] = [];
  160.         }
  161.         if (!\is_array($query['params'])) {
  162.             $query['params'] = [$query['params']];
  163.         }
  164.         if (!\is_array($query['types'])) {
  165.             $query['types'] = [];
  166.         }
  167.         foreach ($query['params'] as $j => $param) {
  168.             $e null;
  169.             if (isset($query['types'][$j])) {
  170.                 // Transform the param according to the type
  171.                 $type $query['types'][$j];
  172.                 if (\is_string($type)) {
  173.                     $type Type::getType($type);
  174.                 }
  175.                 if ($type instanceof Type) {
  176.                     $query['types'][$j] = $type->getBindingType();
  177.                     try {
  178.                         $param $type->convertToDatabaseValue($param$this->registry->getConnection($connectionName)->getDatabasePlatform());
  179.                     } catch (\TypeError $e) {
  180.                     } catch (ConversionException $e) {
  181.                     }
  182.                 }
  183.             }
  184.             [$query['params'][$j], $explainable$runnable] = $this->sanitizeParam($param$e);
  185.             if (!$explainable) {
  186.                 $query['explainable'] = false;
  187.             }
  188.             if (!$runnable) {
  189.                 $query['runnable'] = false;
  190.             }
  191.         }
  192.         $query['params'] = $this->cloneVar($query['params']);
  193.         return $query;
  194.     }
  195.     /**
  196.      * Sanitizes a param.
  197.      *
  198.      * The return value is an array with the sanitized value and a boolean
  199.      * indicating if the original value was kept (allowing to use the sanitized
  200.      * value to explain the query).
  201.      */
  202.     private function sanitizeParam($var, ?\Throwable $error): array
  203.     {
  204.         if (\is_object($var)) {
  205.             return [$o = new ObjectParameter($var$error), false$o->isStringable() && !$error];
  206.         }
  207.         if ($error) {
  208.             return ['⚠ '.$error->getMessage(), falsefalse];
  209.         }
  210.         if (\is_array($var)) {
  211.             $a = [];
  212.             $explainable $runnable true;
  213.             foreach ($var as $k => $v) {
  214.                 [$value$e$r] = $this->sanitizeParam($vnull);
  215.                 $explainable $explainable && $e;
  216.                 $runnable $runnable && $r;
  217.                 $a[$k] = $value;
  218.             }
  219.             return [$a$explainable$runnable];
  220.         }
  221.         if (\is_resource($var)) {
  222.             return [sprintf('/* Resource(%s) */'get_resource_type($var)), falsefalse];
  223.         }
  224.         return [$vartruetrue];
  225.     }
  226. }