Skip to content

Commit

Permalink
- improved Debug::dump(), added Debug::$maxDepth & $maxLen
Browse files Browse the repository at this point in the history
- fixed ServiceLocator
  • Loading branch information
dg committed May 2, 2008
1 parent 974e74f commit 3154e91
Show file tree
Hide file tree
Showing 12 changed files with 212 additions and 56 deletions.
8 changes: 8 additions & 0 deletions Nette/Caching/Cache.php
Expand Up @@ -92,6 +92,14 @@ public function release()

/**
* Writes item into the cache.
* Dependencies are:
* priority => (int) priority
* expire => (timestamp) expiration
* refresh => (bool) use sliding expiration?
* tags => (array) tags
* files => (array|string) file names
* items => (array|string) cache items
*
* @param string key
* @param mixed
* @param array
Expand Down
67 changes: 63 additions & 4 deletions Nette/Debug.php
Expand Up @@ -44,6 +44,12 @@ final class Debug
/** @var bool send the error report to the browser? */
public static $display = TRUE;

/** @var int how many nested levels of array/object properties display Debug::dump()? */
public static $maxDepth = 3;

/** @var int how long strings display Debug::dump()? */
public static $maxLen = 150;

/** @var string directory where reports are written to files */
public static $logDir; // TODO: or $logFileMask ?
Expand Down Expand Up @@ -148,12 +154,65 @@ public static function configure(Config $config)
public static function dump($var, $return = FALSE)
{
ob_start();
var_dump($var);
if (extension_loaded('xdebug')) {
// or call xdebug_var_dump ?
$old = ini_set('html_errors', '0');
var_dump($var);
ini_set('html_errors', $old);
} else {
var_dump($var);
}
$output = ob_get_clean();


// shorten long strings & limit nesting
if (self::$maxLen) {
$space = str_repeat(' ', self::$maxDepth);
$replace = "\n$space ...";
$delta = $min = 0;
preg_match_all(
"#(string|\n$space&?array|\n$space&?object)\\((.+?)\\)[^{\"]*[{\"]#",
$output,
$matches,
PREG_OFFSET_CAPTURE | PREG_SET_ORDER
);
foreach ($matches as $m) {
$pos = $m[0][1] + strlen($m[0][0]) + $delta;
if ($pos < $min) continue;

if ($m[1][0] === 'string') {
$len = (int) $m[2][0];
if ($len > self::$maxLen) {
$min = $pos + self::$maxLen;
$output = substr_replace(
$output,
' ... ',
$pos + self::$maxLen,
$len - self::$maxLen
);
$delta -= $len - self::$maxLen - 5;
} else {
$min = $pos + $len;
}
} else {
$len = strpos($output, "\n$space}\n", $pos) - $pos;
$min = $pos;
$output = substr_replace(
$output,
$replace,
$pos,
$len
);
$delta -= $len - strlen($replace);
}
}
}


// reformat
if (self::$html) {
$output = htmlspecialchars($output, ENT_NOQUOTES);
$output = preg_replace('#\]=&gt;\n\ +([a-z]+)#i', '] => <span>$1</span>', $output);
$output = preg_replace('#\[(.+?)\]=&gt;\n\ +([a-z]+)#i', '$1 => <span>$2</span>', $output);
$output = preg_replace('#^([a-z]+)#i', '<span>$1</span>', $output);
$output = "<pre class=\"dump\">$output</pre>\n";
} else {
Expand Down Expand Up @@ -342,7 +401,7 @@ private static function handleMessage($message)
}

if (self::$display) {
while (ob_get_level() && ob_end_clean());
while (ob_get_level() && @ob_end_clean());

echo $message;

Expand Down Expand Up @@ -416,7 +475,7 @@ private static function safedump($var, $key = NULL)
}

return preg_replace(
'#^(\s*\["(' . implode('|', self::$keysToHide) . ')"\] => <span>string</span>).+#mi',
'#^(\s*"(' . implode('|', self::$keysToHide) . ')" => <span>string</span>).+#mi',
'$1 (?) <i>*** hidden ***</i>',
self::dump($var, TRUE)
);
Expand Down
12 changes: 12 additions & 0 deletions Nette/Environment.php
Expand Up @@ -381,6 +381,18 @@ public static function factoryCacheStorage()



/**
* Returns instance of session namespace.
* @param string
* @return Nette::Web::SessionNamespace
*/
public static function getSession($name = 'default')
{
return /*Nette::*/Session::getNamespace($name);
}



/********************* global configuration ****************d*g**/


Expand Down
35 changes: 23 additions & 12 deletions Nette/ServiceLocator.php
Expand Up @@ -82,9 +82,6 @@ public function addService($service, $name = NULL, $promote = FALSE)
throw new /*::*/InvalidArgumentException('Service must be class/interface name, object or factory callback.');
}

