How to rename a bundle in symfony?

14,595

Solution 1

I have just completed renaming a bundle myself and ended up at the exact same point as you did a couple of minutes ago. To this point I have used my editors replace-function. It turns out there are some files that need some extra care, which are located here (assuming your old Bundle name was AcmeOldBundle, new will be AcmeNewBundle):

  1. Acme\OldBundle\OldBundle.php -> NewBundle.php
  2. Acme\OldBundle\DependencyInjection\AcmeOldExtension.php -> AcmeNewExtension.php

Do not forget to update the class-names inside the files as well.

Solution 2

The current answers do indeed explain what to do to rename a bundle but they leave out a few things:

  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.

Solution 3

You need to update the entire namespace in each files.

You were using namespace Cynergy\ComponentBundle\Something; but you're now using namespace XXXCynergy\ComponentBundle\Something;

Don't forget to update your services.(xml|yml).

Share:
14,595
BetaRide
Author by

BetaRide

Developer, maker, youtuber. Visit my youtube channel: https://www.youtube.com/channel/UCN8wBEouFtaAyIjjIH2LjJQ

Updated on June 04, 2022

Comments

  • BetaRide
    BetaRide almost 2 years

    My bundle is located at src/Cinergy/Bundle/ComponentBundle/CinergyComponentBundle.php and the logical name of the bundle is 'CinergyComponentBundle'.

    Since I did not stick to the naming convention of the company I have to change the logcial name of the bundle. Lets asume I have to rename it to 'XXXCinergyComponentBundle'

    What files do I need to change?

    I tried renaming the CinergyComponentBundle.php file and the containing class into XXXCinergyComponentBundle.php. I also changed the reference in AppKernel.

    Unfortunately this does not work. When I try to clear the cache I get this error message:

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

    This two services do belong to the bundle with the new name. This leads me to the conclusion that I'm missing something in the renaming process.

    Any ideas?

  • BetaRide
    BetaRide over 11 years
    As far as I understand, this is not correct. I'm not changing the path of the bundle. I'm only changing the logical name.
  • Boris Guéry
    Boris Guéry over 11 years
    Then you're wrong, because of PSR-0/1/2 you need to map your namespace with the filesystem, or manually set the autoloader.
  • BetaRide
    BetaRide over 11 years
    I'm changing the name of one class and it's PHP file only. I'm not changing the name of an entire namespace/directory. Since AppKernel happyly finds the renamed class, I doubt that there's an autoloader/class/file name issue.
  • Boris Guéry
    Boris Guéry over 11 years
    Mmm. You should really check why the DIC can't load your service. It tells you that it doesn't exist, why doesn't it exist? Is it correctly defined in your services configuration? Does the service actually exist?
  • fazy
    fazy about 11 years
    Just echoing the above; editor/search replace won't rename the files, which are easily overlooked. So to be sure, use this: find . -name '*AcmeOldExtension*' and then manually rename the files (which might just be as above).