Skip to content
Advertisement

For each class in directory

I’m facing a question, I’m making a command on Symfony which is supposed to launch different operations (undefined for the moment) so I would like to create a directory, put all operations in different files but in this directory and browse it when the command is launched.

/MyCommand.php
/Verifications/CheckUser.php
/Verifications/CheckEmail.php
/Verifications/...

Do you have a better way to avoid to declare them into MyCommand ? Any design pattern ? How can I get all those class ?

Thanks all!

Advertisement

Answer

Are them command you need to execute in MyCommand?

If you’re on symfony > 3.3 you can take advantage of autoconfigure, assign a custom tag (automatically with autoconfigure) and fetch them as you need (here you can find an example; maybe you’ll not going to need CompilerPass but the concept is the same)

If you’re on symfony < 3.3 you need to tag them explicitly and fetch when you need.

With this methodology you’re not even bounded to file location on your src (or whatever) directory as long as Symfony can discover those services (or as long as you declare them explicitly) and tag them

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement