This repository has been archived by the owner on Sep 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e488e32
commit b2e8b97
Showing
34 changed files
with
1,758 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]> | ||
*/ | ||
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; | ||
} | ||
|
||
/** | ||
* @param string $_format The format of the sitemap. | ||
* | ||
* @return Response | ||
*/ | ||
public function indexAction($_format) | ||
{ | ||
$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); | ||
} | ||
|
||
$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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
DependencyInjection/Compiler/RegisterUrlInformationProviderPass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]> | ||
*/ | ||
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'); | ||
|
||
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.