Get info about a Kirby website

I would like to get information about an arbitrary website build with Kirby from an external program, i.e. a bash script, and just pick the piece of information I need.

Apparently, it is not possible to get this by a method of the Kirby object. I discovered that I need to build my own associative array with different methods:

<?php
// getConfig.php to get the configuration of a Kirby site
// Usage: php getConfig.php DOCROOT
// Example: php getConfig.php /srv/www/vhost/kirby-test/htdocs

require $argv[1] . '/kirby/bootstrap.php';

$kirby = new Kirby([ 'roots' => [ 'index' => $argv[1] ] ]);

$myKirby = array(
  'languages' => $kirby->languages()->toArray(),
  'options'   => $kirby->options(),
  'plugins'   => $kirby->plugins(),
//  'roles'     => $kirby->roles()->toArray(),
  'roots'     => $kirby->roots()->toArray(),
//  'routes'    => $kirby->routes(),
  'urls'      => $kirby->urls()->toArray(),
  'users'     => $kirby->users()->toArray(),
  'version'   => $kirby->version(),
);

echo json_encode($myKirby);

?>

which would result in this output (when piped through jq):

{
  "languages": [],
  "options": {
    "debug": true
  },
  "plugins": [],
  "roots": {
    "kirby": "/usr/local/lib/kirby/3.4.5",
    "i18n": "/usr/local/lib/kirby/3.4.5/i18n",
    "i18n:translations": "/usr/local/lib/kirby/3.4.5/i18n/translations",
    "i18n:rules": "/usr/local/lib/kirby/3.4.5/i18n/rules",
    "index": "/home/uwe/vhosts/kirby-test1/htdocs",
    "assets": "/home/uwe/vhosts/kirby-test1/htdocs/assets",
    "content": "/home/uwe/vhosts/kirby-test1/htdocs/content",
    "media": "/home/uwe/vhosts/kirby-test1/htdocs/media",
    "panel": "/usr/local/lib/kirby/3.4.5/panel",
    "site": "/home/uwe/vhosts/kirby-test1/htdocs/site",
    "accounts": "/home/uwe/vhosts/kirby-test1/htdocs/site/accounts",
    "blueprints": "/home/uwe/vhosts/kirby-test1/htdocs/site/blueprints",
    "cache": "/home/uwe/vhosts/kirby-test1/htdocs/site/cache",
    "collections": "/home/uwe/vhosts/kirby-test1/htdocs/site/collections",
    "config": "/home/uwe/vhosts/kirby-test1/htdocs/site/config",
    "controllers": "/home/uwe/vhosts/kirby-test1/htdocs/site/controllers",
    "languages": "/home/uwe/vhosts/kirby-test1/htdocs/site/languages",
    "models": "/home/uwe/vhosts/kirby-test1/htdocs/site/models",
    "plugins": "/home/uwe/vhosts/kirby-test1/htdocs/site/plugins",
    "sessions": "/home/uwe/vhosts/kirby-test1/htdocs/site/sessions",
    "snippets": "/home/uwe/vhosts/kirby-test1/htdocs/site/snippets",
    "templates": "/home/uwe/vhosts/kirby-test1/htdocs/site/templates",
    "roles": "/home/uwe/vhosts/kirby-test1/htdocs/site/blueprints/users"
  },
  "urls": {
    "index": "/",
    "base": "",
    "current": "/",
    "assets": "/assets",
    "api": "/api",
    "media": "/media",
    "panel": "/panel"
  },
  "users": {
    "XakNcEEq": {
      "avatar": null,
      "content": [],
      "email": "uwe@somewhere.de",
      "id": "XakNcEEq",
      "language": "en",
      "role": "admin",
      "username": "Uwe Gehring"
    }
  },
  "version": "3.4.5"
}

which is fine, but I do not like the fact that I have to create an array first with the info I am interested in, using different methods. It would be much better, if one could just get the info about the whole Kirby object with one call:

...
require $argv[1] . '/kirby/bootstrap.php';
$kirby = new Kirby([ 'roots' => [ 'index' => $argv[1] ] ]);
echo json_encode($kirby);
...

But this does not work. Is there another possibility to achieve this?

The only way to achieve that would be to go via the magic __debugInfo() method:

$json = json_encode($kirby->__debugInfo());

There is no other method on the Kirby object that returns all data as an array.

If I do this, then the output (or the $json variable) contains only this:

{
  "languages": {
    "data": []
  },
  "options": {
    "debug": true
  },
  "request": {},
  "roots": {},
  "site": {
    "content": null,
    "translations": null,
    "children": null,
    "drafts": null
  },
  "urls": {},
  "version": "3.4.5"
}

So I stick with the array construction I created above.

Ah, ok, the reason this doesn’t work as expected is that json_encode cannot resolve the objects inside the debug array to array, so you would have to to something like this:

$debugInfo = $kirby->__debugInfo();
$debugInfo = array_map(function($info) {
    if ( is_object( $info ) && method_exists( $info, 'toArray' ) ) {
      return $info->toArray();
    }
    return $info;
}, $debugInfo);
echo json_encode($debugInfo, JSON_PRETTY_PRINT);
1 Like