"location" => "github",
"title" => "Github"
]);
- $this->app->getView()->registerFunction("check", [$this, "checkRepoHook"]);
+ $this->app->getView()->registerFunction("check", [$github, "checkRepoHook"]);
if (($header = $this->app->getRequest()->getHeader("Cache-Control", Header::class))) {
$params = $header->getParams();
]));
return false;
}
-
- /**
- * Check if the pharext webhook is set for the repo and return it
- * @param object $repo
- * @return int hook id
- */
- function checkRepoHook($repo) {
- if ($repo->hooks) {
- foreach ($repo->hooks as $hook) {
- if ($hook->name === "web" && $hook->config->url === $this->github->getConfig()->hook->url) {
- return $hook;
- }
- }
- }
- return null;
- }
}
use app\Controller\Github;
use app\Github\API;
-use app\Github\Exception;
use app\Model\Accounts;
use app\Session;
use app\Web;
class Callback extends Github
{
-
/**
* @var Accounts
*/
use app\Web;
use http\Params;
use pharext\Task;
-use pharext\SourceDir\Git;
+use pharext\SourceDir;
require_once __DIR__."/../../../../vendor/m6w6/pharext/src/pharext/Version.php";
private function uploadAssetForRelease($release, $repo) {
$this->setTokenForUser($repo->owner->login);
- $asset = $this->createReleaseAsset($release, $repo);
- $name = sprintf("%s-%s.ext.phar", $repo->name, $release->tag_name);
- $url = uri_template($release->upload_url, compact("name"));
- $this->github->createReleaseAsset($url, $asset, "application/phar", function($json) {
- $response = $this->app->getResponse();
- $response->setResponseCode(201);
- $response->setHeader("Location", $json->url);
+ $this->github->listHooks($repo->full_name, function($hooks) use($release, $repo) {
+ $repo->hooks = $hooks;
+ $asset = $this->createReleaseAsset($release, $repo);
+ $name = sprintf("%s-%s.ext.phar", $repo->name, $release->tag_name);
+ $url = uri_template($release->upload_url, compact("name"));
+ $this->github->createReleaseAsset($url, $asset, "application/phar", function($json) {
+ $response = $this->app->getResponse();
+ $response->setResponseCode(201);
+ $response->setHeader("Location", $json->url);
+ });
})->send();
}
private function createReleaseAsset($release, $repo) {
$source = (new Task\GitClone($repo->clone_url, $release->tag_name))->run();
- $iterator = new Git($source);
+ $iterator = new SourceDir\Git($source);
$meta = [
"header" => sprintf("pharext v%s (c) Michael Wallner <mike@php.net>", \pharext\VERSION),
"version" => \pharext\VERSION,
{
function __invoke(array $args = null) {
if ($this->checkToken()) {
- $this->github->listRepos(
+ list($repos, $links) = $this->github->listRepos(
$this->app->getRequest()->getQuery("page"),
- [$this, "reposCallback"]
+ new \app\Github\API\Repos\ReposCallback($this->github)
)->send();
- $this->app->display("github/index");
- }
- }
-
- function reposCallback($repos, $links) {
- $this->app->getView()->addData(compact("repos", "links"));
-
- foreach ($repos as $repo) {
- $this->github->listHooks($repo->full_name, function($hooks) use($repo) {
- $repo->hooks = $hooks;
- });
+ $this->app->display("github/index", compact("repos", "links"));
}
}
}
namespace app\Controller\Github;
use app\Controller\Github;
+use app\Github\API\Repos\RepoCallback;
class Repo extends Github
{
function __invoke(array $args = null) {
extract($args);
- $this->app->getView()->addData(compact("owner", "name"));
if ($this->checkToken()) {
- $this->github->readRepo(
- "$owner/$name",
- [$this, "repoCallback"]
- )->send();
+ list($repo) = $this->github->readRepo("$owner/$name", new RepoCallback($this->github))->send();
+ $hook = $this->github->checkRepoHook($repo);
+
+ $this->app->getView()->addData(compact("owner", "name", "repo", "hook"));
if (($modal = $this->app->getRequest()->getQuery("modal"))) {
- $this->app->getView()->addData(compact("modal") + [
- "action" => $this->app->getRequest()->getQuery($modal)
- ]);
+ $action = $this->app->getRequest()->getQuery($modal);
+ $this->app->getView()->addData(compact("modal", "action"));
}
$this->app->display("github/repo");
}
}
-
- function repoCallback($repo) {
- $this->app->getView()->addData(compact("repo") + [
- "title" => "Github: {$repo->name}"
- ]);
- settype($repo->tags, "object");
- $this->github->listHooks($repo->full_name, function($hooks) use($repo) {
- $repo->hooks = $hooks;
- $this->app->getView()->addData(["hook" => $this->checkRepoHook($repo)]);
- });
- $this->github->listTags($repo->full_name, 1, $this->createTagsCallback($repo));
- $this->github->listReleases($repo->full_name, 1, $this->createReleasesCallback($repo));
- $this->github->readContents($repo->full_name, null, $this->createContentsCallback($repo));
- }
-
- function createReleasesCallback($repo) {
- return function($releases, $links) use($repo) {
- foreach ($releases as $release) {
- $tag = $release->tag_name;
- settype($repo->tags->$tag, "object");
- $repo->tags->$tag->release = $release;
- $this->github->listReleaseAssets($repo->full_name, $release->id, function($assets) use($release) {
- $release->assets = $assets;
- });
- }
- };
- }
-
- function createTagsCallback($repo) {
- return function($tags, $links) use ($repo) {
- foreach ($tags as $tag) {
- $name = $tag->name;
- settype($repo->tags->$name, "object");
- $repo->tags->$name->tag = $tag;
- }
- };
- }
-
- function createContentsCallback($repo) {
- return function($tree) use($repo) {
- foreach ($tree as $entry) {
- if ($entry->type !== "file" || $entry->size <= 0) {
- continue;
- }
- if ($entry->name === "config.m4" || fnmatch("config?.m4", $entry->name)) {
- $repo->config_m4 = $entry->name;
- } elseif ($entry->name === "package.xml" || $entry->name === "package2.xml") {
- $repo->package_xml = $entry->name;
- } elseif ($entry->name === "pharext_package.php") {
- $repo->pharext_package_php = $entry->name;
- }
- }
- };
- }
}
$this->github->readRepo("$owner/$repo", function($repo) {
$call = $this->github->listHooks($repo->full_name, function($hooks, $links) use($repo, &$call) {
$repo->hooks = $hooks;
- if (($hook = $this->checkRepoHook($repo))) {
+ if (($hook = $this->github->checkRepoHook($repo))) {
$hook_conf = $this->app->getRequest()->getForm();
$this->github->updateRepoHook($repo->full_name, $hook->id, $hook_conf, function($changed_hook) use($repo, $hook, $hooks, $links, &$call) {
foreach ($changed_hook as $key => $val) {
$this->github->readRepo("$owner/$repo", function($repo) {
$call = $this->github->listHooks($repo->full_name, function($hooks) use($repo, &$call) {
$repo->hooks = $hooks;
- if (($hook = $this->checkRepoHook($repo))) {
+ if (($hook = $this->github->checkRepoHook($repo))) {
$this->github->deleteRepoHook($repo->full_name, $hook->id, function() use($repo, &$call) {
$call->dropFromCache();
$this->redirectBack($repo->full_name);
return $call($callback);
}
+ /**
+ * Check if the pharext webhook is set for the repo and return it
+ * @param object $repo
+ * @return stdClass hook
+ */
+ function checkRepoHook($repo) {
+ if ($repo->hooks) {
+ foreach ($repo->hooks as $hook) {
+ if ($hook->name === "web" && $hook->config->url === $this->config->hook->url) {
+ return $hook;
+ }
+ }
+ }
+ return null;
+ }
+
function listHooks($repo, callable $callback) {
$call = new API\Hooks\ListHooks($this, compact("repo"));
return $call($callback);
*/
protected $query;
+ /**
+ * @var array
+ */
+ protected $result;
+
/**
* Queue this call to the API client
*/
}
function __invoke(callable $callback) {
- if ($this->readFromCache($cached)) {
- call_user_func_array($callback, $cached);
+ if ($this->readFromCache($this->result)) {
+ call_user_func_array($callback, $this->result);
} else {
$this->enqueue($callback);
}
* Call Client::send()
*/
function send() {
- return $this->api->getClient()->send();
+ $this->api->getClient()->send();
+ return $this->result;
}
/**
--- /dev/null
+<?php
+
+namespace app\Github\API;
+
+use app\Github\API;
+
+abstract class Callback
+{
+ protected $api;
+
+ abstract function __invoke($json, $links = null);
+
+ function __construct(API $api) {
+ $this->api = $api;
+ }
+}
if ($response->getResponseCode() >= 400 || null === ($json = json_decode($response->getBody()))) {
throw new RequestException($response);
}
+ $this->result = [$json];
$callback($json);
return true;
});
--- /dev/null
+<?php
+
+namespace app\Github\API\Hooks;
+
+use app\Github\API;
+use app\Github\API\Callback;
+
+class HooksCallback extends Callback
+{
+ private $repo;
+
+ function __construct(API $api, $repo) {
+ parent::__construct($api);
+ $this->repo = $repo;
+ }
+
+ function __invoke($json, $links = null) {
+ $this->repo->hooks = $json;
+ }
+}
throw new RequestException($response);
}
$links = new Links($response->getHeader("Link"));
- $this->saveToCache([$json, $links]);
+ $this->result = [$json, $links];
+ $this->saveToCache($this->result);
$callback($json, $links);
return true;
});
if ($response->getResponseCode() >= 400 || null === ($json = json_decode($response->getBody()))) {
throw new \app\Github\Exception\RequestException($response);
}
+ $this->result = [$json];
$callback($json);
return true;
});
if ($response->getResponseCode() >= 400 || null === ($json = json_decode($response->getBody()))) {
throw new RequestException($response);
}
+ $this->result = [$json];
$callback($json);
return true;
});
if ($response->getResponseCode() >= 400 || null === ($json = json_decode($response->getBody()))) {
throw new RequestException($response);
}
+ $this->result = [$json];
$callback($json);
return true;
});
throw new RequestException($response);
}
$links = new Links($response->getHeader("Link"));
- $this->saveToCache([$json, $links]);
+ $this->result = [$json, $links];
+ $this->saveToCache($this->result);
$callback($json, $links);
return true;
});
throw new RequestException($response);
}
$links = new Links($response->getHeader("Link"));
- $this->saveToCache([$json, $links]);
+ $this->result = [$json, $links];
+ $this->saveToCache($this->result);
$callback($json, $links);
return true;
});
--- /dev/null
+<?php
+
+namespace app\Github\API\Repos;
+
+use app\Github\API;
+use app\Github\API\Callback;
+
+class ContentsCallback extends Callback
+{
+ private $repo;
+
+ function __construct(API $api, $repo) {
+ parent::__construct($api);
+ $this->repo = $repo;
+ }
+
+ function __invoke($json, $links = null) {
+ foreach ($json as $entry) {
+ if ($entry->type !== "file" || $entry->size <= 0) {
+ continue;
+ }
+ if ($entry->name === "config.m4" || fnmatch("config?.m4", $entry->name)) {
+ $this->repo->config_m4 = $entry->name;
+ } elseif ($entry->name === "package.xml" || $entry->name === "package2.xml") {
+ $this->repo->package_xml = $entry->name;
+ } elseif ($entry->name === "pharext_package.php") {
+ $this->repo->pharext_package_php = $entry->name;
+ }
+ }
+ }
+}
throw new RequestException($response);
}
$links = new Links($response->getHeader("Link"));
- $this->saveToCache([$json, $links]);
+ $this->result = [$json, $links];
+ $this->saveToCache($this->result);
$callback($json, $links);
return true;
});
if ($response->getResponseCode() >= 400 || null === ($json = json_decode($response->getBody()))) {
throw new RequestException($response);
}
- $this->saveToCache([$json]);
+ $this->result = [$json];
+ $this->saveToCache($this->result);
$callback($json);
return true;
});
if ($response->getResponseCode() >= 400 || null === ($json = json_decode($response->getBody()))) {
throw new RequestException($response);
}
- $this->saveToCache([$json]);
+ $this->result = [$json];
+ $this->saveToCache($this->result);
$callback($json);
return true;
});
--- /dev/null
+<?php
+
+namespace app\Github\API\Repos;
+
+use app\Github\API;
+use app\Github\API\Callback;
+
+class ReleasesCallback extends Callback
+{
+ private $repo;
+
+ function __construct(API $api, $repo) {
+ parent::__construct($api);
+ $this->repo = $repo;
+ }
+
+ function __invoke($json, $links = null) {
+ settype($this->repo->tags, "object");
+ foreach ($json as $release) {
+ $tag = $release->tag_name;
+ settype($this->repo->tags->$tag, "object");
+ $this->repo->tags->$tag->release = $release;
+ $this->api->listReleaseAssets($this->repo->full_name, $release->id, function($assets) use($release) {
+ $release->assets = $assets;
+ });
+ }
+ }
+}
--- /dev/null
+<?php
+
+namespace app\Github\API\Repos;
+
+use app\Github\API\Callback;
+use app\Github\API\Hooks\HooksCallback;
+use app\Github\API\Tags\TagsCallback;
+
+class RepoCallback extends Callback
+{
+ function __invoke($repo, $links = null) {
+ $this->api->listHooks($repo->full_name, new HooksCallback($this->api, $repo));
+ $this->api->listTags($repo->full_name, 1, new TagsCallback($this->api, $repo));
+ $this->api->listReleases($repo->full_name, 1, new ReleasesCallback($this->api, $repo));
+ $this->api->readContents($repo->full_name, null, new ContentsCallback($this->api, $repo));;
+ }
+}
--- /dev/null
+<?php
+
+namespace app\Github\API\Repos;
+
+use app\Github\API\Callback;
+use app\Github\API\Hooks\HooksCallback;
+
+class ReposCallback extends Callback
+{
+ function __invoke($json, $links = null) {
+ foreach ($json as $repo) {
+ $this->api->listHooks($repo->full_name, new HooksCallback($this->api, $repo));
+ }
+ }
+}
throw new RequestException($response);
}
$links = new Links($response->getHeader("Link"));
- $this->saveToCache([$json, $links]);
+ $this->result = [$json, $links];
+ $this->saveToCache($this->result);
$callback($json, $links);
return true;
});
--- /dev/null
+<?php
+
+namespace app\Github\API\Tags;
+
+use app\Github\API;
+use app\Github\API\Callback;
+
+class TagsCallback extends Callback
+{
+ private $repo;
+
+ function __construct(API $api, $repo) {
+ parent::__construct($api);
+ $this->repo = $repo;
+ }
+
+ function __invoke($json, $links = null) {
+ settype($this->repo->tags, "object");
+ foreach ($json as $tag) {
+ $name = $tag->name;
+ settype($this->repo->tags->$name, "object");
+ $this->repo->tags->$name->tag = $tag;
+ }
+ }
+}
if ($response->getResponseCode() >= 400 || null === ($json = json_decode($response->getBody()))) {
throw new RequestException($response);
}
+ $this->result = [$json];
$callback($json);
return true;
});
if ($response->getResponseCode() >= 400 || null === ($json = json_decode($response->getBody()))) {
throw new RequestException($response);
}
- $this->saveToCache([$json]);
+ $this->result = [$json];
+ $this->saveToCache($this->result);
$callback($json);
return true;
});
-<?php $this->layout("layout") ?>
+<?php $this->layout("layout", ["title" => "Github: {$repo->name}"]) ?>
<div class="page-header">
<h1>