#!/usr/bin/php
<?php
require __DIR__.'/../vendor/autoload.php';

use nulib\cache\CacheData;
use nulib\cache\CacheFile;
use nulib\ext\yaml;
use nulib\os\sh;

function show(string $prefix, CacheFile $cache, bool $dumpInfos=true): void {
  Txx("$prefix=", $cache->get());
  if ($dumpInfos) {
    yaml::dump($cache->getInfos());
  }
}

//system("rm -f *.cache .*.cache");

$what = [
  "null",
  "one",
  "two",
  "three",
];
$duration = 10;

if (in_array("null", $what)) {
  $null = new CacheFile("null", null, [
    "duration" => $duration,
  ]);
  show("null", $null);
}

if (in_array("one", $what)) {
  $one = new class("one", null, [
    "duration" => $duration,
  ]) extends CacheFile {
    protected function compute() {
      return 1;
    }
  };
  show("one", $one);
}

if (in_array("two", $what)) {
  $two = new CacheFile("two", new CacheData(function () {
    return 2;
  }), [
    "duration" => $duration,
  ]);
  show("two", $two);
}

if (in_array("three", $what)) {
  $data31 = new CacheData(function () {
    return 31;
  }, "data31name");

  $data32 = new CacheData(function () {
    return 32;
  });

  $three = new CacheFile("three", [
    "data31" => $data31,
    $data31, # name=data31name
    "data32" => $data32,
    $data32, # name=""
  ]);
  Txx("three.0=", $three->get("data31"));
  Txx("three.1=", $three->get("data31name"));
  Txx("three.2=", $three->get("data32"));
  Txx("three.3=", $three->get(""));
}
