1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Components Bundle Project |
5
|
|
|
* |
6
|
|
|
* (c) Daniel West <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentsBundle\Helper\Route; |
15
|
|
|
|
16
|
|
|
use Cocur\Slugify\SlugifyInterface; |
17
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
18
|
|
|
use Silverback\ApiComponentsBundle\Entity\Core\RoutableInterface; |
19
|
|
|
use Silverback\ApiComponentsBundle\Entity\Core\Route; |
20
|
|
|
use Silverback\ApiComponentsBundle\Exception\InvalidArgumentException; |
21
|
|
|
use Silverback\ApiComponentsBundle\Helper\Timestamped\TimestampedDataPersister; |
22
|
|
|
use Silverback\ApiComponentsBundle\Repository\Core\RouteRepository; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author Daniel West <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class RouteGenerator implements RouteGeneratorInterface |
28
|
|
|
{ |
29
|
|
|
private SlugifyInterface $slugify; |
30
|
|
|
private ManagerRegistry $registry; |
31
|
|
|
private TimestampedDataPersister $timestampedDataPersister; |
32
|
|
|
private RouteRepository $routeRepository; |
33
|
|
|
|
34
|
|
|
public function __construct(SlugifyInterface $slugify, ManagerRegistry $registry, TimestampedDataPersister $timestampedDataPersister, RouteRepository $routeRepository) |
35
|
|
|
{ |
36
|
|
|
$this->slugify = $slugify; |
37
|
|
|
$this->registry = $registry; |
38
|
|
|
$this->timestampedDataPersister = $timestampedDataPersister; |
39
|
|
|
$this->routeRepository = $routeRepository; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function create(RoutableInterface $object, ?Route $route = null): Route |
43
|
|
|
{ |
44
|
|
|
$entityManager = $this->registry->getManagerForClass($className = \get_class($object)); |
45
|
|
|
if (!$entityManager) { |
46
|
|
|
throw new InvalidArgumentException(sprintf('Could not find entity manager for %s', $className)); |
47
|
|
|
} |
48
|
|
|
$uow = $entityManager->getUnitOfWork(); |
|
|
|
|
49
|
|
|
/** @var RoutableInterface $originalPage */ |
50
|
|
|
$originalPage = $uow->getOriginalEntityData($object); |
51
|
|
|
$existingRoute = $originalPage['route'] ?? null; |
52
|
|
|
|
53
|
|
|
$isNew = !((bool) $route); |
54
|
|
|
$route = $route ?? new Route(); |
55
|
|
|
|
56
|
|
|
$this->timestampedDataPersister->persistTimestampedFields($route, $isNew); |
57
|
|
|
$titleSlug = $this->slugify->slugify($object->getTitle()); |
58
|
|
|
$name = $titleSlug; |
59
|
|
|
|
60
|
|
|
$path = '/' . ltrim($titleSlug, '/'); |
61
|
|
|
|
62
|
|
|
if ($parentRoute = $object->getParentRoute()) { |
63
|
|
|
$path = '/' . ltrim($parentRoute->getPath(), '/') . $path; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$conflicts = $this->routeRepository->findConflicts($name, $path); |
67
|
|
|
|
68
|
|
|
$baseName = $name; |
69
|
|
|
$basePath = $path; |
70
|
|
|
$conflictCounter = 0; |
71
|
|
|
while ($this->conflictExists($name, $path, $conflicts)) { |
72
|
|
|
++$conflictCounter; |
73
|
|
|
$name = sprintf('%s-%d', $baseName, $conflictCounter); |
74
|
|
|
$path = sprintf('%s-%d', $basePath, $conflictCounter); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$route |
78
|
|
|
->setName($name) |
79
|
|
|
->setPath($path); |
80
|
|
|
$object->setRoute($route); |
81
|
|
|
|
82
|
|
|
if ($existingRoute) { |
83
|
|
|
$existingRoute->setRedirect($route); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $route; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param Route[] $conflicts |
91
|
|
|
*/ |
92
|
|
|
private function conflictExists(string $name, string $path, array $conflicts): bool |
93
|
|
|
{ |
94
|
|
|
foreach ($conflicts as $conflict) { |
95
|
|
|
if ($conflict->getPath() === $path) { |
96
|
|
|
return true; |
97
|
|
|
} |
98
|
|
|
if ($conflict->getName() === $name) { |
99
|
|
|
return true; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return false; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|