Permalink
Browse files

Merge pull request #6 from mariano/i5

Fixes #5: notices when rendering application errors
  • Loading branch information...
2 parents 8d23a27 + 123a3bc commit c9b25f957a87ced2103c2e692a20473d6097bc13 @tmaiaroto committed Jan 10, 2012
Showing with 73 additions and 69 deletions.
  1. +26 −24 config/bootstrap/dispatcher.php
  2. +47 −45 extensions/helper/Li3perf.php
@@ -9,28 +9,28 @@
// At this point, the routing has completed. In order to call _callable, it's routed.
// So this is ever so slightly off actually.
Data::append('timers', array('li3_perf_has_route' => microtime(true)));
-
+
$result = $chain->next($self, $params, $chain);
-
+
// Now that we know whether or not the request is callable, it's going to be called.
// This is esentially right before the code in the controller action is executed.
// So mark the time.
Data::append('timers', array('li3_perf_start_call' => microtime(true)));
-
+
return $result;
});
Dispatcher::applyFilter('_call', function($self, $params, $chain) {
-
+
$result = $chain->next($self, $params, $chain);
-
+
// At this point the controller action has been called and now a response will be returned.
// $result here contains the response and we've been setting timers all along the way...
- // The next time we'll be working with the same response is under the next filter below on
+ // The next time we'll be working with the same response is under the next filter below on
// run() AFTER $result = $chain->next() is called... That's the end of the dispatch cycle.
// The $result = part below is actually before this filter and the filter on _callable() above.
Data::append('timers', array('li3_perf_end_call' => microtime(true)));
-
+
return $result;
});
@@ -39,15 +39,15 @@
if(substr($params['request']->url, 0, 17) == '/li3_perf/profile') {
return $chain->next($self, $params, $chain);
}
-
+
Data::append('timers', array('li3_perf_start_dispatch' => microtime(true)));
-
+
$result = $chain->next($self, $params, $chain);
-
- // Mark the end of li3_perf.
+
+ // Mark the end of li3_perf.
// Note: The time it takes to render the toolbar will not be included.
Data::append('timers', array('li3_perf_end' => microtime(true)));
-
+
// Render the toolbar (unless it's an asset from the li3_perf library)
// Why? See li3_perf\extensions\util\Asset
if(!isset($params['request']->params['asset_type'])) {
@@ -57,15 +57,17 @@
'layout' => '{:library}/views/layouts/{:layout}.{:type}.php',
)
));
-
+
$timers = Data::get('timers');
-
- $toolbar = $View->render('all',
+
+ $toolbar = $View->render('all',
array(
'timers' => $timers += array(
'dispatch_cycle' => $timers['li3_perf_end'] - $timers['li3_perf_start_dispatch'],
'routing' => $timers['li3_perf_has_route'] - $timers['li3_perf_start_dispatch'],
- 'call' => $timers['li3_perf_end_call'] - $timers['li3_perf_start_call'],
+ 'call' => isset($timers['li3_perf_end_call']) && isset($timers['li3_perf_start_call']) ?
+ $timers['li3_perf_end_call'] - $timers['li3_perf_start_call'] :
+ 0,
'complete_load_with_li3_perf' => microtime(true) - $timers['li3_perf_start'],
'complete_load' => ($timers['li3_perf_end'] - $timers['li3_perf_start']) - $timers['_filter_for_variables'] - $timers['_filter_for_queries']
),
@@ -74,13 +76,13 @@
'view' => Data::get('view_vars')
),
'queries' => Data::get('queries')
- ),
+ ),
array(
'library' => 'li3_perf',
'template' => 'toolbar',
'layout' => 'default'
));
-
+
// Add the toolbar to the body of the current page. Don't just echo it out now.
// There are sometimes issues with the headers already being sent otherwise.
// TODO: IF proper HTML were to be desired, perhaps insert $toolbar into the body in the
@@ -91,34 +93,34 @@
$controller = isset($params['request']->params['controller']) ? $params['request']->params['controller']:null;
$action = isset($params['request']->params['action']) ? $params['request']->params['action']:null;
$library = isset($params['request']->params['library']) ? $params['request']->params['library']:null;
-
+
// Check to see if the toolbar should be shown for this library
if(isset($li3_perf['skip']['library'])) {
if(in_array($library, $li3_perf['skip']['library'])) {
$skip = true;
}
}
-
+
// Check to see if the toolbar should be shown for this controller
if(isset($li3_perf['skip']['controller'])) {
if(in_array($controller, $li3_perf['skip']['controller'])) {
$skip = true;
}
}
-
+
// Check to see if the toolbar should be shown for this action
if(isset($li3_perf['skip']['action'])) {
if(in_array($action, $li3_perf['skip']['action'])) {
$skip = true;
}
}
}
-
+
if(isset($result->body[0]) && !$skip) {
$result->body[0] = $toolbar . $result->body[0];
}
}
-
+
return $result;
});
-?>
+?>
@@ -2,60 +2,60 @@
namespace li3_perf\extensions\helper;
class Li3perf extends \lithium\template\helper\Html {
-
+
public function printVars($var) {
$html = "<pre>";
-
+
if (@is_array($var)) {
$html .= var_dump($var);
} else {
$html .= $var;
}
-
+
$html .= "</pre>";
-
+
return $html;
}
-
+
/**
* Formats a size in bytes as kb, mb, or gb.
- *
+ *
* @param int $bytes
* @param array $size_labels Keyed for 'k', 'm', and 'g' the labels for the sizes.
* @return type string
*/
- public function byteSize($bytes, $size_labels=array()) {
+ public function byteSize($bytes, $size_labels=array()) {
$size_labels += array('k' => 'K', 'm' => 'M', 'g' => 'G');
-
- $size = $bytes / 1024;
- if($size < 1024)
- {
- $size = number_format($size, 2);
- $size .= $size_labels['k'];
- }
- else
- {
- if($size / 1024 < 1024)
- {
- $size = number_format($size / 1024, 2);
- $size .= $size_labels['m'];
- }
- else if ($size / 1024 / 1024 < 1024)
- {
- $size = number_format($size / 1024 / 1024, 2);
- $size .= $size_labels['g'];
- }
- }
- return $size;
+
+ $size = $bytes / 1024;
+ if($size < 1024)
+ {
+ $size = number_format($size, 2);
+ $size .= $size_labels['k'];
+ }
+ else
+ {
+ if($size / 1024 < 1024)
+ {
+ $size = number_format($size / 1024, 2);
+ $size .= $size_labels['m'];
+ }
+ else if ($size / 1024 / 1024 < 1024)
+ {
+ $size = number_format($size / 1024 / 1024, 2);
+ $size .= $size_labels['g'];
+ }
+ }
+ return $size;
}
-
+
/**
* Calculates the size (memory use) of a variable.
* NOTE: This is not 100% accurate by any means, but should give a good ballpark.
* It works by recording the current memory usage and then getting the memory usage
* after copying a variable. The difference between these two values is approximately the
* memory usage. There is no PHP function to do this otherwise.
- *
+ *
* There's several ways people calculate the size of a variable and it all depends on if the
* variable is an array, object, string, or integer. This is the most accurate way I've seen
* when using APC as a control. APC stores variables in an optimized fashion and even a null
@@ -64,33 +64,33 @@ public function byteSize($bytes, $size_labels=array()) {
* The value is a lot lower when trying to determine the size of an integer and it should be.
* How accurate is this method or even APC's memory usage compared to PHP's?
* Likely not accurate at all...But this is the best ballpark guess that we have.
- *
- * Xdebug profiling may be another real good way to determine memory usage of the overall
+ *
+ * Xdebug profiling may be another real good way to determine memory usage of the overall
* application, but I saw no functions that return memory size of invidual variabes.
* It may be possible with Xdebug...But this method doesn't require it to be installed.
- *
+ *
* Another note, a string of 'hello' may be assumed to be 5 bytes but in PHP it can be more.
- * Not only can strings be multi-byte encoded, but PHP also carries extra overhead due to
+ * Not only can strings be multi-byte encoded, but PHP also carries extra overhead due to
* it's non-strict type nature. PHP is not efficient at storing things in memory and that's ok.
- * But let's let PHP tell us it's memory usage instead of assuming based on how much space
+ * But let's let PHP tell us it's memory usage instead of assuming based on how much space
* something takes up in a database or an assumption of a character being equal to one byte.
- *
+ *
* @param mixed $var The variable you want to know the size for
* @return int Size in bytes
*/
public function varSize($var=null) {
-
+
$current_mem = memory_get_usage();
$tmp_var = $this->_varCopy($var);
$variable_size = memory_get_usage() - $current_mem;
unset($tmp_var);
return $variable_size;
-
+
}
-
+
/**
* Used in determining the size of a variable.
- *
+ *
* @see varSize()
* @param mixed $src The variable
* @return mixed A copy of the variable
@@ -111,10 +111,12 @@ private function _varCopy($src=0) {
return NULL;
}
- if (is_object($src)) {
+ // Filters can trigger an endlees loop when iterated from code that
+ // is reached by the same filtered being iterated
+ if (is_object($src) && !$src instanceof \lithium\util\Collection\Filters) {
$new = (object) array();
foreach ($src as $key => $val) {
- $new->$key = $this->_varCopy($val);
+ $new->$key = $this->_varCopy($val);
}
return $new;
}
@@ -129,9 +131,9 @@ private function _varCopy($src=0) {
foreach ($src as $key => $val) {
$new[$key] = $this->_varCopy($val);
}
-
+
return $new;
}
-
+
}
-?>
+?>

0 comments on commit c9b25f9

Please sign in to comment.