Add an unphp script for deserialising PHP objects and converting them to nice friendly JSON

This commit is contained in:
Danielle McLean 2018-04-09 08:37:16 +10:00
parent a935bfe173
commit 5862c9c0a8
Signed by: 00dani
GPG key ID: 8EB789DDF3ABD240

22
local/bin/unphp Executable file
View 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;
}