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

Commit

Permalink
Merge pull request #104 from ZF-Commons/remove-cache
Browse files Browse the repository at this point in the history
Remove cache
  • Loading branch information
bakura10 committed Dec 2, 2013
2 parents 72b149a + 2f0660f commit eeb3ccc
Show file tree
Hide file tree
Showing 16 changed files with 37 additions and 353 deletions.
1 change: 0 additions & 1 deletion config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

'factories' => [
/* Factories that do not map to a class */
'ZfcRbac\Cache' => 'ZfcRbac\Factory\CacheFactory',
'ZfcRbac\Guards' => 'ZfcRbac\Factory\GuardsFactory',

/* Factories that map to a class */
Expand Down
10 changes: 0 additions & 10 deletions config/zfc_rbac.global.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,6 @@ return [
// 'previous_uri_query_key' => 'redirectTo'
],

/**
* Cache options used by role and permission providers
*
* You can either specify a string key (the cache will then be fetched from service locator) or use
* a StorageFactory compliant config array
*
* @see http://framework.zend.com/manual/2.2/en/modules/zend.cache.storage.adapter.html#quick-start
*/
// 'cache' => []|string,

/**
* Various plugin managers for guards, role providers and permission providers. Each of them must
* follow a common plugin manager config format, and can be used to create your custom objects
Expand Down
72 changes: 20 additions & 52 deletions docs/07. Cookbook.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@ any other recipe you'd like to add, please open an issue!

## A Real World Application

In this example we are going to create a very little real world application. We will create a controller `PostController` that interacts with a service called
`PostService`. For the sake of simplicity we will only cover the `delete`-methods of both parts.
In this example we are going to create a very little real world application. We will create a controller `PostController` that interacts with a service called
`PostService`. For the sake of simplicity we will only cover the `delete`-methods of both parts.

Let's start by creating a controller that has the `PostService` as dependency:

```php
class PostController
{
protected $postService;

public function __construct(PostService $postService)
{
$this->postService = $postService;
}
// addAction(), editAction(), etc...

// addAction(), editAction(), etc...

public function deleteAction()
{
$id = $this->params()->fromQuery('id');

$this->postService->deletePost($id);

return $this->redirect()->toRoute('posts');
}
}
Expand All @@ -36,10 +36,10 @@ class PostController
Since we have a dependency, let's inject it using the `ControllerManager`, we will do this inside our `Module`-Class

```php
class Module
class Module
{
// getAutoloaderConfig(), getConfig(), etc...

public function getControllerConfig()
{
return [
Expand All @@ -65,7 +65,7 @@ use Doctrine\Common\Persistence\ObjectManager;
class PostService
{
protected $objectManager;

public function __construct(ObjectManager $objectManager)
{
$this->objectManager = $objectManager;
Expand All @@ -83,10 +83,10 @@ class PostService
And for this one, too, let's quickly create the factory, again within our `Module` class.

```php
class Module
class Module
{
// getAutoloaderConfig(), getConfig(), etc...

public function getServiceConfig()
{
return [
Expand All @@ -107,7 +107,7 @@ With this set up we can now cover some best practices.
## Best practices

One mistake most beginners do is protecting their applications using guards only. This leaves your application open for
some undesired side-effects. As a best practice you should protect all your services by injecting the authorization
some undesired side-effects. As a best practice you should protect all your services by injecting the authorization
service. But let's go step by step:

Assuming the application example above we can easily use ZfcRbac to protect our route using the following guard:
Expand Down Expand Up @@ -150,12 +150,12 @@ use Doctrine\Common\Persistence\ObjectManager;
class PostService
{
protected $objectManager;

protected $authorizationService;

public function __construct(
ObjectManager $objectManager,
AuthorizationService $autorizationService
AuthorizationService $autorizationService
) {
$this->objectManager = $objectManager;
$this->authorizationService = $autorizationService;
Expand All @@ -167,7 +167,7 @@ class PostService
if (!$this->authorizationService->isGranted('deletePost')) {
throw UnauthorizedException('You are not allowed !');
}

$post = $this->objectManager->find('Post', $id);
$this->objectManager->remove($post);
$this->objectManager->flush();
Expand All @@ -178,10 +178,10 @@ class PostService
Since we now have an additional dependency we should inject it through our factory, again within our `Module` class.

```php
class Module
class Module
{
// getAutoloaderConfig(), getConfig(), etc...

public function getServiceConfig()
{
return [
Expand Down Expand Up @@ -315,38 +315,6 @@ public function deletePost($id)
}
```

## Optimize for production

ZfcRbac is already quite efficient, but there are some ways to make it even faster.

### Use cache

ZfcRbac allows you to specify a cache for role and permission providers (especially useful for providers that load
data from database).

You can either specify a service key that is fetched from service manager (the returned cache must
implement `Zend\Cache\Storage\StorageInterface`):

```php
return [
'zfc_rbac' => [
'cache' => 'myCache'
]
];
```

Or using a `Zend\Cache\Storage\StorageFactory::factory` compliant config:

```php
return [
'zfc_rbac' => [
'cache' => [
'adapter' => 'apc'
]
]
];
```

## Using ZfcRbac with Doctrine ORM

TBD
Expand Down
7 changes: 3 additions & 4 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ If you are looking for some information that is not listed in the documentation,
1. [A real world example](/docs/07. Cookbook.md#a-real-world-application)
2. [Best practices](/docs/07. Cookbook.md#best-practices)
3. [Using dynamic assertions](/docs/07. Cookbook.md#using-dynamic-assertions)
4. [Optimize for production](/docs/07. Cookbook.md#optimize-for-production)
5. [Using ZfcRbac with Doctrine ORM](/docs/07. Cookbook.md#using-zfcrbac-with-doctrine-orm)
6. [How to lazy-load roles and permissions for very complex use cases?](/docs/07. Cookbook.md#how-to-lazy-load-roles-and-permissions-for-very-complex-use-cases)
7. [Using ZfcRbac and ZF2 Assetic](/docs/07. Cookbook.md#using-zfcrbac-and-zf2-assetic)
4. [Using ZfcRbac with Doctrine ORM](/docs/07. Cookbook.md#using-zfcrbac-with-doctrine-orm)
5. [How to lazy-load roles and permissions for very complex use cases?](/docs/07. Cookbook.md#how-to-lazy-load-roles-and-permissions-for-very-complex-use-cases)
6. [Using ZfcRbac and ZF2 Assetic](/docs/07. Cookbook.md#using-zfcrbac-and-zf2-assetic)
51 changes: 0 additions & 51 deletions src/ZfcRbac/Factory/CacheFactory.php

This file was deleted.

5 changes: 1 addition & 4 deletions src/ZfcRbac/Factory/PermissionLoaderListenerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,12 @@ class PermissionLoaderListenerFactory implements FactoryInterface
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
/* @var \Zend\Cache\Storage\StorageInterface $cacheStorage */
$cacheStorage = $serviceLocator->get('ZfcRbac\Cache');

/* @var \ZfcRbac\Permission\PermissionProviderPluginManager $pluginManager */
$pluginManager = $serviceLocator->get('ZfcRbac\Permission\PermissionProviderPluginManager');

/* @var \ZfcRbac\Permission\PermissionProviderChain $permissionProviderChain */
$permissionProviderChain = $pluginManager->get('ZfcRbac\Permission\PermissionProviderChain');

return new PermissionLoaderListener($permissionProviderChain, $cacheStorage);
return new PermissionLoaderListener($permissionProviderChain);
}
}
5 changes: 1 addition & 4 deletions src/ZfcRbac/Factory/RoleLoaderListenerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,12 @@ class RoleLoaderListenerFactory implements FactoryInterface
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
/* @var \Zend\Cache\Storage\StorageInterface $cacheStorage */
$cacheStorage = $serviceLocator->get('ZfcRbac\Cache');

