4 * This file is part of the Symfony CMF package.
6 * (c) 2011-2015 Symfony CMF
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Cmf\Component\Routing\Enhancer;
14 use Symfony\Cmf\Component\Routing\ContentRepositoryInterface;
15 use Symfony\Cmf\Component\Routing\RouteObjectInterface;
16 use Symfony\Component\HttpFoundation\Request;
19 * This enhancer uses a ContentRepositoryInterface to load a content if $target
22 * $source specifies the field that contains the ID to load, $target specifies
23 * the field where to put the content returned by the repository.
25 * @author Samusev Andrey
27 class ContentRepositoryEnhancer implements RouteEnhancerInterface
30 * @var ContentRepositoryInterface
32 private $contentRepository;
45 * @param ContentRepositoryInterface $contentRepository repository to search for the content
46 * @param string $target the field name to set content
47 * @param string $source the field name of the request parameter that contains the id
49 public function __construct(
50 ContentRepositoryInterface $contentRepository,
51 $target = RouteObjectInterface::CONTENT_OBJECT,
52 $source = RouteObjectInterface::CONTENT_ID
54 $this->contentRepository = $contentRepository;
55 $this->target = $target;
56 $this->source = $source;
62 public function enhance(array $defaults, Request $request)
64 if (!isset($defaults[$this->target]) && isset($defaults[$this->source])) {
65 $defaults[$this->target] = $this->contentRepository->findById($defaults[$this->source]);