diff --git a/Controller/DefaultController.php b/Controller/DefaultController.php index 41c1295..e474527 100755 --- a/Controller/DefaultController.php +++ b/Controller/DefaultController.php @@ -101,13 +101,10 @@ public function searchAction(Request $request) */ public function previewAction(Request $request) { - $token = $request->query->get('token'); /** @var PrismicContext $ctx */ $ctx = $this->get('prismic.context'); - $url = $ctx->getApi()->previewSession($token, $ctx->getLinkResolver(), '/'); - $response = new RedirectResponse($url); - $response->headers->setCookie(new Cookie(Prismic\PREVIEW_COOKIE, $token, time() + 1800, '/', null, false, false)); - return $response; + + return $ctx->previewSession($request->query->get('token'), '/'); } } diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 39eebb8..b32ae64 100755 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -41,6 +41,7 @@ public function getConfigTreeBuilder() ->end() ->end() ->scalarNode('cache')->defaultTrue()->end() + ->scalarNode('link_resolver_route')->defaultValue('detail')->end() ->end() ; diff --git a/DependencyInjection/PrismicExtension.php b/DependencyInjection/PrismicExtension.php index 2582cfa..24e8558 100755 --- a/DependencyInjection/PrismicExtension.php +++ b/DependencyInjection/PrismicExtension.php @@ -29,9 +29,11 @@ public function load(array $configs, ContainerBuilder $container) $container->setParameter($this->getAlias() . '.oauth.redirect_route', $config['oauth']['redirect_route']); $container->setParameter($this->getAlias() . '.oauth.redirect_route_params', $config['oauth']['redirect_route_params']); + $container->setParameter($this->getAlias() . '.link_resolver_route', $config['link_resolver_route']); + $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); $loader->load('services.xml'); - $container->setAlias('prismic.cache', 'prismic.cache.' . ($config['cache'] ? 'default' : 'no')); + $container->setAlias($this->getAlias() . '.cache', $this->getAlias() . '.cache.' . ($config['cache'] ? 'default' : 'no')); } } diff --git a/Helper/LocalLinkResolver.php b/Helper/LocalLinkResolver.php index 95ef5b8..88e29a5 100755 --- a/Helper/LocalLinkResolver.php +++ b/Helper/LocalLinkResolver.php @@ -2,12 +2,8 @@ namespace Prismic\Bundle\PrismicBundle\Helper; -use Symfony\Component\Routing\Generator\UrlGeneratorInterface; - -use Prismic\Api; -use Prismic\Ref; use Prismic\LinkResolver; -use Prismic\Fragment\Link\DocumentLink; +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; /** * Class LocalLinkResolver @@ -20,29 +16,36 @@ class LocalLinkResolver extends LinkResolver * @var UrlGeneratorInterface */ private $urlGenerator; + /** - * @var Api + * @var string */ - private $api; + private $routeName; /** * @param UrlGeneratorInterface $urlGenerator * @param Api $api */ - public function __construct(UrlGeneratorInterface $urlGenerator, Api $api) + public function __construct(UrlGeneratorInterface $urlGenerator, $routeName) { $this->urlGenerator = $urlGenerator; - $this->api = $api; + $this->routeName = $routeName; } /** - * @param DocumentLink $link + * @param \Prismic\Fragment\Link\DocumentLink $link * * @return string */ public function resolve($link) { - return $this->urlGenerator->generate('detail', array('id' => $link->getId(), 'slug' => $link->getSlug())); + return $this->urlGenerator->generate( + $this->routeName, + [ + 'id' => $link->getId(), + 'slug' => $link->getSlug(), + ] + ); } } diff --git a/Helper/PrismicContext.php b/Helper/PrismicContext.php index be75247..477d2e7 100755 --- a/Helper/PrismicContext.php +++ b/Helper/PrismicContext.php @@ -2,12 +2,12 @@ namespace Prismic\Bundle\PrismicBundle\Helper; -use Symfony\Component\Routing\Generator\UrlGeneratorInterface; - -use Prismic\api; -use Prismic\Ref; +use Prismic\Api; use Prismic\Document; use Prismic\Fragment\Link\DocumentLink; +use Prismic\LinkResolver; +use Prismic\Ref; +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; /** * Class PrismicContext @@ -28,10 +28,11 @@ class PrismicContext * @param PrismicHelper $prismic * @param UrlGeneratorInterface $urlGenerator */ - public function __construct(PrismicHelper $prismic, UrlGeneratorInterface $urlGenerator) + public function __construct(PrismicHelper $prismic, UrlGeneratorInterface $urlGenerator, LinkResolver $linkResolver) { $this->prismic = $prismic; $this->urlGenerator = $urlGenerator; + $this->linkResolver = $linkResolver; } /** @@ -48,8 +49,6 @@ public function getHelper() public function setAccessToken($accessToken) { $this->accessToken = $accessToken; - - $this->api = $this->linkResolver = null; } /** @@ -58,8 +57,6 @@ public function setAccessToken($accessToken) public function setRef($ref) { $this->ref = $ref; - - $this->linkResolver = null; } /** @@ -111,14 +108,10 @@ public function getUrlGenerator() } /** - * @return LocalLinkResolver + * @return LinkResolver */ public function getLinkResolver() { - if (!$this->linkResolver) { - $this->linkResolver = new LocalLinkResolver($this->urlGenerator, $this->getApi()); - } - return $this->linkResolver; } @@ -129,7 +122,15 @@ public function getLinkResolver() */ public function resolveLink(Document $doc) { - $link = new DocumentLink($doc->getId(), $doc->getType(), $doc->getTags(), $doc->getSlug(), false); + $link = new DocumentLink( + $doc->getId(), + $doc->getUid(), + $doc->getType(), + $doc->getTags(), + $doc->getSlug(), + $doc->getFragments(), + false + ); return $this->getLinkResolver()->resolve($link); } @@ -156,4 +157,20 @@ public function getDocument($id) return null; } + /** + * Get redirect response for preview session + * + * @param string $token + * @param string $defaultUrl + * @return RedirectResponse + */ + public function previewSession($token, $defaultUrl) + { + $url = $this->getApi()->previewSession($token, $this->getLinkResolver(), '/'); + $response = new RedirectResponse($url); + $response->headers->setCookie(new Cookie(Prismic\PREVIEW_COOKIE, $token, time() + 1800, '/', null, false, false)); + + return $response; + } + } diff --git a/README.md b/README.md index ba129cd..a76a9d4 100644 --- a/README.md +++ b/README.md @@ -18,23 +18,27 @@ Add the following dependencies to your projects ``composer.json`` file: ## Configuration -Add the following configuration to your projects ``app/config/config.yml`` file: - - # Default configuration for extension with alias: "prismic" - prismic: - api: - endpoint: ~ # Required - access_token: ~ - client_id: ~ - client_secret: ~ - -You can override the redirect route from the bundle configuration: - - # Default configuration for extension with alias: "prismic" - prismic: - oauth: - redirect_route: home # Name of the route - redirect_route_params: [] # An array with additional route params +Full default configuration for bundle: + +```yaml +prismic: + api: + endpoint: ~ # Required + access_token: ~ + client_id: ~ + client_secret: ~ + oauth: + redirect_route: home # Name of the route + redirect_route_params: [] # An array with additional route params + cache: true # Default apc built-in cache + link_resolver_route: detail # Name of the route +``` + +## LinkResolver Customization + +You can override `prismic.link_resolver_route` parameter with route name to handle link resolver. +This route can have `$id` or `$slug` parameter to find document. +If you want to implement custom logic for your LinkResolver you can override service `prismic.link_resolver`. ## TODOs diff --git a/Resources/config/services.xml b/Resources/config/services.xml index e84f73d..95ce32f 100755 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -13,6 +13,7 @@ Prismic\Bundle\PrismicBundle\EventListener\ContextListener Prismic\Bundle\PrismicBundle\Controller\OAuthController Prismic\Bundle\PrismicBundle\Helper\PrismicHelper + Prismic\Bundle\PrismicBundle\Helper\LocalLinkResolver @@ -21,6 +22,11 @@ + + + %prismic.link_resolver_route% + + %prismic.api.endpoint% %prismic.api.accessToken% @@ -33,6 +39,7 @@ +