/* @var \ZfcRbac\Role\RoleProviderPluginManager $pluginManager */
$pluginManager = $serviceLocator->get('ZfcRbac\Role\RoleProviderPluginManager');

/** @var \ZfcRbac\Role\RoleProviderChain $roleProviderChain */
$roleProviderChain = $pluginManager->get('ZfcRbac\Role\RoleProviderChain');

return new RoleLoaderListener($roleProviderChain, $cacheStorage);
return new RoleLoaderListener($roleProviderChain);
}
}
28 changes: 0 additions & 28 deletions src/ZfcRbac/Options/ModuleOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,6 @@ class ModuleOptions extends AbstractOptions
*/
protected $redirectStrategy;

/**
* Either a string fetched from service locator, or a StorageFactory compliant config
*
* @var string|array
*/
protected $cache;

/**
* Constructor
*
Expand Down Expand Up @@ -334,25 +327,4 @@ public function getRedirectStrategy()

return $this->redirectStrategy;
}

/**
* Set the cache config of key
*
* @param array|string $cache
* @return void
*/
public function setCache($cache)
{
$this->cache = $cache;
}

/**
* Get the cache config or key
*
* @return array|string
*/
public function getCache()
{
return $this->cache;
}
}
43 changes: 3 additions & 40 deletions src/ZfcRbac/Permission/PermissionLoaderListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

namespace ZfcRbac\Permission;

use Zend\Cache\Storage\StorageInterface as CacheInterface;
use Zend\EventManager\AbstractListenerAggregate;
use Zend\EventManager\EventManagerInterface;
use ZfcRbac\Service\RbacEvent;
Expand All @@ -33,31 +32,14 @@ class PermissionLoaderListener extends AbstractListenerAggregate
*/
protected $permissionProvider;

/**
* @var CacheInterface
*/
protected $cache;

/**
* @var string
*/
protected $cacheKey;

/**
* Constructor
*
* @param PermissionProviderInterface $permissionProvider
* @param CacheInterface $cache
* @param string $cacheKey
*/
public function __construct(
PermissionProviderInterface $permissionProvider,
CacheInterface $cache,
$cacheKey = 'zfc_rbac_permissions'
) {
public function __construct(PermissionProviderInterface $permissionProvider)
{
$this->permissionProvider = $permissionProvider;
$this->cache = $cache;
$this->cacheKey = (string) $cacheKey;
}

/**
Expand All @@ -77,7 +59,7 @@ public function attach(EventManagerInterface $events)
public function onLoadPermissions(RbacEvent $event)
{
$rbac = $event->getRbac();
$permissions = $this->getPermissions($event);
$permissions = $this->permissionProvider->getPermissions($event);

foreach ($permissions as $key => $value) {
if ($value instanceof PermissionInterface) {
Expand All @@ -97,23 +79,4 @@ public function onLoadPermissions(RbacEvent $event)
}
}
}

/**
* Get the permissions, optionally fetched from cache
*
* @param RbacEvent $event
* @return array|PermissionInterface[]
*/
protected function getPermissions(RbacEvent $event)
{
$success = false;
$result = $this->cache->getItem($this->cacheKey, $success);

if (!$success) {
$result = $this->permissionProvider->getPermissions($event);
$this->cache->setItem($this->cacheKey, $result);
}

return $result;
}
}
Loading

0 comments on commit eeb3ccc

Please sign in to comment.