Skip to content
This repository has been archived by the owner on Sep 16, 2021. It is now read-only.

Commit

Permalink
implementing alternate languages for content documents
Browse files Browse the repository at this point in the history
fix other tests

add first tests

present current state

fix bundle class

create alternate_locale.enable flag

fix file name

fix provider creation

changelog entry

fix orm tests
  • Loading branch information
ElectricMaxxx committed Aug 21, 2014
1 parent 6626e63 commit ec65e44
Show file tree
Hide file tree
Showing 18 changed files with 545 additions and 4 deletions.
38 changes: 38 additions & 0 deletions AlternateLocaleProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?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;

use Symfony\Cmf\Bundle\SeoBundle\Model\AlternateLocaleCollection;

/**
* An interface for providing alternate locale urls.
*
* @author Maximilian Berghoff <[email protected]>
*/
interface AlternateLocaleProviderInterface
{
/**
* Creates a collection of AlternateLocales for one content object.
*
* @param object $content
* @return AlternateLocaleCollection
*/
public function createForContent($content);

/**
* Creates a collection of AlternateLocales for many content object.
*
* @param array|object[] $contents
* @return AlternateLocaleCollection
*/
public function createForManyContent(array $contents);
}
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Changelog
1.1.0-RC1
---------

* **2014-02-08**: Implement alternate locale support and its configuration
* **2014-06-06**: Updated to PSR-4 autoloading

1.0.0
Expand Down
1 change: 0 additions & 1 deletion CmfSeoBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\HttpKernel\Bundle\Bundle;

use Symfony\Cmf\Bundle\SeoBundle\DependencyInjection\Compiler\RegisterExtractorsPass;
Expand Down
55 changes: 55 additions & 0 deletions DependencyInjection/CmfSeoExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Cmf\Bundle\RoutingBundle\Routing\DynamicRouter;
Expand Down Expand Up @@ -56,6 +58,8 @@ public function load(array $configs, ContainerBuilder $container)
$config['persistence']['phpcr']['manager_name']
);
$sonataBundles[] = 'SonataDoctrinePHPCRAdminBundle';

$this->loadPhpcr($config['persistence']['phpcr'], $loader, $container);
}

if ($this->isConfigEnabled($container, $config['persistence']['orm'])) {
Expand All @@ -70,6 +74,10 @@ public function load(array $configs, ContainerBuilder $container)
if (count($sonataBundles) && $config['sonata_admin_extension']['enabled']) {
$this->loadSonataAdmin($config['sonata_admin_extension'], $loader, $container, $sonataBundles);
}

if ($this->isConfigEnabled($container, $config['alternate_locale'])) {
$this->loadAlternateLocaleProvider($config['alternate_locale'], $container);
}
}

/**
Expand Down Expand Up @@ -131,4 +139,51 @@ public function getXsdValidationBasePath()
{
return __DIR__.'/../Resources/config/schema';
}

/**
* @param $config
* @param XmlFileLoader $loader
* @param ContainerBuilder $container
*/
private function loadPhpcr($config, XmlFileLoader $loader, ContainerBuilder $container)
{
$loader->load('phpcr.xml');

// chose the default alternate locale provider, when nothing else was set
if (!isset($config['alternate_locale.provider_id']) || null === $config['alternate_locale.provider_id']) {
$container->setParameter(
$this->getAlias().'.alternate_locale.provider_id',
'cmf_seo.alternate_locale.provider'
);
}
}

/**
* If somebody (event loading of phpcr configuration) set the
* cmf_seo.alternate_locale.provider_id this one will be used
* to get the locale provider.
*
* @param $config
* @param ContainerBuilder $container
*/
private function loadAlternateLocaleProvider($config, ContainerBuilder $container)
{
if ($container->hasParameter($this->getAlias().'.alternate_locale.provider_id')
&& null !== $container->getParameter($this->getAlias().'.alternate_locale.provider_id')
&& $container->hasDefinition(
$container->getParameter($this->getAlias().'.alternate_locale.provider_id')
)
) {
$definition = $container->getDefinition('cmf_seo.event_listener.seo_content');
$definition
->addMethodCall(
'setAlternateLocaleProvider',
array(
$container->getDefinition(
$container->getParameter($this->getAlias().'.alternate_locale.provider_id')
)
)
);
}
}
}
7 changes: 7 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ public function getConfigTreeBuilder()
->scalarNode('form_group')->defaultValue('form.group_seo')->end()
->end()
->end()
->arrayNode('alternate_locale')
->addDefaultsIfNotSet()
->canBeEnabled()
->children()
->scalarNode('provider_id')->defaultNull()->end()
->end()
->end()
->end()
;

Expand Down
29 changes: 26 additions & 3 deletions EventListener/ContentListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

namespace Symfony\Cmf\Bundle\SeoBundle\EventListener;

use Symfony\Cmf\Bundle\CoreBundle\Translatable\TranslatableInterface;
use Symfony\Cmf\Bundle\SeoBundle\AlternateLocaleProviderInterface;
use Symfony\Cmf\Bundle\SeoBundle\SeoPresentationInterface;
use Symfony\Cmf\Component\Routing\RouteReferrersInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
Expand All @@ -37,8 +40,13 @@ class ContentListener
private $requestKey;

