Skip to content
This repository was archived by the owner on Jul 3, 2020. It is now read-only.

Commit 68691c4

Browse files
authored
Merge pull request #380 from svycka/plugin-managers-refactor
Plugin managers refactor and more improvements
2 parents 1859bc1 + e52eb56 commit 68691c4

27 files changed

+320
-407
lines changed

.php_cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ class Config extends PhpCsFixerConfig
1818
'@PHP71Migration' => true,
1919
'array_syntax' => ['syntax' => 'short'],
2020
'binary_operator_spaces' => [
21-
'align_double_arrow' => true,
22-
'align_equals' => false,
21+
'default' => 'single_space',
2322
],
2423
'blank_line_after_opening_tag' => true,
2524
'blank_line_after_namespace' => true,

config/dependencies.global.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,17 @@
2121
return [
2222
'dependencies' => [
2323
'factories' => [
24-
ZfcRbac\Assertion\AssertionPluginManager::class => ZfcRbac\Container\AssertionPluginManagerFactory::class,
25-
ZfcRbac\Options\ModuleOptions::class => ZfcRbac\Container\ModuleOptionsFactory::class,
26-
ZfcRbac\Role\RoleProviderPluginManager::class => ZfcRbac\Container\RoleProviderPluginManagerFactory::class,
24+
ZfcRbac\Assertion\AssertionContainerInterface::class => ZfcRbac\Container\AssertionContainerFactory::class,
25+
ZfcRbac\Options\ModuleOptions::class => ZfcRbac\Container\ModuleOptionsFactory::class,
26+
ZfcRbac\Role\InMemoryRoleProvider::class => ZfcRbac\Container\InMemoryRoleProviderFactory::class,
27+
ZfcRbac\Role\ObjectRepositoryRoleProvider::class => ZfcRbac\Container\ObjectRepositoryRoleProviderFactory::class,
2728
ZfcRbac\Service\AuthorizationServiceInterface::class => ZfcRbac\Container\AuthorizationServiceFactory::class,
28-
ZfcRbac\Service\RoleServiceInterface::class => ZfcRbac\Container\RoleServiceFactory::class,
29-
ZfcRbac\Rbac::class => \Zend\ServiceManager\Factory\InvokableFactory::class,
29+
ZfcRbac\Service\RoleServiceInterface::class => ZfcRbac\Container\RoleServiceFactory::class,
30+
ZfcRbac\Rbac::class => \Zend\ServiceManager\Factory\InvokableFactory::class,
3031
],
3132
],
3233

3334
'zfc_rbac' => [
34-
// Role provider plugin manager
35-
'role_provider_manager' => [],
36-
3735
// Assertion plugin manager
3836
'assertion_manager' => [],
3937
],
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,16 @@
2828
*
2929
* @author Aeneas Rekkas
3030
* @licence MIT
31-
*
32-
* @method AssertionInterface get($name)
3331
*/
34-
class AssertionPluginManager extends AbstractPluginManager
32+
final class AssertionContainer extends AbstractPluginManager implements AssertionContainerInterface
3533
{
3634
protected $instanceOf = AssertionInterface::class;
35+
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
public function get($name, array $options = null): AssertionInterface
40+
{
41+
return parent::get($name);
42+
}
3743
}

src/Role/RoleProviderPluginManager.php renamed to src/Assertion/AssertionContainerInterface.php

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,11 @@
1919

2020
declare(strict_types=1);
2121

22-
namespace ZfcRbac\Role;
22+
namespace ZfcRbac\Assertion;
2323

24-
use Zend\ServiceManager\AbstractPluginManager;
25-
use Zend\ServiceManager\Factory\InvokableFactory;
26-
use ZfcRbac\Container\ObjectRepositoryRoleProviderFactory;
24+
use Psr\Container\ContainerInterface;
2725

28-
/**
29-
* Plugin manager to create role providers
30-
*
31-
* @method RoleProviderInterface get($name)
32-
*
33-
* @author Michaël Gallego <mic.gallego@gmail.com>
34-
* @license MIT
35-
*/
36-
final class RoleProviderPluginManager extends AbstractPluginManager
26+
interface AssertionContainerInterface extends ContainerInterface
3727
{
38-
protected $instanceOf = RoleProviderInterface::class;
39-
40-
/**
41-
* @var array
42-
*/
43-
protected $factories = [
44-
InMemoryRoleProvider::class => InvokableFactory::class,
45-
ObjectRepositoryRoleProvider::class => ObjectRepositoryRoleProviderFactory::class,
46-
];
28+
public function get($name): AssertionInterface;
4729
}

src/Assertion/AssertionSet.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,18 @@ final class AssertionSet implements AssertionInterface
3333
const CONDITION_AND = 'condition_and';
3434

3535
/**
36-
* @var AssertionPluginManager
36+
* @var AssertionContainerInterface
3737
*/
38-
private $assertionPluginManager;
38+
private $assertionContainer;
3939

4040
/**
4141
* @var array
4242
*/
43-
private $assertions = [];
43+
private $assertions;
4444

4545
private $condition = self::CONDITION_AND;
4646

47-
public function __construct(AssertionPluginManager $assertionPluginManager, array $assertions)
47+
public function __construct(AssertionContainerInterface $assertionContainer, array $assertions)
4848
{
4949
if (isset($assertions['condition'])) {
5050
if ($assertions['condition'] !== AssertionSet::CONDITION_AND
@@ -58,7 +58,7 @@ public function __construct(AssertionPluginManager $assertionPluginManager, arra
5858
}
5959

6060
$this->assertions = $assertions;
61-
$this->assertionPluginManager = $assertionPluginManager;
61+
$this->assertionContainer = $assertionContainer;
6262
}
6363

6464
public function assert(string $permission, IdentityInterface $identity = null, $context = null): bool
@@ -78,12 +78,12 @@ public function assert(string $permission, IdentityInterface $identity = null, $
7878
$asserted = $assertion->assert($permission, $identity, $context);
7979
break;
8080
case is_string($assertion):
81-
$this->assertions[$index] = $assertion = $this->assertionPluginManager->get($assertion);
81+
$this->assertions[$index] = $assertion = $this->assertionContainer->get($assertion);
8282

8383
$asserted = $assertion->assert($permission, $identity, $context);
8484
break;
8585
case is_array($assertion):
86-
$this->assertions[$index] = $assertion = new AssertionSet($this->assertionPluginManager, $assertion);
86+
$this->assertions[$index] = $assertion = new AssertionSet($this->assertionContainer, $assertion);
8787
$asserted = $assertion->assert($permission, $identity, $context);
8888
break;
8989
default:

src/Container/AssertionPluginManagerFactory.php renamed to src/Container/AssertionContainerFactory.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,20 @@
2222
namespace ZfcRbac\Container;
2323

2424
use Psr\Container\ContainerInterface;
25-
use ZfcRbac\Assertion\AssertionPluginManager;
25+
use ZfcRbac\Assertion\AssertionContainer;
2626

2727
/**
2828
* Factory to create a assertion plugin manager
2929
*
3030
* @author Aeneas Rekkas
3131
* @licence MIT
3232
*/
33-
final class AssertionPluginManagerFactory
33+
final class AssertionContainerFactory
3434
{
35-
public function __invoke(ContainerInterface $container): AssertionPluginManager
35+
public function __invoke(ContainerInterface $container): AssertionContainer
3636
{
3737
$config = $container->get('config')['zfc_rbac']['assertion_manager'];
3838

39-
return new AssertionPluginManager($container, $config);
39+
return new AssertionContainer($container, $config);
4040
}
4141
}

src/Container/AuthorizationServiceFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
namespace ZfcRbac\Container;
2323

2424
use Psr\Container\ContainerInterface;
25-
use ZfcRbac\Assertion\AssertionPluginManager;
25+
use ZfcRbac\Assertion\AssertionContainerInterface;
2626
use ZfcRbac\Options\ModuleOptions;
2727
use ZfcRbac\Rbac;
2828
use ZfcRbac\Service\AuthorizationService;
@@ -43,7 +43,7 @@ public function __invoke(ContainerInterface $container): AuthorizationService
4343
return new AuthorizationService(
4444
$container->get(Rbac::class),
4545
$container->get(RoleServiceInterface::class),
46-
$container->get(AssertionPluginManager::class),
46+
$container->get(AssertionContainerInterface::class),
4747
$moduleOptions->getAssertionMap()
4848
);
4949
}

src/Container/RoleProviderPluginManagerFactory.php renamed to src/Container/InMemoryRoleProviderFactory.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,23 @@
2222
namespace ZfcRbac\Container;
2323

2424
use Psr\Container\ContainerInterface;
25-
use ZfcRbac\Role\RoleProviderPluginManager;
25+
use ZfcRbac\Options\ModuleOptions;
26+
use ZfcRbac\Role\InMemoryRoleProvider;
2627

2728
/**
28-
* Factory to create a role provider plugin manager
29+
* Factory used to create an in memory role provider
2930
*
30-
* @author Michaël Gallego <mic.gallego@gmail.com>
31+
* @author Vytautas Stankus
3132
* @licence MIT
3233
*/
33-
final class RoleProviderPluginManagerFactory
34+
final class InMemoryRoleProviderFactory
3435
{
35-
public function __invoke(ContainerInterface $container): RoleProviderPluginManager
36+
public function __invoke(ContainerInterface $container): InMemoryRoleProvider
3637
{
37-
$config = $container->get('config')['zfc_rbac']['role_provider_manager'];
38+
$moduleOptions = $container->get(ModuleOptions::class);
3839

39-
return new RoleProviderPluginManager($container, $config);
40+
return new InMemoryRoleProvider(
41+
$moduleOptions->getRoleProvider()[InMemoryRoleProvider::class] ?? []
42+
);
4043
}
4144
}

src/Container/ObjectRepositoryRoleProviderFactory.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
use Psr\Container\ContainerInterface;
2525
use ZfcRbac\Exception;
26+
use ZfcRbac\Options\ModuleOptions;
2627
use ZfcRbac\Role\ObjectRepositoryRoleProvider;
2728

2829
/**
@@ -33,8 +34,11 @@
3334
*/
3435
final class ObjectRepositoryRoleProviderFactory
3536
{
36-
public function __invoke(ContainerInterface $container, $requestedName, ?array $options = []): ObjectRepositoryRoleProvider
37+
public function __invoke(ContainerInterface $container): ObjectRepositoryRoleProvider
3738
{
39+
$moduleOptions = $container->get(ModuleOptions::class);
40+
$options = $moduleOptions->getRoleProvider()[ObjectRepositoryRoleProvider::class] ?? [];
41+
3842
if (! isset($options['role_name_property'])) {
3943
throw new Exception\RuntimeException('The "role_name_property" option is missing');
4044
}

src/Container/RoleServiceFactory.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@
2222
namespace ZfcRbac\Container;
2323

2424
use Psr\Container\ContainerInterface;
25-
use ZfcRbac\Exception\RuntimeException;
2625
use ZfcRbac\Options\ModuleOptions;
27-
use ZfcRbac\Role\RoleProviderPluginManager;
26+
use ZfcRbac\Role\RoleProviderInterface;
2827
use ZfcRbac\Service\RoleService;
2928

3029
/**
@@ -39,17 +38,6 @@ public function __invoke(ContainerInterface $container): RoleService
3938
{
4039
$moduleOptions = $container->get(ModuleOptions::class);
4140

42-
$roleProviderConfig = $moduleOptions->getRoleProvider();
43-
44-
if (empty($roleProviderConfig)) {
45-
throw new RuntimeException('No role provider has been set for ZfcRbac');
46-
}
47-
48-
$pluginManager = $container->get(RoleProviderPluginManager::class);
49-
$roleProvider = $pluginManager->get(key($roleProviderConfig), current($roleProviderConfig));
50-
$roleService = new RoleService($roleProvider);
51-
$roleService->setGuestRole($moduleOptions->getGuestRole());
52-
53-
return $roleService;
41+
return new RoleService($container->get(RoleProviderInterface::class), $moduleOptions->getGuestRole());
5442
}
5543
}

0 commit comments

Comments
 (0)