Skip to content

Commit

Permalink
Merge pull request #3 from rbroen/master
Browse files Browse the repository at this point in the history
Added Mezzio support.
  • Loading branch information
Dwarfex committed Jan 16, 2021
2 parents 2218b5f + a2460f4 commit 270a69e
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 5 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Laminas & Doctrine Encrypt Module

Provides a Laminas & Doctrine 2 encryption module.
Provides a Laminas & Doctrine 2 encryption module. With support for Mezzio.

# Installation

Expand Down Expand Up @@ -37,6 +37,22 @@ If these are filled in, it works out of the box using [Halite](https://github.co
However, must be said, at the moment of writing this ReadMe, the Halite module contains duplicate `const` declarations,
as such, you must disable your `E_NOTICE` warnings in your PHP config :(

## Mezzio

When using Mezzio, you will want to add the ConfigProvider to your `config/config.php` file.
```
\Keet\Encrypt\ConfigProvider::class,
```

When declaring the path to your entities, be sure to pass the path(s) as an array.
```
'my_entity' => [
'class' => AnnotationDriver::class,
'cache' => 'array',
'paths' => [ __DIR__ . '/Entity' ],
],
```

## Annotation Examples

### Encryption
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"ext-sodium": "*",
"doctrine/annotations": "^1.6",
"doctrine/orm": "^2.6",
"doctrine/doctrine-module": "^4.1.1",
"paragonie/halite": "^4.4",
"paragonie/hidden-string": "^1.0",
"laminas/laminas-modulemanager": "^2.8",
Expand Down
103 changes: 103 additions & 0 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php declare(strict_types=1);

namespace Keet\Encrypt;

use Doctrine\Common\Annotations\AnnotationReader;
use Keet\Encrypt\Adapter\EncryptionAdapter;
use Keet\Encrypt\Adapter\HashingAdapter;
use Keet\Encrypt\Factory\Adapter\EncryptionAdapterFactory;
use Keet\Encrypt\Factory\Adapter\HashingAdapterFactory;
use Keet\Encrypt\Factory\Service\EncryptionServiceFactory;
use Keet\Encrypt\Factory\Service\HashingServiceFactory;
use Keet\Encrypt\Factory\Subscriber\EncryptionSubscriberFactory;
use Keet\Encrypt\Factory\Subscriber\HashingSubscriberFactory;
use Keet\Encrypt\Service\EncryptionService;
use Keet\Encrypt\Service\HashingService;

/**
* Config provider for Laminas Doctrine Encrypt config
*/
class ConfigProvider
{
/**
* @return mixed[]
*/
public function __invoke(): array
{
return [
'doctrine_factories' => $this->getDoctrineFactoryConfig(),
'doctrine' => $this->getDoctrine(),
'dependencies' => $this->getDependencies(),
];
}

/**
* Factory mappings - used to define which factory to use to instantiate a particular doctrine service type
*
* @return mixed[]
*/
public function getDoctrineFactoryConfig(): array
{
return [
'encryption' => EncryptionSubscriberFactory::class,
'hashing' => HashingSubscriberFactory::class,
];
}

/**
* Default configuration for Doctrine module
*
* Notice that the Doctrine event manager has key 'event_manager'
*
* @return mixed[]
*/
public function getDoctrine(): array
{
return [
'encryption' => [
'orm_default' => [
'adapter' => 'encryption_adapter',
'reader' => AnnotationReader::class,
],
],
'hashing' => [
'orm_default' => [
'adapter' => 'hashing_adapter',
'reader' => AnnotationReader::class,
],
],
'event_manager' => [
'orm_default' => [
'subscribers' => [
'doctrine.encryption.orm_default',
'doctrine.hashing.orm_default',
],
],
],
];
}

/**
* Return application-level dependency configuration
*
* @return mixed[]
*/
public function getDependencies(): array
{
return [
'aliases' => [
// Using aliases so someone else can use own adapter/factory
'encryption_adapter' => EncryptionAdapter::class,
'encryption_service' => EncryptionService::class,
'hashing_adapter' => HashingAdapter::class,
'hashing_service' => HashingService::class,
],
'factories' => [
EncryptionAdapter::class => EncryptionAdapterFactory::class,
EncryptionService::class => EncryptionServiceFactory::class,
HashingAdapter::class => HashingAdapterFactory::class,
HashingService::class => HashingServiceFactory::class,
],
];
}
}
2 changes: 1 addition & 1 deletion src/Factory/Service/EncryptionServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class EncryptionServiceFactory implements FactoryInterface
*/
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$config = $container->get('Config');
$config = $container->get('config');

if ( ! isset($config['doctrine']['encryption']['orm_default'])) {
throw new Exception(
Expand Down
2 changes: 1 addition & 1 deletion src/Factory/Service/HashingServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class HashingServiceFactory implements FactoryInterface
*/
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$config = $container->get('Config');
$config = $container->get('config');

if ( ! isset($config['doctrine']['hashing']['orm_default'])) {
throw new Exception(
Expand Down
2 changes: 1 addition & 1 deletion src/Factory/Subscriber/EncryptionSubscriberFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o
*
* @return string
*/
public function getOptionsClass()
public function getOptionsClass(): string
{
return EncryptionOptions::class;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Factory/Subscriber/HashingSubscriberFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o
*
* @return string
*/
public function getOptionsClass()
public function getOptionsClass(): string
{
return HashingOptions::class;
}
Expand Down

0 comments on commit 270a69e

Please sign in to comment.