vendor/twig/twig/src/Profiler/Profile.php line 19

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Twig.
  4.  *
  5.  * (c) Fabien Potencier
  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 Twig\Profiler;
  11. /**
  12.  * @author Fabien Potencier <fabien@symfony.com>
  13.  *
  14.  * @final since Twig 2.4.0
  15.  */
  16. class Profile implements \IteratorAggregate\Serializable
  17. {
  18.     const ROOT 'ROOT';
  19.     const BLOCK 'block';
  20.     const TEMPLATE 'template';
  21.     const MACRO 'macro';
  22.     private $template;
  23.     private $name;
  24.     private $type;
  25.     private $starts = [];
  26.     private $ends = [];
  27.     private $profiles = [];
  28.     public function __construct(string $template 'main'string $type self::ROOTstring $name 'main')
  29.     {
  30.         if (__CLASS__ !== \get_class($this)) {
  31.             @trigger_error('Overriding '.__CLASS__.' is deprecated since Twig 2.4.0 and the class will be final in 3.0.'E_USER_DEPRECATED);
  32.         }
  33.         $this->template $template;
  34.         $this->type $type;
  35.         $this->name === strpos($name'__internal_') ? 'INTERNAL' $name;
  36.         $this->enter();
  37.     }
  38.     public function getTemplate()
  39.     {
  40.         return $this->template;
  41.     }
  42.     public function getType()
  43.     {
  44.         return $this->type;
  45.     }
  46.     public function getName()
  47.     {
  48.         return $this->name;
  49.     }
  50.     public function isRoot()
  51.     {
  52.         return self::ROOT === $this->type;
  53.     }
  54.     public function isTemplate()
  55.     {
  56.         return self::TEMPLATE === $this->type;
  57.     }
  58.     public function isBlock()
  59.     {
  60.         return self::BLOCK === $this->type;
  61.     }
  62.     public function isMacro()
  63.     {
  64.         return self::MACRO === $this->type;
  65.     }
  66.     public function getProfiles()
  67.     {
  68.         return $this->profiles;
  69.     }
  70.     public function addProfile(self $profile)
  71.     {
  72.         $this->profiles[] = $profile;
  73.     }
  74.     /**
  75.      * Returns the duration in microseconds.
  76.      *
  77.      * @return float
  78.      */
  79.     public function getDuration()
  80.     {
  81.         if ($this->isRoot() && $this->profiles) {
  82.             // for the root node with children, duration is the sum of all child durations
  83.             $duration 0;
  84.             foreach ($this->profiles as $profile) {
  85.                 $duration += $profile->getDuration();
  86.             }
  87.             return $duration;
  88.         }
  89.         return isset($this->ends['wt']) && isset($this->starts['wt']) ? $this->ends['wt'] - $this->starts['wt'] : 0;
  90.     }
  91.     /**
  92.      * Returns the memory usage in bytes.
  93.      *
  94.      * @return int
  95.      */
  96.     public function getMemoryUsage()
  97.     {
  98.         return isset($this->ends['mu']) && isset($this->starts['mu']) ? $this->ends['mu'] - $this->starts['mu'] : 0;
  99.     }
  100.     /**
  101.      * Returns the peak memory usage in bytes.
  102.      *
  103.      * @return int
  104.      */
  105.     public function getPeakMemoryUsage()
  106.     {
  107.         return isset($this->ends['pmu']) && isset($this->starts['pmu']) ? $this->ends['pmu'] - $this->starts['pmu'] : 0;
  108.     }
  109.     /**
  110.      * Starts the profiling.
  111.      */
  112.     public function enter()
  113.     {
  114.         $this->starts = [
  115.             'wt' => microtime(true),
  116.             'mu' => memory_get_usage(),
  117.             'pmu' => memory_get_peak_usage(),
  118.         ];
  119.     }
  120.     /**
  121.      * Stops the profiling.
  122.      */
  123.     public function leave()
  124.     {
  125.         $this->ends = [
  126.             'wt' => microtime(true),
  127.             'mu' => memory_get_usage(),
  128.             'pmu' => memory_get_peak_usage(),
  129.         ];
  130.     }
  131.     public function reset()
  132.     {
  133.         $this->starts $this->ends $this->profiles = [];
  134.         $this->enter();
  135.     }
  136.     public function getIterator()
  137.     {
  138.         return new \ArrayIterator($this->profiles);
  139.     }
  140.     public function serialize()
  141.     {
  142.         return serialize($this->__serialize());
  143.     }
  144.     public function unserialize($data)
  145.     {
  146.         $this->__unserialize(unserialize($data));
  147.     }
  148.     /**
  149.      * @internal
  150.      */
  151.     public function __serialize()
  152.     {
  153.         return [$this->template$this->name$this->type$this->starts$this->ends$this->profiles];
  154.     }
  155.     /**
  156.      * @internal
  157.      */
  158.     public function __unserialize(array $data)
  159.     {
  160.         list($this->template$this->name$this->type$this->starts$this->ends$this->profiles) = $data;
  161.     }
  162. }
  163. class_alias('Twig\Profiler\Profile''Twig_Profiler_Profile');