1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Superdesk Web Publisher Content Bundle. |
5
|
|
|
* |
6
|
|
|
* Copyright 2016 Sourcefabric z.ú. and contributors. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please see the |
9
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @copyright 2016 Sourcefabric z.ú |
12
|
|
|
* @license http://www.superdesk.org/license |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace SWP\Bundle\ContentBundle\Provider\ODM\PHPCR; |
16
|
|
|
|
17
|
|
|
use SWP\Bundle\ContentBundle\Model\ArticleInterface; |
18
|
|
|
use SWP\Bundle\ContentBundle\Provider\RouteProviderInterface; |
19
|
|
|
use SWP\Component\MultiTenancy\PathBuilder\TenantAwarePathBuilderInterface; |
20
|
|
|
use SWP\Component\Storage\Repository\RepositoryInterface; |
21
|
|
|
use Symfony\Component\Routing\Exception\RouteNotFoundException; |
22
|
|
|
|
23
|
|
|
class RouteProvider implements RouteProviderInterface |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var RepositoryInterface |
27
|
|
|
*/ |
28
|
|
|
private $routeRepository; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var TenantAwarePathBuilderInterface |
32
|
|
|
*/ |
33
|
|
|
private $pathBuilder; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
private $basePaths; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $defaultPath; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* RouteProvider constructor. |
47
|
|
|
* |
48
|
|
|
* @param RepositoryInterface $routeRepository Route repository |
49
|
|
|
* @param TenantAwarePathBuilderInterface $pathBuilder Path builder |
50
|
|
|
* @param array $basePaths Tenant's path under which all other routes are stored |
51
|
|
|
* @param string $defaultPath Default path under which the articles are stored |
52
|
|
|
*/ |
53
|
|
|
public function __construct( |
54
|
|
|
RepositoryInterface $routeRepository, |
55
|
|
|
TenantAwarePathBuilderInterface $pathBuilder, |
56
|
|
|
array $basePaths, |
57
|
|
|
$defaultPath |
58
|
|
|
) { |
59
|
|
|
$this->routeRepository = $routeRepository; |
60
|
|
|
$this->pathBuilder = $pathBuilder; |
61
|
|
|
$this->basePaths = $basePaths; |
62
|
|
|
$this->defaultPath = $defaultPath; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
public function getBaseRoute() |
69
|
|
|
{ |
70
|
|
|
return $this->routeRepository->find($this->pathBuilder->build($this->basePaths[0])); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
|
public function getOneById($id) |
77
|
|
|
{ |
78
|
|
|
$basePath = $this->pathBuilder->build($this->basePaths[0]); |
79
|
|
|
$id = ltrim(str_replace($basePath, '', $id), '/'); |
80
|
|
|
|
81
|
|
|
if ('' === $id) { |
82
|
|
|
return $this->getBaseRoute(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $this->routeRepository->find($this->pathBuilder->build($this->basePaths[0].'/'.$id)); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
|
|
public function getRouteForArticle(ArticleInterface $article) |
92
|
|
|
{ |
93
|
|
|
$routeId = $this->pathBuilder->build($this->basePaths[0].'/'.$this->defaultPath); |
94
|
|
|
$route = $this->routeRepository->find($routeId); |
95
|
|
|
|
96
|
|
|
if (null === $route) { |
97
|
|
|
throw new RouteNotFoundException(sprintf('No route found: %s', $routeId)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $route; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|