29 private static $_enabled =
false;
36 private static $_currentPath = [];
43 private static $_pathCount = 0;
50 private static $_pathIndex = [];
57 private static $_drivers = [];
64 private static $_defaultTags = [];
71 private static $_tagFilters = [];
78 private static $_hasTagFilters =
false;
88 self::$_defaultTags = $tags;
100 if (!isset(self::$_tagFilters[$tagName])) {
101 self::$_tagFilters[$tagName] = [];
103 self::$_tagFilters[$tagName][] = $tagValue;
104 self::$_hasTagFilters =
true;
113 private static function _checkTags(array $tags =
null)
115 if (self::$_hasTagFilters) {
116 if (is_array($tags)) {
117 $keysToCheck = array_intersect(array_keys(self::$_tagFilters), array_keys($tags));
119 foreach ($keysToCheck as $keyToCheck) {
120 if (in_array($tags[$keyToCheck], self::$_tagFilters[$keyToCheck])) {
139 self::$_drivers[] = $driver;
149 private static function _getTimerId($timerName =
null)
151 if (!self::$_currentPath) {
152 return (
string)$timerName;
154 return implode(self::NESTING_SEPARATOR, self::$_currentPath) . self::NESTING_SEPARATOR . $timerName;
156 return implode(self::NESTING_SEPARATOR, self::$_currentPath);
166 private static function _getTags(array $tags =
null)
168 if (self::$_defaultTags) {
169 return (array)$tags + self::$_defaultTags;
184 self::$_enabled =
true;
196 self::$_enabled =
false;
206 return self::$_enabled;
216 public static function clear($timerName =
null)
218 if (strpos($timerName, self::NESTING_SEPARATOR) !==
false) {
219 throw new \InvalidArgumentException(
'Timer name must not contain a nesting separator.');
221 $timerId = self::_getTimerId($timerName);
223 foreach (self::$_drivers as $driver) {
224 $driver->clear($timerId);
236 self::$_enabled =
false;
237 self::$_currentPath = [];
238 self::$_tagFilters = [];
239 self::$_defaultTags = [];
240 self::$_hasTagFilters =
false;
241 self::$_drivers = [];
242 self::$_pathCount = 0;
243 self::$_pathIndex = [];
254 public static function start($timerName, array $tags =
null)
256 if (!self::$_enabled) {
260 $tags = self::_getTags($tags);
261 if (!self::_checkTags($tags)) {
265 if (strpos($timerName, self::NESTING_SEPARATOR) !==
false) {
266 throw new \InvalidArgumentException(
'Timer name must not contain a nesting separator.');
269 $timerId = self::_getTimerId($timerName);
271 foreach (self::$_drivers as $driver) {
272 $driver->start($timerId, $tags);
275 self::$_currentPath[] = $timerName;
277 self::$_pathIndex[$timerName][] = self::$_pathCount;
290 public static function stop($timerName =
null)
292 if (!self::$_enabled || !self::_checkTags(self::_getTags())) {
296 if ($timerName ===
null) {
299 $timerPosition =
false;
300 if (!empty(self::$_pathIndex[$timerName])) {
301 $timerPosition = array_pop(self::$_pathIndex[$timerName]);
303 if ($timerPosition ===
false) {
304 throw new \InvalidArgumentException(sprintf(
'Timer "%s" has not been started.', $timerName));
305 }
elseif ($timerPosition === 1) {
308 $timersToStop = self::$_pathCount + 1 - $timerPosition;
312 for (
$i = 0;
$i < $timersToStop;
$i++) {
313 $timerId = self::_getTimerId();
315 foreach (self::$_drivers as $driver) {
316 $driver->stop($timerId);
319 array_pop(self::$_currentPath);
335 if (
$config[
'driverConfigs']) {
336 foreach (
$config[
'driverConfigs'] as $driverConfig) {
340 foreach (
$config[
'tagFilters'] as $tagName => $tagValue) {
359 if (is_scalar($profilerConfig)) {
361 [
'output' => is_numeric($profilerConfig) ?
'html' : $profilerConfig],
367 $driverConfigs = (array)(isset(
$config[
'drivers']) ?
$config[
'drivers'] : []);
369 $tagFilters = (array)(isset(
$config[
'tagFilters']) ?
$config[
'tagFilters'] : []);
373 'driverFactory' => $driverFactory,
374 'tagFilters' => $tagFilters,
375 'baseDir' =>
$config[
'baseDir'],
390 foreach ($driverConfigs as
$code => $driverConfig) {
392 if ($driverConfig ===
false) {
395 if (!isset($driverConfig[
'type']) && !is_numeric(
$code)) {
396 $driverConfig[
'type'] =
$code;
398 if (!isset($driverConfig[
'baseDir']) &&
$baseDir) {
399 $driverConfig[
'baseDir'] =
$baseDir;
415 if (is_array($driverConfig)) {
417 }
elseif (is_scalar($driverConfig) && $driverConfig) {
418 if (is_numeric($driverConfig)) {
421 $result = [
'type' => $driverConfig];
elseif(isset( $params[ 'redirect_parent']))
static add(DriverInterface $driver)
static setDefaultTags(array $tags)
static _parseDriverConfigs(array $driverConfigs, $baseDir)
static _parseDriverConfig($driverConfig)
static _parseConfig($profilerConfig, $baseDir, $isAjax)
static applyConfig($config, $baseDir, $isAjax=false)
static addTagFilter($tagName, $tagValue)