/**
* @param SeoPresentationInterface $seoPage Service Handling SEO information.
* @param string $requestKey The key to look up the content in the request attributes.
* @var AlternateLocaleProviderInterface|null
*/
private $alternateLocaleProvider;

/**
* @param SeoPresentationInterface $seoPage Service Handling SEO information.
* @param string $requestKey The key to look up the content in the request attributes.
*/
public function __construct(SeoPresentationInterface $seoPage, $requestKey)
{
Expand All @@ -52,13 +60,20 @@ public function __construct(SeoPresentationInterface $seoPage, $requestKey)
public function onKernelRequest(GetResponseEvent $event)
{
if ($event->getRequest()->attributes->has($this->requestKey)) {
$this->seoPresentation->updateSeoPage($event->getRequest()->attributes->get($this->requestKey));
$content = $event->getRequest()->attributes->get($this->requestKey);
$this->seoPresentation->updateSeoPage($content);

// look if the strategy is redirectResponse and if there is a route to redirectResponse to
$response = $this->seoPresentation->getRedirectResponse();
if (false !== $response && $this->canBeRedirected($event->getRequest(), $response)) {
$event->setResponse($response);
}

if (null !== $this->alternateLocaleProvider) {
$this->seoPresentation->updateAlternateLocales(
$this->alternateLocaleProvider->createForContent($content)
);
}
}
}

Expand All @@ -73,4 +88,12 @@ protected function canBeRedirected(Request $request, RedirectResponse $response)

return $targetPath !== $currentPath;
}

/**
* @param AlternateLocaleProviderInterface $alternateLocaleProvider
*/
public function setAlternateLocaleProvider($alternateLocaleProvider)
{
$this->alternateLocaleProvider = $alternateLocaleProvider;
}
}
39 changes: 39 additions & 0 deletions Model/AlternateLocale.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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\Model;

/**
* Value object for properties of an alternate locale.
*
* @author Maximilian Berghoff <[email protected]>
*/
class AlternateLocale
{
const REL = 'alternate';

/**
* @var string The complete url for that locale.
*/
public $href;

/**
* @var string The locale/language in the following formats: de, de-DE
*/
public $hrefLocale;

public function __construct($href, $hrefLocale)
{

$this->href = $href;
$this->hrefLocale = $hrefLocale;
}
}
24 changes: 24 additions & 0 deletions Model/AlternateLocaleCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?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\Model;

use Doctrine\Common\Collections\ArrayCollection;

/**
* Collection for alternate locales.
*
* @author Maximilian Berghoff <[email protected]>
*/
class AlternateLocaleCollection extends ArrayCollection
{

}
107 changes: 107 additions & 0 deletions PhpcrAlternateLocaleProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?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;

use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ODM\PHPCR\DocumentManager;
use Symfony\Cmf\Bundle\CoreBundle\Translatable\TranslatableInterface;
use Symfony\Cmf\Bundle\SeoBundle\Model\AlternateLocale;
use Symfony\Cmf\Bundle\SeoBundle\Model\AlternateLocaleCollection;
use Symfony\Cmf\Component\Routing\RouteReferrersReadInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

/**
* This provider will be the default one for creating AlternateLocales
* by the help of the phpcr-odm.
*
* @author Maximilian Berghoff <[email protected]>
*/
class PhpcrAlternateLocaleProvider implements AlternateLocaleProviderInterface
{
/**
* @var UrlGeneratorInterface
*/
private $urlGenerator;

/**
* @var ManagerRegistry
*/
private $managerRegistry;

/**
* @param ManagerRegistry $managerRegistry
* @param UrlGeneratorInterface $urlGenerator
*/
public function __construct(ManagerRegistry $managerRegistry, UrlGeneratorInterface $urlGenerator)
{
$this->managerRegistry = $managerRegistry;
$this->urlGenerator = $urlGenerator;
}

/**
* Creates a collection of AlternateLocales for one content object.
*
* @param object $content
* @return AlternateLocaleCollection
*/
public function createForContent($content)
{
$documentManager = $this->getDocumentManagerForClass(get_class($content));
$alternateLocaleCollection = new AlternateLocaleCollection();
if (null === $documentManager
|| !$content instanceof TranslatableInterface
|| !$content instanceof RouteReferrersReadInterface
) {
return $alternateLocaleCollection;
}

$alternateLocales = $documentManager->getLocalesFor($content);
$currentLocale = $content->getLocale();
foreach ($alternateLocales as $locale) {
if ($locale === $currentLocale) {
continue;
}

$alternateLocaleCollection->add(
new AlternateLocale(
$this->urlGenerator->generate($content, array('_locale' => $locale)),
$locale
)
);
}

return $alternateLocaleCollection;
}

/**
* Creates a collection of AlternateLocales for many content object.
*
* @param array|object[] $contents
* @return AlternateLocaleCollection
*/
public function createForManyContent(array $contents)
{
// todo[max] implement for sitemap
}

/**
* When the registry was set, this method figure out
* the document manager of a given class.
*
* @param $class
* @return DocumentManager|null|object
*/
private function getDocumentManagerForClass($class)
{
return $this->managerRegistry->getManagerForClass($class);
}
}
Loading

0 comments on commit ec65e44

Please sign in to comment.