vendor/shopware/core/Framework/Script/Debugging/ScriptTraces.php line 63

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Script\Debugging;
  3. use Shopware\Core\Framework\Script\Execution\FunctionHook;
  4. use Shopware\Core\Framework\Script\Execution\Hook;
  5. use Shopware\Core\Framework\Script\Execution\Script;
  6. use Symfony\Bundle\FrameworkBundle\DataCollector\AbstractDataCollector;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\VarDumper\Cloner\Data;
  10. use Symfony\Contracts\Service\ResetInterface;
  11. /**
  12.  * @deprecated tag:v6.5.0 - reason:becomes-internal - will be internal
  13.  */
  14. class ScriptTraces extends AbstractDataCollector implements ResetInterface
  15. {
  16.     /**
  17.      * @var array<string, mixed>
  18.      */
  19.     protected array $traces = [];
  20.     protected static array $deprecationNotices = [];
  21.     public static function addDeprecationNotice(string $deprecationNotice): void
  22.     {
  23.         static::$deprecationNotices[] = $deprecationNotice;
  24.     }
  25.     public function collect(Request $requestResponse $response, ?\Throwable $exception null): void
  26.     {
  27.         $this->data $this->traces;
  28.     }
  29.     /**
  30.      * @deprecated tag:v6.5.0 will be removed, use `initHook` instead
  31.      */
  32.     public function init(string $hook): void
  33.     {
  34.         // dummy implementation to not break
  35.     }
  36.     public function initHook(Hook $hook): void
  37.     {
  38.         $name $this->getHookName($hook);
  39.         if (\array_key_exists($name$this->traces)) {
  40.             // don't overwrite existing traces
  41.             return;
  42.         }
  43.         $this->traces[$name] = [];
  44.     }
  45.     public function trace(Hook $hookScript $script\Closure $execute): void
  46.     {
  47.         $time microtime(true);
  48.         $debug = new Debug();
  49.         static::$deprecationNotices = [];
  50.         $execute($debug);
  51.         $deprecations = static::$deprecationNotices;
  52.         static::$deprecationNotices = [];
  53.         $took round(microtime(true) - $time3);
  54.         $name explode('/'$script->getName());
  55.         $name array_pop($name);
  56.         $this->add($hook$name$took$debug$deprecations);
  57.     }
  58.     public function getHookCount(): int
  59.     {
  60.         if ($this->data instanceof Data) {
  61.             return $this->data->count();
  62.         }
  63.         return \count($this->data);
  64.     }
  65.     public function getHooks(): array
  66.     {
  67.         if ($this->data instanceof Data) {
  68.             return [];
  69.         }
  70.         return array_keys($this->data);
  71.     }
  72.     public function getScripts(string $hook): array
  73.     {
  74.         return $this->data[$hook] ?? [];
  75.     }
  76.     public function getTook(): float
  77.     {
  78.         $data $this->data instanceof Data $this->data->getIterator() : $this->data;
  79.         $took 0.0;
  80.         foreach ($data as $scripts) {
  81.             $took += array_sum(array_column($scripts'took'));
  82.         }
  83.         return $took;
  84.     }
  85.     public function getScriptCount(): int
  86.     {
  87.         $count 0;
  88.         foreach ($this->data as $scripts) {
  89.             $count += \count($scripts);
  90.         }
  91.         return $count;
  92.     }
  93.     public function getDeprecationCount(): int
  94.     {
  95.         $count 0;
  96.         foreach ($this->data as $scripts) {
  97.             foreach ($scripts as $script) {
  98.                 foreach ($script['deprecations'] as $deprecationCount) {
  99.                     $count += $deprecationCount;
  100.                 }
  101.             }
  102.         }
  103.         return $count;
  104.     }
  105.     public static function getTemplate(): ?string
  106.     {
  107.         return '@Profiling/Collector/script_traces.html.twig';
  108.     }
  109.     /**
  110.      * @return array|Data
  111.      */
  112.     public function getData()
  113.     {
  114.         return $this->data;
  115.     }
  116.     /**
  117.      * @return array<string, mixed>
  118.      */
  119.     public function getTraces(): array
  120.     {
  121.         return $this->traces;
  122.     }
  123.     public function reset(): void
  124.     {
  125.         parent::reset();
  126.         $this->traces = [];
  127.     }
  128.     private function add(Hook $hookstring $namefloat $tookDebug $output, array $deprecations): void
  129.     {
  130.         $deprecations array_count_values($deprecations);
  131.         arsort($deprecations);
  132.         $this->traces[$this->getHookName($hook)][] = [
  133.             'name' => $name,
  134.             'took' => $took,
  135.             'output' => $output->all(),
  136.             'deprecations' => $deprecations,
  137.         ];
  138.     }
  139.     private function getHookName(Hook $hook): string
  140.     {
  141.         if (!$hook instanceof FunctionHook) {
  142.             return $hook->getName();
  143.         }
  144.         return $hook->getName() . '::' $hook->getFunctionName();
  145.     }
  146. }