ReSpawN
ReSpawN

Reputation: 689

Symfony3 - creating a vendor-based bundle

We'd like to create a bundle which can deployed via composer/packagist for others to use. It'll wrap the logic created by the owners of MessageBird. Basically a kind of Service which will indeed be called with the container via ourvendor.messagebird.messaging.

Since it's a type of bundle (as per the docs of Sf3), we created a bundle while following the documentation: http://symfony.com/doc/current/bundles/SensioGeneratorBundle/commands/generate_bundle.html

As the directory /src we used /vendor instead. That's when it all went wrong. Our namespace could not be located, loaded or even when we manually added it to the autoloading classes of Composer it failed all the same.

The question is, what is the best practice to go about this? We got it working right now and what we did was the following:

  1. We created a bundle wit the following cmd:
    • bin/console generate:bundle --shared --namespace=OurVendor/MessageBird/MessageBirdBundle --bundle-name=MessageBirdBundle --format=yml
  2. We moved the /src/OurVendor directory to /vendor/OurVendor as the only way to get a perfect generation was to use the default /src folder.
  3. We manually updated the AppKernel.php
  4. We did some debugging with namespaces for Composer but eventually we added "OurVendor\\":"vendor/" to the "autoload/psr-4" directive in root composer.json
  5. We ran composer dumpautoload && bin/console cache:clear -e dev which resulted in an error.
  6. We ran composer -o update which checked all dependencies and updated accordingly, including autogenerated autoload files
  7. Strangely enough we had to add the Bundle to the AppKernel.php class and cleaned the cache again.

After all this it worked but the documentation said no such thing about developing a 3rd party vendor bundle. http://symfony.com/doc/current/bundles/best_practices.html

So long story short, did we go about it the wrong way or what?

Upvotes: 2

Views: 966

Answers (1)

Alex Blex
Alex Blex

Reputation: 37048

/vendor directory is managed by composer. Do not copy/move anything there. Don't even edit anything there, unless you understand all consequences.

When you create a shared bundle, you need to push it to a VCS of your choice, and add it as a dependency in composer.json of the project which uses it.

When you run composer update it will check-out your bundle into /vendor directory and generate correct autoload file.

Please read more how to use private repositories with composer.

Upvotes: 1

Related Questions