1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* File containing the ExpressionRouterRootResourceBuilder class. |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
8
|
|
|
* |
9
|
|
|
* @version //autogentag// |
10
|
|
|
*/ |
11
|
|
|
namespace eZ\Publish\Core\REST\Server\Service; |
12
|
|
|
|
13
|
|
|
use eZ\Publish\Core\REST\Common\Values; |
14
|
|
|
use eZ\Publish\Core\REST\Common\Values\Root; |
15
|
|
|
use Symfony\Component\ExpressionLanguage\ExpressionLanguage; |
16
|
|
|
use Symfony\Component\Routing\RouterInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class ExpressionRouterRootResourceBuilder. |
20
|
|
|
* |
21
|
|
|
* This class builds a Root from an array building the href's using ExpressionLanguage |
22
|
|
|
* to build href's from the router or templateRouter. |
23
|
|
|
* |
24
|
|
|
* Example $resourceConfig structure: |
25
|
|
|
* |
26
|
|
|
* array( |
27
|
|
|
* 'content' => array( |
28
|
|
|
* 'mediaType' => '', |
29
|
|
|
* 'href' => 'router.generate("ezpublish_rest_listContentTypes")', |
30
|
|
|
* ), |
31
|
|
|
* 'usersByRoleId' => array( |
32
|
|
|
* 'mediaType' => 'UserRefList', |
33
|
|
|
* 'href' => 'templateRouter.generate("ezpublish_rest_loadUsers", {roleId: "{roleId}"})', |
34
|
|
|
* ), |
35
|
|
|
* ) |
36
|
|
|
*/ |
37
|
|
|
class ExpressionRouterRootResourceBuilder implements RootResourceBuilderInterface |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* @var \Symfony\Component\Routing\RouterInterface |
41
|
|
|
*/ |
42
|
|
|
protected $router; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var \Symfony\Component\Routing\RouterInterface |
46
|
|
|
*/ |
47
|
|
|
protected $templateRouter; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
protected $resourceConfig; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Creates a new resource builder. |
56
|
|
|
* |
57
|
|
|
* @param \Symfony\Component\Routing\RouterInterface $router |
58
|
|
|
* @param \Symfony\Component\Routing\RouterInterface $templateRouter |
59
|
|
|
* @param array $resourceConfig |
60
|
|
|
*/ |
61
|
|
|
public function __construct(RouterInterface $router, RouterInterface $templateRouter, array $resourceConfig) |
62
|
|
|
{ |
63
|
|
|
$this->router = $router; |
64
|
|
|
$this->templateRouter = $templateRouter; |
65
|
|
|
$this->resourceConfig = $resourceConfig; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Build root resource. |
70
|
|
|
* |
71
|
|
|
* @return array|\eZ\Publish\Core\REST\Common\Values\Root |
72
|
|
|
*/ |
73
|
|
|
public function buildRootResource() |
74
|
|
|
{ |
75
|
|
|
$language = new ExpressionLanguage(); |
76
|
|
|
|
77
|
|
|
$resources = array(); |
78
|
|
|
foreach ($this->resourceConfig as $name => $resource) { |
79
|
|
|
$resources[] = new Values\Resource( |
80
|
|
|
$name, |
81
|
|
|
$resource['mediaType'], |
82
|
|
|
$language->evaluate($resource['href'], [ |
83
|
|
|
'router' => $this->router, |
84
|
|
|
'templateRouter' => $this->templateRouter, |
85
|
|
|
]) |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return new Root($resources); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|