Skip to content

Commit d25bd2a

Browse files
committed
Repo list loader
1 parent 9163f93 commit d25bd2a

File tree

9 files changed

+177
-35
lines changed

9 files changed

+177
-35
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/.idea
22
/vendor
33
/style/.sass-cache
4-
/www
4+
/www
5+
/data

class/Github/Cache.php

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
namespace Gt\Website\Github;
3+
4+
use Gt\Fetch\BodyResponse;
5+
use Gt\Fetch\Http;
6+
use Gt\WebEngine\FileSystem\Path;
7+
8+
class Cache {
9+
const DO_NOT_SHOW_REPOS = ["www.php.gt", "Maintenance", "StyleGuide"];
10+
const CACHE_FILE = "repoList.dat";
11+
12+
protected static $data;
13+
14+
public static function refresh():void {
15+
$data = [];
16+
17+
$http = new Http();
18+
$http->fetch("https://api.github.com/orgs/phpgt/repos")
19+
->then(function(BodyResponse $response) {
20+
return $response->json();
21+
})
22+
->then(function($json)use(&$data) {
23+
foreach($json as $repoData) {
24+
if(in_array($repoData->name, self::DO_NOT_SHOW_REPOS)) {
25+
continue;
26+
}
27+
28+
$repo = new Repo(
29+
$repoData->name,
30+
$repoData->description
31+
);
32+
$data []= $repo;
33+
}
34+
});
35+
$http->wait();
36+
37+
$dataDir = Path::getDataDirectory();
38+
if(!is_dir($dataDir)) {
39+
mkdir($dataDir, 0775, true);
40+
}
41+
42+
file_put_contents(
43+
implode(DIRECTORY_SEPARATOR, [
44+
$dataDir,
45+
self::CACHE_FILE,
46+
]),
47+
serialize($data)
48+
);
49+
}
50+
}

class/Github/Repo.php

+11
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,16 @@
22
namespace Gt\Website\Github;
33

44
class Repo {
5+
public $name;
6+
public $description;
7+
public $repo;
58

9+
public function __construct(
10+
string $name,
11+
string $description
12+
) {
13+
$this->name = $name;
14+
$this->repo = strtolower($name);
15+
$this->description = $description;
16+
}
617
}

class/Github/RepoList.php

+63-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,68 @@
11
<?php
22
namespace Gt\Website\Github;
33

4-
class RepoList {
4+
use Gt\Fetch\BodyResponse;
5+
use Gt\Fetch\Http;
6+
use Gt\WebEngine\FileSystem\Path;
7+
use Iterator;
58

9+
class RepoList implements Iterator {
10+
/** @var Repo[] */
11+
protected $iterator;
12+
/** @var int */
13+
protected $iteratorKey;
14+
15+
public function __construct() {
16+
$this->iteratorKey = 0;
17+
$this->iterator = [];
18+
$this->loadGithubRepos();
19+
}
20+
21+
protected function loadGithubRepos() {
22+
$dataDir = Path::getDataDirectory();
23+
$cacheFilePath = implode(DIRECTORY_SEPARATOR, [
24+
$dataDir,
25+
Cache::CACHE_FILE,
26+
]);
27+
if(!is_file($cacheFilePath)) {
28+
Cache::refresh();
29+
}
30+
31+
$this->iterator = unserialize(file_get_contents($cacheFilePath));
32+
}
33+
34+
/**
35+
* @link https://php.net/manual/en/iterator.rewind.php
36+
*/
37+
public function rewind():void {
38+
$this->iteratorKey = 0;
39+
}
40+
41+
/**
42+
* @link https://php.net/manual/en/iterator.valid.php
43+
*/
44+
public function valid():bool {
45+
return isset($this->iterator[$this->iteratorKey]);
46+
}
47+
48+
/**
49+
* @link https://php.net/manual/en/iterator.key.php
50+
*/
51+
public function key():int {
52+
return $this->iteratorKey;
53+
}
54+
55+
/**
56+
* @link https://php.net/manual/en/iterator.current.php
57+
*/
58+
public function current():Repo {
59+
return $this->iterator[$this->iteratorKey];
60+
}
61+
62+
/**
63+
* @link https://php.net/manual/en/iterator.next.php
64+
*/
65+
public function next():void {
66+
$this->iteratorKey++;
67+
}
668
}

crontab

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0 * * * * Gt\Website\Github\Cache::refresh()

log.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ Smooth scroll clicking logo
1414
Begin styling concertina (complex due to different display on mobile/desktop)
1515
Add pips to centre of concertina circles
1616
Style empty repo list
17-
Grid layout repo list
17+
Grid layout repo list
18+
Github repo loader
19+
Show more button on repo list

page/index.html

+8-27
Original file line numberDiff line numberDiff line change
@@ -132,39 +132,20 @@ <h1>
132132
Sorted by number of Github stars.
133133
</p>
134134

135+
<input type="checkbox" id="repo-list-show-all" />
135136
<ul>
136-
<li>
137-
<a href="/docs/{repo}">
138-
<h1>Repo Name</h1>
139-
<p>Repo description.</p>
140-
</a>
141-
</li>
142-
<li>
143-
<a href="/docs/{repo}">
144-
<h1>Repo Name</h1>
145-
<p>Repo description.</p>
146-
</a>
147-
</li>
148-
<li>
149-
<a href="/docs/{repo}">
150-
<h1>Repo Name</h1>
151-
<p>Repo description.</p>
152-
</a>
153-
</li>
154-
<li>
155-
<a href="/docs/{repo}">
156-
<h1>Repo Name</h1>
157-
<p>Repo description.</p>
158-
</a>
159-
</li>
160-
<li>
137+
<li data-template>
161138
<a href="/docs/{repo}">
162-
<h1>Repo Name</h1>
163-
<p>Repo description.</p>
139+
<h1 data-bind:text="name">Repo Name</h1>
140+
<p data-bind:text="description">Repo description.</p>
164141
</a>
165142
</li>
166143
</ul>
167144

145+
<div>
146+
<label for="repo-list-show-all">Show all…</label>
147+
</div>
148+
168149
</section>
169150

170151
<section class="_contributing">

page/index.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@
22
namespace Gt\Website\Page;
33

44
use Gt\WebEngine\Logic\Page;
5+
use Gt\Website\Github\RepoList;
56

67
class IndexPage extends Page {
78
public function go() {
8-
$this->document->querySelector("._repo-list");
9+
$this->outputRepoList();
10+
}
11+
12+
protected function outputRepoList():void {
13+
$this->document->querySelector("._repo-list ul")->bind(
14+
new RepoList()
15+
);
916
}
1017
}

style/component/repo-list.scss

+31-4
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@
3030
position: relative;
3131
border-bottom: 1px solid rgba($palette-text-alt, 0.5);
3232

33-
&:last-of-type {
34-
border: none;
35-
}
36-
3733
>a {
3834
display: block;
3935
overflow: hidden;
@@ -79,4 +75,35 @@
7975
}
8076
}
8177
}
78+
79+
#repo-list-show-all {
80+
display: none;
81+
82+
&:not(:checked) {
83+
~ul {
84+
li {
85+
@for $i from 7 through 20 {
86+
&:nth-child(#{$i}) {
87+
display: none;
88+
}
89+
}
90+
}
91+
}
92+
}
93+
&:checked {
94+
~div {
95+
display: none;
96+
}
97+
}
98+
99+
~div {
100+
@extend %padding-m, %reading-column;
101+
text-align: center;
102+
103+
label {
104+
@extend button;
105+
display: inline-block;
106+
}
107+
}
108+
}
82109
}

0 commit comments

Comments
 (0)