1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Shopware\Core\Content\Seo; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
6
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\Dbal\QueryBuilder; |
7
|
|
|
use Shopware\Core\Framework\Log\Package; |
8
|
|
|
use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException; |
9
|
|
|
use Shopware\Core\Framework\Uuid\Uuid; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @phpstan-import-type ResolvedSeoUrl from AbstractSeoResolver |
13
|
|
|
*/ |
14
|
|
|
#[Package('sales-channel')] |
15
|
|
|
class SeoResolver extends AbstractSeoResolver |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @internal |
19
|
|
|
*/ |
20
|
|
|
public function __construct(private readonly Connection $connection) |
21
|
|
|
{ |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function getDecorated(): AbstractSeoResolver |
25
|
|
|
{ |
26
|
|
|
throw new DecorationPatternException(self::class); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return ResolvedSeoUrl |
|
|
|
|
31
|
|
|
*/ |
32
|
|
|
public function resolve(string $languageId, string $salesChannelId, string $pathInfo): array |
33
|
|
|
{ |
34
|
|
|
$seoPathInfo = ltrim($pathInfo, '/'); |
35
|
|
|
|
36
|
|
|
$query = (new QueryBuilder($this->connection)) |
37
|
|
|
->select('id', 'path_info pathInfo', 'is_canonical isCanonical', 'sales_channel_id salesChannelId') |
38
|
|
|
->from('seo_url') |
39
|
|
|
->where('language_id = :language_id') |
40
|
|
|
->andWhere('(sales_channel_id = :sales_channel_id OR sales_channel_id IS NULL)') |
41
|
|
|
->andWhere('seo_path_info = :seoPath') |
42
|
|
|
->setParameter('language_id', Uuid::fromHexToBytes($languageId)) |
43
|
|
|
->setParameter('sales_channel_id', Uuid::fromHexToBytes($salesChannelId)) |
44
|
|
|
->setParameter('seoPath', $seoPathInfo); |
45
|
|
|
|
46
|
|
|
$query->setTitle('seo-url::resolve'); |
47
|
|
|
|
48
|
|
|
$seoPaths = $query->executeQuery()->fetchAllAssociative(); |
49
|
|
|
|
50
|
|
|
// sort seoPaths by filled salesChannelId, save file sort on SQL server |
51
|
|
|
usort($seoPaths, static function ($a, $b) { |
52
|
|
|
if ($a['salesChannelId'] === null) { |
53
|
|
|
return 1; |
54
|
|
|
} |
55
|
|
|
if ($b['salesChannelId'] === null) { |
56
|
|
|
return -1; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return 0; |
60
|
|
|
}); |
61
|
|
|
|
62
|
|
|
$seoPath = $seoPaths[0] ?? ['pathInfo' => $seoPathInfo, 'isCanonical' => false]; |
63
|
|
|
|
64
|
|
|
if (!$seoPath['isCanonical']) { |
65
|
|
|
$query = $this->connection->createQueryBuilder() |
66
|
|
|
->select('path_info pathInfo', 'seo_path_info seoPathInfo') |
67
|
|
|
->from('seo_url') |
68
|
|
|
->where('language_id = :language_id') |
69
|
|
|
->andWhere('sales_channel_id = :sales_channel_id') |
70
|
|
|
->andWhere('path_info = :pathInfo') |
71
|
|
|
->andWhere('is_canonical = 1') |
72
|
|
|
->setMaxResults(1) |
73
|
|
|
->setParameter('language_id', Uuid::fromHexToBytes($languageId)) |
74
|
|
|
->setParameter('sales_channel_id', Uuid::fromHexToBytes($salesChannelId)) |
75
|
|
|
->setParameter('pathInfo', '/' . ltrim((string) $seoPath['pathInfo'], '/')); |
76
|
|
|
|
77
|
|
|
// we only have an id when the hit seo url was not a canonical url, save the one filter condition |
78
|
|
|
if (isset($seoPath['id'])) { |
79
|
|
|
$query->andWhere('id != :id') |
80
|
|
|
->setParameter('id', $seoPath['id']); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$canonical = $query->executeQuery()->fetchAssociative(); |
84
|
|
|
if ($canonical) { |
85
|
|
|
$seoPath['canonicalPathInfo'] = '/' . ltrim((string) $canonical['seoPathInfo'], '/'); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$seoPath['pathInfo'] = '/' . ltrim((string) $seoPath['pathInfo'], '/'); |
90
|
|
|
|
91
|
|
|
return $seoPath; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths