Add an unphp script for deserialising PHP objects and converting them to nice friendly JSON
This commit is contained in:
parent
a935bfe173
commit
5862c9c0a8
1 changed files with 22 additions and 0 deletions
22
local/bin/unphp
Executable file
22
local/bin/unphp
Executable file
|
@ -0,0 +1,22 @@
|
|||
#!/usr/bin/env php
|
||||
<?php
|
||||
if (is_readable('./vendor/autoload.php')) require './vendor/autoload.php';
|
||||
|
||||
function to_array($obj) {
|
||||
if (is_array($obj)) return array_map('to_array', $obj);
|
||||
if (!is_object($obj)) return $obj;
|
||||
$cls = new \ReflectionClass($obj);
|
||||
$fields = ['__class__' => $cls->name];
|
||||
foreach ($cls->getProperties() as $prop) {
|
||||
$prop->setAccessible(true);
|
||||
$fields[$prop->getName()] = to_array($prop->getValue($obj));
|
||||
}
|
||||
return $fields;
|
||||
}
|
||||
|
||||
$args = array_slice($argv, 1);
|
||||
if (empty($args)) $args[] = '-';
|
||||
foreach ($args as $arg) {
|
||||
if ($arg === '-') $arg = 'php://stdin';
|
||||
echo json_encode(to_array(unserialize(file_get_contents($arg)))) . PHP_EOL;
|
||||
}
|
Loading…
Reference in a new issue