vendor/se7enxweb/legacy-bridge/mvc/Kernel/Loader.php line 76

Open in your IDE?
  1. <?php
  2. /**
  3. * @copyright Copyright (C) eZ Systems AS. All rights reserved.
  4. * @license For full copyright and license information view LICENSE file distributed with this source code.
  5. */
  6. namespace eZ\Publish\Core\MVC\Legacy\Kernel;
  7. use eZ\Bundle\EzPublishLegacyBundle\Rest\ResponseWriter;
  8. use eZ\Publish\Core\MVC\Legacy\Event\PostBuildKernelEvent;
  9. use eZ\Publish\Core\MVC\Legacy\Event\PreResetLegacyKernelEvent;
  10. use eZ\Publish\Core\MVC\Legacy\Kernel as LegacyKernel;
  11. use eZ\Publish\Core\MVC\Legacy\LegacyEvents;
  12. use eZ\Publish\Core\MVC\Legacy\Event\PreBuildKernelWebHandlerEvent;
  13. use eZ\Publish\Core\MVC\Legacy\Event\PreBuildKernelEvent;
  14. use ezpKernelHandler;
  15. use ezpKernelRest;
  16. use ezpKernelTreeMenu;
  17. use Symfony\Component\DependencyInjection\ContainerAwareTrait;
  18. use Psr\Log\LoggerInterface;
  19. use Symfony\Component\HttpFoundation\ParameterBag;
  20. use Symfony\Component\HttpFoundation\RequestStack;
  21. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  22. /**
  23. * Legacy kernel loader.
  24. */
  25. class Loader
  26. {
  27. use ContainerAwareTrait;
  28. /**
  29. * @var string Absolute path to the legacy root directory (eZPublish 4 install dir)
  30. */
  31. protected $legacyRootDir;
  32. /**
  33. * @var string Absolute path to the new webroot directory (web/)
  34. */
  35. protected $webrootDir;
  36. /**
  37. * @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface
  38. */
  39. protected $eventDispatcher;
  40. /**
  41. * @var URIHelper
  42. */
  43. protected $uriHelper;
  44. /**
  45. * @var \Psr\Log\LoggerInterface
  46. */
  47. protected $logger;
  48. /**
  49. * @var bool
  50. */
  51. private $buildEventsEnabled = true;
  52. /** @var ezpKernelHandler */
  53. private $webHandler;
  54. /** @var ezpKernelHandler */
  55. private $cliHandler;
  56. /** @var ezpKernelHandler */
  57. private $restHandler;
  58. /**
  59. * @var \Symfony\Component\HttpFoundation\RequestStack
  60. */
  61. private $requestStack;
  62. public function __construct($legacyRootDir, $webrootDir, EventDispatcherInterface $eventDispatcher, URIHelper $uriHelper, LoggerInterface $logger = null)
  63. {
  64. $this->legacyRootDir = $legacyRootDir;
  65. $this->webrootDir = $webrootDir;
  66. $this->eventDispatcher = $eventDispatcher;
  67. $this->uriHelper = $uriHelper;
  68. $this->logger = $logger;
  69. }
  70. /**
  71. * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
  72. */
  73. public function setRequestStack(RequestStack $requestStack)
  74. {
  75. $this->requestStack = $requestStack;
  76. }
  77. /**
  78. * @param bool $enabled
  79. */
  80. public function setBuildEventsEnabled($enabled = true)
  81. {
  82. $this->buildEventsEnabled = (bool)$enabled;
  83. }
  84. /**
  85. * @return bool
  86. */
  87. public function getBuildEventsEnabled()
  88. {
  89. return $this->buildEventsEnabled;
  90. }
  91. /**
  92. * Builds up the legacy kernel and encapsulates it inside a closure, allowing lazy loading.
  93. *
  94. * @param \ezpKernelHandler|\Closure A kernel handler instance or a closure returning a kernel handler instance
  95. *
  96. * @return \Closure
  97. */
  98. public function buildLegacyKernel($legacyKernelHandler)
  99. {
  100. $legacyRootDir = $this->legacyRootDir;
  101. $webrootDir = $this->webrootDir;
  102. $eventDispatcher = $this->eventDispatcher;
  103. $logger = $this->logger;
  104. $that = $this;
  105. return static function () use ($legacyKernelHandler, $legacyRootDir, $webrootDir, $eventDispatcher, $logger, $that) {
  106. if (LegacyKernel::hasInstance()) {
  107. return LegacyKernel::instance();
  108. }
  109. if ($legacyKernelHandler instanceof \Closure) {
  110. $legacyKernelHandler = $legacyKernelHandler();
  111. }
  112. $legacyKernel = new LegacyKernel($legacyKernelHandler, $legacyRootDir, $webrootDir, $logger);
  113. if ($that->getBuildEventsEnabled()) {
  114. $eventDispatcher->dispatch(
  115. new PostBuildKernelEvent($legacyKernel, $legacyKernelHandler),
  116. LegacyEvents::POST_BUILD_LEGACY_KERNEL
  117. );
  118. }
  119. return $legacyKernel;
  120. };
  121. }
  122. /**
  123. * Builds the default legacy kernel handler and selects CLI when no HTTP request exists.
  124. *
  125. * @return \Closure
  126. */
  127. public function buildLegacyKernelHandler()
  128. {
  129. $container = $this->container;
  130. $requestStack = $this->requestStack;
  131. return static function () use ($container, $requestStack) {
  132. if (null === $requestStack->getCurrentRequest()) {
  133. $legacyHandler = $container->get('ezpublish_legacy.kernel_handler.cli');
  134. return $legacyHandler instanceof \Closure ? $legacyHandler() : $legacyHandler;
  135. }
  136. $legacyHandler = $container->get('ezpublish_legacy.kernel_handler.web');
  137. return $legacyHandler instanceof \Closure ? $legacyHandler() : $legacyHandler;
  138. };
  139. }
  140. /**
  141. * Builds up the legacy kernel web handler and encapsulates it inside a closure, allowing lazy loading.
  142. *
  143. * @param string $webHandlerClass The legacy kernel handler class to use
  144. * @param array $defaultLegacyOptions Hash of options to pass to the legacy kernel handler
  145. *
  146. * @return \Closure
  147. */
  148. public function buildLegacyKernelHandlerWeb($webHandlerClass, array $defaultLegacyOptions = [])
  149. {
  150. $legacyRootDir = $this->legacyRootDir;
  151. $webrootDir = $this->webrootDir;
  152. $uriHelper = $this->uriHelper;
  153. $eventDispatcher = $this->eventDispatcher;
  154. $container = $this->container;
  155. $that = $this;
  156. return function () use ($legacyRootDir, $webrootDir, $container, $defaultLegacyOptions, $webHandlerClass, $uriHelper, $eventDispatcher, $that) {
  157. if (!$that->getWebHandler()) {
  158. chdir($legacyRootDir);
  159. $legacyParameters = new ParameterBag($defaultLegacyOptions);
  160. $legacyParameters->set('service-container', $container);
  161. $request = $this->requestStack->getCurrentRequest();
  162. if ($that->getBuildEventsEnabled()) {
  163. // PRE_BUILD_LEGACY_KERNEL for non request related stuff
  164. $eventDispatcher->dispatch(new PreBuildKernelEvent($legacyParameters), LegacyEvents::PRE_BUILD_LEGACY_KERNEL);
  165. // Pure web stuff
  166. $eventDispatcher->dispatch(
  167. new PreBuildKernelWebHandlerEvent($legacyParameters, $request),
  168. LegacyEvents::PRE_BUILD_LEGACY_KERNEL_WEB
  169. );
  170. }
  171. $interfaces = class_implements($webHandlerClass);
  172. if (!isset($interfaces['ezpKernelHandler'])) {
  173. throw new \InvalidArgumentException('A legacy kernel handler must be an instance of ezpKernelHandler.');
  174. }
  175. $that->setWebHandler(new $webHandlerClass($legacyParameters->all()));
  176. // Fix up legacy URI for global use cases (i.e. using runCallback()).
  177. $uriHelper->updateLegacyURI($request);
  178. chdir($webrootDir);
  179. }
  180. return $that->getWebHandler();
  181. };
  182. }
  183. /**
  184. * @param $handler
  185. */
  186. public function setWebHandler(ezpKernelHandler $handler)
  187. {
  188. $this->webHandler = $handler;
  189. }
  190. /**
  191. * @return ezpKernelHandler
  192. */
  193. public function getWebHandler()
  194. {
  195. return $this->webHandler;
  196. }
  197. /**
  198. * Builds legacy kernel handler CLI.
  199. *
  200. * @param \eZ\Publish\Core\MVC\Symfony\SiteAccess|null $siteAccess
  201. *
  202. * @return \Closure
  203. */
  204. public function buildLegacyKernelHandlerCLI($siteAccess = null)
  205. {
  206. $legacyRootDir = $this->legacyRootDir;
  207. $eventDispatcher = $this->eventDispatcher;
  208. $container = $this->container;
  209. $that = $this;
  210. return static function () use ($legacyRootDir, $container, $eventDispatcher, $that, $siteAccess) {
  211. if (!$that->getCLIHandler()) {
  212. $currentDir = getcwd();
  213. chdir($legacyRootDir);
  214. $legacyParameters = new ParameterBag($container->getParameter('ezpublish_legacy.kernel_handler.cli.options'));
  215. if ($that->getBuildEventsEnabled()) {
  216. $eventDispatcher->dispatch(new PreBuildKernelEvent($legacyParameters), LegacyEvents::PRE_BUILD_LEGACY_KERNEL);
  217. }
  218. $that->setCLIHandler(
  219. new CLIHandler($legacyParameters->all(), $siteAccess, $container)
  220. );
  221. chdir($currentDir);
  222. }
  223. return $that->getCLIHandler();
  224. };
  225. }
  226. /**
  227. * @return ezpKernelhandler
  228. */
  229. public function getCLIHandler()
  230. {
  231. return $this->cliHandler;
  232. }
  233. public function setCLIHandler(ezpKernelHandler $kernelHandler)
  234. {
  235. $this->cliHandler = $kernelHandler;
  236. }
  237. /**
  238. * Builds the legacy kernel handler for the tree menu in admin interface.
  239. *
  240. * @return \Closure a closure returning an \ezpKernelTreeMenu instance
  241. */
  242. public function buildLegacyKernelHandlerTreeMenu()
  243. {
  244. return $this->buildLegacyKernelHandlerWeb(
  245. ezpKernelTreeMenu::class,
  246. [
  247. 'use-cache-headers' => false,
  248. 'use-exceptions' => true,
  249. ]
  250. );
  251. }
  252. /**
  253. * Builds the legacy kernel handler for the tree menu in admin interface.
  254. *
  255. * @return \Closure a closure returning an \ezpKernelTreeMenu instance
  256. */
  257. public function buildLegacyKernelHandlerRest($mvcConfiguration)
  258. {
  259. $legacyRootDir = $this->legacyRootDir;
  260. $webrootDir = $this->webrootDir;
  261. $uriHelper = $this->uriHelper;
  262. $eventDispatcher = $this->eventDispatcher;
  263. $container = $this->container;
  264. $that = $this;
  265. return function () use ($legacyRootDir, $webrootDir, $container, $uriHelper, $eventDispatcher, $that) {
  266. if (!$that->getRestHandler()) {
  267. chdir($legacyRootDir);
  268. $legacyParameters = new ParameterBag();
  269. $request = $this->requestStack->getCurrentRequest();
  270. if ($that->getBuildEventsEnabled()) {
  271. // PRE_BUILD_LEGACY_KERNEL for non request related stuff
  272. $eventDispatcher->dispatch(new PreBuildKernelEvent($legacyParameters), LegacyEvents::PRE_BUILD_LEGACY_KERNEL);
  273. // Pure web stuff
  274. $eventDispatcher->dispatch(
  275. new PreBuildKernelWebHandlerEvent($legacyParameters, $request),
  276. LegacyEvents::PRE_BUILD_LEGACY_KERNEL_WEB
  277. );
  278. }
  279. $that->setRestHandler(new ezpKernelRest($legacyParameters->all(), ResponseWriter::class));
  280. chdir($webrootDir);
  281. }
  282. return $that->getRestHandler();
  283. };
  284. }
  285. /**
  286. * @return ezpKernelhandler
  287. */
  288. public function getRestHandler()
  289. {
  290. return $this->restHandler;
  291. }
  292. public function setRestHandler(ezpKernelHandler $handler)
  293. {
  294. $this->restHandler = $handler;
  295. }
  296. /**
  297. * Resets the legacy kernel instances from the container.
  298. */
  299. public function resetKernel()
  300. {
  301. // Reset the kernel only if it has been initialized.
  302. if (LegacyKernel::hasInstance()) {
  303. /** @var \Closure $kernelClosure */
  304. $kernelClosure = $this->container->get('ezpublish_legacy.kernel');
  305. $this->eventDispatcher->dispatch(
  306. new PreResetLegacyKernelEvent($kernelClosure()),
  307. LegacyEvents::PRE_RESET_LEGACY_KERNEL
  308. );
  309. }
  310. LegacyKernel::resetInstance();
  311. $this->webHandler = null;
  312. $this->cliHandler = null;
  313. $this->restHandler = null;
  314. }
  315. }