How to execute methods of all classes that extend an interface in symfony

0

I needed something similar to console doctrine:fixtures:load in relation to class DataFixtureLoader and interface OrderedFixtureInterface ie I want to make a Command where it looks for all classes that have extended the abstract class or interface and run the interface method.

The problem is that I do not know where to look up these classes in Symfony.

How do I list all the classes that extend certain class or interface in Symfony?

    
asked by anonymous 11.04.2015 / 21:05

1 answer

1

I'll give you an answer based on what doctrine/data-fixtures-bundle does, and you can base your system on it.

In the LoadDataFixturesCommand.php file, which is exactly where the doctrine:fixtures:load command resides, it has this code block:

$dirOrFile = $input->getOption('fixtures');
if ($dirOrFile) {
    $paths = is_array($dirOrFile) ? $dirOrFile : array($dirOrFile);
} else {
    $paths = array();
    foreach ($this->getApplication()->getKernel()->getBundles() as $bundle) {
        $paths[] = $bundle->getPath().'/DataFixtures/ORM';
    }
}
$loader = new DataFixturesLoader($this->getContainer());
foreach ($paths as $path) {
    if (is_dir($path)) {
        $loader->loadFromDirectory($path);
    }
}

First, the code checks to see if you've specified any particular fixtures - otherwise, it scans the /DataFixtures/ORM of all bundles directories in your project to fetch all fixtures that are in them. Then the command iterates through all directories to see if they are valid and finally load the fixtures contained in each of them.

What you can do is something like this: set a directory pattern where the files that will run, load them, check if each one implements the desired interface (with the class_implements function) using the method implemented. :)

    
12.04.2015 / 00:27