Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
drupal-3174996
Manage
Activity
Members
Labels
Plan
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Issue forks
drupal-3174996
Commits
ab69f178
Commit
ab69f178
authored
11 years ago
by
catch
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#2022931
by tim.plunkett, ParisLiakos: Move drupal_var_export() to \Drupal\Component\Utility.
parent
b761f76a
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
core/includes/utility.inc
+5
-45
5 additions, 45 deletions
core/includes/utility.inc
core/lib/Drupal/Component/Utility/Variable.php
+74
-0
74 additions, 0 deletions
core/lib/Drupal/Component/Utility/Variable.php
with
79 additions
and
45 deletions
core/includes/utility.inc
+
5
−
45
View file @
ab69f178
...
...
@@ -5,6 +5,8 @@
* Miscellaneous functions.
*/
use
Drupal\Component\Utility\Variable
;
/**
* Drupal-friendly var_export().
*
...
...
@@ -15,51 +17,9 @@
*
* @return string
* The variable exported in a way compatible to Drupal's coding standards.
*
* @deprecated Use \Drupal\Component\Utility\Variable::export().
*/
function
drupal_var_export
(
$var
,
$prefix
=
''
)
{
if
(
is_array
(
$var
))
{
if
(
empty
(
$var
))
{
$output
=
'array()'
;
}
else
{
$output
=
"array(
\n
"
;
// Don't export keys if the array is non associative.
$export_keys
=
array_values
(
$var
)
!=
$var
;
foreach
(
$var
as
$key
=>
$value
)
{
$output
.
=
' '
.
(
$export_keys
?
drupal_var_export
(
$key
)
.
' => '
:
''
)
.
drupal_var_export
(
$value
,
' '
,
FALSE
)
.
",
\n
"
;
}
$output
.
=
')'
;
}
}
elseif
(
is_bool
(
$var
))
{
$output
=
$var
?
'TRUE'
:
'FALSE'
;
}
elseif
(
is_string
(
$var
))
{
if
(
strpos
(
$var
,
"
\n
"
)
!==
FALSE
||
strpos
(
$var
,
"'"
)
!==
FALSE
)
{
// If the string contains a line break or a single quote, use the
// double quote export mode. Encode backslash and double quotes and
// transform some common control characters.
$var
=
str_replace
(
array
(
'\\'
,
'"'
,
"
\n
"
,
"
\r
"
,
"
\t
"
),
array
(
'\\\\'
,
'\"'
,
'\n'
,
'\r'
,
'\t'
),
$var
);
$output
=
'"'
.
$var
.
'"'
;
}
else
{
$output
=
"'"
.
$var
.
"'"
;
}
}
elseif
(
is_object
(
$var
)
&&
get_class
(
$var
)
===
'stdClass'
)
{
// var_export() will export stdClass objects using an undefined
// magic method __set_state() leaving the export broken. This
// workaround avoids this by casting the object as an array for
// export and casting it back to an object when evaluated.
$output
=
'(object) '
.
drupal_var_export
((
array
)
$var
,
$prefix
);
}
else
{
$output
=
var_export
(
$var
,
TRUE
);
}
if
(
$prefix
)
{
$output
=
str_replace
(
"
\n
"
,
"
\n
$prefix
"
,
$output
);
}
return
$output
;
return
Variable
::
export
(
$var
,
$prefix
);
}
This diff is collapsed.
Click to expand it.
core/lib/Drupal/Component/Utility/Variable.php
0 → 100644
+
74
−
0
View file @
ab69f178
<?php
/**
* @file
* Contains Drupal\Component\Utility\Export.
*/
namespace
Drupal\Component\Utility
;
/**
* Provides helpers for dealing with variables.
*/
class
Variable
{
/**
* Drupal-friendly var_export().
*
* @param mixed $var
* The variable to export.
* @param string $prefix
* A prefix that will be added at the beginning of every lines of the output.
*
* @return string
* The variable exported in a way compatible to Drupal's coding standards.
*/
public
static
function
export
(
$var
,
$prefix
=
''
)
{
if
(
is_array
(
$var
))
{
if
(
empty
(
$var
))
{
$output
=
'array()'
;
}
else
{
$output
=
"array(
\n
"
;
// Don't export keys if the array is non associative.
$export_keys
=
array_values
(
$var
)
!=
$var
;
foreach
(
$var
as
$key
=>
$value
)
{
$output
.
=
' '
.
(
$export_keys
?
static
::
export
(
$key
)
.
' => '
:
''
)
.
static
::
export
(
$value
,
' '
,
FALSE
)
.
",
\n
"
;
}
$output
.
=
')'
;
}
}
elseif
(
is_bool
(
$var
))
{
$output
=
$var
?
'TRUE'
:
'FALSE'
;
}
elseif
(
is_string
(
$var
))
{
if
(
strpos
(
$var
,
"
\n
"
)
!==
FALSE
||
strpos
(
$var
,
"'"
)
!==
FALSE
)
{
// If the string contains a line break or a single quote, use the
// double quote export mode. Encode backslash and double quotes and
// transform some common control characters.
$var
=
str_replace
(
array
(
'\\'
,
'"'
,
"
\n
"
,
"
\r
"
,
"
\t
"
),
array
(
'\\\\'
,
'\"'
,
'\n'
,
'\r'
,
'\t'
),
$var
);
$output
=
'"'
.
$var
.
'"'
;
}
else
{
$output
=
"'"
.
$var
.
"'"
;
}
}
elseif
(
is_object
(
$var
)
&&
get_class
(
$var
)
===
'stdClass'
)
{
// var_export() will export stdClass objects using an undefined
// magic method __set_state() leaving the export broken. This
// workaround avoids this by casting the object as an array for
// export and casting it back to an object when evaluated.
$output
=
'(object) '
.
static
::
export
((
array
)
$var
,
$prefix
);
}
else
{
$output
=
var_export
(
$var
,
TRUE
);
}
if
(
$prefix
)
{
$output
=
str_replace
(
"
\n
"
,
"
\n
$prefix
"
,
$output
);
}
return
$output
;
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment