vendor/se7enxweb/legacy-bridge/bundle/LegacyMapper/SiteAccess.php line 42

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\Bundle\EzPublishLegacyBundle\LegacyMapper;
  7. use eZ\Publish\Core\MVC\Legacy\LegacyEvents;
  8. use eZ\Publish\Core\MVC\Legacy\Event\PreBuildKernelWebHandlerEvent;
  9. use eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher\CompoundInterface;
  10. use eZSiteAccess;
  11. use Symfony\Component\DependencyInjection\ContainerAwareTrait;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. /**
  14. * Maps the SiteAccess object to the legacy parameters.
  15. */
  16. class SiteAccess implements EventSubscriberInterface
  17. {
  18. use ContainerAwareTrait;
  19. protected $options = [];
  20. public function __construct(array $options = [])
  21. {
  22. $this->options = $options;
  23. }
  24. public static function getSubscribedEvents()
  25. {
  26. return [
  27. LegacyEvents::PRE_BUILD_LEGACY_KERNEL_WEB => ['onBuildKernelWebHandler', 128],
  28. ];
  29. }
  30. /**
  31. * Maps matched siteaccess to the legacy parameters.
  32. *
  33. * @param \eZ\Publish\Core\MVC\Legacy\Event\PreBuildKernelWebHandlerEvent $event
  34. */
  35. public function onBuildKernelWebHandler(PreBuildKernelWebHandlerEvent $event)
  36. {
  37. $siteAccess = $this->container->get('ezpublish.siteaccess');
  38. $request = $event->getRequest();
  39. $uriPart = [];
  40. // Convert matching type
  41. switch ($siteAccess->matchingType) {
  42. case 'default':
  43. $legacyAccessType = eZSiteAccess::TYPE_DEFAULT;
  44. break;
  45. case 'env':
  46. $legacyAccessType = eZSiteAccess::TYPE_SERVER_VAR;
  47. break;
  48. case 'uri:map':
  49. case 'uri:element':
  50. case 'uri:text':
  51. case 'uri:regexp':
  52. $legacyAccessType = eZSiteAccess::TYPE_URI;
  53. break;
  54. case 'host:map':
  55. case 'host:element':
  56. case 'host:text':
  57. case 'host:regexp':
  58. $legacyAccessType = eZSiteAccess::TYPE_HTTP_HOST;
  59. break;
  60. case 'port':
  61. $legacyAccessType = eZSiteAccess::TYPE_PORT;
  62. break;
  63. default:
  64. $legacyAccessType = eZSiteAccess::TYPE_CUSTOM;
  65. }
  66. // uri_part
  67. $pathinfo = str_replace($request->attributes->get('viewParametersString'), '', rawurldecode($request->getPathInfo()));
  68. $fragmentPathInfo = implode('/', $this->getFragmentPathItems());
  69. if ($fragmentPathInfo !== '_fragment') {
  70. $semanticPathinfo = $fragmentPathInfo;
  71. } else {
  72. $semanticPathinfo = $request->attributes->get('semanticPathinfo', $pathinfo);
  73. }
  74. $pos = mb_strrpos($pathinfo, $semanticPathinfo);
  75. if ($legacyAccessType !== eZSiteAccess::TYPE_DEFAULT && $pathinfo != $semanticPathinfo && $pos !== false) {
  76. if ($pos === 0) {
  77. $pos = mb_strlen($pathinfo) + 1;
  78. }
  79. $uriPart = mb_substr($pathinfo, 1, $pos - 1);
  80. $uriPart = explode('/', $uriPart);
  81. }
  82. // Handle host_uri match
  83. if ($siteAccess->matcher instanceof CompoundInterface) {
  84. $subMatchers = $siteAccess->matcher->getSubMatchers();
  85. if (!$subMatchers) {
  86. throw new \RuntimeException('Compound matcher used but not submatchers found.');
  87. }
  88. if (\count($subMatchers) == 2 && isset($subMatchers['Map\Host']) && isset($subMatchers['Map\URI'])) {
  89. $legacyAccessType = eZSiteAccess::TYPE_HTTP_HOST_URI;
  90. $uriPart = [$subMatchers['Map\URI']->getMapKey()];
  91. }
  92. }
  93. $event->getParameters()->set(
  94. 'siteaccess',
  95. [
  96. 'name' => $siteAccess->name,
  97. 'type' => $legacyAccessType,
  98. 'uri_part' => $uriPart,
  99. ]
  100. );
  101. }
  102. /**
  103. * Returns an array with all the components of the fragment_path option.
  104. *
  105. * @return array
  106. */
  107. protected function getFragmentPathItems()
  108. {
  109. if (isset($this->options['fragment_path'])) {
  110. return explode('/', trim($this->options['fragment_path'], '/'));
  111. }
  112. return ['_fragment'];
  113. }
  114. }