diff --git a/composer.json b/composer.json index 4937ec5..df4c614 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,8 @@ "require" : { "composer/installers": "~1.0", "symfony/stopwatch" : "^3.4.4", - "mustache/mustache" : "~2.4" + "mustache/mustache" : "~2.4", + "pimple/pimple" : "^3.2.3" }, "require-dev": { "twbs/bootstrap": "~3.2" diff --git a/composer.lock b/composer.lock index 8cb2016..b3fa2b3 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "b21c8f912e4fca89ec02d03e3514d017", + "content-hash": "10f3fc63a5cb37afcc5e4384bc58270a", "packages": [ { "name": "composer/installers", @@ -172,6 +172,105 @@ ], "time": "2017-07-11T12:54:05+00:00" }, + { + "name": "pimple/pimple", + "version": "v3.2.3", + "source": { + "type": "git", + "url": "https://github.com/silexphp/Pimple.git", + "reference": "9e403941ef9d65d20cba7d54e29fe906db42cf32" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/silexphp/Pimple/zipball/9e403941ef9d65d20cba7d54e29fe906db42cf32", + "reference": "9e403941ef9d65d20cba7d54e29fe906db42cf32", + "shasum": "" + }, + "require": { + "php": ">=5.3.0", + "psr/container": "^1.0" + }, + "require-dev": { + "symfony/phpunit-bridge": "^3.2" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.2.x-dev" + } + }, + "autoload": { + "psr-0": { + "Pimple": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + } + ], + "description": "Pimple, a simple Dependency Injection Container", + "homepage": "http://pimple.sensiolabs.org", + "keywords": [ + "container", + "dependency injection" + ], + "time": "2018-01-21T07:42:36+00:00" + }, + { + "name": "psr/container", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/php-fig/container.git", + "reference": "b7ce3b176482dbbc1245ebf52b181af44c2cf55f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/container/zipball/b7ce3b176482dbbc1245ebf52b181af44c2cf55f", + "reference": "b7ce3b176482dbbc1245ebf52b181af44c2cf55f", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Container\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common Container Interface (PHP FIG PSR-11)", + "homepage": "https://github.com/php-fig/container", + "keywords": [ + "PSR-11", + "container", + "container-interface", + "container-interop", + "psr" + ], + "time": "2017-02-14T16:28:37+00:00" + }, { "name": "symfony/stopwatch", "version": "v3.4.4", diff --git a/src/Laps.php b/src/Laps.php index 7df7ffe..a6b97bc 100644 --- a/src/Laps.php +++ b/src/Laps.php @@ -2,6 +2,7 @@ namespace Rarst\Laps; +use Pimple\Container; use Rarst\Laps\Events\Core_Events; use Rarst\Laps\Events\Laps_Events; use Symfony\Component\Stopwatch\Stopwatch; @@ -9,13 +10,33 @@ use Symfony\Component\Stopwatch\Stopwatch; /** * Main plugin's class. */ -class Laps { +class Laps extends Container { - /** @var Stopwatch $stopwatch */ - public $stopwatch; public $events = array(); public $query_starts = array(); + public function __construct( array $values = [] ) { + + parent::__construct(); + + $laps = $this; + + $laps['mustache'] = function () { + return new \Mustache_Engine( [ + 'loader' => new \Mustache_Loader_FilesystemLoader( dirname( __DIR__ ) . '/views' ), + 'cache' => new Mustache_Cache_FrozenCache( dirname( __DIR__ ) . '/views/cache' ), + ] ); + }; + + $laps['stopwatch'] = function () { + return new Stopwatch(); + }; + + foreach ( $values as $key => $value ) { + $this->offsetSet( $key, $value ); + } + } + /** * Start Stopwatch and timing plugin load immediately, then set up core events and needed hooks. */ @@ -25,8 +46,7 @@ class Laps { return; } - $this->stopwatch = new Stopwatch(); - $this->stopwatch->start( 'Plugins Load', 'plugin' ); + $this['stopwatch']->start( 'Plugins Load', 'plugin' ); $events = new Core_Events(); $this->add_events( $events->get() ); @@ -110,11 +130,13 @@ class Laps { ) ); - if ( 'stop' === $event['action'] && ! $this->stopwatch->isStarted( $event['event'] ) ) { + $stopwatch = $this['stopwatch']; + + if ( 'stop' === $event['action'] && ! $stopwatch->isStarted( $event['event'] ) ) { return $input; } - $this->stopwatch->{$event['action']}( $event['event'], $event['category'] ); + $stopwatch->{$event['action']}( $event['event'], $event['category'] ); return $input; } @@ -150,7 +172,7 @@ class Laps { */ public function pre_http_request( $false, $args, $url ) { - $this->stopwatch->start( $url, 'http' ); + $this['stopwatch']->start( $url, 'http' ); return $false; } @@ -168,7 +190,7 @@ class Laps { */ public function http_api_debug( $response, $type, $class, $args, $url ) { - $this->stopwatch->stop( $url ); + $this['stopwatch']->stop( $url ); return $response; } @@ -219,18 +241,14 @@ class Laps { global $timestart, $wpdb; - $mustache = new \Mustache_Engine( - array( - 'loader' => new \Mustache_Loader_FilesystemLoader( dirname( __DIR__ ) . '/views' ), - 'cache' => new Mustache_Cache_FrozenCache( dirname( __DIR__ ) . '/views/cache' ), - ) - ); + /** @var Stopwatch $stopwatch */ + $stopwatch = $this['stopwatch']; - if ( $this->stopwatch->isStarted( 'Toolbar' ) ) { - $this->stopwatch->stop( 'Toolbar' ); + if ( $stopwatch->isStarted( 'Toolbar' ) ) { + $stopwatch->stop( 'Toolbar' ); } - $events = $this->stopwatch->getSectionEvents( '__root__' ); + $events = $stopwatch->getSectionEvents( '__root__' ); $start = $timestart * 1000; $end = microtime( true ) * 1000; $total = $end - $start; @@ -295,16 +313,13 @@ class Laps { } } - $html = $mustache->render( - 'laps', - array( - 'events' => $event_data, - 'queries' => $query_data, - 'savequeries' => defined( 'SAVEQUERIES' ) && SAVEQUERIES, - 'http' => $http_data, - 'savehttp' => ! empty( $http_data ), - ) - ); + $html = $this['mustache']->render( 'laps', [ + 'events' => $event_data, + 'queries' => $query_data, + 'savequeries' => defined( 'SAVEQUERIES' ) && SAVEQUERIES, + 'http' => $http_data, + 'savehttp' => ! empty( $http_data ), + ] ); $wp_admin_bar->add_node( array( 'id' => 'laps',