Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
346 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.ctags | ||
vendor/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
<?php | ||
/* | ||
* This file is part of Pomm's Silex™ ServiceProvider package. | ||
* | ||
* (c) 2014 Grégoire HUBERT <hubert.greg@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace PommProject\Silex\ServiceProvider; | ||
|
||
use PommProject\Foundation\Listener\Listener; | ||
use PommProject\Foundation\Session\Session; | ||
use PommProject\Foundation\Pomm; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\DataCollector\DataCollector; | ||
|
||
/** | ||
* DatabaseDataCollector | ||
* | ||
* Data collector for the database profiler. | ||
* | ||
* @package PommServiceProvider | ||
* @copyright 2014 Grégoire HUBERT | ||
* @author Jérôme MACIAS | ||
* @license X11 {@link http://opensource.org/licenses/mit-license.php} | ||
* @see DataCollector | ||
*/ | ||
class DatabaseDataCollector extends DataCollector | ||
{ | ||
private $queries; | ||
|
||
/** | ||
* __construct | ||
* | ||
* Attach profiler actions to each query builder. | ||
* | ||
* @access public | ||
* @param Pomm $pomm | ||
* @return null | ||
*/ | ||
public function __construct(Pomm $pomm) | ||
{ | ||
$this->queries = []; | ||
$callable = [$this, 'execute']; | ||
|
||
foreach ($pomm->getSessionBuilders() as $name => $builder) { | ||
$pomm->addPostConfiguration($name, function($session) use ($callable) { | ||
$session | ||
->getClientUsingPooler('listener', 'query') | ||
->attachAction($callable) | ||
; | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* execute | ||
* | ||
* Action attached to the query listener. | ||
* It attaches each new query in a query stack. | ||
* | ||
* @access public | ||
* @param name event name | ||
* @param array $data | ||
* @param Session $session | ||
* @return null | ||
*/ | ||
public function execute($name, $data, Session $session) | ||
{ | ||
if (!in_array($name, array('query:pre', 'query:post'))) { | ||
return; | ||
} | ||
|
||
if ('query:post' === $name) { | ||
end($this->queries); | ||
$key = key($this->queries); | ||
reset($this->queries); | ||
|
||
$this->queries[$key] += $data; | ||
|
||
return; | ||
} | ||
|
||
$this->queries[] = $data; | ||
} | ||
|
||
/** | ||
* collect | ||
* | ||
* @see DataCollector | ||
*/ | ||
public function collect(Request $request, Response $response, \Exception $exception = null) | ||
{ | ||
$time = 0; | ||
$querycount = 0; | ||
$queries = $this->queries; | ||
|
||
foreach ($queries as $query) { | ||
++$querycount; | ||
$time += $query['time_ms']; | ||
} | ||
|
||
$this->data = compact('queries', 'querycount', 'time'); | ||
} | ||
|
||
/** | ||
* getQueries | ||
* | ||
* Return the list of queries sent. | ||
* | ||
* @access public | ||
* @return array | ||
*/ | ||
public function getQueries() | ||
{ | ||
return $this->data['queries']; | ||
} | ||
|
||
/** | ||
* getQuerycount | ||
* | ||
* Return the number of queries sent. | ||
* | ||
* @access public | ||
* @return integer | ||
*/ | ||
public function getQuerycount() | ||
{ | ||
return $this->data['querycount']; | ||
} | ||
|
||
/** | ||
* getTime | ||
* | ||
* Return queries total time. | ||
* | ||
* @access public | ||
* @return float | ||
*/ | ||
public function getTime() | ||
{ | ||
return $this->data['time']; | ||
} | ||
|
||
/** | ||
* getName | ||
* | ||
* Return profiler identifier. | ||
* | ||
* @access public | ||
* @return string | ||
*/ | ||
public function getName() | ||
{ | ||
return 'db'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
/* | ||
* This file is part of Pomm's Silex™ ServiceProvider package. | ||
* | ||
* (c) 2014 Grégoire HUBERT <hubert.greg@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
namespace PommProject\Silex\ServiceProvider; | ||
|
||
use Silex\Application; | ||
use Silex\ServiceProviderInterface; | ||
|
||
use PommProject\Silex\ServiceProvider\DatabaseDataCollector; | ||
|
||
use Symfony\Bridge\Twig\Extension\YamlExtension; | ||
|
||
/** | ||
* PommProfilerServiceProvider | ||
* | ||
* Silex ServiceProvider for Pomm profiler. | ||
* | ||
* @package PommServiceProvider | ||
* @copyright 2014 Grégoire HUBERT | ||
* @author Jérôme MACIAS | ||
* @author Grégoire HUBERT | ||
* @license X11 {@link http://opensource.org/licenses/mit-license.php} | ||
* @see ServiceProviderInterface | ||
*/ | ||
class PommProfilerServiceProvider implements ServiceProviderInterface | ||
{ | ||
/** | ||
* register | ||
* | ||
* @see ServiceProviderInterface | ||
*/ | ||
public function register(Application $app) | ||
{ | ||
$app['data_collectors'] = $app->share( | ||
$app->extend( | ||
'data_collectors', | ||
function ($collectors, $app) { | ||
$collectors['db'] = function () use ($app) { | ||
return new DatabaseDataCollector($app['pomm']); | ||
}; | ||
|
||
return $collectors; | ||
})); | ||
|
||
$app['data_collector.templates'] = array_merge( | ||
$app['data_collector.templates'], | ||
[['db', '@Pomm/Profiler/db.html.twig']] | ||
); | ||
|
||
$app['twig'] = $app->share($app->extend('twig', function ($twig, $app) { | ||
if (!$twig->hasExtension('yaml')) { | ||
$twig->addExtension(new YamlExtension()); | ||
} | ||
|
||
return $twig; | ||
})); | ||
|
||
$app->extend('twig.loader.filesystem', function ($loader, $app) { | ||
$loader->addPath($app['pomm.templates_path'], 'Pomm'); | ||
|
||
return $loader; | ||
}); | ||
|
||
$app['pomm.templates_path'] = function () { | ||
$r = new \ReflectionClass('PommProject\Silex\ServiceProvider\DatabaseDataCollector'); | ||
|
||
return dirname(dirname(dirname($r->getFileName()))).'/views'; | ||
}; | ||
} | ||
|
||
/** | ||
* boot | ||
* | ||
* @see ServiceProviderInterface | ||
*/ | ||
public function boot(Application $app) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.