如何在Symfony中重命名Bundle?

11

我的bundle位于 src/Cinergy/Bundle/ComponentBundle/CinergyComponentBundle.php,bundle的逻辑名称为 'CinergyComponentBundle'

由于我没有遵循公司的命名规范,我必须更改bundle的逻辑名称。假设我要将其重命名为 'XXXCinergyComponentBundle'

我需要更改哪些文件?

我尝试将CinergyComponentBundle.php文件和其中包含的类重命名为XXXCinergyComponentBundle.php。我还更改了AppKernel中的引用。

不幸的是,这并不起作用。当我尝试清除缓存时,我收到此错误消息:

[Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException]                                   
The service "cinergy.people.direct.php" has a dependency on a non-existent service "cinergy.work.registry". 

这两个服务确实属于具有新名称的捆绑包。这使我得出结论,在重命名过程中我漏掉了一些内容。

有任何想法吗?


请查看此视频 - Snopzer
3个回答

9
我刚刚完成了重命名一个bundle的工作,和你几分钟前遇到一样的问题。到目前为止,我使用了编辑器的替换功能。结果发现有些文件需要特别注意,这些文件位于这里(假设你的旧Bundle名称为AcmeOldBundle,新名称为AcmeNewBundle):
  1. Acme\OldBundle\OldBundle.php -> NewBundle.php
  2. Acme\OldBundle\DependencyInjection\AcmeOldExtension.php -> AcmeNewExtension.php
不要忘记在文件内部更新类名。

1
仅重复上面的内容;编辑器/搜索替换不会重命名文件,这很容易被忽视。所以为了确保,请使用以下命令:find . -name '*AcmeOldExtension*' 然后手动重命名文件(可能和上面一样简单)。 - fazy

7

目前的答案确实解释了如何重命名一个bundle,但是它们遗漏了一些内容:

  1. Rename the bundle in /app/AppKernel.php

    From:

    $bundles = [
        // ...
        new Acme\OldBundle\AcmeOldBundle(),
    ];
    

    To:

    $bundles = [
        // ...
        new Acme\NewBundle\AcmeNewBundle(),
    ];
    
  2. Renaming files and folders

    You need to rename the files and folders containing your code:

    src/Acme/OldBundle -> src/Acme/NewBundle
    src/Acme/OldBundle/OldBundle.php -> src/Acme/NewBundle/NewBundle.php
    
    # If present
    src/Acme/OldBundle/DependencyInjection/AcmeOldExtension.php -> src/Acme/NewBundle/DependencyInjection/AcmeNewExtension.php
    
  3. Namespaces

    This will probably go with forgetting a few files and searching around your bundle for each and every match. The best thing to do is doing a find-replace:

    # find -> replace
    acme_old -> acme_new
    Acme\OldBundle\ -> Acme\NewBundle
    AcmeOld -> AcmeNew
    Acme:Old -> Acme:New
    Acme:OldBundle -> Acme:NewBundle
    

    Be sure to check all namespaces, class names and configuration files in your bundle.

  4. Global configuration files

    Search for any occurrence of your old bundle name in app/config/ and replace it with your new bundle name.


3

您需要在每个文件中更新整个命名空间。

您之前使用的是namespace Cynergy\ComponentBundle\Something;,但现在您使用的是namespace XXXCynergy\ComponentBundle\Something;

不要忘记更新您的services.(xml|yml)文件。


据我所知,这是不正确的。我没有改变bundle的路径,我只改变了逻辑名称。 - BetaRide
3
如果你认为不需要,那么你是错误的。由于PSR-0/1/2,你需要将命名空间与文件系统进行映射,或者手动设置自动加载器。 - Boris Guéry
我只是更改一个类的名称和它的 PHP 文件,而不是更改整个命名空间/目录的名称。由于 AppKernel 能够成功找到重命名后的类,我怀疑这不是自动加载器/类/文件名称的问题。 - BetaRide
嗯,您应该检查一下为什么DIC无法加载您的服务。它告诉您该服务不存在,那为什么不存在呢?它在服务配置中是否正确定义?该服务是否确实存在? - Boris Guéry

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接