forked from trilbymedia/grav-plugin-git-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-sync.php
More file actions
242 lines (198 loc) · 6.82 KB
/
git-sync.php
File metadata and controls
242 lines (198 loc) · 6.82 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
namespace Grav\Plugin;
use Grav\Common\Data\Data;
use Grav\Common\Plugin;
use Grav\Plugin\GitSync\AdminController;
use Grav\Plugin\GitSync\GitSync;
use Grav\Plugin\GitSync\Helper;
use RocketTheme\Toolbox\Event\Event;
/**
* Class GitSyncPlugin
*
* @package Grav\Plugin
*/
class GitSyncPlugin extends Plugin
{
protected $controller;
protected $git;
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 1000],
'onPageInitialized' => ['onPageInitialized', 0],
'onFormProcessed' => ['onFormProcessed', 0]
];
}
/**
* Initialize the plugin
*/
public function onPluginsInitialized()
{
require_once __DIR__ . '/vendor/autoload.php';
$this->enable(['gitsync' => ['synchronize', 0]]);
$this->init();
if ($this->isAdmin()) {
$this->enable([
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
'onTwigSiteVariables' => ['onTwigSiteVariables', 0],
'onAdminSave' => ['onAdminSave', 0],
'onAdminAfterSave' => ['onAdminAfterSave', 0],
'onAdminAfterSaveAs' => ['synchronize', 0],
'onAdminAfterDelete' => ['synchronize', 0],
'onAdminAfterAddMedia' => ['synchronize', 0],
'onAdminAfterDelMedia' => ['synchronize', 0],
]);
return;
} else {
$config = $this->config->get('plugins.' . $this->name);
$route = $this->grav['uri']->route();
$webhook = isset($config['webhook']) ? $config['webhook'] : false;
if ($route === $webhook) {
try {
$this->synchronize();
echo json_encode([
'status' => 'success',
'message' => 'GitSync completed the synchronization'
]);
} catch (\Exception $e) {
echo json_encode([
'status' => 'error',
'message' => 'GitSync failed to synchronize'
]);
}
exit;
}
}
}
public function init()
{
if ($this->isAdmin()) {
/** @var AdminController controller */
$this->controller = new AdminController($this);
} else {
$this->controller = new \stdClass;
$this->controller->git = new GitSync($this);
}
$this->git = $this->controller->git;
}
public function synchronize()
{
if (!Helper::isGitInstalled() || !Helper::isGitInitialized()) {
return true;
}
$this->grav->fireEvent('onGitSyncBeforeSynchronize');
if (!$this->git->isWorkingCopyClean()) {
// commit any change
$this->git->commit();
}
// synchronize with remote
$this->git->sync();
$this->grav->fireEvent('onGitSyncAfterSynchronize');
return true;
}
public function reset()
{
if (!Helper::isGitInstalled() || !Helper::isGitInitialized()) {
return true;
}
$this->grav->fireEvent('onGitSyncBeforeReset');
$this->git->reset();
$this->grav->fireEvent('onGitSyncAfterReset');
return true;
}
/**
* Add current directory to twig lookup paths.
*/
public function onTwigTemplatePaths()
{
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
}
/**
* Set needed variables to display cart.
*/
public function onTwigSiteVariables()
{
// workaround for admin plugin issue that doesn't properly unsubscribe
// events upon plugin uninstall
if (!class_exists('Grav\Plugin\GitSync\Helper')) {
return false;
}
$settings = [
'first_time' => !Helper::isGitInitialized(),
'git_installed' => Helper::isGitInstalled()
];
$this->grav['twig']->twig_vars['git_sync'] = $settings;
if ($this->grav['uri']->path() === '/admin/plugins/git-sync') {
$this->grav['assets']->addCss('plugin://git-sync/css-compiled/git-sync.css');
$this->grav['assets']->addJs('plugin://git-sync/js/app.js', ['loading' => 'defer']);
}
return true;
}
public function onPageInitialized()
{
if ($this->isAdmin() && $this->controller->isActive()) {
$this->controller->execute();
$this->controller->redirect();
}
}
public function onAdminSave($event)
{
$obj = $event['object'];
$isPluginRoute = $this->grav['uri']->path() == '/admin/plugins/' . $this->name;
if ($obj instanceof Data) {
if (!$isPluginRoute || !Helper::isGitInstalled()) {
return true;
} else {
// empty password, keep current one or encrypt if haven't already
$password = $obj->get('password', false);
if (!$password) { // set to !()
$current_password = $this->controller->git->getPassword();
// password exists but was never encrypted
if (substr($current_password, 0, 8) !== 'gitsync-') {
$current_password = Helper::encrypt($current_password);
}
} else {
// password is getting changed
$current_password = Helper::encrypt($password);
}
$obj->set('password', $current_password);
}
}
return $obj;
}
public function onAdminAfterSave($event)
{
$obj = $event['object'];
$isPluginRoute = $this->grav['uri']->path() == '/admin/plugins/' . $this->name;
/*
$folders = $this->controller->git->getConfig('folders', []);
if (!$isPluginRoute && !in_array('config', $folders)) {
return true;
}
*/
if ($obj instanceof Data) {
if (!$isPluginRoute || !Helper::isGitInstalled()) {
return true;
} else {
$this->controller->git->setConfig($obj);
// initialize git if not done yet
$this->controller->git->initializeRepository();
// set committer and remote data
$this->controller->git->setUser();
$this->controller->git->addRemote();
}
}
$this->synchronize();
return true;
}
public function onFormProcessed(Event $event)
{
$action = $event['action'];
if ($action == 'gitsync') {
$this->synchronize();
}
}
}