Skip to content
This repository was archived by the owner on May 20, 2024. It is now read-only.

Commit 9984b8b

Browse files
committed
make LinkResolver as service
1 parent 3aecfd8 commit 9984b8b

File tree

7 files changed

+80
-49
lines changed

7 files changed

+80
-49
lines changed

Controller/DefaultController.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,10 @@ public function searchAction(Request $request)
101101
*/
102102
public function previewAction(Request $request)
103103
{
104-
$token = $request->query->get('token');
105104
/** @var PrismicContext $ctx */
106105
$ctx = $this->get('prismic.context');
107-
$url = $ctx->getApi()->previewSession($token, $ctx->getLinkResolver(), '/');
108-
$response = new RedirectResponse($url);
109-
$response->headers->setCookie(new Cookie(Prismic\PREVIEW_COOKIE, $token, time() + 1800, '/', null, false, false));
110-
return $response;
106+
107+
return $ctx->previewSession($request->query->get('token'), '/');
111108
}
112109

113110
}

DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public function getConfigTreeBuilder()
4141
->end()
4242
->end()
4343
->scalarNode('cache')->defaultTrue()->end()
44+
->scalarNode('link_resolver_route')->defaultValue('detail')->end()
4445
->end()
4546
;
4647

DependencyInjection/PrismicExtension.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ public function load(array $configs, ContainerBuilder $container)
2929
$container->setParameter($this->getAlias() . '.oauth.redirect_route', $config['oauth']['redirect_route']);
3030
$container->setParameter($this->getAlias() . '.oauth.redirect_route_params', $config['oauth']['redirect_route_params']);
3131

32+
$container->setParameter($this->getAlias() . '.link_resolver_route', $config['link_resolver_route']);
33+
3234
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
3335
$loader->load('services.xml');
3436

35-
$container->setAlias('prismic.cache', 'prismic.cache.' . ($config['cache'] ? 'default' : 'no'));
37+
$container->setAlias($this->getAlias() . '.cache', $this->getAlias() . '.cache.' . ($config['cache'] ? 'default' : 'no'));
3638
}
3739
}

Helper/LocalLinkResolver.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@
22

33
namespace Prismic\Bundle\PrismicBundle\Helper;
44

5-
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
6-
7-
use Prismic\Api;
8-
use Prismic\Ref;
95
use Prismic\LinkResolver;
10-
use Prismic\Fragment\Link\DocumentLink;
6+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
117

128
/**
139
* Class LocalLinkResolver
@@ -20,29 +16,36 @@ class LocalLinkResolver extends LinkResolver
2016
* @var UrlGeneratorInterface
2117
*/
2218
private $urlGenerator;
19+
2320
/**
24-
* @var Api
21+
* @var string
2522
*/
26-
private $api;
23+
private $routeName;
2724

2825
/**
2926
* @param UrlGeneratorInterface $urlGenerator
3027
* @param Api $api
3128
*/
32-
public function __construct(UrlGeneratorInterface $urlGenerator, Api $api)
29+
public function __construct(UrlGeneratorInterface $urlGenerator, $routeName)
3330
{
3431
$this->urlGenerator = $urlGenerator;
35-
$this->api = $api;
32+
$this->routeName = $routeName;
3633
}
3734

3835
/**
39-
* @param DocumentLink $link
36+
* @param \Prismic\Fragment\Link\DocumentLink $link
4037
*
4138
* @return string
4239
*/
4340
public function resolve($link)
4441
{
45-
return $this->urlGenerator->generate('detail', array('id' => $link->getId(), 'slug' => $link->getSlug()));
42+
return $this->urlGenerator->generate(
43+
$this->routeName,
44+
[
45+
'id' => $link->getId(),
46+
'slug' => $link->getSlug(),
47+
]
48+
);
4649
}
4750

4851
}

Helper/PrismicContext.php

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
namespace Prismic\Bundle\PrismicBundle\Helper;
44

5-
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
6-
7-
use Prismic\api;
8-
use Prismic\Ref;
5+
use Prismic\Api;
96
use Prismic\Document;
107
use Prismic\Fragment\Link\DocumentLink;
8+
use Prismic\LinkResolver;
9+
use Prismic\Ref;
10+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1111

1212
/**
1313
* Class PrismicContext
@@ -28,10 +28,11 @@ class PrismicContext
2828
* @param PrismicHelper $prismic
2929
* @param UrlGeneratorInterface $urlGenerator
3030
*/
31-
public function __construct(PrismicHelper $prismic, UrlGeneratorInterface $urlGenerator)
31+
public function __construct(PrismicHelper $prismic, UrlGeneratorInterface $urlGenerator, LinkResolver $linkResolver)
3232
{
3333
$this->prismic = $prismic;
3434
$this->urlGenerator = $urlGenerator;
35+
$this->linkResolver = $linkResolver;
3536
}
3637

