diff --git a/search.php b/search.php index 14fb1c2..b784778 100644 --- a/search.php +++ b/search.php @@ -352,16 +352,17 @@ private static function addRepoSubCommands() if (isset($parts[1][0]) && in_array($parts[1][0], ['#', '@', '*', '/'])) { switch ($parts[1][0]) { case '*': - $commits = Workflow::requestApi('/repos/'.$parts[0].'/commits'); - foreach ($commits as $commit) { - Workflow::addItemIfMatches(Item::create() + $commits = Workflow::requestApi('/repos/'.$parts[0].'/commits', transformItem: static function (stdClass $commit) use ($parts) { + return Item::create() ->title($commit->commit->message) ->comparator($parts[0].' *'.$commit->sha) ->subtitle($commit->commit->author->date.' ('.$commit->sha.')') ->icon('commits') ->arg('/'.$parts[0].'/commit/'.$commit->sha) - ->prio(strtotime($commit->commit->author->date)) - ); + ->prio(strtotime($commit->commit->author->date)); + }); + foreach ($commits as $commit) { + Workflow::addItemIfMatches($commit); } break; case '@': @@ -392,16 +393,17 @@ private static function addRepoSubCommands() } break; case '#': - $issues = Workflow::requestApi('/repos/'.$parts[0].'/issues?sort=updated&state=all'); - foreach ($issues as $issue) { - Workflow::addItemIfMatches(Item::create() + $issues = Workflow::requestApi('/repos/'.$parts[0].'/issues?sort=updated&state=all', transformItem: static function (stdClass $issue) use ($parts) { + return Item::create() ->title($issue->title) ->comparator($parts[0].' #'.$issue->number.' '.$issue->title) ->subtitle('#'.$issue->number) ->icon(isset($issue->pull_request) ? 'pull-request' : 'issue') ->arg($issue->html_url) - ->prio(strtotime($issue->updated_at)) - ); + ->prio(strtotime($issue->updated_at)); + }); + foreach ($issues as $issue) { + Workflow::addItemIfMatches($issue); } break; } diff --git a/workflow.php b/workflow.php index 9017ca9..fb3ae0d 100644 --- a/workflow.php +++ b/workflow.php @@ -169,10 +169,11 @@ public static function request(string $url, ?Curl $curl = null, $callback = null * @param bool $firstPageOnly * @param int $maxAge * @param bool $refreshInBackground + * @param callable $transformItem * * @return mixed */ - public static function requestCache(string $url, ?Curl $curl = null, $callback = null, bool $firstPageOnly = false, int $maxAge = self::DEFAULT_CACHE_MAX_AGE, bool $refreshInBackground = true) + public static function requestCache(string $url, ?Curl $curl = null, $callback = null, bool $firstPageOnly = false, int $maxAge = self::DEFAULT_CACHE_MAX_AGE, bool $refreshInBackground = true, ?callable $transformItem = null) { $return = false; $returnValue = null; @@ -183,6 +184,13 @@ public static function requestCache(string $url, ?Curl $curl = null, $callback = $returnValue = $content; }; } + $transformContent = static function (mixed $content) use ($transformItem) { + if (!$transformItem) { + return $content; + } + + return array_is_list($content) ? array_map($transformItem, $content) : $transformItem($content); + }; $stmt = self::getStatement('SELECT * FROM request_cache WHERE url = ?'); $stmt->execute([$url]); @@ -203,11 +211,14 @@ public static function requestCache(string $url, ?Curl $curl = null, $callback = if (!$shouldRefresh || $refreshInBackground) { self::log('using cached content for %s', $url); $content = json_decode($content); + $content = $transformContent($content); if (!$firstPageOnly) { $stmt = self::getStatement('SELECT url, content FROM request_cache WHERE parent = ? ORDER BY `timestamp` DESC'); while ($stmt->execute([$url]) && $data = $stmt->fetchObject()) { - $content = array_merge($content, json_decode($data->content)); + $cachedContent = json_decode($data->content); + $cachedContent = $transformContent($cachedContent); + $content = array_merge($content, $cachedContent); $url = $data->url; } } @@ -221,7 +232,7 @@ public static function requestCache(string $url, ?Curl $curl = null, $callback = $responses = []; - $handleResponse = static function (CurlResponse $response, $content, $parent = null) use (&$handleResponse, $curl, &$responses, $stmt, $callback, $firstPageOnly) { + $handleResponse = static function (CurlResponse $response, $content, $parent = null) use (&$handleResponse, $curl, &$responses, $stmt, $callback, $firstPageOnly, $transformContent) { $url = $response->request->url; if ($response && in_array($response->status, [200, 304])) { $checkNext = false; @@ -235,7 +246,7 @@ public static function requestCache(string $url, ?Curl $curl = null, $callback = if (isset($response->content->items)) { $response->content = $response->content->items; } - $responses[] = $response->content; + $responses[] = $transformContent($response->content); self::getStatement('REPLACE INTO request_cache VALUES(?, ?, ?, ?, 0, ?)') ->execute([$url, time(), $response->etag, json_encode($response->content), $parent]); @@ -296,11 +307,11 @@ public static function requestCache(string $url, ?Curl $curl = null, $callback = return $returnValue; } - public static function requestApi(string $url, ?Curl $curl = null, $callback = null, bool $firstPageOnly = false, int $maxAge = self::DEFAULT_CACHE_MAX_AGE) + public static function requestApi(string $url, ?Curl $curl = null, $callback = null, bool $firstPageOnly = false, int $maxAge = self::DEFAULT_CACHE_MAX_AGE, ?callable $transformItem = null) { $url = self::getApiUrl($url); - return self::requestCache($url, $curl, $callback, $firstPageOnly, $maxAge); + return self::requestCache($url, $curl, $callback, $firstPageOnly, $maxAge, transformItem: $transformItem); } public static function cleanCache()