Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added registry for community-manager #104

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions Command/InitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
namespace Sulu\Bundle\CommunityBundle\Command;

use Doctrine\ORM\EntityManagerInterface;
use Sulu\Bundle\CommunityBundle\DependencyInjection\CompilerPass\Normalizer;
use Sulu\Bundle\CommunityBundle\DependencyInjection\Configuration;
use Sulu\Bundle\CommunityBundle\Manager\CommunityManagerInterface;
use Sulu\Bundle\CommunityBundle\Manager\CommunityManagerRegistryInterface;
use Sulu\Bundle\SecurityBundle\Entity\Role;
use Sulu\Bundle\SecurityBundle\Entity\RoleRepository;
use Sulu\Component\Webspace\Manager\WebspaceManagerInterface;
Expand Down Expand Up @@ -79,14 +78,14 @@ protected function initWebspace($webspace, OutputInterface $output)
{
$webspaceKey = $webspace->getKey();

$communityServiceName = sprintf('sulu_community.%s.community_manager', Normalizer::normalize($webspaceKey));
/** @var CommunityManagerRegistryInterface $registry */
$registry = $this->getContainer()->get('sulu_community.community_manager.registry');

if (!$webspace->getSecurity() || !$this->getContainer()->has($communityServiceName)) {
if (!$webspace->getSecurity() || !$registry->has($webspaceKey)) {
return;
}

/** @var CommunityManagerInterface $communityManager */
$communityManager = $this->getContainer()->get($communityServiceName);
$communityManager = $registry->get($webspaceKey);
$roleName = $communityManager->getConfigProperty(Configuration::ROLE);
$system = $webspace->getSecurity()->getSystem();

Expand Down
9 changes: 1 addition & 8 deletions Controller/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Sulu\Bundle\CommunityBundle\Controller;

use Sulu\Bundle\CommunityBundle\DependencyInjection\CompilerPass\Normalizer;
use Sulu\Bundle\CommunityBundle\DependencyInjection\Configuration;
use Sulu\Bundle\CommunityBundle\Manager\CommunityManagerInterface;
use Sulu\Bundle\SecurityBundle\Entity\User;
Expand Down Expand Up @@ -43,13 +42,7 @@ abstract class AbstractController extends Controller
*/
protected function getCommunityManager($webspaceKey)
{
if (!isset($this->communityManagers[$webspaceKey])) {
$this->communityManagers[$webspaceKey] = $this->get(
sprintf('sulu_community.%s.community_manager', Normalizer::normalize($webspaceKey))
);
}

return $this->communityManagers[$webspaceKey];
return $this->get('sulu_community.community_manager.registry')->get($webspaceKey);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\DefinitionDecorator;
use Symfony\Component\DependencyInjection\Reference;

/**
* Create foreach configured webspace a community manager.
Expand All @@ -28,6 +29,7 @@ public function process(ContainerBuilder $container)
{
$webspacesConfig = $container->getParameter('sulu_community.webspaces_config');

$references = [];
foreach ($webspacesConfig as $webspaceKey => $webspaceConfig) {
$webspaceConfig = $this->updateWebspaceConfig($webspaceKey, $webspaceConfig);
$webspacesConfig[$webspaceKey] = $webspaceConfig;
Expand All @@ -36,10 +38,9 @@ public function process(ContainerBuilder $container)
$definition->replaceArgument(0, $webspaceConfig);
$definition->replaceArgument(1, $webspaceKey);

$container->setDefinition(
sprintf('sulu_community.%s.community_manager', Normalizer::normalize($webspaceKey)),
$definition
);
$id = sprintf('sulu_community.%s.community_manager', Normalizer::normalize($webspaceKey));
$references[$webspaceKey] = new Reference($id);
$container->setDefinition($id, $definition);

if (false !== strpos($webspaceKey, '-')) {
$container->setAlias(
Expand All @@ -49,6 +50,7 @@ public function process(ContainerBuilder $container)
}
}

$container->getDefinition('sulu_community.community_manager.registry')->replaceArgument(0, $references);
$container->setParameter('sulu_community.webspaces_config', $webspacesConfig);
}

Expand Down
53 changes: 53 additions & 0 deletions Manager/CommunityManagerRegistry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/*
* This file is part of Sulu.
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\CommunityBundle\Manager;

class CommunityManagerRegistry implements CommunityManagerRegistryInterface
{
/**
* @var array
*/
private $managers;

/**
* @param CommunityManagerInterface[] $managers
*/
public function __construct(array $managers = [])
{
$this->managers = $managers;
}

/**
* {@inheritdoc}
*/
public function get($webspaceKey)
{
if (!$this->has($webspaceKey)) {
throw new \Exception(
sprintf(
'Webspace "%s" is not configured.',
$webspaceKey
)
);
}

return $this->managers[$webspaceKey];
}

/**
* {@inheritdoc}
*/
public function has($webspaceKey)
{
return array_key_exists($webspaceKey, $this->managers);
}
}
29 changes: 29 additions & 0 deletions Manager/CommunityManagerRegistryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of Sulu.
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\CommunityBundle\Manager;

interface CommunityManagerRegistryInterface
{
/**
* @param string $webspaceKey
*
* @return CommunityManagerInterface
*/
public function get($webspaceKey);

/**
* @param string $webspaceKey
*
* @return bool
*/
public function has($webspaceKey);
}
5 changes: 5 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@

<services>
<!-- Community Manager -->
<service id="sulu_community.community_manager.registry"
class="Sulu\Bundle\CommunityBundle\Manager\CommunityManagerRegistry"
public="true">
<argument type="collection"/>
</service>
<service id="sulu_community.community_manager"
class="Sulu\Bundle\CommunityBundle\Manager\CommunityManager"
abstract="true">
Expand Down
44 changes: 44 additions & 0 deletions Tests/Unit/Manager/CommunityManagerRegistryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of Sulu.
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\CommunityBundle\Tests\Unit\Manager;

use Sulu\Bundle\CommunityBundle\Manager\CommunityManager;
use Sulu\Bundle\CommunityBundle\Manager\CommunityManagerRegistry;

class CommunityManagerRegistryTest extends \PHPUnit_Framework_TestCase
{
public function testGet()
{
$manager = $this->prophesize(CommunityManager::class);
$registry = new CommunityManagerRegistry(['sulu_io' => $manager->reveal()]);

$this->assertEquals($manager->reveal(), $registry->get('sulu_io'));
}

public function testGetNotExists()
{
$this->setExpectedException(\Exception::class);

$registry = new CommunityManagerRegistry();

$registry->get('sulu_io');
}

public function testHas()
{
$manager = $this->prophesize(CommunityManager::class);
$registry = new CommunityManagerRegistry(['sulu_io' => $manager->reveal()]);

$this->assertTrue($registry->has('sulu_io'));
$this->assertFalse($registry->has('test_io'));
}
}