From 180f21e29e49b067daccb8ecd2f7ee991198cf95 Mon Sep 17 00:00:00 2001 From: Shish Date: Sat, 4 Feb 2023 13:27:27 +0000 Subject: [PATCH] Use graphqlite-like API --- core/imageboard/image.php | 42 ++++++++++++++++++++------------------- core/imageboard/tag.php | 12 ++++++----- core/send_event.php | 2 +- core/user.php | 12 ++++++----- ext/comment/main.php | 17 +++++++++------- ext/pm/main.php | 17 ++++++++-------- tests/phpstan.neon | 2 +- 7 files changed, 57 insertions(+), 47 deletions(-) diff --git a/core/imageboard/image.php b/core/imageboard/image.php index 27149baf..b02b3ea7 100644 --- a/core/imageboard/image.php +++ b/core/imageboard/image.php @@ -4,7 +4,9 @@ declare(strict_types=1); namespace Shimmie2; -use GQLA\Expose; +use GQLA\Type; +use GQLA\Field; +use GQLA\Query; /** * Class Image @@ -15,27 +17,27 @@ use GQLA\Expose; * image per se, but could be a video, sound file, or any * other supported upload type. */ -#[Expose(name: "Post")] +#[Type(name: "Post")] class Image { public const IMAGE_DIR = "images"; public const THUMBNAIL_DIR = "thumbs"; - #[Expose] + #[Field] public ?int $id = null; - #[Expose] + #[Field] public int $height = 0; - #[Expose] + #[Field] public int $width = 0; - #[Expose] + #[Field] public string $hash; - #[Expose] + #[Field] public int $filesize; - #[Expose] + #[Field] public string $filename; - #[Expose] + #[Field] private string $ext; - #[Expose] + #[Field] private string $mime; /** @var ?string[] */ @@ -84,7 +86,7 @@ class Image } } - #[Expose(extends: "Query", name: "post_by_id")] + #[Query(name: "post_by_id")] public static function by_id(int $id): ?Image { global $database; @@ -154,7 +156,7 @@ class Image * #param string[] $tags * #return Image[] */ - #[Expose(extends: "Query", name: "posts", type: "[Post]", args: ["tags" => "[string]"])] + #[Query(name: "posts", type: "[Post]", args: ["tags" => "[string]"])] public static function find_images(?int $start = 0, ?int $limit = null, array $tags=[]): array { $result = self::find_images_internal($start, $limit, $tags); @@ -360,7 +362,7 @@ class Image /** * Find the User who owns this Image */ - #[Expose(name: "owner")] + #[Field(name: "owner")] public function get_owner(): User { return User::by_id($this->owner_id); @@ -459,9 +461,9 @@ class Image /** * Get this image's tags as an array. * - * #return string[] + * @return String[] */ - #[Expose(name: "tags", type: "[string]")] + #[Field(name: "tags", type: "[string]")] public function get_tag_array(): array { global $database; @@ -489,7 +491,7 @@ class Image /** * Get the URL for the full size image */ - #[Expose(name: "image_link")] + #[Field(name: "image_link")] public function get_image_link(): string { return $this->get_link(ImageConfig::ILINK, '_images/$hash/$id%20-%20$tags.$ext', 'image/$id.$ext'); @@ -498,7 +500,7 @@ class Image /** * Get the nicely formatted version of the file name */ - #[Expose(name: "nice_name")] + #[Field(name: "nice_name")] public function get_nice_image_name(): string { $plte = new ParseLinkTemplateEvent('$id - $tags.$ext', $this); @@ -509,7 +511,7 @@ class Image /** * Get the URL for the thumbnail */ - #[Expose(name: "thumb_link")] + #[Field(name: "thumb_link")] public function get_thumb_link(): string { global $config; @@ -544,7 +546,7 @@ class Image * Get the tooltip for this image, formatted according to the * configured template. */ - #[Expose(name: "tooltip")] + #[Field(name: "tooltip")] public function get_tooltip(): string { global $config; @@ -557,7 +559,7 @@ class Image * Get the info for this image, formatted according to the * configured template. */ - #[Expose(name: "info")] + #[Field(name: "info")] public function get_info(): string { global $config; diff --git a/core/imageboard/tag.php b/core/imageboard/tag.php index 1a00c20d..a8fecb3d 100644 --- a/core/imageboard/tag.php +++ b/core/imageboard/tag.php @@ -4,14 +4,16 @@ declare(strict_types=1); namespace Shimmie2; -use GQLA\Expose; +use GQLA\Type; +use GQLA\Field; +use GQLA\Query; -#[Expose(name: "TagUsage")] +#[Type(name: "TagUsage")] class TagUsage { - #[Expose] + #[Field] public string $tag; - #[Expose] + #[Field] public int $uses; public function __construct(string $tag, int $uses) @@ -20,7 +22,7 @@ class TagUsage $this->uses = $uses; } - #[Expose(extends: "Query", name: "tags", type: '[TagUsage]')] + #[Query(name: "tags", type: '[TagUsage]')] public static function tags(string $search, int $limit=10): array { global $cache, $database; diff --git a/core/send_event.php b/core/send_event.php index fbba1999..9eacf5cc 100644 --- a/core/send_event.php +++ b/core/send_event.php @@ -96,7 +96,7 @@ $_shm_event_count = 0; /** * Send an event to all registered Extensions. - * + * * @template T of Event * @param T $event * @return T diff --git a/core/user.php b/core/user.php index afb80bbb..55819a84 100644 --- a/core/user.php +++ b/core/user.php @@ -4,7 +4,9 @@ declare(strict_types=1); namespace Shimmie2; -use GQLA\Expose; +use GQLA\Type; +use GQLA\Field; +use GQLA\Query; function _new_user(array $row): User { @@ -19,12 +21,12 @@ function _new_user(array $row): User * * The currently logged in user will always be accessible via the global variable $user. */ -#[Expose(name: "User")] +#[Type(name: "User")] class User { - #[Expose] + #[Field] public int $id; - #[Expose] + #[Field] public string $name; public ?string $email; public string $join_date; @@ -63,7 +65,7 @@ class User } } - #[Expose(extends: "Query")] + #[Query] public static function me(): User { global $user; diff --git a/ext/comment/main.php b/ext/comment/main.php index 707910ba..615d3c16 100644 --- a/ext/comment/main.php +++ b/ext/comment/main.php @@ -4,7 +4,10 @@ declare(strict_types=1); namespace Shimmie2; -use GQLA\Expose; +use GQLA\Type; +use GQLA\Field; +use GQLA\Query; +use GQLA\Mutation; require_once "vendor/ifixit/php-akismet/akismet.class.php"; @@ -43,7 +46,7 @@ class CommentPostingException extends SCoreException { } -#[Expose(name: "Comment")] +#[Type(name: "Comment")] class Comment { public ?User $owner; @@ -51,12 +54,12 @@ class Comment public string $owner_name; public ?string $owner_email; public string $owner_class; - #[Expose] + #[Field] public string $comment; public int $comment_id; public int $image_id; public string $poster_ip; - #[Expose] + #[Field] public string $posted; public function __construct($row) @@ -83,7 +86,7 @@ class Comment ", ["owner_id"=>$user->id]); } - #[Expose(name: "owner")] + #[Field(name: "owner")] public function get_owner(): User { if (empty($this->owner)) { @@ -92,13 +95,13 @@ class Comment return $this->owner; } - #[Expose(extends: "Post", name: "comments", type: "[Comment]")] + #[Field(extends: "Post", name: "comments", type: "[Comment]")] public function get_comments(Image $post): array { return CommentList::get_comments($post->id); } - #[Expose(extends: "Mutation", name: "create_comment")] + #[Mutation(name: "create_comment")] public function create_comment(int $post_id, string $comment): bool { global $user; diff --git a/ext/pm/main.php b/ext/pm/main.php index 1e13d062..ab17b0cb 100644 --- a/ext/pm/main.php +++ b/ext/pm/main.php @@ -4,7 +4,9 @@ declare(strict_types=1); namespace Shimmie2; -use GQLA\Expose; +use GQLA\Type; +use GQLA\Field; +use GQLA\Query; class SendPMEvent extends Event { @@ -17,21 +19,20 @@ class SendPMEvent extends Event } } -#[Expose] +#[Type] class PM { - #[Expose] + #[Field] public int $id; public int $from_id; public string $from_ip; public int $to_id; - /** @var mixed */ - public $sent_date; - #[Expose] + public mixed $sent_date; + #[Field] public string $subject; - #[Expose] + #[Field] public string $message; - #[Expose] + #[Field] public bool $is_read; public function __construct($from_id=0, string $from_ip="0.0.0.0", int $to_id=0, string $subject="A Message", string $message="Some Text", bool $read=false) diff --git a/tests/phpstan.neon b/tests/phpstan.neon index 951171fd..0b5b4f23 100644 --- a/tests/phpstan.neon +++ b/tests/phpstan.neon @@ -6,5 +6,5 @@ parameters: - ../tests - ../themes/default ignoreErrors: - - '#Attribute class GQLA\\Expose#' + - '#Attribute class GQLA\\#' - '#Access to an undefined property Shimmie2\\Image::\$#'