Skip to content

Commit

Permalink
csfixed
Browse files Browse the repository at this point in the history
  • Loading branch information
hiqsol committed Aug 23, 2018
1 parent faf6cec commit ed34c6e
Showing 1 changed file with 43 additions and 43 deletions.
86 changes: 43 additions & 43 deletions src/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,41 +51,15 @@ public static function mergeConfig(): array
return $res;
}

/**
* Dumps closure object to string.
* Based on http://www.metashock.de/2013/05/dump-source-code-of-closure-in-php/.
* @param Closure $closure
* @return string
*/
public static function dumpClosure(Closure $closure): string
public static function exportDefines(array $defines): string
{
$res = 'function (';
$fun = new ReflectionFunction($closure);
$args = [];
foreach ($fun->getParameters() as $arg) {
$str = '';
if ($arg->isArray()) {
$str .= 'array ';
} elseif ($arg->getClass()) {
$str .= $arg->getClass()->name . ' ';
}
if ($arg->isPassedByReference()) {
$str .= '&';
}
$str .= '$' . $arg->name;
if ($arg->isOptional()) {
$str .= ' = ' . var_export($arg->getDefaultValue(), true);
}
$args[] = $str;
}
$res .= implode(', ', $args);
$res .= ') {' . PHP_EOL;
$lines = file($fun->getFileName());
for ($i = $fun->getStartLine(); $i < $fun->getEndLine(); ++$i) {
$res .= $lines[$i];
$res = '';
foreach ($defines as $key => $value) {
$var = static::exportVar($value);
$res .= "defined('$key') or define('$key', $var);\n";
}

return rtrim($res, "\r\n ,");
return $res;
}

/**
Expand All @@ -109,17 +83,6 @@ public static function exportVar($value): string
return $res;
}

public static function exportDefines(array $defines): string
{
$res = '';
foreach ($defines as $key => $value) {
$var = static::exportVar($value);
$res .= "defined('$key') or define('$key', $var);\n";
}

return $res;
}

/**
* Collects closures from given input.
* Substitutes closures with a tag.
Expand All @@ -145,4 +108,41 @@ private static function collectClosures(&$input): array

return $closures;
}

/**
* Dumps closure object to string.
* Based on http://www.metashock.de/2013/05/dump-source-code-of-closure-in-php/.
* @param Closure $closure
* @return string
*/
public static function dumpClosure(Closure $closure): string
{
$res = 'function (';
$fun = new ReflectionFunction($closure);
$args = [];
foreach ($fun->getParameters() as $arg) {
$str = '';
if ($arg->isArray()) {
$str .= 'array ';
} elseif ($arg->getClass()) {
$str .= $arg->getClass()->name . ' ';
}
if ($arg->isPassedByReference()) {
$str .= '&';
}
$str .= '$' . $arg->name;
if ($arg->isOptional()) {
$str .= ' = ' . var_export($arg->getDefaultValue(), true);
}
$args[] = $str;
}
$res .= implode(', ', $args);
$res .= ') {' . PHP_EOL;
$lines = file($fun->getFileName());
for ($i = $fun->getStartLine(); $i < $fun->getEndLine(); ++$i) {
$res .= $lines[$i];
}

return rtrim($res, "\r\n ,");
}
}

0 comments on commit ed34c6e

Please sign in to comment.