Skip to content

Commit

Permalink
Added Profiler for Silex 1.
Browse files Browse the repository at this point in the history
  • Loading branch information
chanmix51 committed Dec 8, 2014
1 parent 8a8dad5 commit 404a833
Show file tree
Hide file tree
Showing 5 changed files with 346 additions and 31 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
.ctags
vendor/*
161 changes: 161 additions & 0 deletions sources/lib/DatabaseDataCollector.php
@@ -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';
}
}
85 changes: 85 additions & 0 deletions sources/lib/PommProfilerServiceProvider.php
@@ -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)
{
}
}
62 changes: 31 additions & 31 deletions sources/lib/PommServiceProvider.php
Expand Up @@ -9,53 +9,53 @@
*/
namespace PommProject\Silex\ServiceProvider;

use PommProject\Foundation\Pomm;

use Silex\Application;
use Silex\ServiceProviderInterface;

use PommProject\Foundation\Pomm;

/**
* PommServiceProvider
*
* Silex ServiceProvider for Pomm.
* Silex ServiceProvider for Pomm2.
*
* @package PommServiceProvider
* @copyright 2014 Grégoire HUBERT
* @author Grégoire HUBERT
* @license X11 {@link http://opensource.org/licenses/mit-license.php}
* @see ServiceProviderInterface
*/
class PommServiceProvider implements ServiceProviderInterface
class PommServiceProvider implements ServiceProviderInterface
{
/**
* register
*
* @see ServiceProviderInterface
*/
public function register(Application $app)
{
$app['pomm'] = $app->share(function($c) {
$pomm = new Pomm($c['pomm.configuration']);
/**
* register
*
* @see ServiceProviderInterface
*/
public function register(Application $app)
{
$app['pomm'] = $app->share(function () use ($app) {
$pomm = new Pomm($app['pomm.configuration']);

$service = isset($c['pomm.logger.service'])
? $c['pomm.logger.service']
: 'monolog'
;
$service = isset($app['pomm.logger.service'])
? $app['pomm.logger.service']
: 'monolog'
;

if (isset($c[$service])) {
$pomm->setLogger($c[$service]);
}
if (isset($app[$service])) {
$pomm->setLogger($app[$service]);
}

return $pomm;
});
}
return $pomm;
});
}

/**
* boot
*
* @see ServiceProviderInterface
*/
public function boot(Application $app)
{
}
/**
* boot
*
* @see ServiceProviderInterface
*/
public function boot(Application $app)
{
}
}

0 comments on commit 404a833

Please sign in to comment.