<?php
namespace nur\sery\db\cache;

use nur\sery\db\Capacitor;
use nur\sery\db\CapacitorStorage;
use nur\sery\db\sqlite\SqliteStorage;

class cache {
  protected static ?CapacitorStorage $storage = null;

  static function set_storage(CapacitorStorage $storage): CapacitorStorage {
    return self::$storage = $storage;
  }

  protected static function get_storage(): CapacitorStorage {
    return self::$storage ??= new SqliteStorage("");
  }

  protected static ?CacheChannel $channel = null;

  static function set(?CacheChannel $channel): CacheChannel {
    $channel ??= new CacheChannel();
    new Capacitor(self::get_storage(), $channel);
    return self::$channel = $channel;
  }

  static function get(): CacheChannel {
    if (self::$channel !== null) return self::$channel;
    else return self::set(null);
  }

  static function new(?RowsChannel $channel, $id=null, ?callable $builder=null): RowsChannel {
    $channel ??= new RowsChannel($id, $builder);
    new Capacitor(self::get_storage(), $channel);
    return $channel;
  }
}
