Skip to content

Commit

Permalink
Add test for dependency-injection using new namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
Zombaya committed Dec 16, 2024
1 parent a4f19bd commit 32d3cc4
Show file tree
Hide file tree
Showing 10 changed files with 565 additions and 54 deletions.
28 changes: 14 additions & 14 deletions src-new/DependencyInjection/ServiceConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace DoctrineEncryptBundle\DoctrineEncryptBundle\DependencyInjection;

use Ambta\DoctrineEncryptBundle\DependencyInjection\DoctrineEncryptExtension as AmbtaExtension;
use Ambta\DoctrineEncryptBundle\DependencyInjection\VersionTester;
use DoctrineEncryptBundle\DoctrineEncryptBundle\DependencyInjection\DoctrineEncryptExtension as BundleExtension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
Expand All @@ -25,11 +24,8 @@ final class ServiceConfigurator
/** @var bool */
private $useNewNames;

/** @var string */
private $prefix;

/** @var int */
private $index;
private $nameIndex;

private const NAMES = [
'parameters' => [
Expand Down Expand Up @@ -99,11 +95,9 @@ public function __construct(
$this->useNewNames = $useNewNames;

if ($this->useNewNames) {
$this->prefix = 'doctrine_encrypt_bundle.';
$this->index = 1;
$this->nameIndex = 1;
} else {
$this->prefix = 'ambta_doctrine_encrypt.';
$this->index = 0;
$this->nameIndex = 0;
}
}

Expand All @@ -114,12 +108,12 @@ private function setParameter(ContainerBuilder $container, string $name, $value)

private function getParameterName(string $name): string
{
return self::NAMES['parameters'][$name][$this->index];
return self::NAMES['parameters'][$name][$this->nameIndex];
}

private function getServiceName(string $name): string
{
return self::NAMES['services'][$name][$this->index];
return self::NAMES['services'][$name][$this->nameIndex];
}

private function registerService(ContainerBuilder $container, string $name, string $oldClass, string $newClass): Definition
Expand Down Expand Up @@ -197,9 +191,14 @@ public function configure(array $config, ContainerBuilder $container): void
trigger_deprecation(
'doctrineencryptbundle/doctrine-encrypt-bundle',
'5.4.2',
<<<EOF
$this->useNewNames
? <<<EOF
Starting from 6.0, all exceptions thrown by this library will be wrapped by \DoctrineEncryptBundle\DoctrineEncryptBundle\Exception\DoctrineEncryptBundleException or a child-class of it.
You can start using these exceptions today by setting 'doctrine_encrypt.wrap_exceptions' to TRUE.
EOF
: <<<EOF
Starting from 6.0, all exceptions thrown by this library will be wrapped by \Ambta\DoctrineEncryptBundle\Exception\DoctrineEncryptBundleException or a child-class of it.
You can start using these exceptions today by setting '{$this->prefix}wrap_exceptions' to TRUE.
You can start using these exceptions today by setting 'ambta_doctrine_encrypt.wrap_exceptions' to TRUE.
EOF
);
}
Expand Down Expand Up @@ -309,6 +308,7 @@ private function defineServicesUsingSecretFactory(ContainerBuilder $container):
->setArguments([
new Parameter($this->getParameterName('secret_directory_path')),
new Parameter($this->getParameterName('enable_secret_generation')),
new Parameter($this->getParameterName('supported_encryptors')),
])
;
}
Expand All @@ -322,7 +322,7 @@ private function defineServicesUsingAnnotations(ContainerBuilder $container): vo
$container,
'orm_subscriber',
\Ambta\DoctrineEncryptBundle\Subscribers\DoctrineEncryptSubscriber::class,
\Ambta\DoctrineEncryptBundle\Subscribers\DoctrineEncryptSubscriber::class
\DoctrineEncryptBundle\DoctrineEncryptBundle\Subscribers\DoctrineEncryptSubscriber::class
)
->setArguments([
new Reference($this->getServiceName('annotation_reader')),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Ambta\DoctrineEncryptBundle\DependencyInjection;
namespace DoctrineEncryptBundle\DoctrineEncryptBundle\DependencyInjection;

use Symfony\Component\HttpKernel\Kernel;

Expand Down
2 changes: 1 addition & 1 deletion src/AmbtaDoctrineEncryptBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Ambta\DoctrineEncryptBundle\DependencyInjection\DeprecatedDoctrineEncryptExtension;
use Ambta\DoctrineEncryptBundle\DependencyInjection\DoctrineEncryptExtension;
use Ambta\DoctrineEncryptBundle\DependencyInjection\VersionTester;
use DoctrineEncryptBundle\DoctrineEncryptBundle\DependencyInjection\VersionTester;
use JetBrains\PhpStorm\Pure;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
Expand Down
6 changes: 3 additions & 3 deletions src/Command/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public function __construct(
array $supportedEncryptors
) {
parent::__construct();
$this->entityManager = $entityManager;
$this->annotationReader = $annotationReader;
$this->subscriber = $subscriber;
$this->entityManager = $entityManager;
$this->annotationReader = $annotationReader;
$this->subscriber = $subscriber;
$this->supportedEncryptors = $supportedEncryptors;
}

Expand Down
1 change: 0 additions & 1 deletion src/Command/DoctrineDecryptDatabaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Ambta\DoctrineEncryptBundle\Command;

use Ambta\DoctrineEncryptBundle\DependencyInjection\DoctrineEncryptExtension;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputArgument;
Expand Down
1 change: 0 additions & 1 deletion src/Command/DoctrineEncryptDatabaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Ambta\DoctrineEncryptBundle\Command;

use Ambta\DoctrineEncryptBundle\DependencyInjection\DoctrineEncryptExtension;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand Down
1 change: 1 addition & 0 deletions src/DependencyInjection/DoctrineEncryptExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Ambta\DoctrineEncryptBundle\Encryptors\DefuseEncryptor;
use Ambta\DoctrineEncryptBundle\Encryptors\HaliteEncryptor;
use DoctrineEncryptBundle\DoctrineEncryptBundle\DependencyInjection\ServiceConfigurator;
use DoctrineEncryptBundle\DoctrineEncryptBundle\DependencyInjection\VersionTester;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;

Expand Down
21 changes: 13 additions & 8 deletions src/Factories/SecretFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Ambta\DoctrineEncryptBundle\Factories;

use Ambta\DoctrineEncryptBundle\Encryptors\DefuseEncryptor;
use Ambta\DoctrineEncryptBundle\Encryptors\HaliteEncryptor;
use ParagonIE\Halite\KeyFactory;
use Symfony\Component\Filesystem\Filesystem;

Expand All @@ -18,10 +16,17 @@ class SecretFactory
*/
private $enableSecretCreation;

public function __construct(string $secretDirectory, bool $enableSecretCreation)
{
/** @var array<string,string> */
private $supportedEncryptors;

public function __construct(
string $secretDirectory,
bool $enableSecretCreation,
array $supportedEncryptors
) {
$this->secretDirectory = $secretDirectory;
$this->enableSecretCreation = $enableSecretCreation;
$this->supportedEncryptors = $supportedEncryptors;
}

/**
Expand All @@ -31,11 +36,11 @@ public function __construct(string $secretDirectory, bool $enableSecretCreation)
*/
public function getSecret(string $className)
{
if (!in_array($className, [DefuseEncryptor::class,HaliteEncryptor::class])) {
if (!in_array($className, $this->supportedEncryptors)) {
throw new \RuntimeException(sprintf('Class "%s" is not supported by %s', $className, self::class));
}

if ($className === HaliteEncryptor::class) {
if (str_contains($className, 'HaliteEncryptor')) {
$filename = '.Halite.key';
} else {
$filename = '.Defuse.key';
Expand Down Expand Up @@ -72,11 +77,11 @@ public function getSecret(string $className)
*/
private function createSecret(string $secretPath, string $className)
{
if ($className === HaliteEncryptor::class) {
if (str_contains($className, 'HaliteEncryptor')) {
$encryptionKey = KeyFactory::generateEncryptionKey();
KeyFactory::save($encryptionKey, $secretPath);
$secret = KeyFactory::export($encryptionKey)->getString();
} elseif ($className === DefuseEncryptor::class) {
} else {
$secret = bin2hex(random_bytes(255));
file_put_contents($secretPath, $secret);
}
Expand Down
Loading

0 comments on commit 32d3cc4

Please sign in to comment.