vendor/se7enxweb/legacy-bridge/mvc/Kernel.php line 57

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;
  7. use Exception;
  8. use ezpKernel;
  9. use ezpKernelHandler;
  10. use ezxFormToken;
  11. use Psr\Log\LoggerInterface;
  12. use RuntimeException;
  13. /**
  14. * Class wrapping the legacy kernel.
  15. */
  16. class Kernel extends ezpKernel
  17. {
  18. /**
  19. * Legacy root directory.
  20. *
  21. * @var string
  22. */
  23. private $legacyRootDir;
  24. /**
  25. * Original webroot directory.
  26. *
  27. * @var string
  28. */
  29. private $webRootDir;
  30. private $runningCallback = false;
  31. /**
  32. * Directory where kernel was originally running from.
  33. *
  34. * @see self::runCallback()
  35. *
  36. * @var string
  37. */
  38. private $previousRunningDir;
  39. /**
  40. * @var \Psr\Log\LoggerInterface
  41. */
  42. private $logger;
  43. /**
  44. * @param \ezpKernelHandler $kernelHandler
  45. * @param string $legacyRootDir Must be a absolute dir
  46. * @param string $webRootDir Must be a absolute dir
  47. * @param \Psr\Log\LoggerInterface $logger
  48. */
  49. public function __construct(ezpKernelHandler $kernelHandler, $legacyRootDir, $webRootDir, LoggerInterface $logger = null)
  50. {
  51. $this->legacyRootDir = $legacyRootDir;
  52. $this->webRootDir = $webRootDir;
  53. $this->logger = $logger;
  54. $this->enterLegacyRootDir();
  55. parent::__construct($kernelHandler);
  56. $this->leaveLegacyRootDir();
  57. $this->setUseExceptions(true);
  58. }
  59. public static function resetInstance()
  60. {
  61. static::$instance = null;
  62. }
  63. /**
  64. * Changes the current working directory to the legacy root dir.
  65. * Calling this method is mandatory to use legacy kernel since a lot of resources in eZ Publish 4.x relatively defined.
  66. */
  67. public function enterLegacyRootDir()
  68. {
  69. $this->previousRunningDir = getcwd();
  70. if ($this->logger) {
  71. $this->logger->debug("Legacy kernel: Leaving '$this->previousRunningDir' for '$this->legacyRootDir'");
  72. }
  73. chdir($this->legacyRootDir);
  74. }
  75. /**
  76. * Leaves the legacy root dir and switches back to the dir where execution was happening before we entered LegacyRootDir.
  77. */
  78. public function leaveLegacyRootDir()
  79. {
  80. $previousDir = $this->previousRunningDir;
  81. if (!$previousDir) {
  82. if ($this->logger) {
  83. $this->logger->warning(
  84. "Trying to leave legacy root dir without a previously executing dir. Falling back to '$this->webRootDir'"
  85. );
  86. }
  87. $previousDir = $this->webRootDir;
  88. }
  89. $this->previousRunningDir = null;
  90. if ($this->logger) {
  91. $this->logger->debug("Legacy kernel: Leaving '$this->legacyRootDir' for '$previousDir'");
  92. }
  93. chdir($previousDir);
  94. }
  95. /**
  96. * Runs current request through legacy kernel.
  97. *
  98. * @return \ezpKernelResult
  99. */
  100. public function run()
  101. {
  102. $this->enterLegacyRootDir();
  103. $return = parent::run();
  104. $this->leaveLegacyRootDir();
  105. return $return;
  106. }
  107. /**
  108. * Runs a callback function in the legacy kernel environment.
  109. * This is useful to run eZ Publish 4.x code from a non-related context (like eZ Publish 5).
  110. * Will throw a \RuntimeException if trying to run a callback inside a callback.
  111. *
  112. * @param \Closure $callback
  113. * @param bool $postReinitialize Default is true.
  114. * If set to false, the kernel environment will not be reinitialized.
  115. * This can be useful to optimize several calls to the kernel within the same context.
  116. * @param bool|null $formTokenEnable Force ezxFormToken to be enabled or disabled, use system settings when null
  117. *
  118. * @throws \RuntimeException
  119. * @throws \Exception
  120. *
  121. * @return mixed The result of the callback
  122. */
  123. public function runCallback(\Closure $callback, $postReinitialize = true, $formTokenEnable = null)
  124. {
  125. if ($this->runningCallback) {
  126. throw new RuntimeException('Trying to run recursive callback in legacy kernel! Inception!');
  127. }
  128. $this->runningCallback = true;
  129. $this->enterLegacyRootDir();
  130. if ($formTokenEnable !== null && class_exists('ezxFormToken')) {
  131. $formTokenWasEnabled = ezxFormToken::isEnabled();
  132. ezxFormToken::setIsEnabled($formTokenEnable);
  133. }
  134. try {
  135. $return = parent::runCallback($callback, $postReinitialize);
  136. } catch (Exception $e) {
  137. $this->leaveLegacyRootDir();
  138. $this->runningCallback = false;
  139. throw $e;
  140. }
  141. if (isset($formTokenWasEnabled)) {
  142. ezxFormToken::setIsEnabled($formTokenWasEnabled);
  143. }
  144. $this->leaveLegacyRootDir();
  145. $this->runningCallback = false;
  146. return $return;
  147. }
  148. }