├── .gitignore
├── PrismicBundle.php
├── Resources
├── views
│ ├── layout.html.twig
│ ├── Default
│ │ ├── detail.html.twig
│ │ ├── search.html.twig
│ │ └── index.html.twig
│ └── pagination.html.twig
└── config
│ ├── routing
│ └── oauth.xml
│ ├── routing.xml
│ └── services.xml
├── composer.json
├── Helper
├── LocalLinkResolver.php
├── PrismicHelper.php
└── PrismicContext.php
├── DependencyInjection
├── PrismicExtension.php
└── Configuration.php
├── EventListener
└── ContextListener.php
├── README.md
└── Controller
└── DefaultController.php
/.gitignore:
--------------------------------------------------------------------------------
1 | composer.lock
2 | coverage
3 | phpunit.xml
4 | vendor
5 | .idea
6 |
--------------------------------------------------------------------------------
/PrismicBundle.php:
--------------------------------------------------------------------------------
1 |
6 |
7 |
8 | Your prismic.io project
9 |
10 |
11 |
12 |
13 | {% block content %}{% endblock %}
14 |
15 | {% endblock %}
16 |
--------------------------------------------------------------------------------
/Resources/views/Default/detail.html.twig:
--------------------------------------------------------------------------------
1 | {% extends "PrismicBundle::layout.html.twig" %}
2 |
3 | {% block content %}
4 |
5 |
6 | {% autoescape false %}
7 |
8 | {{ doc.asHtml(ctx.linkResolver) }}
9 | {% endautoescape %}
10 |
11 |
12 | {% endblock %}
13 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "prismic/prismic-bundle",
3 | "type": "symfony-bundle",
4 | "description": "Prismic.io integration with Symfony2",
5 | "keywords": ["CMS", "Prismic", "Symfony2", "Persistence"],
6 | "homepage": "http://www.prismic.io",
7 | "license": "Apache 2 license",
8 |
9 | "autoload": {
10 | "psr-4": { "Prismic\\Bundle\\PrismicBundle\\": "" }
11 | },
12 |
13 | "require": {
14 | "php": ">=5.3.3",
15 | "symfony/framework-bundle": "~2.4",
16 | "prismic/php-sdk": "~1.6.1"
17 | },
18 |
19 | "extra": {
20 | "branch-alias": {
21 | "dev-master": "1.0.x-dev"
22 | }
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/Resources/views/Default/search.html.twig:
--------------------------------------------------------------------------------
1 | {% extends "PrismicBundle::layout.html.twig" %}
2 |
3 | {% block content %}
4 |
5 |
6 | {% if docs.totalResultsSize == 1 %}
7 | One document found
8 | {% elseif docs.totalResultsSize > 0 %}
9 | {{ docs.totalResultsSize }} documents found
10 | {% else %}
11 | No results
12 | {% endif %}
13 |
14 |
15 |
24 |
25 | {% include 'PrismicBundle::pagination.html.twig' %}
26 |
27 | {% endblock %}
28 |
--------------------------------------------------------------------------------
/Resources/config/routing/oauth.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
6 |
7 |
8 | prismic.controller.oauth:signinAction
9 |
10 |
11 |
12 | prismic.controller.oauth:callbackAction
13 |
14 |
15 |
16 | prismic.controller.oauth:signoutAction
17 |
18 |
19 |
--------------------------------------------------------------------------------
/Resources/config/routing.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
6 |
7 |
8 | PrismicBundle:Default:index
9 |
10 |
11 |
12 | PrismicBundle:Default:detail
13 |
14 |
15 |
16 | PrismicBundle:Default:search
17 |
18 |
19 |
20 | PrismicBundle:Default:preview
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/Resources/views/Default/index.html.twig:
--------------------------------------------------------------------------------
1 | {% extends "PrismicBundle::layout.html.twig" %}
2 |
3 | {% block content %}
4 |
5 |
9 |
10 |
11 |
12 |
13 | {% if docs.totalResultsSize == 1 %}
14 | One document found
15 | {% elseif docs.totalResultsSize > 0 %}
16 | {{ docs.totalResultsSize }} documents found
17 | {% else %}
18 | No documents found
19 | {% endif %}
20 |
21 |
22 |
31 |
32 | {% include 'PrismicBundle::pagination.html.twig' %}
33 |
34 | {% endblock %}
35 |
--------------------------------------------------------------------------------
/Resources/views/pagination.html.twig:
--------------------------------------------------------------------------------
1 | {% if docs.totalPages > 1 %}
2 |
19 | {% endif %}
20 |
--------------------------------------------------------------------------------
/Helper/LocalLinkResolver.php:
--------------------------------------------------------------------------------
1 | urlGenerator = $urlGenerator;
35 | $this->api = $api;
36 | }
37 |
38 | /**
39 | * @param DocumentLink $link
40 | *
41 | * @return string
42 | */
43 | public function resolve($link)
44 | {
45 | return $this->urlGenerator->generate('detail', array('id' => $link->getId(), 'slug' => $link->getSlug()));
46 | }
47 |
48 | }
49 |
--------------------------------------------------------------------------------
/DependencyInjection/PrismicExtension.php:
--------------------------------------------------------------------------------
1 | processConfiguration(new Configuration(), $configs);
23 |
24 | $container->setParameter($this->getAlias() . '.api.endpoint', $config['api']['endpoint']);
25 | $container->setParameter($this->getAlias() . '.api.accessToken', $config['api']['access_token']);
26 | $container->setParameter($this->getAlias() . '.api.clientId', $config['api']['client_id']);
27 | $container->setParameter($this->getAlias() . '.api.clientSecret', $config['api']['client_secret']);
28 |
29 | $container->setParameter($this->getAlias() . '.oauth.redirect_route', $config['oauth']['redirect_route']);
30 | $container->setParameter($this->getAlias() . '.oauth.redirect_route_params', $config['oauth']['redirect_route_params']);
31 |
32 | $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
33 | $loader->load('services.xml');
34 |
35 | $container->setAlias('prismic.cache', 'prismic.cache.' . ($config['cache'] ? 'default' : 'no'));
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/EventListener/ContextListener.php:
--------------------------------------------------------------------------------
1 |
13 | *
14 | * @package Prismic\Bundle\PrismicBundle\EventListener
15 | */
16 | class ContextListener
17 | {
18 | /**
19 | * @var PrismicContext
20 | */
21 | private $context;
22 |
23 | /**
24 | * Constructor.
25 | *
26 | * @param PrismicContext $context
27 | */
28 | public function __construct(PrismicContext $context)
29 | {
30 | $this->context = $context;
31 | }
32 |
33 | /**
34 | * @param GetResponseEvent $event
35 | */
36 | public function onKernelRequest(GetResponseEvent $event)
37 | {
38 | if (false === $event->isMasterRequest()) {
39 | return;
40 | }
41 |
42 | $request = $event->getRequest();
43 |
44 | $previewCookie = str_replace('.', '_', Prismic\PREVIEW_COOKIE);
45 | $experimentsCookie = str_replace('.', '_', Prismic\EXPERIMENTS_COOKIE);
46 |
47 | if ($request->cookies->has($previewCookie)) {
48 | $newRef = $request->cookies->get($previewCookie);
49 | } else if ($request->cookies->has($experimentsCookie)) {
50 | $cookie = $request->cookies->get($experimentsCookie);
51 | $newRef = $this->context->getApi()->getExperiments()->refFromCookie($cookie);
52 | }
53 |
54 | isset($newRef) and $this->context->setRef($newRef);
55 | }
56 |
57 | }
58 |
--------------------------------------------------------------------------------
/DependencyInjection/Configuration.php:
--------------------------------------------------------------------------------
1 | root('prismic')
26 | ->children()
27 | ->arrayNode('api')
28 | ->isRequired()
29 | ->children()
30 | ->scalarNode('endpoint')->isRequired()->end()
31 | ->scalarNode('access_token')->defaultNull()->end()
32 | ->scalarNode('client_id')->defaultNull()->end()
33 | ->scalarNode('client_secret')->defaultNull()->end()
34 | ->end()
35 | ->end()
36 | ->arrayNode('oauth')
37 | ->addDefaultsIfNotSet()
38 | ->children()
39 | ->scalarNode('redirect_route')->defaultValue('home')->end()
40 | ->scalarNode('redirect_route_params')->defaultValue(array())->end()
41 | ->end()
42 | ->end()
43 | ->scalarNode('cache')->defaultTrue()->end()
44 | ->end()
45 | ;
46 |
47 | return $treeBuilder;
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # PrismicBundle
2 |
3 | ## :warning: UNMAINTAINED PROJECT :warning:
4 |
5 | This project is now unmaintained and becomes more and more outdated comparing to the evolution of the Symfony framework. We invite you to read our [PHP documentation](https://prismic.io/docs/php/getting-started/with-the-php-starter-kit) and integrate yourself our [PHP SDK](https://packagist.org/packages/prismic/php-sdk) in your Symfony project.
6 |
7 | ### Info
8 |
9 | This Bundle integrates the http://prismic.io php-kit with the Symfony Framework:
10 | https://github.com/prismicio/php-kit
11 |
12 | For an example use see:
13 | https://github.com/prismicio/php-symfony-starter
14 |
15 | ### Installation
16 |
17 | Add the following dependencies to your projects ``composer.json`` file:
18 |
19 | "require": {
20 | # ..
21 | "prismic/prismic-bundle": "~1.0@dev"
22 | # ..
23 | }
24 |
25 | ### Configuration
26 |
27 | Add the following configuration to your projects ``app/config/config.yml`` file:
28 |
29 | # Default configuration for extension with alias: "prismic"
30 | prismic:
31 | api:
32 | endpoint: ~ # Required
33 | access_token: ~
34 | client_id: ~
35 | client_secret: ~
36 |
37 | You can override the redirect route from the bundle configuration:
38 |
39 | # Default configuration for extension with alias: "prismic"
40 | prismic:
41 | oauth:
42 | redirect_route: home # Name of the route
43 | redirect_route_params: [] # An array with additional route params
44 |
45 | ### TODOs
46 |
47 | - [x] Add a listener for Symfony 2.3 to set the request data into the context as 2.3 does not support ExpressionLanguage
48 | - [ ] Add unit (and functional?) tests
49 | - [ ] Provide twig templates to render documents
50 | - [ ] Make caching configurable once https://github.com/prismicio/php-kit/issues/32 is implemented
51 |
52 | ### Credits
53 |
54 | Kudos to [lsmith77](https://github.com/lsmith77) who did all the hard work!
55 |
--------------------------------------------------------------------------------
/Helper/PrismicHelper.php:
--------------------------------------------------------------------------------
1 | apiEndpoint = $apiEndpoint;
54 | $this->accessToken = $accessToken;
55 | $this->clientId = $clientId;
56 | $this->clientSecret = $clientSecret;
57 | $this->client = $client;
58 | $this->cache = $cache;
59 | }
60 |
61 | /**
62 | * @return String API Endpoint
63 | */
64 | public function getApiEndpoint()
65 | {
66 | return $this->apiEndpoint;
67 | }
68 |
69 | /**
70 | * @param string $customAccessToken
71 | * @return Api
72 | */
73 | public function getApiHome($customAccessToken = null)
74 | {
75 | return Api::get($this->apiEndpoint, $customAccessToken ? $customAccessToken : $this->accessToken, $this->client, $this->cache);
76 | }
77 |
78 | /**
79 | * @return null|CacheInterface
80 | */
81 | public function getCache()
82 | {
83 | return $this->cache;
84 | }
85 |
86 | /**
87 | * @return HttpAdapterInterface|null
88 | */
89 | public function getClient()
90 | {
91 | return $this->client;
92 | }
93 |
94 | /**
95 | * @return string
96 | */
97 | public function getClientId()
98 | {
99 | return $this->clientId;
100 | }
101 |
102 | /**
103 | * @return string
104 | */
105 | public function getClientSecret()
106 | {
107 | return $this->clientSecret;
108 | }
109 |
110 | }
111 |
--------------------------------------------------------------------------------
/Resources/config/services.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
6 |
7 |
8 | Prismic\Api
9 | Prismic\Cache\ApcCache
10 | Prismic\Cache\NoCache
11 | Guzzle\Http\Client
12 | Prismic\Bundle\PrismicBundle\Helper\PrismicContext
13 | Prismic\Bundle\PrismicBundle\EventListener\ContextListener
14 | Prismic\Bundle\PrismicBundle\Controller\OAuthController
15 | Prismic\Bundle\PrismicBundle\Helper\PrismicHelper
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 | %prismic.api.endpoint%
26 | %prismic.api.accessToken%
27 | %prismic.api.clientId%
28 | %prismic.api.clientSecret%
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 | %prismic.oauth.redirect_route%
47 | %prismic.oauth.redirect_route_params%
48 |
49 |
50 |
51 |
--------------------------------------------------------------------------------
/Controller/DefaultController.php:
--------------------------------------------------------------------------------
1 | get('prismic.context');
30 | $docs = $ctx->getApi()->forms()->everything->ref($ctx->getRef())
31 | ->pageSize(10)
32 | ->page($request->query->get('page', 1))
33 | ->submit();
34 |
35 | return $this->render('PrismicBundle:Default:index.html.twig', array(
36 | 'ctx' => $ctx,
37 | 'docs' => $docs
38 | ));
39 | }
40 |
41 | /**
42 | * @param string $id
43 | * @param string $slug
44 | *
45 | * @return RedirectResponse|Response
46 | *
47 | * @throws NotFoundHttpException
48 | */
49 | public function detailAction($id, $slug)
50 | {
51 | /** @var PrismicContext $ctx */
52 | $ctx = $this->get('prismic.context');
53 | $doc = $ctx->getDocument($id);
54 |
55 | if ($doc) {
56 | if ($doc->getSlug() == $slug) {
57 | return $this->render('PrismicBundle:Default:detail.html.twig', array(
58 | 'ctx' => $ctx,
59 | 'doc' => $doc
60 | ));
61 | }
62 |
63 | if (in_array($slug, $doc->getSlugs())) {
64 | return $this->redirect(
65 | $this->generateUrl('detail', array('id' => $id, 'slug' => $doc->getSlug()))
66 | );
67 | }
68 |
69 | }
70 |
71 | throw $this->createNotFoundException('Document not found');
72 | }
73 |
74 | /**
75 | * @param Request $request
76 | *
77 | * @return Response
78 | */
79 | public function searchAction(Request $request)
80 | {
81 | $q = $request->query->get('q');
82 | /** @var PrismicContext $ctx */
83 | $ctx = $this->get('prismic.context');
84 | $docs = $ctx->getApi()->forms()->everything->ref ($ctx->getRef())->query(
85 | '[[:d = fulltext(document, "'.$q.'")]]'
86 | )
87 | ->pageSize(10)
88 | ->page($request->query->get('page', 1))
89 | ->submit();
90 |
91 | return $this->render('PrismicBundle:Default:search.html.twig', array(
92 | 'ctx' => $ctx,
93 | 'docs' => $docs
94 | ));
95 | }
96 |
97 | /**
98 | * @param Request $request
99 | *
100 | * @return RedirectResponse
101 | */
102 | public function previewAction(Request $request)
103 | {
104 | $token = $request->query->get('token');
105 | /** @var PrismicContext $ctx */
106 | $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;
111 | }
112 |
113 | }
114 |
--------------------------------------------------------------------------------
/Helper/PrismicContext.php:
--------------------------------------------------------------------------------
1 | prismic = $prismic;
34 | $this->urlGenerator = $urlGenerator;
35 | }
36 |
37 | /**
38 | * @return PrismicHelper
39 | */
40 | public function getHelper()
41 | {
42 | return $this->prismic;
43 | }
44 |
45 | /**
46 | * @param string $accessToken
47 | */
48 | public function setAccessToken($accessToken)
49 | {
50 | $this->accessToken = $accessToken;
51 |
52 | $this->api = $this->linkResolver = null;
53 | }
54 |
55 | /**
56 | * @param string $ref
57 | */
58 | public function setRef($ref)
59 | {
60 | $this->ref = $ref;
61 |
62 | $this->linkResolver = null;
63 | }
64 |
65 | /**
66 | * @return bool
67 | */
68 | public function hasPrivilegedAccess()
69 | {
70 | return isset($this->accessToken);
71 | }
72 |
73 | /**
74 | * @return Api
75 | */
76 | public function getApi()
77 | {
78 | if (!$this->api) {
79 | $this->api = $this->prismic->getApiHome($this->accessToken);
80 | }
81 |
82 | return $this->api;
83 | }
84 |
85 | /**
86 | * @return Ref
87 | */
88 | public function getRef()
89 | {
90 | if (null === $this->ref) {
91 | $this->ref = $this->getMasterRef();
92 | }
93 |
94 | return $this->ref;
95 | }
96 |
97 | /**
98 | * @return Ref
99 | */
100 | public function getMasterRef()
101 | {
102 | return $this->getApi()->master()->getRef();
103 | }
104 |
105 | /**
106 | * @return UrlGeneratorInterface
107 | */
108 | public function getUrlGenerator()
109 | {
110 | return $this->urlGenerator;
111 | }
112 |
113 | /**
114 | * @return LocalLinkResolver
115 | */
116 | public function getLinkResolver()
117 | {
118 | if (!$this->linkResolver) {
119 | $this->linkResolver = new LocalLinkResolver($this->urlGenerator, $this->getApi());
120 | }
121 |
122 | return $this->linkResolver;
123 | }
124 |
125 | /**
126 | * @param Document $doc
127 | *
128 | * @return string
129 | */
130 | public function resolveLink(Document $doc)
131 | {
132 | $link = new DocumentLink($doc->getId(), $doc->getType(), $doc->getTags(), $doc->getSlug(), false);
133 |
134 | return $this->getLinkResolver()->resolve($link);
135 | }
136 |
137 | /**
138 | * @param string $id
139 | *
140 | * @return Document|null
141 | */
142 | public function getDocument($id)
143 | {
144 |
145 | $docs = $this->getApi()->forms()->everything->ref($this->getRef())->query(
146 | '[[:d = at(document.id, "'.$id.'")]]'
147 | )
148 | ->submit()
149 | ;
150 |
151 | if (is_array($docs->getResults()) && count($docs->getResults()) > 0) {
152 | $results = $docs->getResults();
153 | return $results[0];
154 | }
155 |
156 | return null;
157 | }
158 |
159 | }
160 |
--------------------------------------------------------------------------------