2014-05-02 18:35:41 +01:00
< ? php
/*
* This file is part of the Symfony package .
*
* ( c ) Fabien Potencier < fabien @ symfony . com >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
namespace Symfony\Component\Stopwatch ;
/**
* Stopwatch section .
*
* @ author Fabien Potencier < fabien @ symfony . com >
*/
class Section
{
/**
* @ var StopwatchEvent []
*/
2019-01-16 09:39:14 +00:00
private $events = [];
2014-05-02 18:35:41 +01:00
/**
2018-10-02 04:12:00 +01:00
* @ var float | null
2014-05-02 18:35:41 +01:00
*/
private $origin ;
2017-06-19 10:42:01 +01:00
/**
* @ var bool
*/
private $morePrecision ;
2014-05-02 18:35:41 +01:00
/**
* @ var string
*/
private $id ;
/**
* @ var Section []
*/
2019-01-16 09:39:14 +00:00
private $children = [];
2014-05-02 18:35:41 +01:00
/**
2017-06-19 10:42:01 +01:00
* @ param float | null $origin Set the origin of the events in this section , use null to set their origin to their start time
* @ param bool $morePrecision If true , time is stored as float to keep the original microsecond precision
2014-05-02 18:35:41 +01:00
*/
2017-10-28 19:15:32 +01:00
public function __construct ( float $origin = null , bool $morePrecision = false )
2014-05-02 18:35:41 +01:00
{
2017-10-28 19:15:32 +01:00
$this -> origin = $origin ;
2017-06-19 10:42:01 +01:00
$this -> morePrecision = $morePrecision ;
2014-05-02 18:35:41 +01:00
}
/**
* Returns the child section .
*
* @ param string $id The child section identifier
*
2016-12-27 10:48:22 +00:00
* @ return self | null The child section or null when none found
2014-05-02 18:35:41 +01:00
*/
public function get ( $id )
{
2019-07-06 15:12:52 +01:00
if ( null === $id ) {
@ trigger_error ( sprintf ( 'Passing null as 1st ("$id") argument of the "%s()" method is deprecated since Symfony 4.4, pass a valid child section identifier instead.' , __METHOD__ ), E_USER_DEPRECATED );
}
2014-05-02 18:35:41 +01:00
foreach ( $this -> children as $child ) {
if ( $id === $child -> getId ()) {
return $child ;
}
}
}
/**
* Creates or re - opens a child section .
*
2017-10-28 19:51:46 +01:00
* @ param string | null $id Null to create a new section , the identifier to re - open an existing one
2014-05-02 18:35:41 +01:00
*
2016-12-26 07:50:27 +00:00
* @ return self
2014-05-02 18:35:41 +01:00
*/
public function open ( $id )
{
if ( null === $session = $this -> get ( $id )) {
2017-06-19 10:42:01 +01:00
$session = $this -> children [] = new self ( microtime ( true ) * 1000 , $this -> morePrecision );
2014-05-02 18:35:41 +01:00
}
return $session ;
}
/**
* @ return string The identifier of the section
*/
public function getId ()
{
return $this -> id ;
}
/**
* Sets the session identifier .
*
* @ param string $id The session identifier
*
2016-12-26 07:50:27 +00:00
* @ return $this
2014-05-02 18:35:41 +01:00
*/
public function setId ( $id )
{
$this -> id = $id ;
return $this ;
}
/**
* Starts an event .
*
* @ param string $name The event name
* @ param string $category The event category
*
* @ return StopwatchEvent The event
*/
public function startEvent ( $name , $category )
{
if ( ! isset ( $this -> events [ $name ])) {
2017-06-19 10:42:01 +01:00
$this -> events [ $name ] = new StopwatchEvent ( $this -> origin ? : microtime ( true ) * 1000 , $category , $this -> morePrecision );
2014-05-02 18:35:41 +01:00
}
return $this -> events [ $name ] -> start ();
}
/**
2015-08-24 07:53:33 +01:00
* Checks if the event was started .
2014-05-02 18:35:41 +01:00
*
* @ param string $name The event name
*
* @ return bool
*/
public function isEventStarted ( $name )
{
return isset ( $this -> events [ $name ]) && $this -> events [ $name ] -> isStarted ();
}
/**
* Stops an event .
*
* @ param string $name The event name
*
* @ return StopwatchEvent The event
*
* @ throws \LogicException When the event has not been started
*/
public function stopEvent ( $name )
{
if ( ! isset ( $this -> events [ $name ])) {
throw new \LogicException ( sprintf ( 'Event "%s" is not started.' , $name ));
}
return $this -> events [ $name ] -> stop ();
}
/**
* Stops then restarts an event .
*
* @ param string $name The event name
*
* @ return StopwatchEvent The event
*
* @ throws \LogicException When the event has not been started
*/
public function lap ( $name )
{
return $this -> stopEvent ( $name ) -> start ();
}
/**
2015-08-24 07:53:33 +01:00
* Returns a specific event by name .
2014-05-02 18:35:41 +01:00
*
* @ param string $name The event name
*
* @ return StopwatchEvent The event
*
* @ throws \LogicException When the event is not known
*/
public function getEvent ( $name )
{
if ( ! isset ( $this -> events [ $name ])) {
throw new \LogicException ( sprintf ( 'Event "%s" is not known.' , $name ));
}
return $this -> events [ $name ];
}
/**
* Returns the events from this section .
*
* @ return StopwatchEvent [] An array of StopwatchEvent instances
*/
public function getEvents ()
{
return $this -> events ;
}
}