3738
/**
@@ -48,8 +49,6 @@ public function getHelper()
4849
public function setAccessToken($accessToken)
4950
{
5051
$this->accessToken = $accessToken;
51-
52-
$this->api = $this->linkResolver = null;
5352
}
5453

5554
/**
@@ -58,8 +57,6 @@ public function setAccessToken($accessToken)
5857
public function setRef($ref)
5958
{
6059
$this->ref = $ref;
61-
62-
$this->linkResolver = null;
6360
}
6461

6562
/**
@@ -111,14 +108,10 @@ public function getUrlGenerator()
111108
}
112109

113110
/**
114-
* @return LocalLinkResolver
111+
* @return LinkResolver
115112
*/
116113
public function getLinkResolver()
117114
{
118-
if (!$this->linkResolver) {
119-
$this->linkResolver = new LocalLinkResolver($this->urlGenerator, $this->getApi());
120-
}
121-
122115
return $this->linkResolver;
123116
}
124117

@@ -129,7 +122,15 @@ public function getLinkResolver()
129122
*/
130123
public function resolveLink(Document $doc)
131124
{
132-
$link = new DocumentLink($doc->getId(), $doc->getType(), $doc->getTags(), $doc->getSlug(), false);
125+
$link = new DocumentLink(
126+
$doc->getId(),
127+
$doc->getUid(),
128+
$doc->getType(),
129+
$doc->getTags(),
130+
$doc->getSlug(),
131+
$doc->getFragments(),
132+
false
133+
);
133134

134135
return $this->getLinkResolver()->resolve($link);
135136
}
@@ -156,4 +157,20 @@ public function getDocument($id)
156157
return null;
157158
}
158159

160+
/**
161+
* Get redirect response for preview session
162+
*
163+
* @param string $token
164+
* @param string $defaultUrl
165+
* @return RedirectResponse
166+
*/
167+
public function previewSession($token, $defaultUrl)
168+
{
169+
$url = $this->getApi()->previewSession($token, $this->getLinkResolver(), '/');
170+
$response = new RedirectResponse($url);
171+
$response->headers->setCookie(new Cookie(Prismic\PREVIEW_COOKIE, $token, time() + 1800, '/', null, false, false));
172+
173+
return $response;
174+
}
175+
159176
}

README.md

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,27 @@ Add the following dependencies to your projects ``composer.json`` file:
1818

1919
## Configuration
2020

21-
Add the following configuration to your projects ``app/config/config.yml`` file:
22-
23-
# Default configuration for extension with alias: "prismic"
24-
prismic:
25-
api:
26-
endpoint: ~ # Required
27-
access_token: ~
28-
client_id: ~
29-
client_secret: ~
30-
31-
You can override the redirect route from the bundle configuration:
32-
33-
# Default configuration for extension with alias: "prismic"
34-
prismic:
35-
oauth:
36-
redirect_route: home # Name of the route
37-
redirect_route_params: [] # An array with additional route params
21+
Full default configuration for bundle:
22+
23+
```yaml
24+
prismic:
25+
api:
26+
endpoint: ~ # Required
27+
access_token: ~
28+
client_id: ~
29+
client_secret: ~
30+
oauth:
31+
redirect_route: home # Name of the route
32+
redirect_route_params: [] # An array with additional route params
33+
cache: true # Default apc built-in cache
34+
link_resolver_route: detail # Name of the route
35+
```
36+
37+
## LinkResolver Customization
38+
39+
You can override `prismic.link_resolver_route` parameter with route name to handle link resolver.
40+
This route can have `$id` or `$slug` parameter to find document.
41+
If you want to implement custom logic for your LinkResolver you can override service `prismic.link_resolver`.
3842

3943
## TODOs
4044

Resources/config/services.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<parameter key="prismic.context_listener.class">Prismic\Bundle\PrismicBundle\EventListener\ContextListener</parameter>
1414
<parameter key="prismic.controller.oauth.class">Prismic\Bundle\PrismicBundle\Controller\OAuthController</parameter>
1515
<parameter key="prismic.helper.class">Prismic\Bundle\PrismicBundle\Helper\PrismicHelper</parameter>
16+
<parameter key="prismic.helper.link_resolver.class">Prismic\Bundle\PrismicBundle\Helper\LocalLinkResolver</parameter>
1617
</parameters>
1718

1819
<services>
@@ -21,6 +22,11 @@
2122

2223
<service id="prismic.client" class="%prismic.client.class%" factory-class="%prismic.api.class%" factory-method="defaultHttpAdapter" />
2324

25+
<service id="prismic.link_resolver" class="%prismic.helper.link_resolver.class%">
26+
<argument type="service" id="router"></argument>
27+
<argument>%prismic.link_resolver_route%</argument>
28+
</service>
29+
2430
<service id="prismic.helper" class="%prismic.helper.class%">
2531
<argument>%prismic.api.endpoint%</argument>
2632
<argument>%prismic.api.accessToken%</argument>
@@ -33,6 +39,7 @@
3339
<service id="prismic.context" class="%prismic.context.class%">
3440
<argument type="service" id="prismic.helper" />
3541
<argument type="service" id="router" />
42+
<argument type="service" id="prismic.link_resolver" />
3643
</service>
3744

3845
<service id="prismic.context_listener" class="%prismic.context_listener.class%">

0 commit comments

Comments
 (0)