Symfony 4.4, I want to keep some of my classes as "simple/pure" PHP class = no service and use just "new MyClass(..)" to initialize it inside my code. What's more important, I want to have constructor with my own parameters and prevent DI to inject parameters automatically (even if NULL is allowed). In other word, keep autowire+DI everywhere, but exclude selected classes.
Problem is, when I have such a class, it's called automatically everytime I run "php bin/console ...". I don't want such a behavior and I want to prevent running constructor of MyClass or any other pure PHP class when running bin/console.
Pure PHP MyClass inside of "src/Classes/" folder:
<?php
namespace App\Classes;
use App\Entity\MyEntity;
class MyClass {
public function __construct(?MyEntity $myEntity) {
...
}
...
}
It's initialized in both, Service and Command as:
$this->myClass = new MyClass($myEntity);
I also configured exclusion in "services.yaml":
services:
_defaults:
autowire: true
autoconfigure: true
App\:
resource: '../src/'
exclude: [
'../src/Classes/'
, '../src/DependencyInjection/'
, '../src/Entity/'
, '../src/Tests/'
, '../src/Kernel.php'
]
My case is very similar as discussed here: https://github.com/symfony/symfony/issues/25946
But I'm not initializing my "pure" PHP class in Command constructor. I'm using "new" keyword later inside my code. Here is my Command example:
<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Doctrine\ORM\EntityManagerInterface;
use App\Classes\MyClass;
class MyCommand extends Command {
private $kernel;
private $em;
public function __construct(KernelInterface $kernel, EntityManagerInterface $em) {
parent::__construct();
$this->krenel = $kernel;
$this->em = $em;
}
protected function configure() {
$this->setName('app:myCommand');
}
protected function execute(InputInterface $input, OutputInterface $output) {
...
$this->myClass = new MyClass($myEntity);
...
}
}
How to solve? Thank you.