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
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Rotua Panjaitan
yii2
Commits
2188da0c
Commit
2188da0c
authored
May 20, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #3419 from klimov-paul/3232-var-export
Fix for #3232 var export
parents
b898553c
6f3ffae7
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
144 additions
and
0 deletions
+144
-0
CHANGELOG.md
framework/CHANGELOG.md
+1
-0
BaseVarDumper.php
framework/helpers/BaseVarDumper.php
+54
-0
VarDumperTest.php
tests/unit/framework/helpers/VarDumperTest.php
+89
-0
No files found.
framework/CHANGELOG.md
View file @
2188da0c
...
...
@@ -42,6 +42,7 @@ Yii Framework 2 Change Log
-
Enh #3154: Added validation error display for
`GridView`
filters (ivan-kolmychek)
-
Enh #3222: Added
`useTablePrefix`
option to the model generator for Gii (horizons2)
-
Enh #3230: Added
`yii\filters\AccessControl::user`
to support access control with different actors (qiangxue)
-
Enh #3232: Added
`export()`
and
`exportAsString()`
methods to
`yii\helpers\BaseVarDumper`
(klimov-paul)
-
Enh #3252: Added support for case insensitive matching using ILIKE to PostgreSQL QueryBuilder (cebe)
-
Enh #3298: Supported configuring
`View::theme`
using a class name (netyum, qiangxue)
-
Enh #3328:
`BaseMailer`
generates better text body from html body (armab)
...
...
framework/helpers/BaseVarDumper.php
View file @
2188da0c
...
...
@@ -7,6 +7,8 @@
namespace
yii\helpers
;
use
yii\base\Arrayable
;
/**
* BaseVarDumper provides concrete implementation for [[VarDumper]].
*
...
...
@@ -123,4 +125,56 @@ class BaseVarDumper
break
;
}
}
/**
* Returns a parsable string representation of a variable.
* This method achieves the similar functionality as var_export
* but is more robust when handling arrays and objects.
* @param mixed $var variable to be exported.
* @return string parsable string representation of a variable.
*/
public
static
function
export
(
$var
)
{
self
::
$_output
=
''
;
self
::
exportInternal
(
$var
,
0
);
return
self
::
$_output
;
}
/**
* @param mixed $var variable to be exported
* @param integer $level depth level
*/
private
static
function
exportInternal
(
$var
,
$level
)
{
switch
(
gettype
(
$var
))
{
case
'NULL'
:
self
::
$_output
.=
'null'
;
break
;
case
'array'
:
if
(
empty
(
$var
))
{
self
::
$_output
.=
'[]'
;
}
else
{
$keys
=
array_keys
(
$var
);
$outputKeys
=
(
$keys
!==
range
(
0
,
sizeof
(
$var
)
-
1
));
$spaces
=
str_repeat
(
' '
,
$level
*
4
);
self
::
$_output
.=
'['
;
foreach
(
$keys
as
$key
)
{
self
::
$_output
.=
"
\n
"
.
$spaces
.
' '
;
if
(
$outputKeys
)
{
self
::
exportInternal
(
$key
,
0
);
self
::
$_output
.=
' => '
;
}
self
::
exportInternal
(
$var
[
$key
],
$level
+
1
);
self
::
$_output
.=
','
;
}
self
::
$_output
.=
"
\n
"
.
$spaces
.
']'
;
}
break
;
case
'object'
:
self
::
$_output
.=
'unserialize('
.
var_export
(
serialize
(
$var
),
true
)
.
')'
;
break
;
default
:
self
::
$_output
.=
var_export
(
$var
,
true
);
}
}
}
tests/unit/framework/helpers/VarDumperTest.php
View file @
2188da0c
<?php
namespace
yiiunit\framework\helpers
;
use
yii\data\ArrayDataProvider
;
use
yii\helpers\VarDumper
;
use
yiiunit\TestCase
;
...
...
@@ -17,4 +18,92 @@ class VarDumperTest extends TestCase
$this
->
assertEquals
(
"stdClass#1
\n
(
\n
)"
,
ob_get_contents
());
ob_end_clean
();
}
/**
* Data provider for [[testExport()]]
* @return array test data
*/
public
function
dataProviderExport
()
{
// Regular :
$data
=
[
[
'test string'
,
var_export
(
'test string'
,
true
)
],
[
75
,
var_export
(
75
,
true
)
],
[
7.5
,
var_export
(
7.5
,
true
)
],
[
null
,
'null'
],
[
true
,
'true'
],
[
false
,
'false'
],
[
[],
'[]'
],
];
// Arrays :
$var
=
[
'key1'
=>
'value1'
,
'key2'
=>
'value2'
,
];
$expectedResult
=
<<<RESULT
[
'key1' => 'value1',
'key2' => 'value2',
]
RESULT;
$data
[]
=
[
$var
,
$expectedResult
];
$var
=
[
'value1'
,
'value2'
,
];
$expectedResult
=
<<<RESULT
[
'value1',
'value2',
]
RESULT;
$data
[]
=
[
$var
,
$expectedResult
];
// Objects :
$var
=
new
\StdClass
();
$var
->
testField
=
'Test Value'
;
$expectedResult
=
"unserialize('"
.
serialize
(
$var
)
.
"')"
;
$data
[]
=
[
$var
,
$expectedResult
];
return
$data
;
}
/**
* @dataProvider dataProviderExport
*
* @param mixed $var
* @param string $expectedResult
*/
public
function
testExport
(
$var
,
$expectedResult
)
{
$exportResult
=
VarDumper
::
export
(
$var
);
$this
->
assertEquals
(
$expectedResult
,
$exportResult
);
$this
->
assertEquals
(
$var
,
eval
(
'return '
.
$exportResult
.
';'
));
}
}
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