| 1 |
<?php |
| 2 |
/* |
| 3 |
* This file is part of the ManageWP Worker plugin. |
| 4 |
* |
| 5 |
* (c) ManageWP LLC <contact@managewp.com> |
| 6 |
* |
| 7 |
* For the full copyright and license information, please view the LICENSE |
| 8 |
* file that was distributed with this source code. |
| 9 |
*/ |
| 10 |
|
| 11 |
abstract class MWP_ServiceContainer_Abstract implements MWP_ServiceContainer_Interface |
| 12 |
{ |
| 13 |
|
| 14 |
private $parameters; |
| 15 |
|
| 16 |
private $wordPressContext; |
| 17 |
|
| 18 |
private $eventDispatcher; |
| 19 |
|
| 20 |
private $actionMapper; |
| 21 |
|
| 22 |
private $requestStack; |
| 23 |
|
| 24 |
private $userQuery; |
| 25 |
|
| 26 |
private $postQuery; |
| 27 |
|
| 28 |
private $commentQuery; |
| 29 |
|
| 30 |
private $pluginProvider; |
| 31 |
|
| 32 |
private $themeProvider; |
| 33 |
|
| 34 |
private $workerBrand; |
| 35 |
|
| 36 |
private $autoUpdateManager; |
| 37 |
|
| 38 |
private $updateManager; |
| 39 |
|
| 40 |
private $signer; |
| 41 |
|
| 42 |
private $signer256; |
| 43 |
|
| 44 |
private $crypter; |
| 45 |
|
| 46 |
private $nonceManager; |
| 47 |
|
| 48 |
private $configuration; |
| 49 |
|
| 50 |
private $logger; |
| 51 |
|
| 52 |
private $responseCallback; |
| 53 |
|
| 54 |
private $errorHandler; |
| 55 |
|
| 56 |
private $executableFinder; |
| 57 |
|
| 58 |
private $hitCounter; |
| 59 |
|
| 60 |
private $jsonMessageHandler; |
| 61 |
|
| 62 |
private $errorLogger; |
| 63 |
|
| 64 |
private $systemEnvironment; |
| 65 |
|
| 66 |
private $updaterSkin; |
| 67 |
|
| 68 |
private $sessionStore; |
| 69 |
|
| 70 |
private $migration; |
| 71 |
|
| 72 |
private $gelfPublisher; |
| 73 |
|
| 74 |
public function __construct(array $parameters = array()) |
| 75 |
{ |
| 76 |
$this->parameters = $parameters; |
| 77 |
|
| 78 |
$this->parameters += array( |
| 79 |
'request_id' => null, |
| 80 |
// This plugin's main file absolute path. |
| 81 |
'worker_realpath' => null, |
| 82 |
// This plugin's "basename", ie. 'worker/init.php'. |
| 83 |
'worker_basename' => null, |
| 84 |
// Always use PhpSecLib, even if the PHP extension 'openssl' is loaded. |
| 85 |
'prefer_phpseclib' => false, |
| 86 |
// Force using fallbacks instead of shell commands |
| 87 |
'disable_shell' => false, |
| 88 |
// Emulate disabled PDO extension |
| 89 |
'disable_pdo' => false, |
| 90 |
// Emulate disabled mysqli extension |
| 91 |
'disable_mysqli' => false, |
| 92 |
// Emulate disabled mysql |
| 93 |
'disable_mysql' => false, |
| 94 |
// Do not do self request on the website |
| 95 |
'disable_ping_back' => false, |
| 96 |
// When was the logging started (timestamp) |
| 97 |
'log_start' => false, |
| 98 |
// Log file to use for all worker logs. |
| 99 |
'log_file' => null, |
| 100 |
// GrayLog2 server to use for all worker logs. |
| 101 |
'gelf_server' => null, |
| 102 |
// UDP port. |
| 103 |
'gelf_port' => null, |
| 104 |
// TCP port. |
| 105 |
'gelf_port_fallback' => null, |
| 106 |
// Capture errors in master response. |
| 107 |
'log_errors' => false, |
| 108 |
// Pad length used for progress message flushing. |
| 109 |
'message_pad_length' => 16384, |
| 110 |
// Minimum log level for streamed messages. |
| 111 |
'message_minimum_level' => Monolog_Logger::INFO, |
| 112 |
// Memory size (in kilobytes) to allocate for fatal error handling when the request is authenticated. |
| 113 |
'fatal_error_reserved_memory_size' => 1024, |
| 114 |
// This boundary will be different with each request and it's pretty much a hack for handling exceptions |
| 115 |
// @see MWP_EventListener_ActionException_MultipartException |
| 116 |
'multipart_boundary' => uniqid(), |
| 117 |
'hit_counter_blacklisted_ips' => array( |
| 118 |
// Uptime monitoring robot. |
| 119 |
'/^46\.137\.190\.132$/', |
| 120 |
'/^54\.79\.28\.129$/', |
| 121 |
'/^54\.94\.142\.218$/', |
| 122 |
'/^54\.67\.10\.127$/', |
| 123 |
'/^54\.64\.67\.106$/', |
| 124 |
'/^69\.162\.124\.226$/', |
| 125 |
'/^69\.162\.124\.227$/', |
| 126 |
'/^69\.162\.124\.228$/', |
| 127 |
'/^69\.162\.124\.229$/', |
| 128 |
'/^69\.162\.124\.230$/', |
| 129 |
'/^69\.162\.124\.231$/', |
| 130 |
'/^69\.162\.124\.232$/', |
| 131 |
'/^69\.162\.124\.233$/', |
| 132 |
'/^69\.162\.124\.234$/', |
| 133 |
'/^69\.162\.124\.235$/', |
| 134 |
'/^69\.162\.124\.236$/', |
| 135 |
'/^69\.162\.124\.237$/', |
| 136 |
'/^69\.162\.124\.238$/', |
| 137 |
'/^74\.86\.158\.106$/', |
| 138 |
'/^74\.86\.158\.107$/', |
| 139 |
'/^74\.86\.158\.109$/', |
| 140 |
'/^74\.86\.158\.110$/', |
| 141 |
'/^74\.86\.158\.108$/', |
| 142 |
'/^122\.248\.234\.23$/', |
| 143 |
'/^178\.62\.52\.237$/', |
| 144 |
'/^188\.226\.183\.141$/', |
| 145 |
), |
| 146 |
'hit_counter_blacklisted_user_agents' => array( |
| 147 |
'/bot/', |
| 148 |
'/crawl/', |
| 149 |
'/feed/', |
| 150 |
'/java\//', |
| 151 |
'/spider/', |
| 152 |
'/curl/', |
| 153 |
'/libwww/', |
| 154 |
'/alexa/', |
| 155 |
'/altavista/', |
| 156 |
'/aolserver/', |
| 157 |
'/appie/', |
| 158 |
'/Ask Jeeves/', |
| 159 |
'/baidu/', |
| 160 |
'/Bing/', |
| 161 |
'/borg/', |
| 162 |
'/BrowserMob/', |
| 163 |
'/ccooter/', |
| 164 |
'/dataparksearch/', |
| 165 |
'/Download Demon/', |
| 166 |
'/echoping/', |
| 167 |
'/FAST/', |
| 168 |
'/findlinks/', |
| 169 |
'/Firefly/', |
| 170 |
'/froogle/', |
| 171 |
'/GomezA/', |
| 172 |
'/Google/', |
| 173 |
'/grub-client/', |
| 174 |
'/htdig/', |
| 175 |
'/http%20client/', |
| 176 |
'/HttpMonitor/', |
| 177 |
'/ia_archiver/', |
| 178 |
'/InfoSeek/', |
| 179 |
'/inktomi/', |
| 180 |
'/larbin/', |
| 181 |
'/looksmart/', |
| 182 |
'/Microsoft URL Control/', |
| 183 |
'/Minimo/', |
| 184 |
'/mogimogi/', |
| 185 |
'/NationalDirectory/', |
| 186 |
'/netcraftsurvey/', |
| 187 |
'/nuhk/', |
| 188 |
'/oegp/', |
| 189 |
'/panopta/', |
| 190 |
'/rabaz/', |
| 191 |
'/Read%20Later/', |
| 192 |
'/Scooter/', |
| 193 |
'/scrubby/', |
| 194 |
'/SearchExpress/', |
| 195 |
'/searchsight/', |
| 196 |
'/semanticdiscovery/', |
| 197 |
'/Slurp/', |
| 198 |
'/snappy/', |
| 199 |
'/Spade/', |
| 200 |
'/TechnoratiSnoop/', |
| 201 |
'/TECNOSEEK/', |
| 202 |
'/teoma/', |
| 203 |
'/twiceler/', |
| 204 |
'/URL2PNG/', |
| 205 |
'/vortex/', |
| 206 |
'/WebBug/', |
| 207 |
'/www\.galaxy\.com/', |
| 208 |
'/yahoo/', |
| 209 |
'/yandex/', |
| 210 |
'/zao/', |
| 211 |
'/zeal/', |
| 212 |
'/ZooShot/', |
| 213 |
'/ZyBorg/', |
| 214 |
), |
| 215 |
); |
| 216 |
} |
| 217 |
|
| 218 |
public function getParameter($name) |
| 219 |
{ |
| 220 |
if (!array_key_exists($name, $this->parameters)) { |
| 221 |
throw new InvalidArgumentException(sprintf('The parameter named "%s" does not exist.', $name)); |
| 222 |
} |
| 223 |
|
| 224 |
return $this->parameters[$name]; |
| 225 |
} |
| 226 |
|
| 227 |
public function setParameter($name, $value) |
| 228 |
{ |
| 229 |
$this->parameters[$name] = $value; |
| 230 |
} |
| 231 |
|
| 232 |
public function getWordPressContext() |
| 233 |
{ |
| 234 |
if ($this->wordPressContext === null) { |
| 235 |
$this->wordPressContext = $this->createWordPressContext(); |
| 236 |
} |
| 237 |
|
| 238 |
return $this->wordPressContext; |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* @return MWP_WordPress_Context |
| 243 |
*/ |
| 244 |
protected abstract function createWordPressContext(); |
| 245 |
|
| 246 |
public function getEventDispatcher() |
| 247 |
{ |
| 248 |
if ($this->eventDispatcher === null) { |
| 249 |
$this->eventDispatcher = $this->createEventDispatcher(); |
| 250 |
} |
| 251 |
|
| 252 |
return $this->eventDispatcher; |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* @return Symfony_EventDispatcher_EventDispatcherInterface |
| 257 |
*/ |
| 258 |
protected abstract function createEventDispatcher(); |
| 259 |
|
| 260 |
public function getRequestStack() |
| 261 |
{ |
| 262 |
if ($this->requestStack === null) { |
| 263 |
$this->requestStack = $this->createRequestStack(); |
| 264 |
} |
| 265 |
|
| 266 |
return $this->requestStack; |
| 267 |
} |
| 268 |
|
| 269 |
protected abstract function createRequestStack(); |
| 270 |
|
| 271 |
public function getActionRegistry() |
| 272 |
{ |
| 273 |
if ($this->actionMapper === null) { |
| 274 |
$this->actionMapper = $this->createActionRegistry(); |
| 275 |
} |
| 276 |
|
| 277 |
return $this->actionMapper; |
| 278 |
} |
| 279 |
|
| 280 |
/** |
| 281 |
* @return MWP_Action_Registry |
| 282 |
*/ |
| 283 |
protected abstract function createActionRegistry(); |
| 284 |
|
| 285 |
public function getUserQuery() |
| 286 |
{ |
| 287 |
if ($this->userQuery === null) { |
| 288 |
$this->userQuery = $this->createUserQuery(); |
| 289 |
} |
| 290 |
|
| 291 |
return $this->userQuery; |
| 292 |
} |
| 293 |
|
| 294 |
/** |
| 295 |
* @return MWP_WordPress_Query_User |
| 296 |
*/ |
| 297 |
protected abstract function createUserQuery(); |
| 298 |
|
| 299 |
public function getPostQuery() |
| 300 |
{ |
| 301 |
if ($this->postQuery === null) { |
| 302 |
$this->postQuery = $this->createPostQuery(); |
| 303 |
} |
| 304 |
|
| 305 |
return $this->postQuery; |
| 306 |
} |
| 307 |
|
| 308 |
protected abstract function createPostQuery(); |
| 309 |
|
| 310 |
public function getCommentQuery() |
| 311 |
{ |
| 312 |
if ($this->commentQuery === null) { |
| 313 |
$this->commentQuery = $this->createCommentQuery(); |
| 314 |
} |
| 315 |
|
| 316 |
return $this->commentQuery; |
| 317 |
} |
| 318 |
|
| 319 |
protected abstract function createCommentQuery(); |
| 320 |
|
| 321 |
/** |
| 322 |
* @return MWP_WordPress_Provider_Plugin |
| 323 |
*/ |
| 324 |
public function getPluginProvider() |
| 325 |
{ |
| 326 |
if ($this->pluginProvider === null) { |
| 327 |
$this->pluginProvider = $this->createPluginProvider(); |
| 328 |
} |
| 329 |
|
| 330 |
return $this->pluginProvider; |
| 331 |
} |
| 332 |
|
| 333 |
protected abstract function createPluginProvider(); |
| 334 |
|
| 335 |
/** |
| 336 |
* @return MWP_WordPress_Provider_Theme |
| 337 |
*/ |
| 338 |
public function getThemeProvider() |
| 339 |
{ |
| 340 |
if ($this->themeProvider === null) { |
| 341 |
$this->themeProvider = $this->createThemeProvider(); |
| 342 |
} |
| 343 |
|
| 344 |
return $this->themeProvider; |
| 345 |
} |
| 346 |
|
| 347 |
protected abstract function createThemeProvider(); |
| 348 |
|
| 349 |
/** |
| 350 |
* @return MWP_Worker_Brand |
| 351 |
*/ |
| 352 |
public function getBrand() |
| 353 |
{ |
| 354 |
if ($this->workerBrand === null) { |
| 355 |
$this->workerBrand = $this->createWorkerBrand(); |
| 356 |
} |
| 357 |
|
| 358 |
return $this->workerBrand; |
| 359 |
} |
| 360 |
|
| 361 |
protected abstract function createWorkerBrand(); |
| 362 |
|
| 363 |
public function getAutoUpdateManager() |
| 364 |
{ |
| 365 |
if ($this->autoUpdateManager === null) { |
| 366 |
$this->autoUpdateManager = $this->createAutoUpdateManager(); |
| 367 |
} |
| 368 |
|
| 369 |
return $this->autoUpdateManager; |
| 370 |
} |
| 371 |
|
| 372 |
protected abstract function createAutoUpdateManager(); |
| 373 |
|
| 374 |
public function getUpdateManager() |
| 375 |
{ |
| 376 |
if ($this->updateManager === null) { |
| 377 |
$this->updateManager = $this->createUpdateManager(); |
| 378 |
} |
| 379 |
|
| 380 |
return $this->updateManager; |
| 381 |
} |
| 382 |
|
| 383 |
/** |
| 384 |
* @return MWP_Updater_UpdateManager |
| 385 |
*/ |
| 386 |
protected abstract function createUpdateManager(); |
| 387 |
|
| 388 |
public function getSigner() |
| 389 |
{ |
| 390 |
if ($this->signer === null) { |
| 391 |
$this->signer = $this->createSigner(); |
| 392 |
} |
| 393 |
|
| 394 |
return $this->signer; |
| 395 |
} |
| 396 |
|
| 397 |
public function getSigner256() |
| 398 |
{ |
| 399 |
if ($this->signer256 === null) { |
| 400 |
$this->signer256 = MWP_Signer_Factory::createOpenSslSigner(); |
| 401 |
} |
| 402 |
|
| 403 |
return $this->signer256; |
| 404 |
} |
| 405 |
|
| 406 |
/** |
| 407 |
* @return MWP_Signer_Interface |
| 408 |
*/ |
| 409 |
protected abstract function createSigner(); |
| 410 |
|
| 411 |
public function getCrypter() |
| 412 |
{ |
| 413 |
if ($this->crypter === null) { |
| 414 |
$this->crypter = $this->createCrypter(); |
| 415 |
} |
| 416 |
|
| 417 |
return $this->crypter; |
| 418 |
} |
| 419 |
|
| 420 |
/** |
| 421 |
* @return MWP_Crypter_Interface |
| 422 |
*/ |
| 423 |
protected abstract function createCrypter(); |
| 424 |
|
| 425 |
public function getNonceManager() |
| 426 |
{ |
| 427 |
if ($this->nonceManager === null) { |
| 428 |
$this->nonceManager = $this->createNonceManager(); |
| 429 |
} |
| 430 |
|
| 431 |
return $this->nonceManager; |
| 432 |
} |
| 433 |
|
| 434 |
/** |
| 435 |
* @return MWP_Security_NonceManager |
| 436 |
*/ |
| 437 |
protected abstract function createNonceManager(); |
| 438 |
|
| 439 |
public function getConfiguration() |
| 440 |
{ |
| 441 |
if ($this->configuration === null) { |
| 442 |
$this->configuration = $this->createConfiguration(); |
| 443 |
} |
| 444 |
|
| 445 |
return $this->configuration; |
| 446 |
} |
| 447 |
|
| 448 |
/** |
| 449 |
* @return MWP_Worker_Configuration |
| 450 |
*/ |
| 451 |
protected abstract function createConfiguration(); |
| 452 |
|
| 453 |
public function getLogger() |
| 454 |
{ |
| 455 |
if ($this->logger === null) { |
| 456 |
$this->logger = $this->createLogger(); |
| 457 |
} |
| 458 |
|
| 459 |
return $this->logger; |
| 460 |
} |
| 461 |
|
| 462 |
/** |
| 463 |
* @return Monolog_Logger |
| 464 |
*/ |
| 465 |
protected abstract function createLogger(); |
| 466 |
|
| 467 |
/** |
| 468 |
* @return MWP_Worker_ResponseCallback |
| 469 |
*/ |
| 470 |
public function getResponseCallback() |
| 471 |
{ |
| 472 |
if ($this->responseCallback === null) { |
| 473 |
$this->responseCallback = $this->createResponseCallback(); |
| 474 |
} |
| 475 |
|
| 476 |
return $this->responseCallback; |
| 477 |
} |
| 478 |
|
| 479 |
protected abstract function createResponseCallback(); |
| 480 |
|
| 481 |
public function getErrorHandler() |
| 482 |
{ |
| 483 |
if ($this->errorHandler === null) { |
| 484 |
$this->errorHandler = $this->createErrorHandler(); |
| 485 |
} |
| 486 |
|
| 487 |
return $this->errorHandler; |
| 488 |
} |
| 489 |
|
| 490 |
/** |
| 491 |
* @return Monolog_ErrorHandler |
| 492 |
*/ |
| 493 |
protected abstract function createErrorHandler(); |
| 494 |
|
| 495 |
public function getExecutableFinder() |
| 496 |
{ |
| 497 |
if ($this->executableFinder === null) { |
| 498 |
$this->executableFinder = $this->createExecutableFinder(); |
| 499 |
} |
| 500 |
|
| 501 |
return $this->executableFinder; |
| 502 |
} |
| 503 |
|
| 504 |
/** |
| 505 |
* @return Symfony_Process_ExecutableFinder |
| 506 |
*/ |
| 507 |
protected abstract function createExecutableFinder(); |
| 508 |
|
| 509 |
public function getHitCounter() |
| 510 |
{ |
| 511 |
if ($this->hitCounter === null) { |
| 512 |
$this->hitCounter = $this->createHitCounter(); |
| 513 |
} |
| 514 |
|
| 515 |
return $this->hitCounter; |
| 516 |
} |
| 517 |
|
| 518 |
/** |
| 519 |
* @return MWP_Extension_HitCounter |
| 520 |
*/ |
| 521 |
protected abstract function createHitCounter(); |
| 522 |
|
| 523 |
public function getJsonMessageHandler() |
| 524 |
{ |
| 525 |
if ($this->jsonMessageHandler === null) { |
| 526 |
$this->jsonMessageHandler = $this->createJsonMessageHandler(); |
| 527 |
} |
| 528 |
|
| 529 |
return $this->jsonMessageHandler; |
| 530 |
} |
| 531 |
|
| 532 |
/** |
| 533 |
* @return MWP_Monolog_Handler_JsonMessageHandler |
| 534 |
*/ |
| 535 |
protected abstract function createJsonMessageHandler(); |
| 536 |
|
| 537 |
public function getErrorLogger() |
| 538 |
{ |
| 539 |
if ($this->errorLogger === null) { |
| 540 |
$this->errorLogger = $this->createErrorLogger(); |
| 541 |
} |
| 542 |
|
| 543 |
return $this->errorLogger; |
| 544 |
} |
| 545 |
|
| 546 |
/** |
| 547 |
* @return Monolog_Logger |
| 548 |
*/ |
| 549 |
protected abstract function createErrorLogger(); |
| 550 |
|
| 551 |
public function getSystemEnvironment() |
| 552 |
{ |
| 553 |
if ($this->systemEnvironment === null) { |
| 554 |
$this->systemEnvironment = $this->createSystemEnvironment(); |
| 555 |
} |
| 556 |
|
| 557 |
return $this->systemEnvironment; |
| 558 |
} |
| 559 |
|
| 560 |
protected abstract function createSystemEnvironment(); |
| 561 |
|
| 562 |
public function getUpdaterSkin() |
| 563 |
{ |
| 564 |
if ($this->updaterSkin === null) { |
| 565 |
$this->updaterSkin = $this->createUpdaterSkin(); |
| 566 |
} |
| 567 |
|
| 568 |
return $this->updaterSkin; |
| 569 |
} |
| 570 |
|
| 571 |
/** |
| 572 |
* @return MWP_Updater_TraceableUpdaterSkin |
| 573 |
*/ |
| 574 |
protected abstract function createUpdaterSkin(); |
| 575 |
|
| 576 |
public function getSessionStore() |
| 577 |
{ |
| 578 |
if ($this->sessionStore === null) { |
| 579 |
$this->sessionStore = $this->createSessionStore(); |
| 580 |
} |
| 581 |
|
| 582 |
return $this->sessionStore; |
| 583 |
} |
| 584 |
|
| 585 |
/** |
| 586 |
* @return MWP_WordPress_SessionStore |
| 587 |
*/ |
| 588 |
protected abstract function createSessionStore(); |
| 589 |
|
| 590 |
public function getMigration() |
| 591 |
{ |
| 592 |
if ($this->migration === null) { |
| 593 |
$this->migration = $this->createMigration(); |
| 594 |
} |
| 595 |
|
| 596 |
return $this->migration; |
| 597 |
} |
| 598 |
|
| 599 |
/** |
| 600 |
* @return MWP_Migration_Migration |
| 601 |
*/ |
| 602 |
protected abstract function createMigration(); |
| 603 |
|
| 604 |
/** |
| 605 |
* {@inheritdoc} |
| 606 |
*/ |
| 607 |
public function getGelfPublisher() |
| 608 |
{ |
| 609 |
if ($this->gelfPublisher === null) { |
| 610 |
$this->gelfPublisher = $this->createGelfPublisher(); |
| 611 |
} |
| 612 |
|
| 613 |
return $this->gelfPublisher; |
| 614 |
} |
| 615 |
|
| 616 |
/** |
| 617 |
* @return Gelf_Publisher |
| 618 |
*/ |
| 619 |
protected abstract function createGelfPublisher(); |
| 620 |
} |
| 621 |
|