2ee935d3cf47035eaa8477017e6678582047bdc8
[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 if ($this->checkToken()) {
12 try {
13 $this->github->fetchRepo(
14 "$owner/$name",
15 [$this, "repoCallback"]
16 )->send();
17 } catch (\app\Github\Exception $exception) {
18 $this->app->getView()->addData(compact("exception", "owner", "name"));
19 }
20 $this->app->display("github/repo");
21 }
22 }
23
24 function repoCallback($repo, $links) {
25 $this->app->getView()->addData(compact("repo") + [
26 "title" => "Github: {$repo->name}"
27 ]);
28 settype($repo->tags, "object");
29 $this->github->fetchHooks($repo->full_name, function($hooks) use($repo) {
30 $repo->hooks = $hooks;
31 });
32 $this->github->fetchTags($repo->full_name, 1, $this->createTagsCallback($repo));
33 $this->github->fetchReleases($repo->full_name, 1, $this->createReleasesCallback($repo));
34 $this->github->fetchContents($repo->full_name, null, $this->createContentsCallback($repo));
35 }
36
37 function createReleasesCallback($repo) {
38 return function($releases, $links) use($repo) {
39 foreach ($releases as $release) {
40 $tag = $release->tag_name;
41 settype($repo->tags->$tag, "object");
42 $repo->tags->$tag->release = $release;
43 }
44 };
45 }
46
47 function createTagsCallback($repo) {
48 return function($tags, $links) use ($repo) {
49 foreach ($tags as $tag) {
50 $name = $tag->name;
51 settype($repo->tags->$name, "object");
52 $repo->tags->$name->tag = $tag;
53 }
54 };
55 }
56
57 function createContentsCallback($repo) {
58 return function($tree) use($repo) {
59 foreach ($tree as $entry) {
60 if ($entry->type !== "file" || $entry->size <= 0) {
61 continue;
62 }
63 if ($entry->name === "config.m4" || fnmatch("config?.m4", $entry->name)) {
64 $repo->config_m4 = $entry->name;
65 } elseif ($entry->name === "package.xml" || $entry->name === "package2.xml") {
66 $repo->package_xml = $entry->name;
67 } elseif ($entry->name === "pharext_package.php") {
68 $repo->pharext_package_php = $entry->name;
69 }
70 }
71 };
72 }
73 }