ezcDebugTimerStruct) */ private $timers; /** * Similar to {@link $timers} but stores those that are currently running. * * @var array(string=>ezcDebugTimerStruct) */ private $runningTimers; /** * The total number of running timers. * * @var int */ private $totalRunningTimers; /** * The submitted timer number. * * @var int */ private $number = 0; /** * Constructs a timer object with no timers. */ public function __construct() { $this->reset(); } /** * Throws always an {@link ezcBasePropertyNotFoundException}. * * @throws ezcBasePropertyNotFoundException * @param string $name * @return mixed */ public function __get( $name ) { throw new ezcBasePropertyNotFoundException( $name ); } /** * Throws always an {@link ezcBasePropertyNotFoundException}. * * @throws ezcBasePropertyNotFoundException * @param string $name * @param string $value * @return mixed */ public function __set( $name, $value ) { throw new ezcBasePropertyNotFoundException( $name ); } /** * Resets the timer object to its initial state with no timers. * * @return void */ public function reset() { $this->timers = array(); $this->runningTimers = array(); $this->totalRunningTimers = 0; } /** * Starts the timer identified by $name with the group $group and returns true on success. * * If the timer was already started false is returned. * * @param string $name * @param string $group * @return bool */ public function startTimer( $name, $group ) { if ( !isset( $this->runningTimers[ $name ] ) ) { $this->totalRunningTimers++; $this->runningTimers[$name] = new ezcDebugTimerStruct(); $this->runningTimers[$name]->name = $name; $this->runningTimers[$name]->group = $group; $this->runningTimers[$name]->switchTime = array(); $this->runningTimers[$name]->startTime = microtime( true ); $this->runningTimers[$name]->startNumber = $this->number++; return true; } return false; } /** * Stops the timer $oldName and starts the timer $newName and return true on success. * * If the timer $oldName does not exist or if it was omitted with several timers running * false is returned. * * @param string $newName * @param string $oldName * @return bool */ public function switchTimer( $newName, $oldName = false ) { if ( $this->totalRunningTimers < 1 ) { return false; } if ( $oldName === false ) { if ( $this->totalRunningTimers > 1 ) { return false; } $oldName = key( $this->runningTimers ); } if ( isset( $this->runningTimers[ $oldName ] ) ) { if ( $newName != $oldName ) { $this->runningTimers[$newName] = $this->runningTimers[$oldName]; unset( $this->runningTimers[$oldName] ); } $switchStruct = new ezcDebugSwitchTimerStruct(); $switchStruct->name = $newName; $switchStruct->time = microtime( true ); $this->runningTimers[$newName]->switchTime[] = $switchStruct; return true; } return false; } /** * Stop the timer identified by $name and return true on success. * * If the timer $oldName does not exist or if it was omitted with several timers running * false is returned. * * @param $name * @return bool */ public function stopTimer( $name = false ) { if ( $name === false && $this->totalRunningTimers == 1 ) { $name = key( $this->runningTimers ); } if ( isset( $this->runningTimers[ $name ] ) ) { $this->runningTimers[$name]->stopTime = microtime( true ); $this->runningTimers[$name]->elapsedTime = $this->runningTimers[$name]->stopTime - $this->runningTimers[$name]->startTime; $this->runningTimers[$name]->stopNumber = $this->number++; $this->timers[] = $this->runningTimers[$name]; unset( $this->runningTimers[$name] ); $this->totalRunningTimers--; return true; } return false; } /** * Returns an array with the timer data. * * @return array(ezcDebugTimerStruct) */ public function getTimeData() { /* $result = new ezcDebugStructure(); $result->stopTimes = $this->timers; $result->groups = $this->getGroups(); var_dump( $result ); */ return $this->timers; } /** * Returns the time used per group. * * The time is returned as an array in the format * array( 'groupName' => array( number of ms ) ) * * @return array(string=>array(int)) */ protected function getGroups() { $groups = array(); foreach ( $this->timers as $time ) { $groups[$time->group][] = $time; } return $groups; } } ?>