2011-03-05 12:06:17 +00:00
< ? php
/*
* This file is part of the Symfony package .
*
2011-03-16 14:16:21 +00:00
* ( c ) Fabien Potencier < fabien @ symfony . com >
2011-03-05 12:06:17 +00:00
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
namespace Symfony\Component\HttpKernel\Profiler ;
/**
* A ProfilerStorage for Mysql
*
* @ author Jan Schumann < js @ schumann - it . com >
*/
class MysqlProfilerStorage extends PdoProfilerStorage
{
/**
* { @ inheritdoc }
*/
protected function initDb ()
{
2011-03-16 19:47:50 +00:00
if ( null === $this -> db ) {
2012-01-11 19:20:58 +00:00
if ( 0 !== strpos ( $this -> dsn , 'mysql' )) {
2012-03-04 18:43:39 +00:00
throw new \RuntimeException ( sprintf ( 'Please check your configuration. You are trying to use Mysql with an invalid dsn "%s". The expected format is "mysql:dbname=database_name;host=host_name".' , $this -> dsn ));
2011-03-14 14:53:03 +00:00
}
2011-03-16 14:16:21 +00:00
if ( ! class_exists ( 'PDO' ) || ! in_array ( 'mysql' , \PDO :: getAvailableDrivers (), true )) {
2011-03-05 12:06:17 +00:00
throw new \RuntimeException ( 'You need to enable PDO_Mysql extension for the profiler to run properly.' );
}
$db = new \PDO ( $this -> dsn , $this -> username , $this -> password );
2011-07-04 12:53:57 +01:00
$db -> exec ( 'CREATE TABLE IF NOT EXISTS sf_profiler_data (token VARCHAR(255) PRIMARY KEY, data LONGTEXT, ip VARCHAR(64), method VARCHAR(6), url VARCHAR(255), time INTEGER UNSIGNED, parent VARCHAR(255), created_at INTEGER UNSIGNED, KEY (created_at), KEY (ip), KEY (method), KEY (url), KEY (parent))' );
2011-03-05 12:06:17 +00:00
$this -> db = $db ;
}
return $this -> db ;
}
2011-03-14 14:53:03 +00:00
/**
* { @ inheritdoc }
*/
2012-11-25 17:16:08 +00:00
protected function buildCriteria ( $ip , $url , $start , $end , $limit , $method )
2011-03-14 14:53:03 +00:00
{
$criteria = array ();
$args = array ();
if ( $ip = preg_replace ( '/[^\d\.]/' , '' , $ip )) {
$criteria [] = 'ip LIKE :ip' ;
2011-03-15 13:16:19 +00:00
$args [ ':ip' ] = '%' . $ip . '%' ;
2011-03-14 14:53:03 +00:00
}
if ( $url ) {
$criteria [] = 'url LIKE :url' ;
2011-03-15 13:16:19 +00:00
$args [ ':url' ] = '%' . addcslashes ( $url , '%_\\' ) . '%' ;
2011-03-14 14:53:03 +00:00
}
2011-07-04 12:53:57 +01:00
if ( $method ) {
$criteria [] = 'method = :method' ;
$args [ ':method' ] = $method ;
}
2012-12-15 21:37:18 +00:00
if ( ! empty ( $start )) {
$criteria [] = 'time >= :start' ;
$args [ ':start' ] = $start ;
}
2012-11-25 17:16:08 +00:00
2012-12-15 21:37:18 +00:00
if ( ! empty ( $end )) {
$criteria [] = 'time <= :end' ;
$args [ ':end' ] = $end ;
}
2012-11-25 17:16:08 +00:00
2011-03-14 14:53:03 +00:00
return array ( $criteria , $args );
}
2011-03-05 12:06:17 +00:00
}