如何在Symfony4中将参数传递给自定义的教义类型 [英] How to pass parameters to a custom Doctrine type in Symfony4
本文介绍了如何在Symfony4中将参数传递给自定义的教义类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我开发了一种新的Doctrine类型来加密字符串。
I have developed a new Doctrine type to encrypt strings.
<?php
namespace App\Doctrine\DBAL\Types;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\StringType;
use App\Security\Encoder\OpenSslEncoder;
class EncryptedStringType extends StringType {
const MTYPE = 'encrypted_string';
private $cypherMethod;
private $iv;
private $privateKey;
public function convertToPHPValue($value, AbstractPlatform $platform)
{
$openSslEncoder = new OpenSslEncoder($this->cypherMethod, $this->iv, $this->privateKey);
return $openSslEncoder->decrypt($value);
}
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
$openSslEncoder = new OpenSslEncoder($this->cypherMethod, $this->iv, $this->privateKey);
return $openSslEncoder->encrypt($value);
}
public function getName()
{
return self::MTYPE;
}
/**
* @param mixed $cypherMethod
*/
public function setCypherMethod($cypherMethod)
{
$this->cypherMethod = $cypherMethod;
}
/**
* @param mixed $iv
*/
public function setIv($iv)
{
$this->iv = $iv;
}
/**
* @param mixed $privateKey
*/
public function setPrivateKey($privateKey)
{
$this->privateKey = $privateKey;
}
}
在旧的Symfony3应用程序中,我注册了新类型
In the old Symfony3 applications, I registered the new type in the following way:
<?php
namespace AppBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Doctrine\DBAL\Types\Type;
class AppBundle extends Bundle
{
public function __construct()
{
Type::addType('encrypted_string', 'AppBundle\Doctrine\DBAL\Types\EncryptedStringType');
}
public function boot()
{
$encryptedString = Type::getType('encrypted_string');
$encryptedString->setCypherMethod($this->container->getParameter('open_ssl_cypher_method'));
$encryptedString->setIv($this->container->getParameter('open_ssl_iv'));
$encryptedString->setPrivateKey($this->container->getParameter('open_ssl_private_key'));
}
}
我如何在新的Symfony4应用程序中执行相同的操作?我知道我可以在doctrine.yaml配置文件中注册一种新类型。但是我需要设置密码参数...如何在新版本中设置对象参数?
How I can do the same in the new Symfony4 applications? I know that I can register a new type in the doctrine.yaml config file. But I need to set the cypher parameters... How I can set the object parameters in the new version?
非常感谢。
推荐答案
Symfony 4 内核
类具有 boot()
方法,其用途类似,因此可以确保将代码在那里移动:
In the same way, Symfony 4 Kernel
class has a boot()
method with similar purpose, so you can move that code there for sure:
// src/Kernel.php
class Kernel extends BaseKernel
{
// ...
public function boot()
{
parent::boot();
// move here.
}
// ...
这篇关于如何在Symfony4中将参数传递给自定义的教义类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文