/**/// fix for namespaced classes/interfaces in PHP < 5.3
if ($a = strrpos($name, ':')) $name = substr($name, $a + 1);/**/

$lower = strtolower($name);
if (isset($this->registry[$lower]) && is_object($this->registry[$lower])) {
throw new AmbiguousServiceException("Ambiguous service '$name'.");
Expand All @@ -100,6 +97,8 @@ public function addService($service, $name = NULL, $promote = FALSE)

/**
* Removes the specified service type from the service container.
* @param bool promote to higher level?
* @return void
*/
public function removeService($name, $promote = FALSE)
{
Expand All @@ -118,33 +117,45 @@ public function removeService($name, $promote = FALSE)

/**
* Gets the service object of the specified type.
* @param string service name
* @return void
*/
public function getService($name)
{
if (!is_string($name) || $name === '') {
throw new /*::*/InvalidArgumentException('Service name must be a non-empty string.');
}

/**/// fix for namespaced classes/interfaces in PHP < 5.3
if ($a = strrpos($name, ':')) $name = substr($name, $a + 1);/**/

$lower = strtolower($name);

if (isset($this->registry[$lower])) {
$service = $this->registry[$lower];
if (is_object($service)) {
return $service;
}

if (is_string($service)) {
if (substr($service, -2) === '()') {
// trick to pass callback as string
$service = substr($service, 0, -2);

} elseif (is_string($service)) {
/**/// fix for namespaced classes/interfaces in PHP < 5.3
if ($a = strrpos($service, ':')) $service = substr($service, $a + 1);/**/
return $this->registry[$lower] = new $service;
} else {
/**/// fix for namespaced classes/interfaces in PHP < 5.3
if ($a = strrpos($service, ':')) $service = substr($service, $a + 1);/**/

} else {
return $this->registry[$lower] = call_user_func($service);
if (!class_exists($service)) {
throw new AmbiguousServiceException("Class '$service' not found.");
}
return $this->registry[$lower] = new $service;
}
}

return $this->registry[$lower] = call_user_func($service);

} elseif ($this->autoDiscovery) {
/**/// fix for namespaced classes/interfaces in PHP < 5.3
if ($a = strrpos($name, ':')) $name = substr($name, $a + 1);/**/

if (class_exists($name)) {
return $this->registry[$lower] = new $name;
}
Expand Down
11 changes: 4 additions & 7 deletions Nette/Web/HttpRequest.php
Expand Up @@ -334,16 +334,13 @@ public function getHeader($key = NULL, $default = NULL)


/**
* Returns URL object
* @return Uri
* Returns referrer
* @return Uri|NULL
*/
public function getReferer()
{
static $uri;
if ($uri === NULL) {
$uri = new Uri(self::getHeader('referer'));
}
return clone $uri;
$uri = self::getHeader('referer');
return $uri ? new Uri($uri) : NULL;
}


Expand Down
4 changes: 2 additions & 2 deletions Nette/Web/User.php
Expand Up @@ -70,7 +70,7 @@ class User extends /*Nette::*/Object
public function __construct($name = NULL)
{
$this->namespace = $name === NULL ? $this->getClass() : $name;
$this->cookiePath = Environment::getHttpRequest()->getUri()->basePath;
$this->cookiePath = Environment::getHttpRequest()->getUri()->basePath . '/';
$this->initSession();
}

Expand Down Expand Up @@ -130,7 +130,7 @@ final public function getAuthorizationHandler()
*/
protected function initSession()
{
$this->session = $session = Session::getNamespace($this->namespace);
$this->session = $session = Environment::getSession($this->namespace);

if (!($session->identity instanceof /*Nette::Security::*/IIdentity)) {
$session->identity = NULL;
Expand Down
2 changes: 1 addition & 1 deletion Nette/templates/Debug.phtml
Expand Up @@ -301,7 +301,7 @@ if (headers_sent()) {
<?php endif ?>


<h3><a href="#" onclick="return !toggle(this, 'pnl-env-files')">Included files <span>&#x25b6;</span></a></h3>
<h3><a href="#" onclick="return !toggle(this, 'pnl-env-files')">Included files <span>&#x25b6;</span></a> (<?php echo count(get_included_files()) ?>)</h3>
<table id="pnl-env-files" class="collapsed">
<?php
foreach (get_included_files() as $v) {
Expand Down
34 changes: 34 additions & 0 deletions tests/Debug/ref/test.dump.limits.php.html
@@ -0,0 +1,34 @@
<style>
.dump { color:black; background:white; font-size:12px; text-align:left } .dump span { color: gray }
</style>

<h1>Nette::Debug dump with limits test</h1>


array(4) {
["file"] => string(612) "<style>
.dump { color:black; background:white; font-size:12px; text-align:left } .dump span { color: gray }
</style>

<h1>Nette::Debug dump with l ... "
[0] => array(1) {
[0] => array(1) {
[0] => array(1) {
...
}
}
}
["file2"] => string(612) "<style>
.dump { color:black; background:white; font-size:12px; text-align:left } .dump span { color: gray }
</style>

<h1>Nette::Debug dump with l ... "
[1] => object(stdClass)#4 (1) {
[0] => object(stdClass)#3 (1) {
[0] => object(stdClass)#2 (1) {
...
}
}
}
}

24 changes: 12 additions & 12 deletions tests/Debug/ref/test.dump.php.html
Expand Up @@ -10,22 +10,22 @@ <h2>Check mode</h2>bool(false)
<h2>HTML mode</h2><pre class="dump"><span>string</span>(20) "&lt;a href="#"&gt;test&lt;/a&gt;"
</pre>
<pre class="dump"><span>array</span>(5) {
[0] => <span>int</span>(10)
[1] => <span>float</span>(20.2)
[2] => <span>bool</span>(true)
[3] => <span>NULL</span>
[4] => <span>string</span>(5) "hello"
0 => <span>int</span>(10)
1 => <span>float</span>(20.2)
2 => <span>bool</span>(true)
3 => <span>NULL</span>
4 => <span>string</span>(5) "hello"
}
</pre>
<pre class="dump"><span>object</span>(stdClass)#2 (2) {
["item1"] => <span>array</span>(5) {
[0] => <span>int</span>(10)
[1] => <span>float</span>(20.2)
[2] => <span>bool</span>(true)
[3] => <span>NULL</span>
[4] => <span>string</span>(5) "hello"
"item1" => <span>array</span>(5) {
0 => <span>int</span>(10)
1 => <span>float</span>(20.2)
2 => <span>bool</span>(true)
3 => <span>NULL</span>
4 => <span>string</span>(5) "hello"
}
["item2"] => <span>string</span>(5) "hello"
"item2" => <span>string</span>(5) "hello"
}
</pre>
<h2>Text mode</h2>string(20) "<a href="#">test</a>"
Expand Down
16 changes: 8 additions & 8 deletions tests/Debug/ref/test.exception.html.php.html
Expand Up @@ -175,9 +175,9 @@ <h2><a href="#" onclick="return !toggle(this, 'pnl2')">Call stack <span>&#x25bc;
<div class="collapsed" id="args0">
<table>
<tr><td>$arg1</td><td><pre class="dump"><span>array</span>(3) {
[0] => <span>int</span>(1)
[1] => <span>int</span>(2)
[2] => <span>int</span>(3)
0 => <span>int</span>(1)
1 => <span>int</span>(2)
2 => <span>int</span>(3)
}
</pre>
</td></tr> </table>
Expand Down Expand Up @@ -280,9 +280,9 @@ <h3><a href="#" onclick="return !toggle(this, 'pnl-env-const')">Constants <span>
</td></tr> </table>


<h3><a href="#" onclick="return !toggle(this, 'pnl-env-files')">Included files <span>&#x25b6;</span></a></h3>
<h3><a href="#" onclick="return !toggle(this, 'pnl-env-files')">Included files <span>&#x25b6;</span></a> (12)</h3>
<table id="pnl-env-files" class="collapsed">
<tr><td>D:\Web\nette\_trunk\tests\Debug\test.exception.html.php</td></tr><tr><td>D:\Web\_php\tools.debug.php</td></tr><tr><td>D:\Web\nette\_trunk\Nette\loader.php</td></tr><tr><td>D:\Web\nette\_trunk\Nette\exceptions.php</td></tr><tr><td>D:\Web\nette\_trunk\Nette\Loaders\NetteLoader.php</td></tr><tr><td>D:\Web\nette\_trunk\Nette\Loaders\AutoLoader.php</td></tr><tr><td>D:\Web\nette\_trunk\Nette\Object.php</td></tr><tr><td>D:\Web\nette\_trunk\Nette\Debug.php</td></tr><tr><td>D:\Web\nette\_trunk\Nette\Version.php</td></tr><tr><td>D:\Web\nette\_trunk\Nette\templates\Debug.phtml</td></tr><tr><td>D:\Web\nette\_trunk\Nette\templates\Debug.openpanel.phtml</td></tr><tr><td>D:\Web\nette\_trunk\Nette\templates\Debug.closepanel.phtml</td></tr> </table>
<tr><td>D:\Web\nette\_trunk\tests\Debug\test.exception.html.php</td></tr><tr><td>D:\Web\_php\tools.debug.php</td></tr><tr><td>D:\Web\nette\_trunk\Nette\loader.php</td></tr><tr><td>D:\Web\nette\_trunk\Nette\Loaders\NetteLoader.php</td></tr><tr><td>D:\Web\nette\_trunk\Nette\Loaders\AutoLoader.php</td></tr><tr><td>D:\Web\nette\_trunk\Nette\Object.php</td></tr><tr><td>D:\Web\nette\_trunk\Nette\exceptions.php</td></tr><tr><td>D:\Web\nette\_trunk\Nette\Debug.php</td></tr><tr><td>D:\Web\nette\_trunk\Nette\Version.php</td></tr><tr><td>D:\Web\nette\_trunk\Nette\templates\Debug.phtml</td></tr><tr><td>D:\Web\nette\_trunk\Nette\templates\Debug.openpanel.phtml</td></tr><tr><td>D:\Web\nette\_trunk\Nette\templates\Debug.closepanel.phtml</td></tr> </table>


<h3>$_SERVER</h3>
Expand Down Expand Up @@ -369,10 +369,10 @@ <h3>$_SERVER</h3>
</pre>
</td></tr><tr><td>DOCUMENT_ROOT</td><td><pre class="dump"><span>string</span>(0) ""
</pre>
</td></tr><tr><td>REQUEST_TIME</td><td><pre class="dump"><span>int</span>(1209424480)
</td></tr><tr><td>REQUEST_TIME</td><td><pre class="dump"><span>int</span>(1209703775)
</pre>
</td></tr><tr><td>argv</td><td><pre class="dump"><span>array</span>(1) {
[0] => <span>string</span>(23) "test.exception.html.php"
0 => <span>string</span>(23) "test.exception.html.php"
}
</pre>
</td></tr><tr><td>argc</td><td><pre class="dump"><span>int</span>(1)
Expand Down Expand Up @@ -434,7 +434,7 @@ <h3>Headers</h3>
<ul>
<li>PHP version 5.2.5</li>
<li>Nette Framework version 0.7 (Revision: $WCREV$, Date: $WCDATE$)</li>
<li>Report generated at 04/29/08 01:14:40</li>
<li>Report generated at 05/02/08 06:49:35</li>
</ul>

</body>
Expand Down

0 comments on commit 3154e91

Please sign in to comment.