-
Notifications
You must be signed in to change notification settings - Fork 159
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
Showing
7 changed files
with
198 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
/* | ||
* Copyright 2012 Johannes M. Schmitt <[email protected]> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace JMS\I18nRoutingBundle\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
|
||
/** | ||
* Make sure the Twig extension could get a request object | ||
* | ||
* @author Tobias Nyholm <[email protected]> | ||
*/ | ||
class SetRequestPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
$twigExtDev = $container->getDefinition('jms_i18n_routing.twig_extension'); | ||
// If SF=>2.4 | ||
if (null !== $container->hasDefinition('request_stack')) { | ||
$twigExtDev->addMethodCall('setRequestStack', [$container->getDefinition('request_stack')]); | ||
} else { | ||
$twigExtDev->addMethodCall('setContainer', [$container->getDefinition('service_container')]); | ||
} | ||
} | ||
} |
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,5 @@ | ||
|
||
{% for locale in locales %} | ||
{% set arr = routeParams|merge({'_locale':locale}) %} | ||
<link rel="alternate" hreflang="{{ locale }}" href="{{ url(route, arr) }}" /> | ||
{% endfor %} |
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,136 @@ | ||
<?php | ||
|
||
/* | ||
* Copyright 2012 Johannes M. Schmitt <[email protected]> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace JMS\I18nRoutingBundle\Twig; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
|
||
/** | ||
* @author Tobias Nyholm <[email protected]> | ||
*/ | ||
class I18nRoutingExtension extends \Twig_Extension | ||
{ | ||
/** | ||
* @var RequestStack requestStack | ||
*/ | ||
private $requestStack; | ||
|
||
/** | ||
* @var ContainerInterface container | ||
* This is only used in symfony 2.3 | ||
*/ | ||
private $container; | ||
|
||
/** | ||
* @var array locales | ||
*/ | ||
private $locales; | ||
|
||
/** | ||
* @param array $locales | ||
*/ | ||
public function __construct($locales) | ||
{ | ||
$this->locales = $locales; | ||
} | ||
|
||
public function getFunctions() | ||
{ | ||
return array( | ||
new \Twig_SimpleFunction('hreflang', array($this, 'getHreflang'), array( | ||
'needs_environment' => true, | ||
'is_safe' => array('html'), | ||
)), | ||
); | ||
} | ||
|
||
/** | ||
* Return HTML with hreflang attributes. | ||
* | ||
* @param \Twig_Environment $env | ||
*/ | ||
public function getHreflang(\Twig_Environment $env) | ||
{ | ||
if (null === $request = $this->getRequest()) { | ||
return; | ||
} | ||
|
||
if (null === $routeParams = $request->attributes->get('_route_params')) { | ||
return; | ||
} | ||
|
||
if (!isset($routeParams['localized']) || !$routeParams['localized']) { | ||
return; | ||
} | ||
|
||
return $env->render('JMSI18nRoutingBundle::hreflang.html.twig', array( | ||
'locales' => $this->locales, | ||
'route' => $request->attributes->get('_route'), | ||
'routeParams' => $routeParams, | ||
)); | ||
} | ||
|
||
/** | ||
* @param ContainerInterface $container | ||
* | ||
* @return I18nRoutingExtension | ||
*/ | ||
public function setContainer(ContainerInterface $container) | ||
{ | ||
$this->container = $container; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param RequestStack $requestStack | ||
* | ||
* @return I18nRoutingExtension | ||
*/ | ||
public function setRequestStack(RequestStack $requestStack) | ||
{ | ||
$this->requestStack = $requestStack; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return null|\Symfony\Component\HttpFoundation\Request | ||
*/ | ||
private function getRequest() | ||
{ | ||
if ($this->requestStack !== null) { | ||
return $this->requestStack->getMasterRequest(); | ||
} | ||
|
||
if ($this->container !== null) { | ||
$this->container->get('request'); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @return string | ||
*/ | ||
public function getName() | ||
{ | ||
return 'i18n_routing_extension'; | ||
} | ||
} |