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