-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModule.php
155 lines (135 loc) · 4.93 KB
/
Module.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
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
<?php
namespace PueueJobDispatcher;
use Laminas\Mvc\Controller\AbstractController;
use Laminas\View\Renderer\PhpRenderer;
use Omeka\Settings\Settings;
use Omeka\Stdlib\Cli;
use PueueJobDispatcher\Form\ConfigForm;
use Laminas\Config\Factory;
use Omeka\Mvc\Controller\Plugin\Messenger;
use Generic\AbstractModule;
use PueueJobDispatcher\Job\DispatchStrategy\Pueue;
if (!class_exists(\Generic\AbstractModule::class)) {
require file_exists(dirname(__DIR__) . '/Generic/AbstractModule.php')
? dirname(__DIR__) . '/Generic/AbstractModule.php'
: __DIR__ . '/src/Generic/AbstractModule.php';
}
class Module extends AbstractModule
{
const NAMESPACE = __NAMESPACE__;
private array $config = [];
protected array $dependencies = [];
public function getConfig()
{
if ($this->config) {
return $this->config;
}
// Load our configuration.
$this->config = Factory::fromFiles(
glob(__DIR__ . '/config/*.config.php')
);
return $this->config;
}
/**
* Get Configuration Form
*
* @param PhpRenderer $renderer
* @return string
*/
public function getConfigForm(PhpRenderer $renderer)
{
$services = $this->getServiceLocator();
/** @var array $config */
$config = $services->get('Config');
/** @var Settings $settings */
$settings = $services->get('Omeka\Settings');
/** @var Cli $cli */
$cli = $services->get('Omeka\Cli');
$view = $renderer;
$plugins = $services->get('ControllerPluginManager');
/** @var Messenger $messenger */
$messenger = $plugins->get('messenger');
// prepare form values
$data = [];
$defaultSettings = $config[strtolower(__NAMESPACE__)]['config'];
foreach ($defaultSettings as $name => $value) {
$data[$name] = $settings->get($name, $value);
}
// init form
$form = $services->get('FormElementManager')->get(ConfigForm::class);
$form->init();
$form->setData($data);
// checks
// check pueue path
if ($data['pueue_path']) {
$pueuePath = $cli->validateCommand($data['pueue_path']);
if (false === $pueuePath) {
$messenger->addError("Pueue client not found ({$data['pueue_path']}).");
}
} else {
$pueuePath = $cli->getCommandPath('pueue');
if (false === $pueuePath) {
$messenger->addError("Pueue client not found.");
}
}
// check pueue client
if ($pueuePath) {
$version = $cli->execute(
sprintf('%s --version', escapeshellcmd($pueuePath) )
);
if ( strpos($version, 'Pueue client') !== 0 ) {
$messenger->addError('Invalid pueue client. ');
} else {
$version = str_replace('Pueue client ', '', $version);
$messenger->addSuccess("Pueue client {$version} found ({$pueuePath}).");
$status = $cli->execute(
sprintf('%s status --json', escapeshellcmd($pueuePath) )
);
if (false === $status) {
$messenger->addError('Failed to connect to pueue service.');
} else {
$messenger->addSuccess('Pueue service running.');
}
}
}
// check pueue dispatch service
$dispatcher = $services->get('Omeka\Job\DispatchStrategy');
if ($dispatcher instanceof Pueue) {
$messenger->addSuccess('Omeka Pueue Dispatch Service installed.');
}
$form->prepare();
return $view->formCollection($form);
}
/**
* Handle Configuration Form
*
* @param AbstractController $controller
* @return bool
*/
public function handleConfigForm(AbstractController $controller)
{
$services = $this->getServiceLocator();
$config = $services->get('Config');
$settings = $services->get('Omeka\Settings');
$params = $controller->getRequest()->getPost();
// init & validate form
$form = $services->get('FormElementManager')->get(ConfigForm::class);
$form->init();
$form->setData($params);
if (!$form->isValid()) {
$controller->messenger()->addErrors($form->getMessages());
return false;
}
// sanitize data
$params['pueue_path'] = trim($params['pueue_path']) == '' ? null : trim($params['pueue_path']);
$params['pueue_group'] = trim($params['pueue_group']) == '' ? null : trim($params['pueue_group']);
// save settings
$defaultSettings = $config[strtolower(__NAMESPACE__)]['config'];
foreach ($params as $name => $value) {
if (array_key_exists($name, $defaultSettings)) {
$settings->set($name, $value);
}
}
return true;
}
}