1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* File: XmlRewritesRouter.php |
6
|
|
|
* |
7
|
|
|
* @author Maciej Sławik <[email protected]> |
8
|
|
|
* Github: https://github.com/maciejslawik |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace MSlwk\XmlUrlRewrites\Controller; |
12
|
|
|
|
13
|
|
|
use Magento\Framework\App\Action\Forward; |
14
|
|
|
use Magento\Framework\App\ActionInterface; |
15
|
|
|
use Magento\Framework\App\RequestInterface; |
16
|
|
|
use Magento\Framework\App\RouterInterface; |
17
|
|
|
use Magento\Framework\UrlInterface; |
18
|
|
|
use MSlwk\XmlUrlRewrites\Api\RewritesProviderInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class XmlRewritesRouter |
22
|
|
|
* @package MSlwk\XmlUrlRewrites\Controller |
23
|
|
|
*/ |
24
|
|
|
class XmlRewritesRouter implements RouterInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var \Magento\Framework\App\ActionFactory |
28
|
|
|
*/ |
29
|
|
|
private $actionFactory; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var RewritesProviderInterface |
33
|
|
|
*/ |
34
|
|
|
private $rewritesProvider; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* XmlRewritesRouter constructor. |
38
|
|
|
* @param \Magento\Framework\App\ActionFactory $actionFactory |
39
|
|
|
* @param RewritesProviderInterface $rewritesProvider |
40
|
|
|
*/ |
41
|
|
|
public function __construct( |
42
|
|
|
\Magento\Framework\App\ActionFactory $actionFactory, |
43
|
|
|
RewritesProviderInterface $rewritesProvider |
44
|
|
|
) { |
45
|
|
|
$this->actionFactory = $actionFactory; |
46
|
|
|
$this->rewritesProvider = $rewritesProvider; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param RequestInterface $request |
51
|
|
|
* @return ActionInterface|null |
52
|
|
|
*/ |
53
|
|
|
public function match(RequestInterface $request) |
54
|
|
|
{ |
55
|
|
|
$rewrites = $this->rewritesProvider->getRewrites(); |
56
|
|
|
$path = trim($request->getPathInfo(), '/'); |
57
|
|
|
|
58
|
|
|
if (isset($rewrites[$path])) { |
59
|
|
|
$request->setAlias( |
60
|
|
|
UrlInterface::REWRITE_REQUEST_PATH_ALIAS, |
61
|
|
|
$path |
62
|
|
|
); |
63
|
|
|
$request->setPathInfo($rewrites[$path]); |
64
|
|
|
return $this->actionFactory->create( |
65
|
|
|
Forward::class |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|