Vam*_*hna 3 php doctrine config symfony symfony4
我已经设置了一个 symfony 项目。我所有的数据库连接都在app/.env
.
这是我在 .env 文件中配置的方式:
DATABASE_URL=mysql://root@127.1.0.1:3306/abcdefg
Run Code Online (Sandbox Code Playgroud)
现在我想使用像 config.php 这样的 .php 文件,我可以在其中存储数据库配置的值,并且应用程序也应该使用相同的文件,而不是从 .env 文件中获取值。
这是根据应用程序 URL 连接不同的数据库。所以数据库名称取决于 URL。
为了使其动态化,我想使用 PHP 文件而不是 .env 文件。
(假设您使用的是 Symfony 版本 4 或更高版本 - 但也应该可以在稍作修改的早期版本中工作)
<?php
$container->setParameter('my_param', 'something1');
$elements = [];
$elements[] = 'yolo1';
$elements[] = 'yolo2';
$container->setParameter('my_param_which_is_array', $elements);
Run Code Online (Sandbox Code Playgroud)
imports:
- { resource: my_config.php }
Run Code Online (Sandbox Code Playgroud)
php bin/console debug:container --parameter=my_param
----------- ------------
Parameter Value
----------- ------------
my_param something1
----------- ------------
php bin/console debug:container --parameter=my_param_which_is_array
------------------------- -------------------
Parameter Value
------------------------- -------------------
my_param_which_is_array ["yolo1","yolo2"]
------------------------- -------------------
Run Code Online (Sandbox Code Playgroud)
如果上述步骤有效,那么您可以在应用程序中使用容器中的参数。
重要警告:如果您将安全凭证存储在此类 php 文件中(数据库用户和密码等),请确保您没有将其与应用程序其余部分的代码一起添加到存储库中 - 因此将其添加到“.gitignore”中,类似于“ .env”被添加到那里。
有关处理 symfony 参数的更多信息,请参阅https://symfony.com/doc/current/service_container/parameters.html(在代码片段上单击“PHP”选项卡而不是“YAML”来查看 PHP 示例)
要动态选择数据库连接凭据,我们可以使用学说连接工厂。我们将'doctrine.dbal.connection_factory'
用我们的修改版本来装饰默认服务:
创建新文件“src/Doctrine/MyConnectionFactory.php”:
<?php
namespace App\Doctrine;
use Doctrine\Bundle\DoctrineBundle\ConnectionFactory;
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Configuration;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\HttpFoundation\Request;
class MyConnectionFactory
{
/**
* @var array
*/
private $db_credentials_per_site;
/**
* @var ConnectionFactory
*/
private $originalConnectionFactory;
public function __construct($db_credentials_per_site, ConnectionFactory $originalConnectionFactory)
{
$this->db_credentials_per_site = $db_credentials_per_site;
$this->originalConnectionFactory = $originalConnectionFactory;
}
/**
* Decorates following method:
* @see \Doctrine\Bundle\DoctrineBundle\ConnectionFactory::createConnection
*/
public function createConnection(array $params, Configuration $config = null, EventManager $eventManager = null, array $mappingTypes = [])
{
$siteName = $this->getSiteNameFromRequestOrCommand();
if (!isset($this->db_credentials_per_site[$siteName])) {
throw new \RuntimeException("MyConnectionFactory::createConnection - Unknown site name: {$siteName}");
}
return $this->originalConnectionFactory->createConnection(
[
'url' => $this->db_credentials_per_site[$siteName]['url'],
],
$config,
$eventManager,
$mappingTypes
);
}
/**
* @return string
*/
private function getSiteNameFromRequestOrCommand()
{
// If we are inside CLI command then take site name from '--site' command option:
if (isset($_SERVER['argv'])) {
$input = new ArgvInput();
$siteName = $input->getParameterOption(['--site']);
if (!$siteName) {
throw new \RuntimeException("MyConnectionFactory::getSiteNameFromRequestOrCommand - You must provide option '--site=...'");
}
return (string) $siteName;
}
// Otherwise determine site name by request host (domain):
$request = Request::createFromGlobals();
$host = $request->getHost();
switch ($host) {
case 'my-blue-site.local.dev2':
return 'blue_site';
case 'redsite.local.com':
return 'red_site';
}
throw new \RuntimeException("MyConnectionFactory::getSiteNameFromRequestOrCommand - Unknown host: {$host}");
}
}
Run Code Online (Sandbox Code Playgroud)
现在让我们在 services.yaml 中设置装饰:
(您可以在这里阅读有关服务装饰的更多信息:https://symfony.com/doc/current/service_container/service_decoration.html)
App\Doctrine\MyConnectionFactory:
decorates: doctrine.dbal.connection_factory
arguments:
$db_credentials_per_site: '%db_credentials_per_site%'
Run Code Online (Sandbox Code Playgroud)
并'db_credentials_per_site'
在“config/my_config.php”中添加参数 - 正如您所看到的,它被注入到MyConnectionFactory
上面:
$container->setParameter('db_credentials_per_site', [
'blue_site' => [
'url' => 'mysql://user1:pass1@127.0.0.1:3306/dbname-blue',
],
'red_site' => [
'url' => 'mysql://user2:pass2@127.0.0.1:3306/dbname-red',
],
]);
Run Code Online (Sandbox Code Playgroud)
我们还需要一件事来支持 CLI 命令中的此功能 - 我们需要'--site'
为每个命令添加选项。如您所见,它正在被读入\App\Doctrine\MyConnectionFactory::getSiteNameFromRequestOrCommand
。对于所有使用数据库连接的命令都是强制的:
在 services.yaml 中:
App\EventListener\SiteConsoleCommandListener:
tags:
- { name: kernel.event_listener, event: console.command, method: onKernelCommand, priority: 4096 }
Run Code Online (Sandbox Code Playgroud)
创建新文件“src/EventListener/SiteConsoleCommandListener.php”:
<?php
namespace App\EventListener;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Input\InputOption;
class SiteConsoleCommandListener
{
public function onKernelCommand(ConsoleCommandEvent $event)
{
// Add '--site' option to every command:
$command = $event->getCommand();
$command->addOption('site', null, InputOption::VALUE_OPTIONAL);
}
}
Run Code Online (Sandbox Code Playgroud)
现在我们准备测试它是否有效:
http://my-blue-site.local.dev2/something
,'blue_site'
将使用数据库凭据。http://something.blabla.com/something
,'red_site'
将使用数据库凭据。'blue_site'
将使用数据库凭据:php bin/console app:my-command --site=blue_site
Run Code Online (Sandbox Code Playgroud)
'red_site'
将使用数据库凭据:php bin/console app:my-command --site=red_site
Run Code Online (Sandbox Code Playgroud)