Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
2746b6ed
Commit
2746b6ed
authored
May 25, 2012
by
Alexander Makarov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
vardumper
parent
a9509623
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
145 additions
and
0 deletions
+145
-0
VarDumper.php
framework/util/VarDumper.php
+145
-0
No files found.
framework/util/VarDumper.php
0 → 100644
View file @
2746b6ed
<?php
/**
* VarDumper class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright © 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yii\util
;
/**
* VarDumper is intended to replace the buggy PHP function var_dump and print_r.
* It can correctly identify the recursively referenced objects in a complex
* object structure. It also has a recursive depth control to avoid indefinite
* recursive display of some peculiar variables.
*
* VarDumper can be used as follows,
* <pre>
* VarDumper::dump($var);
* </pre>
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class
VarDumper
{
private
static
$_objects
;
private
static
$_output
;
private
static
$_depth
;
/**
* Displays a variable.
* This method achieves the similar functionality as var_dump and print_r
* but is more robust when handling complex objects such as Yii controllers.
* @param mixed $var variable to be dumped
* @param integer $depth maximum depth that the dumper should go into the variable. Defaults to 10.
* @param boolean $highlight whether the result should be syntax-highlighted
*/
public
static
function
dump
(
$var
,
$depth
=
10
,
$highlight
=
false
)
{
echo
self
::
dumpAsString
(
$var
,
$depth
,
$highlight
);
}
/**
* Dumps a variable in terms of a string.
* This method achieves the similar functionality as var_dump and print_r
* but is more robust when handling complex objects such as Yii controllers.
* @param mixed $var variable to be dumped
* @param integer $depth maximum depth that the dumper should go into the variable. Defaults to 10.
* @param boolean $highlight whether the result should be syntax-highlighted
* @return string the string representation of the variable
*/
public
static
function
dumpAsString
(
$var
,
$depth
=
10
,
$highlight
=
false
)
{
self
::
$_output
=
''
;
self
::
$_objects
=
array
();
self
::
$_depth
=
$depth
;
self
::
dumpInternal
(
$var
,
0
);
if
(
$highlight
)
{
$result
=
highlight_string
(
"<?php
\n
"
.
self
::
$_output
,
true
);
self
::
$_output
=
preg_replace
(
'/<\\?php<br \\/>/'
,
''
,
$result
,
1
);
}
return
self
::
$_output
;
}
/*
* @param mixed $var variable to be dumped
* @param integer $level depth level
*/
private
static
function
dumpInternal
(
$var
,
$level
)
{
switch
(
gettype
(
$var
))
{
case
'boolean'
:
self
::
$_output
.=
$var
?
'true'
:
'false'
;
break
;
case
'integer'
:
self
::
$_output
.=
"
$var
"
;
break
;
case
'double'
:
self
::
$_output
.=
"
$var
"
;
break
;
case
'string'
:
self
::
$_output
.=
"'"
.
addslashes
(
$var
)
.
"'"
;
break
;
case
'resource'
:
self
::
$_output
.=
'{resource}'
;
break
;
case
'NULL'
:
self
::
$_output
.=
"null"
;
break
;
case
'unknown type'
:
self
::
$_output
.=
'{unknown}'
;
break
;
case
'array'
:
if
(
self
::
$_depth
<=
$level
)
self
::
$_output
.=
'array(...)'
;
else
if
(
empty
(
$var
))
self
::
$_output
.=
'array()'
;
else
{
$keys
=
array_keys
(
$var
);
$spaces
=
str_repeat
(
' '
,
$level
*
4
);
self
::
$_output
.=
"array
\n
"
.
$spaces
.
'('
;
foreach
(
$keys
as
$key
)
{
if
(
gettype
(
$key
)
==
'integer'
)
$key2
=
$key
;
else
$key2
=
"'"
.
str_replace
(
"'"
,
"
\\
'"
,
$key
)
.
"'"
;
self
::
$_output
.=
"
\n
"
.
$spaces
.
"
$key2
=> "
;
self
::
$_output
.=
self
::
dumpInternal
(
$var
[
$key
],
$level
+
1
);
}
self
::
$_output
.=
"
\n
"
.
$spaces
.
')'
;
}
break
;
case
'object'
:
if
((
$id
=
array_search
(
$var
,
self
::
$_objects
,
true
))
!==
false
)
self
::
$_output
.=
get_class
(
$var
)
.
'#'
.
(
$id
+
1
)
.
'(...)'
;
else
if
(
self
::
$_depth
<=
$level
)
self
::
$_output
.=
get_class
(
$var
)
.
'(...)'
;
else
{
$id
=
array_push
(
self
::
$_objects
,
$var
);
$className
=
get_class
(
$var
);
$members
=
(
array
)
$var
;
$spaces
=
str_repeat
(
' '
,
$level
*
4
);
self
::
$_output
.=
"
$className
#
$id
\n
"
.
$spaces
.
'('
;
foreach
(
$members
as
$key
=>
$value
)
{
$keyDisplay
=
strtr
(
trim
(
$key
),
array
(
"
\0
"
=>
':'
));
self
::
$_output
.=
"
\n
"
.
$spaces
.
" [
$keyDisplay
] => "
;
self
::
$_output
.=
self
::
dumpInternal
(
$value
,
$level
+
1
);
}
self
::
$_output
.=
"
\n
"
.
$spaces
.
')'
;
}
break
;
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment