-
Notifications
You must be signed in to change notification settings - Fork 3
/
github.php
87 lines (73 loc) · 2.15 KB
/
github.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
namespace Grav\Plugin;
use Grav\Common\Plugin;
use Grav\Common\Page\Page;
use Grav\Common\Grav;
class GitHubPlugin extends Plugin
{
protected $active = false;
protected $github;
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0]
];
}
/**
* Initialize configuration
*/
public function onPluginsInitialized()
{
if ($this->isAdmin()) {
$this->active = false;
return;
}
$this->enable([
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
'onPageInitialized' => ['onPageInitialized', 0]
]);
}
/**
* Add current directory to twig lookup paths.
*/
public function onTwigTemplatePaths()
{
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
}
/**
* Initialize github when detected in a page.
*/
public function onPageInitialized()
{
$page = $this->grav['page'];
if (!$page) {
return;
}
if (isset($page->header()->github)) {
$this->active = true;
$config = $this->grav['config'];
$method = $config->get('plugins.github.auth.method');
$token = $config->get('plugins.github.auth.token');
$passwd = $config->get('plugins.github.auth.password');
// Initialize GitHub API Class
require_once __DIR__ . '/classes/github.php';
$this->github = new GitHub($page);
if ($method && $token) {
switch($method) {
case 'api':
$method = \Github\Client::AUTH_URL_TOKEN;
$passwd = null;
break;
case 'password':
$method = \Github\Client::AUTH_HTTP_PASSWORD;
break;
}
$this->github->client->authenticate($token, $passwd, $method);
}
$this->grav['twig']->twig_vars['github'] = $this->github;
}
}
}