Files
wordpress-dev-env/wordpress_plugins/query-monitor/classes/Collectors.php
Felix Förtsch 648ded8896 Initial commit
2020-10-20 14:39:50 +02:00

75 lines
1.3 KiB
PHP

<?php
/**
* Container for data collectors.
*
* @package query-monitor
*/
if ( ! class_exists( 'QM_Collectors' ) ) {
class QM_Collectors implements IteratorAggregate {
private $items = array();
private $processed = false;
public function getIterator() {
return new ArrayIterator( $this->items );
}
public static function add( QM_Collector $collector ) {
$collectors = self::init();
$collectors->items[ $collector->id ] = $collector;
}
/**
* Fetches a collector instance.
*
* @param string $id The collector ID.
* @return QM_Collector|null The collector object.
*/
public static function get( $id ) {
$collectors = self::init();
if ( isset( $collectors->items[ $id ] ) ) {
return $collectors->items[ $id ];
}
return null;
}
public static function init() {
static $instance;
if ( ! $instance ) {
$instance = new QM_Collectors();
}
return $instance;
}
public function process() {
if ( $this->processed ) {
return;
}
foreach ( $this as $collector ) {
$collector->tear_down();
$timer = new QM_Timer();
$timer->start();
$collector->process();
$collector->process_concerns();
$collector->set_timer( $timer->stop() );
}
foreach ( $this as $collector ) {
$collector->post_process();
}
$this->processed = true;
}
}
}