php的学说是独立的

php的学说是独立的

本文介绍了没有cli-config.php的学说是独立的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将Doctrine ORM整合到我的(非Symfony)项目中。我已经在另一个项目中完成了这个工作,并将着名的 cli-config.php 导入项目根目录。



但是现在,在我的新项目中,我使用Symfony控制台组件和依赖注入组件(通过标记它们来引用服务和命令)。



1 我绝对不想在项目根目录中有一个 cli-config.php 。 Sf教义捆绑如何做到这一点?



2。另外(但不重要),我希望将Doctrine命令我的项目CLI。
最好的办法是做什么?在我的 services.yml 中创建对Doctrine命令的引用?或者创建通过PHP调用Doctrine命令的本地装饰器命令?

解决方案

最后,经过一些搜索和实验,完整的解决方案。



只需阅读 vendor / bin 中的doctrine.php。很容易避免硬编码的 config-cli.php 文件。



1。创建一个实体经理



在我的情况下,我使用一个工厂,这种方法使 doctrine.em 服务水合。



$ config 特定于我的应用程序,更改值以使用您自己的逻辑。)

 使用Doctrine\ORM\Tools\Setup; 
使用Doctrine\ORM\EntityManager;

public function createEntityManager()
{
$ config = $ this-> get('config');

$ metadataConfig = Setup :: createAnnotationMetadataConfiguration(
[$ config-> meta('app_path')。'/ Entity'],
$ config-> oc- > doctrine-> dev_mode
);

返回EntityManager :: create((array)$ config-> oc-> doctrine->连接,$ metadataConfig);
}



2。您的CLI命令中的Merge Doctrine CLI命令



在您的代码中,像某些 bootstrap.php 声明您的 Symfony\Component\Console\Application 命令行界面,这就是我这样做( foreach 只需添加我的 services.yml 文件中定义的命令):

  $ application = new Application('MyApp CLI','0.0.1'); 

$ services = $ container-> findTaggedServiceIds('oc.command');

foreach(array_keys($ services)as $ serviceId)
{
$ application-> add($ container-> get($ serviceId));
}

$ application-> run();

现在,我们只是要求Doctrine将其命令注入到我们的应用程序中:

  $ application = new Application('MyApp CLI','0.0.1'); 

$ helperSet = ConsoleRunner :: createHelperSet($ container-> get('doctrine.em'));
$ application-> setHelperSet($ helperSet);

ConsoleRunner :: addCommands($ application);

$ services = $ container-> findTaggedServiceIds('oc.command');

foreach(array_keys($ services)as $ serviceId)
{
$ application-> add($ container-> get($ serviceId));
}

$ application-> run();

就是这样!您也可以通过使用。



3。奖励:仅导入所需的命令并重命名它们



您可以创建继承Doctrine命令的decorator命令(这对于重新定义Doctrine命令的名称很有用,因为Symfony Doctrine Bundle ,例如 orm:validate-schema - > doctrine:schema:validate )。



要执行此操作,请删除步骤2中添加的 ConsoleRunner :: addCommands($ application); 。对于每个要重新定义的命令,您将需要在应用程序中创建一个新的命令。该命令将 extends 目标Doctrine命令并覆盖 configure()方法。



以下是 orm:validate-schema 的示例:

 <?php 

命名空间MyApp\Command\Doctrine;

使用Doctrine\ORM\Tools\Console\Command\ValidateSchemaCommand;

class SchemaValidateCommand extends ValidateSchemaCommand
{
protected function configure()
{
parent :: configure();
$ this-> setName('doctrine:schema:validate');
}
}

一些Doctrine命令有别名会污染您的命令名称空间,如 orm:generate-entities orm:generate:entities
要删除这些别名,请在 configure()中添加 - > setAliases(array())

  $ this-> setName('doctrine:generate:entities') - > setAliases([]);恭喜,您只需重新修改Symfony Doctrine Bundle:p(jk)

$ b $($)
I want to integrate Doctrine ORM into my (non-Symfony) project. I already done this in another project and used the famous cli-config.php into the project root directory.

But now, in my new project, I use the Symfony Console component and the Dependency Injection component (to reference services and commands, by tagging them).

1. I absolutely don't want to have a cli-config.php in the project root. How the Sf Doctrine Bundle do this?

2. Also (but it is less important), I would like to have the Doctrine commands into my project CLI. What would be the best way to do this? Create references to Doctrine commands into my services.yml ? Or create local "decorator commands" that call Doctrine commands via PHP?

解决方案

Finally, after some googling and experiments, I found a complete solution.

Just read the doctrine.php in vendor/bin. It is very easy to avoid the hardcoded config-cli.php file.

1. Create an entity manager

In my case, I use a factory and this method hydrates the doctrine.em service.

($config is specific to my app, change values to use your own logic.)

use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;

public function createEntityManager()
{
    $config = $this->get('config');

    $metadataConfig = Setup::createAnnotationMetadataConfiguration(
        [$config->meta('app_path') . '/Entity'],
        $config->oc->doctrine->dev_mode
    );

    return EntityManager::create((array) $config->oc->doctrine->connection, $metadataConfig);
}

2. Merge Doctrine CLI commands in your CLI commands

Somewere in your code, like in some bootstrap.php, you probably declare your Symfony\Component\Console\Application command line interface, that's how I do this (the foreach simply adds commands defined in my services.yml file):

$application = new Application('MyApp CLI', '0.0.1');

$services = $container->findTaggedServiceIds('oc.command');

foreach(array_keys($services) as $serviceId)
{
    $application->add($container->get($serviceId));
}

$application->run();

Now, we simply ask Doctrine to inject its commands into our Application:

$application = new Application('MyApp CLI', '0.0.1');

$helperSet = ConsoleRunner::createHelperSet($container->get('doctrine.em'));
$application->setHelperSet($helperSet);

ConsoleRunner::addCommands($application);

$services = $container->findTaggedServiceIds('oc.command');

foreach(array_keys($services) as $serviceId)
{
    $application->add($container->get($serviceId));
}

$application->run();

That's it! You can also only add a subset of the Doctrine commands by using arsfeld's answer on this GitHub issue.

3. Bonus: only import needed commands and rename them

You can create decorator commands that inherit Doctrine commands (this is useful to redefine the name of Doctrine commands, as Symfony Doctrine Bundle does, eg. orm:validate-schema -> doctrine:schema:validate).

To do this, remove the line ConsoleRunner::addCommands($application); we added in step 2. For each command you want to redefine, you will need to create an register a new command in your app. This command will "extends" the target Doctrine command and will override the configure() method.

Here is an example with orm:validate-schema:

<?php

namespace MyApp\Command\Doctrine;

use Doctrine\ORM\Tools\Console\Command\ValidateSchemaCommand;

class SchemaValidateCommand extends ValidateSchemaCommand
{
    protected function configure()
    {
        parent::configure();
        $this->setName('doctrine:schema:validate');
    }
}

Some Doctrine commands have aliases that will pollute your command namespaces, like orm:generate-entities and orm:generate:entities.To remove these aliases, in configure(), add ->setAliases(array()).

$this->setName('doctrine:generate:entities')->setAliases([]);

Congratulations, you just redone the Symfony Doctrine Bundle :p (jk)

这篇关于没有cli-config.php的学说是独立的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 05:00