vendor/shopware/core/Profiling/Profiler.php line 57

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Profiling;
  3. use Shopware\Core\Profiling\Integration\ProfilerInterface;
  4. /**
  5.  * @internal experimental atm
  6.  */
  7. class Profiler
  8. {
  9.     /**
  10.      * Profilers will be activated over the shopware.yaml file
  11.      *
  12.      * All enabled profilers will be added here
  13.      *
  14.      * @var ProfilerInterface[]
  15.      */
  16.     private static array $profilers = [];
  17.     /**
  18.      * Tags will be added to each trace
  19.      *
  20.      * @var array<string>
  21.      */
  22.     private static array $tags = [];
  23.     /**
  24.      * @var array<string>
  25.      */
  26.     private static array $openTraces = [];
  27.     /**
  28.      * @param array<string> $activeProfilers
  29.      */
  30.     public function __construct(\Traversable $profilers, array $activeProfilers)
  31.     {
  32.         $profilers iterator_to_array($profilers);
  33.         self::$profilers array_intersect_key($profilersarray_flip($activeProfilers));
  34.         self::$tags = [];
  35.         register_shutdown_function(fn () => self::cleanup());
  36.     }
  37.     /**
  38.      * @return mixed
  39.      */
  40.     public static function trace(string $name\Closure $closurestring $category 'shopware', array $tags = [])
  41.     {
  42.         $tags array_merge(self::$tags$tags);
  43.         try {
  44.             foreach (self::$profilers as $profiler) {
  45.                 $profiler->start($name$category$tags);
  46.             }
  47.             $result $closure();
  48.         } finally {
  49.             foreach (self::$profilers as $profiler) {
  50.                 $profiler->stop($name);
  51.             }
  52.         }
  53.         return $result;
  54.     }
  55.     public static function start(string $titlestring $category, array $tags): void
  56.     {
  57.         self::$openTraces[] = $title;
  58.         $tags array_merge(self::$tags$tags);
  59.         foreach (self::$profilers as $profiler) {
  60.             $profiler->start($title$category$tags);
  61.         }
  62.     }
  63.     public static function stop(string $title): void
  64.     {
  65.         foreach (self::$profilers as $profiler) {
  66.             $profiler->stop($title);
  67.         }
  68.         unset(self::$openTraces[$title]);
  69.     }
  70.     public static function cleanup(): void
  71.     {
  72.         foreach (self::$openTraces as $name) {
  73.             foreach (self::$profilers as $profiler) {
  74.                 $profiler->stop($name);
  75.             }
  76.         }
  77.         self::$openTraces = [];
  78.     }
  79.     public static function addTag(string $keystring $value): void
  80.     {
  81.         self::$tags[$key] = $value;
  82.     }
  83.     public static function removeTag(string $key): void
  84.     {
  85.         unset(self::$tags[$key]);
  86.     }
  87. }