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:
We created a bundle wit the following cmd:
bin/console generate:bundle --shared --namespace=OurVendor/MessageBird/MessageBirdBundle --bundle-name=MessageBirdBundle --format=yml
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.
We manually updated the AppKernel.php
We did some debugging with namespaces for Composer but eventually we added "OurVendor\\":"vendor/" to the "autoload/psr-4" directive in root composer.json
We ran composer dumpautoload && bin/console cache:clear -e dev which resulted in an error.
We ran composer -o update which checked all dependencies and updated accordingly, including autogenerated autoload files
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?
/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.
Related
Scenario
In order to provide a re-usable theme to downstream projects, I'm building a skeleton package so that I can install it and find-replace various elements to customize it for my projects.
For example, I'm building a project called "example_project", and I include the skeleton into my project, then find-and-replace all "SKELETON_blah" placeholders with "example_project_blah".
Method
I've defined an upstream package, and am grabbing it into my project using composer create-project example/skeleton example_root/path/to/example_project_blah.
I'm using a post-create-project-cmd script to do the placeholder replacement.
Problem
I can't find a straightforward way to find the project that was created, specifically I want the path example_root/path/to/example_project_blah.
Composer\Script\Event::getArguments() returns an empty array.
Do I have to go to the ARGV global to get this? Is there a way to get it from the composer event or something more reasonable?
You can use the following script to rename the original package name with the newly created project's directory name:
"post-create-project-cmd": [
"sed -i -e \"s/original-name/${PWD##*/}/g\" composer.json .gitignore"
]
This example would search & replace inside the composer.json and .gitignore files, repacing original-name with the project directory (while also keeping the same package namespace).
When I tried adding a new dependency to my project with composer require xyz I got the following error:
The autoloader expected class "PackageVersions\Versions" to be defined in file ".../vendor/ocramius/package-versions/src/PackageVersions/Versions.php".
The file was found but the class was not in it, the class name or namespace probably has a typo.
I dug into that file, to see that the Versionsclass is there, in the right file, but with a the following name:
Versions_composer_tmp0
The namespace declarations seem to be good in the entire project, as well as php opening tags (I read that might cause such problems).
Additionally, I remarked that all the use statements in the Installer class file, which is the one that creates the Versions class are marked by phpstorm as Undefined Classes. They all should be found in the namespace Composer\xyz.
I tried the following without succes:
clearing the symfony cache
clearing composer cache
composer self-update
deleting the ocramius vendor folder so that composer would download it again
renaming the class, which is pointless since the entire purpose of this Versions class is to be rewritten with each composer install or composer update
edit:
I'm trying to install 1up-lab/OneupUploaderBundle, the Ocramius/PackageVersionsis already there as a dependency probably (I did not require it manually)
edit 2:
I just saw that server:run won't work either. So the problem is definitely not related to the bundle I'm trying to install. I managed to make the server run by renaming the class from Versions_composer_tmp0 to Versions.
It turns out that this is a composer issue:
composer/composer#5237
Ocramius released a fix/workaround for this:
Ocramius/PackageVersions - Release 1.0.4
Can't find a solution for this on stack.
I'm using ubuntu server,
$php app/console generate:bundle --namespace=WMDN/FirstBundle --format=yml
goes fine until :
Generating the bundle code: OK
Checking that the bundle is autoloaded: FAILED
Confirm automatic update of your Kernel [yes]? yes
Enabling the bundle inside the Kernel: OK
Confirm automatic update of the Routing [yes]? yes
Importing the bundle routing resource: OK
The command was not able to configure everything automatically.
You must do the following changes manually.
- Edit the composer.json file and register the bundle
namespace in the "autoload" section:
I thought it was permissions, and set the whole dir to 775 and made sure owner was good.
I thought it was outdated bug for symfony and deleted whole dir, and got 2.4.4
I'm following a tutorial and their bundle generates just fine and I'd like mine to do it as well.
Why can it not autoload? My Namespace should be good to use. I cannot figure it out.
I have composer installed.
Following this tutorial series : http://www.youtube.com/watch?v=GIcY5YYfZ14 and I'd really like to get my first bundle created so I may continue.
In conclusion, what are the reasons a bundle would fail autoload?
Per to comments, solution was to set the --src parameter correctly.
So my question is how to delete bundle I created?
You create bundles with this console command:
php app/console generate:bundle --namespace=Test/BlogBundle --format=yml
And thats awsome but what if I need to delete this bundle?
Is there a console command to delete a bundle I dont need any more?
I know that when you create new bundle from console, you:
1. create /src/Test/BlogBundle directory
2. change /app/config/routing.yml file to include routes
3. include your new bundle in /app/Resources/App.Kernel.php
4. I think there is something changed in /app/cache/...
Now what would be correct way of deleting a bundle completely?
Its joust that using console these bundles are generated "magically" so I dont know what did this command changed in folder structure and files?
It is basically the process you have outlined, only in somewhat different order.
delete /src/Test/BlogBundle directory
change /app/config/routing.yml file to remove the bundle routes
remove your new bundle from /app/AppKernel.php
clear cache (either by deleting cache/{$env} or console cache:clear)
If this wasn't installed using a dependency manager - that should be all.
I know I am late to answer this but Symfony has instructions on how to delete the bundle. This is how I delete. You can use the same instructions for other bundles you created and want to remove now.
To delete a bundle in Symfony 3 (and higher) :
Method 1 :
go to composer.json, search & delete the bundle.
run composer update ( it will automatically remove the bundle and clear the cache)
Method 2 :
run composer remove alias/to/your/bundle
f.e : composer remove nesbot/carbon
That's all.
I have installed composer from this link, but I still could not find where do I need to place the composer.json file.
In the root of your project. Because you mentioned symfony, just have a look at the composer.json from the symfony-standard edition
https://github.com/symfony/symfony-standard/
Or even better: Follow the official instructions and use symfony-standard as starting point for your project directly.
Start by visiting the Symfony2 download page at http://symfony.com/download. On this page, you'll see the Symfony Standard Edition, which is the main Symfony2 distribution.
You can forkt it directly from github.
I just found on other topic that you can use :
php composer.phar --working-dir=/home/user/folder/ update
You may need to create a file called 'composer.json'
See this document "https://getcomposer.org/doc/00-intro.md#declaring-dependencies"
You could create a composer.json file by using the composer init command whilst in the directory you want it to be created in
composer init
or
path/to/composer.phar init