From 2713c1f73a39d4106936d4c52bf8f18d810d7f3c Mon Sep 17 00:00:00 2001 From: winter Date: Fri, 13 Dec 2024 23:53:23 +0000 Subject: add instance information --- Digitigrade/Model/FetchableModel.php | 7 ++++- Digitigrade/Model/Instance.php | 48 +++++++++++++++++++++++++++++++++ Digitigrade/Model/InstanceEndpoints.php | 23 ++++++++++++++++ 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 Digitigrade/Model/Instance.php create mode 100644 Digitigrade/Model/InstanceEndpoints.php (limited to 'Digitigrade/Model') diff --git a/Digitigrade/Model/FetchableModel.php b/Digitigrade/Model/FetchableModel.php index 30b68e7..0b67599 100644 --- a/Digitigrade/Model/FetchableModel.php +++ b/Digitigrade/Model/FetchableModel.php @@ -13,7 +13,7 @@ abstract class FetchableModel extends Model implements RemoteFetchable { self::$mapper->bStrictObjectTypeChecking = false; } - private static function fetchFromRemote(string $uri, bool $autoSave): ?static { + protected static function fetchFromRemote(string $uri, bool $autoSave): ?static { // fetch the object $data = @file_get_contents($uri, context: stream_context_create(['http' => [ 'header' => 'Accept: application/json' @@ -58,6 +58,8 @@ abstract class FetchableModel extends Model implements RemoteFetchable { } } + $obj->beforeSave($uri); + if ($autoSave) { $obj->save(); } @@ -65,6 +67,9 @@ abstract class FetchableModel extends Model implements RemoteFetchable { return $obj; } + protected function beforeSave(string $uri) { + } + public static function findByUri(string $uri, bool $autoSave = true, bool $forceRefetch = false): ?static { if (!$forceRefetch) { $obj = static::findWhere('uri = ?', [$uri]); diff --git a/Digitigrade/Model/Instance.php b/Digitigrade/Model/Instance.php new file mode 100644 index 0000000..2924f71 --- /dev/null +++ b/Digitigrade/Model/Instance.php @@ -0,0 +1,48 @@ +domain]) != null) + return 'domain = ' . $db->quote($this->domain); + if (self::findWhere('id = ?', [$this->id]) != null) + return "id = $this->id"; + return null; + } + + public static function findByDomain(string $domain): ?self { + return self::findByUri("https://$domain/.well-known/pawpub-instance"); + } + + public function hydrate() { + $this->endpoints = InstanceEndpoints::findWhere('instance_id = ?', [$this->id]); + } + + protected function beforeSave(string $uri) { + $this->domain = explode('/', $uri)[2]; + } + + public static function findByUri(string $uri, bool $autoSave = true, bool $forceRefetch = false): ?static { + if (!$forceRefetch) { + $domain = explode('/', $uri)[2]; + $obj = self::findWhere('domain = ?', [$domain]); + if ($obj != null) + return $obj; + } + return self::fetchFromRemote($uri, $autoSave); + } +} \ No newline at end of file diff --git a/Digitigrade/Model/InstanceEndpoints.php b/Digitigrade/Model/InstanceEndpoints.php new file mode 100644 index 0000000..d2b0b24 --- /dev/null +++ b/Digitigrade/Model/InstanceEndpoints.php @@ -0,0 +1,23 @@ +instanceId = $id; + } + + protected function getUpdateWhereClause($db): ?string { + if (self::findWhere('instance_id = ?', [$this->instanceId]) != null) { + return "instance_id = $this->instanceId"; + } + return null; + } +} \ No newline at end of file -- cgit v1.3