refactor guthub api
[pharext/pharext.org] / app / Controller / Github / Repo.php
1 <?php
2
3 namespace app\Controller\Github;
4
5 use app\Controller\Github;
6
7 class Repo extends Github
8 {
9 function __invoke(array $args = null) {
10 extract($args);
11 $this->app->getView()->addData(compact("owner", "name"));
12 if ($this->checkToken()) {
13 $this->github->fetchRepo(
14 "$owner/$name",
15 [$this, "repoCallback"]
16 )->send();
17
18 if (($modal = $this->app->getRequest()->getQuery("modal"))) {
19 $this->app->getView()->addData(compact("modal") + [
20 "action" => $this->app->getRequest()->getQuery($modal)
21 ]);
22 }
23
24 $this->app->display("github/repo");
25 }
26 }
27
28 function repoCallback($repo) {
29 $this->app->getView()->addData(compact("repo") + [
30 "title" => "Github: {$repo->name}"
31 ]);
32 settype($repo->tags, "object");
33 $this->github->fetchHooks($repo->full_name, function($hooks) use($repo) {
34 $repo->hooks = $hooks;
35 });
36 $this->github->fetchTags($repo->full_name, 1, $this->createTagsCallback($repo));
37 $this->github->fetchReleases($repo->full_name, 1, $this->createReleasesCallback($repo));
38 $this->github->fetchContents($repo->full_name, null, $this->createContentsCallback($repo));
39 }
40
41 function createReleasesCallback($repo) {
42 return function($releases, $links) use($repo) {
43 foreach ($releases as $release) {
44 $tag = $release->tag_name;
45 settype($repo->tags->$tag, "object");
46 $repo->tags->$tag->release = $release;
47 }
48 };
49 }
50
51 function createTagsCallback($repo) {
52 return function($tags, $links) use ($repo) {
53 foreach ($tags as $tag) {
54 $name = $tag->name;
55 settype($repo->tags->$name, "object");
56 $repo->tags->$name->tag = $tag;
57 }
58 };
59 }
60
61 function createContentsCallback($repo) {
62 return function($tree) use($repo) {
63 foreach ($tree as $entry) {
64 if ($entry->type !== "file" || $entry->size <= 0) {
65 continue;
66 }
67 if ($entry->name === "config.m4" || fnmatch("config?.m4", $entry->name)) {
68 $repo->config_m4 = $entry->name;
69 } elseif ($entry->name === "package.xml" || $entry->name === "package2.xml") {
70 $repo->package_xml = $entry->name;
71 } elseif ($entry->name === "pharext_package.php") {
72 $repo->pharext_package_php = $entry->name;
73 }
74 }
75 };
76 }
77 }