1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Silverback\ApiComponentBundle\Factory\Entity\Route; |
4
|
|
|
|
5
|
|
|
use Cocur\Slugify\SlugifyInterface; |
6
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
7
|
|
|
use Silverback\ApiComponentBundle\Entity\Route\Route; |
8
|
|
|
use Silverback\ApiComponentBundle\Entity\Route\RouteAwareInterface; |
9
|
|
|
use Silverback\ApiComponentBundle\Factory\Entity\AbstractFactory; |
10
|
|
|
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter; |
11
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
12
|
|
|
|
13
|
|
|
class RouteFactory extends AbstractFactory |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var SlugifyInterface |
17
|
|
|
*/ |
18
|
|
|
private $slugify; |
19
|
|
|
|
20
|
7 |
|
public function __construct( |
21
|
|
|
ObjectManager $manager, |
22
|
|
|
ValidatorInterface $validator, |
23
|
|
|
SlugifyInterface $slugify |
24
|
|
|
) |
25
|
|
|
{ |
26
|
7 |
|
$this->slugify = $slugify; |
27
|
7 |
|
parent::__construct($manager, $validator); |
28
|
7 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @inheritdoc |
32
|
|
|
*/ |
33
|
4 |
|
public function create(?array $ops = null): Route |
34
|
|
|
{ |
35
|
4 |
|
$component = new Route(); |
36
|
4 |
|
$this->init($component, $ops); |
37
|
4 |
|
$this->validate($component); |
38
|
4 |
|
return $component; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritdoc |
43
|
|
|
*/ |
44
|
5 |
|
protected static function defaultOps(): array |
45
|
|
|
{ |
46
|
|
|
return [ |
47
|
5 |
|
'name' => null, |
48
|
|
|
'route' => null, |
49
|
|
|
'content' => null, |
50
|
|
|
'redirect' => null |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param RouteAwareInterface $entity |
56
|
|
|
* @param int|null $postfix |
57
|
|
|
* @return Route |
58
|
|
|
*/ |
59
|
3 |
|
public function createFromRouteAwareEntity(RouteAwareInterface $entity, int $postfix = 0): Route |
60
|
|
|
{ |
61
|
3 |
|
$pageRoute = $this->slugify->slugify($entity->getDefaultRoute() ?: ''); |
62
|
3 |
|
$routePrefix = $this->getRoutePrefix($entity); |
63
|
3 |
|
$fullRoute = $routePrefix . $pageRoute; |
64
|
3 |
|
if ($postfix > 0) { |
65
|
|
|
$fullRoute .= '-' . $postfix; |
66
|
|
|
} |
67
|
3 |
|
$existing = $this->manager->getRepository(Route::class)->find($fullRoute); |
68
|
3 |
|
if ($existing) { |
69
|
|
|
return $this->createFromRouteAwareEntity($entity, $postfix + 1); |
70
|
|
|
} |
71
|
3 |
|
$converter = new CamelCaseToSnakeCaseNameConverter(); |
72
|
3 |
|
$route = $this->create( |
73
|
|
|
[ |
74
|
3 |
|
'name' => $converter->normalize(str_replace(' ', '', $entity->getDefaultRouteName())), |
75
|
3 |
|
'route' => $fullRoute, |
76
|
3 |
|
'content' => $entity |
77
|
|
|
] |
78
|
|
|
); |
79
|
3 |
|
$entity->addRoute($route); |
80
|
3 |
|
return $route; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param RouteAwareInterface $entity |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
3 |
|
private function getRoutePrefix(RouteAwareInterface $entity): string |
88
|
|
|
{ |
89
|
3 |
|
$parent = method_exists($entity, 'getParent') ? $entity->getParent() : null; |
90
|
3 |
|
if ($parent && $parent instanceof RouteAwareInterface) { |
91
|
2 |
|
$parentRoute = $this->getParentRoute($parent); |
92
|
2 |
|
return $parentRoute->getRoute() . '/'; |
93
|
|
|
} |
94
|
3 |
|
return '/'; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param RouteAwareInterface $parent |
99
|
|
|
* @return mixed|Route |
100
|
|
|
*/ |
101
|
2 |
|
private function getParentRoute(RouteAwareInterface $parent) |
102
|
|
|
{ |
103
|
2 |
|
$parentRoute = $parent->getRoutes()->first(); |
104
|
2 |
|
if (!$parentRoute) { |
105
|
1 |
|
$parentRoute = $this->createFromRouteAwareEntity($parent); |
106
|
|
|
} |
107
|
2 |
|
return $parentRoute; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|