Skip to content

Commit

Permalink
Merge pull request gajus#35 from mdevine82/master
Browse files Browse the repository at this point in the history
- Add support for profiling CLI scripts
  • Loading branch information
staabm committed Mar 23, 2015
2 parents ca4e004 + 8704d3b commit a68b0ab
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
5 changes: 0 additions & 5 deletions inc/prepend.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ function xhprof_init()
return false;
}

// currently not supported on CLI
if(php_sapi_name() == 'cli') {
return false;
}

// do not profile debugging sessions (ZendDebugger)
if (!empty($_COOKIE['start_debug'])) {
return false;
Expand Down
24 changes: 16 additions & 8 deletions xhprof/classes/data.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,35 +132,43 @@ public function get($id)
*/
public function save(array $xhprof_data)
{
if(!isset($_SERVER['HTTP_HOST'], $_SERVER['REQUEST_URI'], $_SERVER['REQUEST_METHOD'])) {
throw new DataException('XHProf.io cannot function in a server environment that does not define REQUEST_METHOD, HTTP_HOST or REQUEST_URI.');
if(php_sapi_name() == 'cli') {
$method = 'CRON';
$host = gethostname();
$uri = implode(' ', $_SERVER['argv']);
}else if(isset($_SERVER['HTTP_HOST'], $_SERVER['REQUEST_URI'], $_SERVER['REQUEST_METHOD'])) {
$method = $_SERVER['REQUEST_METHOD'];
$host = $_SERVER['HTTP_HOST'];
$uri = $_SERVER['REQUEST_URI'];
}else{
throw new DataException('XHProf.io cannot function in a server environment that does not define a CRON environment or REQUEST_METHOD, HTTP_HOST or REQUEST_URI.');
}

$query = sprintf("
SELECT
(SELECT `id` FROM `request_methods` WHERE `method` = %s LIMIT 1) as 'method_id',
(SELECT `id` FROM `request_hosts` WHERE `host` = %s LIMIT 1) as 'host_id',
(SELECT `id` FROM `request_uris` WHERE `uri` = %s LIMIT 1) as 'uri_id';",
$this->db->quote($_SERVER['REQUEST_METHOD']),
$this->db->quote($_SERVER['HTTP_HOST']),
$this->db->quote($_SERVER['REQUEST_URI'])
$this->db->quote($method),
$this->db->quote($host),
$this->db->quote($uri)
);
$request = $this->db->query($query)->fetch(PDO::FETCH_ASSOC);

if(!isset($request['method_id'])) {
$this->db->query(sprintf("INSERT INTO `request_methods` SET `method` = %s;", $this->db->quote($_SERVER['REQUEST_METHOD'])));
$this->db->query(sprintf("INSERT INTO `request_methods` SET `method` = %s;", $this->db->quote($method)));

$request['method_id'] = $this->db->lastInsertId();
}

if(!isset($request['host_id'])) {
$this->db->query(sprintf("INSERT INTO `request_hosts` SET `host` = %s;", $this->db->quote($_SERVER['HTTP_HOST'])));
$this->db->query(sprintf("INSERT INTO `request_hosts` SET `host` = %s;", $this->db->quote($host)));

$request['host_id'] = $this->db->lastInsertId();
}

if(!isset($request['uri_id'])) {
$this->db->query(sprintf("INSERT INTO `request_uris` SET `uri` = %s;", $this->db->quote($_SERVER['REQUEST_URI'])));
$this->db->query(sprintf("INSERT INTO `request_uris` SET `uri` = %s;", $this->db->quote($uri)));

$request['uri_id'] = $this->db->lastInsertId();
}
Expand Down

0 comments on commit a68b0ab

Please sign in to comment.