I created a new symfony 5.3 project. I'm developing this app using hexagonal architecture.
I added a Bundle I developed for another symfony project. This bundle is working in the original project. But in the new project I get the following error:
!! Fatal error: Cannot declare class UserBundle\Infrastructure\DependencyInjection\MycompanyUserExtension, because the name is already in use in /var/www/html/src/Mycompany/UserBundle/Infrastructure/DependencyInjection/MycompanyUserExtension.php on line 11 !! Symfony\Component\ErrorHandler\Error\FatalError {#419 !! -error: array:4 [ !! "type" => 64 !! "message" => "Cannot declare class UserBundle\Infrastructure\DependencyInjection\MycompanyUserExtension, because the name is already in use" !! "file" => "/var/www/html/src/Mycompany/UserBundle/Infrastructure/DependencyInjection/MycompanyUserExtension.php" !! "line" => 11 !! ] !! #message: "Compile Error: Cannot declare class UserBundle\Infrastructure\DependencyInjection\MycompanyUserExtension, because the name is already in use" !! #code: 0 !! #file: "./src/Mycompany/UserBundle/Infrastructure/DependencyInjection/MycompanyUserExtension.php" !! #line: 11 !! }
the folder structure is:
src
|- Mycompany
|- UserBundle
|- MycompanyUserBundle.php
|- Domain
|- Infrastructure
|- DependencyInjection
| |- MycompanyUserExtension.php
|- Resources
|- config
|- routes.yaml
|- services.yaml
And the code of MycompanyUserBundle is:
# src/Mycompany/UserBundle/MycompanyUserBundle.php
namespace UserBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use UserBundle\Infrastructure\DependencyInjection\MycompanyUserExtension;
class MycompanyUserBundle extends Bundle
{
public function getContainerExtension()
{
if (null === $this->extension) {
$this->extension = new MycompanyUserExtension();
}
return $this->extension;
}
}
And the code of MycompanyUserExtension is:
# src/Mycompany/UserBundle/Infrastructure/DependencyInjection/MycompanyUserExtension.php
namespace UserBundle\Infrastructure\DependencyInjection;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
class MycompanyUserExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yaml');
}
public function getAlias()
{
return 'user_bundle';
}
}
The namespace UserBundle is declared in the composer.json
"autoload": {
"psr-4": {
"UserBundle\\": "src/Mycompany/UserBundle",
"App\\": "src/"
}
},
And the bundle is also declared in the config/bundles.php as UserBundle\MycompanyUserBundle::class => ['all' => true],
The bundle is also referenced in config/packages/doctrine.yaml under mappings
UserBundle:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Mycompany/UserBundle/Domain/Entity'
prefix: 'UserBundle\Domain\Entity'
alias: UserBundle
The MycompanyUserExtension
is declared only in the same file the error says it is in use, I do not find any duplicated declaration of this class. But the project is not compiling because there is an error. It's a fresh new project without any code or bundle yet.