-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathBootTrait.php
299 lines (248 loc) · 9.84 KB
/
BootTrait.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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
namespace Civi\Cv\Util;
use Civi\Cv\ErrorHandler;
use Civi\Cv\Log\InternalLogger;
use Civi\Cv\Log\SymfonyConsoleLogger;
use Civi\Cv\PharOut\PharOut;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* This trait can be mixed into a Symfony `Command` to provide Civi/CMS-bootstrap options.
*
* It first boots Civi and then boots the CMS.
*/
trait BootTrait {
public function configureBootOptions($defaultLevel = 'full|cms-full') {
$this->addOption('level', NULL, InputOption::VALUE_REQUIRED, 'Bootstrap level (none,classloader,settings,full,cms-only,cms-full)', $defaultLevel);
$this->addOption('hostname', NULL, InputOption::VALUE_REQUIRED, 'Hostname (for a multisite system)');
$this->addOption('test', 't', InputOption::VALUE_NONE, 'Bootstrap the test database (CIVICRM_UF=UnitTests)');
$this->addOption('user', 'U', InputOption::VALUE_REQUIRED, 'CMS user');
}
public function boot(InputInterface $input, OutputInterface $output) {
$logger = $this->bootLogger($output);
$logger->debug('Start');
$GLOBALS['civicrm_url_defaults'] = $GLOBALS['civicrm_url_defaults'] ?? [];
array_unshift($GLOBALS['civicrm_url_defaults'], [
'format' => 'absolute',
'scheme' => 'default',
]);
$this->setupErrorHandling($output);
if ($input->hasOption('test') && $input->getOption('test')) {
$logger->debug('Use test mode');
putenv('CIVICRM_UF=UnitTests');
$_ENV['CIVICRM_UF'] = 'UnitTests';
}
if ($input->getOption('level') === 'full|cms-full') {
if (getenv('CIVICRM_UF') === 'UnitTests') {
$input->setOption('level', 'full');
}
elseif (getenv('CIVICRM_BOOT')) {
$input->setOption('level', 'cms-full');
}
elseif (getenv('CIVICRM_SETTINGS')) {
$input->setOption('level', 'full');
}
else {
$input->setOption('level', 'full');
// TODO (when tests pass, for v0.4): $input->setOption('level', 'cms-full');
}
}
if (getenv('CIVICRM_UF') === 'UnitTests' && preg_match('/^cms-/', $input->getOption('level'))) {
throw new \Exception("UnitTest bootstrapping is not compatible with CMS bootstrapping");
}
$func = '_boot_' . strtr($input->getOption('level'), '-', '_');
if (is_callable([$this, $func])) {
call_user_func([$this, $func], $input, $output);
}
else {
throw new \Exception("Unrecognized bootstrap level");
}
// CMS may have installed wonky error-handling. Add our own.
ErrorHandler::pushHandler();
$logger->debug('Finished');
}
/**
* Do not do anything for bootstrap. Just use the given environment.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
*/
public function _boot_none(InputInterface $input, OutputInterface $output) {
$this->bootLogger($output)->debug('Skip');
}
/**
* Setup the CiviCRM classloader.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
*/
public function _boot_classloader(InputInterface $input, OutputInterface $output) {
$this->bootLogger($output)->debug('Call basic cv bootstrap (' . $input->getOption('level') . ')');
\Civi\Cv\Bootstrap::singleton()->boot($this->createBootParams($input, $output) + array(
'prefetch' => FALSE,
));
}
/**
* Get the CiviCRM classloader and settings file.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
*/
public function _boot_settings(InputInterface $input, OutputInterface $output) {
$this->bootLogger($output)->debug('Call basic cv bootstrap (' . $input->getOption('level') . ')');
\Civi\Cv\Bootstrap::singleton()->boot($this->createBootParams($input, $output) + array(
'prefetch' => FALSE,
));
}
/**
* Boot CiviCRM, then boot the corresponding CMS.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @throws \Exception
*/
public function _boot_full(InputInterface $input, OutputInterface $output) {
PharOut::prepare();
$logger = $this->bootLogger($output);
$logger->debug('Call standard cv bootstrap');
\Civi\Cv\Bootstrap::singleton()->boot($this->createBootParams($input, $output));
$logger->debug('Call core bootstrap');
\CRM_Core_Config::singleton();
$logger->debug('Call CMS bootstrap');
\CRM_Utils_System::loadBootStrap(array(), FALSE);
if ($input->getOption('user')) {
$logger->debug('Set system user');
if (is_callable(array(\CRM_Core_Config::singleton()->userSystem, 'loadUser'))) {
if (!\CRM_Core_Config::singleton()->userSystem->loadUser($input->getOption('user')) || !$this->ensureUserContact($output)) {
throw new \Exception("Failed to determine contactID for user=" . $input->getOption('user'));
}
}
else {
$output->getErrorOutput()->writeln("<error>Failed to set user. Feature not supported by UF (" . CIVICRM_UF . ")</error>");
}
}
if (is_callable([\CRM_Core_Config::singleton()->userSystem, 'setMySQLTimeZone'])) {
$logger->debug('Set active MySQL timezone');
\CRM_Core_Config::singleton()->userSystem->setMySQLTimeZone();
}
if (CIVICRM_UF === 'Joomla') {
PharOut::reset();
}
}
/**
* Boot only the CMS.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return \Civi\Cv\CmsBootstrap
*/
public function _boot_cms_only(InputInterface $input, OutputInterface $output) {
$bootstrap = $this->createCmsBootstrap($input, $output);
$this->bootLogger($output)->debug('Call CMS bootstrap');
$bootstrap->bootCms();
return $bootstrap;
}
/**
* Boot the CMS, then boot CiviCRM.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return \Civi\Cv\CmsBootstrap
*/
public function _boot_cms_full(InputInterface $input, OutputInterface $output) {
$bootstrap = $this->_boot_cms_only($input, $output);
$this->bootLogger($output)->debug('Call Civi bootstrap');
$bootstrap->bootCivi();
return $bootstrap;
}
/**
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return \Civi\Cv\CmsBootstrap
*/
protected function createCmsBootstrap(InputInterface $input, OutputInterface $output) {
if ($output->isDebug()) {
// add the output object to allow the bootstrapper to output debug messages
// and track verboisty
$boot_params = array('output' => $output);
}
else {
$boot_params = array();
}
if ($input->getOption('user')) {
$boot_params['user'] = $input->getOption('user');
}
if ($input->getOption('hostname')) {
$boot_params['httpHost'] = $input->getOption('hostname');
}
return \Civi\Cv\CmsBootstrap::singleton()->addOptions($boot_params);
}
/**
* Ensure that the current user has a contact record.
*
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return int|NULL
* The user's contact ID, or NULL
*/
public function ensureUserContact(OutputInterface $output) {
if ($cid = \CRM_Core_Session::getLoggedInContactID()) {
return $cid;
}
// Ugh, this codepath is ridiculous.
switch (CIVICRM_UF) {
case 'Drupal':
case 'Drupal6':
case 'Backdrop':
\CRM_Core_BAO_UFMatch::synchronize($GLOBALS['user'], TRUE, CIVICRM_UF, 'Individual');
break;
case 'Drupal8':
$user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());
\CRM_Core_BAO_UFMatch::synchronize($user, TRUE, CIVICRM_UF, 'Individual');
break;
case 'Joomla':
\CRM_Core_BAO_UFMatch::synchronize(\JFactory::getUser(), TRUE, CIVICRM_UF, 'Individual');
break;
case 'Standalone':
// At time of writing, support for 'loggedInUser' is in a PR. Once the functionality is merged, we can update/simplify this.
if (empty($GLOBALS['loggedInUser'])) {
$this->bootLogger($output)->error('Failed to sync user/contact. You may be running a pre-release of civicrm-standalone.');
}
else {
\CRM_Core_BAO_UFMatch::synchronize($GLOBALS['loggedInUser'], TRUE, CIVICRM_UF, 'Individual');
}
break;
case 'WordPress':
\CRM_Core_BAO_UFMatch::synchronize($GLOBALS['current_user'], TRUE, CIVICRM_UF, 'Individual');
break;
default:
$this->bootLogger($output)->error("Unrecognized UF: " . CIVICRM_UF);
}
return \CRM_Core_Session::getLoggedInContactID();
}
protected function setupErrorHandling(OutputInterface $output) {
$this->bootLogger($output)->debug('Attempting to set verbose error reporting');
// standard php debug chat settings
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 'stderr');
ini_set('display_startup_errors', TRUE);
}
/**
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return array
*/
protected function createBootParams(InputInterface $input, OutputInterface $output) {
$boot_params = [];
if ($output->isDebug()) {
$boot_params['output'] = $output;
}
if ($input->getOption('hostname')) {
$boot_params['httpHost'] = $input->getOption('hostname');
}
return $boot_params;
}
private function bootLogger(OutputInterface $output): InternalLogger {
return new SymfonyConsoleLogger('BootTrait', $output);
}
}