-
Notifications
You must be signed in to change notification settings - Fork 27
Sitemap generation #196
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony CMF package. | ||
* | ||
* (c) 2011-2014 Symfony CMF | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Cmf\Bundle\SeoBundle\Controller; | ||
|
||
use Symfony\Cmf\Bundle\SeoBundle\Model\UrlInformation; | ||
use Symfony\Cmf\Bundle\SeoBundle\Sitemap\UrlInformationProviderInterface; | ||
use Symfony\Component\HttpFoundation\JsonResponse; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Templating\EngineInterface; | ||
|
||
/** | ||
* Controller to handle requests for sitemap. | ||
* | ||
* @author Maximilian Berghoff <[email protected]> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. requires empty line |
||
*/ | ||
class SitemapController | ||
{ | ||
/** | ||
* @var UrlInformationProviderInterface | ||
*/ | ||
private $urlProvider; | ||
/** | ||
* @var EngineInterface | ||
*/ | ||
private $templating; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $templates; | ||
|
||
/** | ||
* You should provide templates for html and xml. | ||
* | ||
* Json is serialized by default, but can be customized with a template | ||
* | ||
* @param UrlInformationProviderInterface $provider | ||
* @param EngineInterface $templating | ||
* @param array $templates Hash map with key being the format, | ||
* value the name of the twig template | ||
* to render the sitemap in that format | ||
*/ | ||
public function __construct( | ||
UrlInformationProviderInterface $provider, | ||
EngineInterface $templating, | ||
array $templates | ||
) { | ||
$this->urlProvider = $provider; | ||
$this->templating = $templating; | ||
$this->templates = $templates; | ||
} | ||
|
||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we have a short description? |
||
* @param string $_format The format of the sitemap. | ||
* | ||
* @return Response | ||
*/ | ||
public function indexAction($_format) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @lsmith77 is there something to not need the underscore? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. afaik no There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no, it's an internal parameter and as such, it is prefixed with an underscore. The only option is typehinting for a Request instance and doing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dbu thought it should work without it, and i lost some hair in learning it. |
||
{ | ||
$supportedFormats = array_merge(array('json'), array_keys($this->templates)); | ||
if (!in_array($_format, $supportedFormats)) { | ||
$text = sprintf( | ||
'Unknown format %s, use one of %s.', | ||
$_format, | ||
implode(', ', $supportedFormats) | ||
); | ||
|
||
return new Response($text, 406); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe we could also allow other formats and remove the format requirement from the route. then you could start with isset (so that i can specify a template for json too), then try json and otherwise throw a 404 that this page is not found. with a message which formats are supported as you do below. |
||
|
||
$urls = $this->urlProvider->getUrlInformation(); | ||
if (isset($this->templates[$_format])) { | ||
return new Response($this->templating->render($this->templates[$_format], array('urls' => $urls))); | ||
} | ||
|
||
return $this->createJsonResponse($urls); | ||
} | ||
|
||
/** | ||
* @param array|UrlInformation[] $urls | ||
* | ||
* @return JsonResponse | ||
*/ | ||
private function createJsonResponse($urls) | ||
{ | ||
$result = array(); | ||
|
||
foreach ($urls as $url) { | ||
$result[] = $url->toArray(); | ||
} | ||
|
||
return new JsonResponse($result); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could we use the symfony serializer for this? or maybe the JMSSerializer, as there is a complex xml to be handled as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same answer for that dependency. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the symfony serializer is probably already required anyways. its problem is that it can't handle xml and json elegantly at the same time. but if we do the xml with a twig template we could here safe the toArray and delegate that to the serializer. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We decided to keep that. |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony CMF package. | ||
* | ||
* (c) 2011-2014 Symfony CMF | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Cmf\Bundle\SeoBundle\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Exception\LogicException; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
/** | ||
* Compiler adds custom url information provider to the phpcr chain provider. | ||
* | ||
* To do so you need to tag them with "cmf_seo.sitemap.url_information_provider". | ||
* | ||
* @author Maximilian Berghoff <[email protected]> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add one line what this does (register services tagged with ... to the sitemap chain provider.) |
||
*/ | ||
class RegisterUrlInformationProviderPass implements CompilerPassInterface | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
* | ||
* @throws LogicException If a tagged service is not public. | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
// feature not activated means nothing to add | ||
if (!$container->hasDefinition('cmf_seo.sitemap.url_information_provider')) { | ||
return; | ||
} | ||
|
||
$chainProviderDefinition = $container->getDefinition('cmf_seo.sitemap.url_information_provider'); | ||
$taggedServices = $container->findTaggedServiceIds('cmf_seo.sitemap.url_information_provider'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's rename the tag to |
||
|
||
foreach ($taggedServices as $id => $attributes) { | ||
$priority = null; | ||
foreach ($attributes as $attribute) { | ||
if (isset($attribute['priority'])) { | ||
$priority = $attribute['priority']; | ||
break; | ||
} | ||
} | ||
$priority = $priority ?: 0; | ||
|
||
$chainProviderDefinition->addMethodCall('addProvider', array(new Reference($id), $priority)); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's 2015 now :) (and as this file was added in 2015, it should officialy say "(c) 2015 Symfony CMF")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't we chage that in all files on master?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah and all bundles...