Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony CMF package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) 2011-2015 Symfony CMF
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Cmf\Component\Routing\Enhancer;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Cmf\Component\Routing\ContentRepositoryInterface;
|
Chris@0
|
15 use Symfony\Cmf\Component\Routing\RouteObjectInterface;
|
Chris@0
|
16 use Symfony\Component\HttpFoundation\Request;
|
Chris@0
|
17
|
Chris@0
|
18 /**
|
Chris@0
|
19 * This enhancer uses a ContentRepositoryInterface to load a content if $target
|
Chris@0
|
20 * is empty.
|
Chris@0
|
21 *
|
Chris@0
|
22 * $source specifies the field that contains the ID to load, $target specifies
|
Chris@0
|
23 * the field where to put the content returned by the repository.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @author Samusev Andrey
|
Chris@0
|
26 */
|
Chris@0
|
27 class ContentRepositoryEnhancer implements RouteEnhancerInterface
|
Chris@0
|
28 {
|
Chris@0
|
29 /**
|
Chris@0
|
30 * @var ContentRepositoryInterface
|
Chris@0
|
31 */
|
Chris@0
|
32 private $contentRepository;
|
Chris@0
|
33
|
Chris@0
|
34 /**
|
Chris@0
|
35 * @var string
|
Chris@0
|
36 */
|
Chris@0
|
37 private $target;
|
Chris@0
|
38
|
Chris@0
|
39 /**
|
Chris@0
|
40 * @var string
|
Chris@0
|
41 */
|
Chris@0
|
42 private $source;
|
Chris@0
|
43
|
Chris@0
|
44 /**
|
Chris@0
|
45 * @param ContentRepositoryInterface $contentRepository repository to search for the content
|
Chris@0
|
46 * @param string $target the field name to set content
|
Chris@0
|
47 * @param string $source the field name of the request parameter that contains the id
|
Chris@0
|
48 */
|
Chris@0
|
49 public function __construct(
|
Chris@0
|
50 ContentRepositoryInterface $contentRepository,
|
Chris@0
|
51 $target = RouteObjectInterface::CONTENT_OBJECT,
|
Chris@0
|
52 $source = RouteObjectInterface::CONTENT_ID
|
Chris@0
|
53 ) {
|
Chris@0
|
54 $this->contentRepository = $contentRepository;
|
Chris@0
|
55 $this->target = $target;
|
Chris@0
|
56 $this->source = $source;
|
Chris@0
|
57 }
|
Chris@0
|
58
|
Chris@0
|
59 /**
|
Chris@0
|
60 * {@inheritdoc}
|
Chris@0
|
61 */
|
Chris@0
|
62 public function enhance(array $defaults, Request $request)
|
Chris@0
|
63 {
|
Chris@0
|
64 if (!isset($defaults[$this->target]) && isset($defaults[$this->source])) {
|
Chris@0
|
65 $defaults[$this->target] = $this->contentRepository->findById($defaults[$this->source]);
|
Chris@0
|
66 }
|
Chris@0
|
67
|
Chris@0
|
68 return $defaults;
|
Chris@0
|
69 }
|
Chris@0
|
70 }
|