Edit File by line
/home/barbar84/www/wp-conte.../plugins/worker/src/MWP/Worker
File: Kernel.php
<?php
[0] Fix | Delete
/*
[1] Fix | Delete
* This file is part of the ManageWP Worker plugin.
[2] Fix | Delete
*
[3] Fix | Delete
* (c) ManageWP LLC <contact@managewp.com>
[4] Fix | Delete
*
[5] Fix | Delete
* For the full copyright and license information, please view the LICENSE
[6] Fix | Delete
* file that was distributed with this source code.
[7] Fix | Delete
*/
[8] Fix | Delete
[9] Fix | Delete
/**
[10] Fix | Delete
* Class MWP_Worker_Kernel
[11] Fix | Delete
*/
[12] Fix | Delete
class MWP_Worker_Kernel
[13] Fix | Delete
{
[14] Fix | Delete
[15] Fix | Delete
/**
[16] Fix | Delete
* @var MWP_ServiceContainer_Interface
[17] Fix | Delete
*/
[18] Fix | Delete
private $container;
[19] Fix | Delete
[20] Fix | Delete
/**
[21] Fix | Delete
* @var Symfony_EventDispatcher_EventDispatcherInterface
[22] Fix | Delete
*/
[23] Fix | Delete
private $dispatcher;
[24] Fix | Delete
[25] Fix | Delete
/**
[26] Fix | Delete
* @var MWP_Worker_RequestStack|null
[27] Fix | Delete
*/
[28] Fix | Delete
private $requestStack;
[29] Fix | Delete
[30] Fix | Delete
public function __construct(MWP_ServiceContainer_Interface $container)
[31] Fix | Delete
{
[32] Fix | Delete
$this->container = $container;
[33] Fix | Delete
$this->dispatcher = $container->getEventDispatcher();
[34] Fix | Delete
$this->requestStack = $container->getRequestStack();
[35] Fix | Delete
// We will need master response callback for fatal error handling.
[36] Fix | Delete
$this->responseCallback = $container->getResponseCallback();
[37] Fix | Delete
}
[38] Fix | Delete
[39] Fix | Delete
/**
[40] Fix | Delete
* @param MWP_Worker_Request $request
[41] Fix | Delete
* @param callable $deferredCallback
[42] Fix | Delete
* @param bool $catch
[43] Fix | Delete
*
[44] Fix | Delete
* @throws Exception
[45] Fix | Delete
* @throws MWP_Worker_Exception
[46] Fix | Delete
*/
[47] Fix | Delete
public function handleRequest(MWP_Worker_Request $request, $deferredCallback, $catch = true)
[48] Fix | Delete
{
[49] Fix | Delete
$request->initialize();
[50] Fix | Delete
$this->requestStack->push($request);
[51] Fix | Delete
$this->responseCallback->set($deferredCallback);
[52] Fix | Delete
$container = $this->getContainer();
[53] Fix | Delete
$actionName = $request->getAction();
[54] Fix | Delete
$params = $request->getParams();
[55] Fix | Delete
$context = $container->getWordPressContext();
[56] Fix | Delete
if (!is_array($params)) {
[57] Fix | Delete
$params = array();
[58] Fix | Delete
}
[59] Fix | Delete
[60] Fix | Delete
if (!$request->isMasterRequest()) {
[61] Fix | Delete
// This is a public request. Allow the plugin to hook onto WordPress.
[62] Fix | Delete
$publicRequestEvent = new MWP_Event_PublicRequest($request);
[63] Fix | Delete
$this->dispatcher->dispatch(MWP_Event_Events::PUBLIC_REQUEST, $publicRequestEvent);
[64] Fix | Delete
if ($publicRequestEvent->hasResponse()) {
[65] Fix | Delete
call_user_func($deferredCallback, null, $publicRequestEvent->getResponse());
[66] Fix | Delete
}
[67] Fix | Delete
[68] Fix | Delete
return;
[69] Fix | Delete
}
[70] Fix | Delete
[71] Fix | Delete
@ini_set('display_errors', false);
[72] Fix | Delete
[73] Fix | Delete
try {
[74] Fix | Delete
// Get action info.
[75] Fix | Delete
$actionRegistry = $container->getActionRegistry();
[76] Fix | Delete
$actionDefinition = $actionRegistry->getDefinition($actionName);
[77] Fix | Delete
$hookName = $actionDefinition->getOption('hook_name');
[78] Fix | Delete
[79] Fix | Delete
// This is a master request. Allow early hooks to verify and do everything required with the request.
[80] Fix | Delete
$masterRequestEvent = new MWP_Event_MasterRequest($request, $params, empty($hookName));
[81] Fix | Delete
$this->dispatcher->dispatch(MWP_Event_Events::MASTER_REQUEST, $masterRequestEvent);
[82] Fix | Delete
if ($masterRequestEvent->hasResponse()) {
[83] Fix | Delete
call_user_func($deferredCallback, null, $masterRequestEvent->getResponse());
[84] Fix | Delete
[85] Fix | Delete
return;
[86] Fix | Delete
}
[87] Fix | Delete
$params = $masterRequestEvent->getParams();
[88] Fix | Delete
[89] Fix | Delete
$callback = $actionDefinition->getCallback();
[90] Fix | Delete
[91] Fix | Delete
// If the callback is an array with two members (['ClassName, 'methodName']) and implements ContainerAware,
[92] Fix | Delete
// inject the container before executing it.
[93] Fix | Delete
if (is_array($callback) && is_string($callback[0])) {
[94] Fix | Delete
$callback[0] = new $callback[0];
[95] Fix | Delete
}
[96] Fix | Delete
if (is_array($callback) && $callback[0] instanceof MWP_ServiceContainer_ContainerAwareInterface) {
[97] Fix | Delete
$callbackObject = $callback[0];
[98] Fix | Delete
/** @var MWP_ServiceContainer_ContainerAwareInterface $callbackObject */
[99] Fix | Delete
$callbackObject->setContainer($container);
[100] Fix | Delete
}
[101] Fix | Delete
[102] Fix | Delete
// Check if the action call should be deferred.
[103] Fix | Delete
if ($hookName !== null && $deferredCallback !== null) {
[104] Fix | Delete
$proxy = new MWP_WordPress_HookProxy(array($this, 'hookResponse'), $request, $callback, $params, $actionDefinition, $deferredCallback);
[105] Fix | Delete
$context->addAction($hookName, $proxy->getCallable(), $actionDefinition->getOption('hook_priority'));
[106] Fix | Delete
[107] Fix | Delete
mwp_logger()->debug('Finished MU context work');
[108] Fix | Delete
[109] Fix | Delete
return;
[110] Fix | Delete
}
[111] Fix | Delete
[112] Fix | Delete
// Allow listeners to modify action parameters.
[113] Fix | Delete
$actionRequestEvent = new MWP_Event_ActionRequest($request, $params, $actionDefinition);
[114] Fix | Delete
$this->dispatcher->dispatch(MWP_Event_Events::ACTION_REQUEST, $actionRequestEvent);
[115] Fix | Delete
$params = $actionRequestEvent->getParams();
[116] Fix | Delete
[117] Fix | Delete
try {
[118] Fix | Delete
$data = call_user_func($callback, $params, $request);
[119] Fix | Delete
} catch (MWP_Worker_ActionResponse $actionResponse) {
[120] Fix | Delete
$data = $actionResponse->getData();
[121] Fix | Delete
}
[122] Fix | Delete
$response = $this->handleResponse($request, $params, $data);
[123] Fix | Delete
call_user_func($deferredCallback, null, $response);
[124] Fix | Delete
} catch (Exception $e) {
[125] Fix | Delete
if (!$catch) {
[126] Fix | Delete
throw $e;
[127] Fix | Delete
}
[128] Fix | Delete
[129] Fix | Delete
$response = $this->handleException($request, $e);
[130] Fix | Delete
call_user_func($deferredCallback, $e, $response);
[131] Fix | Delete
}
[132] Fix | Delete
}
[133] Fix | Delete
[134] Fix | Delete
/**
[135] Fix | Delete
* @param MWP_Worker_Request $request
[136] Fix | Delete
* @param array $params
[137] Fix | Delete
* @param mixed $data
[138] Fix | Delete
*
[139] Fix | Delete
* @return MWP_Http_ResponseInterface
[140] Fix | Delete
*
[141] Fix | Delete
* @throws RuntimeException If the action response doesn't get converted to an HTTP response.
[142] Fix | Delete
*/
[143] Fix | Delete
private function handleResponse(MWP_Worker_Request $request, array $params, $data)
[144] Fix | Delete
{
[145] Fix | Delete
$actionResponseEvent = new MWP_Event_ActionResponse($request, $params, $data);
[146] Fix | Delete
$this->dispatcher->dispatch(MWP_Event_Events::ACTION_RESPONSE, $actionResponseEvent);
[147] Fix | Delete
[148] Fix | Delete
if ($actionResponseEvent->getResponse() === null) {
[149] Fix | Delete
throw new RuntimeException('Action response did not get converted to an HTTP response.');
[150] Fix | Delete
}
[151] Fix | Delete
[152] Fix | Delete
return $actionResponseEvent->getResponse();
[153] Fix | Delete
}
[154] Fix | Delete
[155] Fix | Delete
/**
[156] Fix | Delete
* @param MWP_Worker_Request $request
[157] Fix | Delete
* @param Exception $e
[158] Fix | Delete
*
[159] Fix | Delete
* @return MWP_Http_ResponseInterface|null
[160] Fix | Delete
*/
[161] Fix | Delete
private function handleException(MWP_Worker_Request $request, Exception $e)
[162] Fix | Delete
{
[163] Fix | Delete
$errorEvent = new MWP_Event_ActionException($request, $e);
[164] Fix | Delete
$this->dispatcher->dispatch(MWP_Event_Events::ACTION_EXCEPTION, $errorEvent);
[165] Fix | Delete
[166] Fix | Delete
return $errorEvent->getResponse();
[167] Fix | Delete
}
[168] Fix | Delete
[169] Fix | Delete
/**
[170] Fix | Delete
* Callback for deferred actions. Used when the action is not executed immediately, but after a WordPress action hook.
[171] Fix | Delete
*
[172] Fix | Delete
* @param MWP_Worker_Request $request
[173] Fix | Delete
* @param callable $callback
[174] Fix | Delete
* @param array $params
[175] Fix | Delete
* @param MWP_Action_Definition $actionDefinition
[176] Fix | Delete
* @param callable $deferredCallback
[177] Fix | Delete
*/
[178] Fix | Delete
public function hookResponse(MWP_Worker_Request $request, $callback, array $params, MWP_Action_Definition $actionDefinition, $deferredCallback)
[179] Fix | Delete
{
[180] Fix | Delete
try {
[181] Fix | Delete
// Allow listeners to modify action parameters.
[182] Fix | Delete
$actionRequestEvent = new MWP_Event_ActionRequest($request, $params, $actionDefinition);
[183] Fix | Delete
$this->dispatcher->dispatch(MWP_Event_Events::ACTION_REQUEST, $actionRequestEvent);
[184] Fix | Delete
$params = $actionRequestEvent->getParams();
[185] Fix | Delete
[186] Fix | Delete
try {
[187] Fix | Delete
$data = call_user_func($callback, $params, $request);
[188] Fix | Delete
} catch (MWP_Worker_ActionResponse $actionResponse) {
[189] Fix | Delete
$data = $actionResponse->getData();
[190] Fix | Delete
}
[191] Fix | Delete
$response = $this->handleResponse($request, $params, $data);
[192] Fix | Delete
call_user_func($deferredCallback, null, $response);
[193] Fix | Delete
} catch (Exception $e) {
[194] Fix | Delete
$response = $this->handleException($request, $e);
[195] Fix | Delete
call_user_func($deferredCallback, $e, $response);
[196] Fix | Delete
}
[197] Fix | Delete
}
[198] Fix | Delete
[199] Fix | Delete
/**
[200] Fix | Delete
* Returns the service container.
[201] Fix | Delete
*
[202] Fix | Delete
* @return MWP_ServiceContainer_Interface
[203] Fix | Delete
*/
[204] Fix | Delete
public function getContainer()
[205] Fix | Delete
{
[206] Fix | Delete
if ($this->container === null) {
[207] Fix | Delete
throw new RuntimeException('Kernel is not booted');
[208] Fix | Delete
}
[209] Fix | Delete
[210] Fix | Delete
return $this->container;
[211] Fix | Delete
}
[212] Fix | Delete
}
[213] Fix | Delete
[214] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function