Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
UrlRewrite.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
17 
21 class UrlRewrite implements ResolverInterface
22 {
26  private $urlFinder;
27 
31  private $storeManager;
32 
36  private $customUrlLocator;
37 
43  public function __construct(
44  UrlFinderInterface $urlFinder,
45  StoreManagerInterface $storeManager,
46  CustomUrlLocatorInterface $customUrlLocator
47  ) {
48  $this->urlFinder = $urlFinder;
49  $this->storeManager = $storeManager;
50  $this->customUrlLocator = $customUrlLocator;
51  }
52 
56  public function resolve(
57  Field $field,
58  $context,
60  array $value = null,
61  array $args = null
62  ) {
63  if (!isset($args['url']) || empty(trim($args['url']))) {
64  throw new GraphQlInputException(__('"url" argument should be specified and not empty'));
65  }
66 
67  $result = null;
68  $url = $args['url'];
69  if (substr($url, 0, 1) === '/' && $url !== '/') {
70  $url = ltrim($url, '/');
71  }
72  $customUrl = $this->customUrlLocator->locateUrl($url);
73  $url = $customUrl ?: $url;
74  $urlRewrite = $this->findCanonicalUrl($url);
75  if ($urlRewrite) {
76  $result = [
77  'id' => $urlRewrite->getEntityId(),
78  'canonical_url' => $urlRewrite->getTargetPath(),
79  'type' => $this->sanitizeType($urlRewrite->getEntityType())
80  ];
81  }
82  return $result;
83  }
84 
91  private function findCanonicalUrl(string $requestPath) : ?\Magento\UrlRewrite\Service\V1\Data\UrlRewrite
92  {
93  $urlRewrite = $this->findUrlFromRequestPath($requestPath);
94  if ($urlRewrite && $urlRewrite->getRedirectType() > 0) {
95  while ($urlRewrite && $urlRewrite->getRedirectType() > 0) {
96  $urlRewrite = $this->findUrlFromRequestPath($urlRewrite->getTargetPath());
97  }
98  }
99  if (!$urlRewrite) {
100  $urlRewrite = $this->findUrlFromTargetPath($requestPath);
101  }
102 
103  return $urlRewrite;
104  }
105 
112  private function findUrlFromRequestPath(string $requestPath) : ?\Magento\UrlRewrite\Service\V1\Data\UrlRewrite
113  {
114  return $this->urlFinder->findOneByData(
115  [
116  'request_path' => $requestPath,
117  'store_id' => $this->storeManager->getStore()->getId()
118  ]
119  );
120  }
121 
128  private function findUrlFromTargetPath(string $targetPath) : ?\Magento\UrlRewrite\Service\V1\Data\UrlRewrite
129  {
130  return $this->urlFinder->findOneByData(
131  [
132  'target_path' => $targetPath,
133  'store_id' => $this->storeManager->getStore()->getId()
134  ]
135  );
136  }
137 
144  private function sanitizeType(string $type) : string
145  {
146  return strtoupper(str_replace('-', '_', $type));
147  }
148 }
$storeManager
__()
Definition: __.php:13
$type
Definition: item.phtml:13
__construct(UrlFinderInterface $urlFinder, StoreManagerInterface $storeManager, CustomUrlLocatorInterface $customUrlLocator)
Definition: UrlRewrite.php:43
$value
Definition: gender.phtml:16
resolve(Field $field, $context, ResolveInfo $info, array $value=null, array $args=null)
Definition: UrlRewrite.php:56
foreach( $_productCollection as $_product)() ?>" class $info
Definition: listing.phtml:52