Load all controller actions, which have been assigned a route in Symfony (see this and this).
Then load the annotations for every found controller action:
use Doctrine\Common\Annotations\AnnotationReader;
use Symfony\Component\HttpFoundation\Request;
$annotationReader = new AnnotationReader();
$routes = $this->container->get('router')->getRouteCollection()->all();
$this->container->set('request', new Request(), 'request');
foreach ($routes as $route => $param) {
$defaults = $params->getDefaults();
if (isset($defaults['_controller'])) {
list($controllerService, $controllerMethod) = explode(':', $defaults['_controller']);
$controllerObject = $this->container->get($controllerService);
$reflectedMethod = new \ReflectionMethod($controllerObject, $controllerMethod);
// the annotations
$annotations = $annotationReader->getMethodAnnotations($reflectedMethod );
}
}
UPDATE:
If you need all controller methods, including those without the @Route
annotation, then I would do what you suggest in your question:
// Load all registered bundles
$bundles = $this->container->getParameter('kernel.bundles');
foreach ($bundles as $name => $class) {
// Check these are really your bundles, not the vendor bundles
$bundlePrefix = 'MyBundle';
if (substr($name, 0, strlen($bundlePrefix)) != $bundlePrefix) continue;
$namespaceParts = explode('\\', $class);
// remove class name
array_pop($namespaceParts);
$bundleNamespace = implode('\\', $namespaceParts);
$rootPath = $this->container->get('kernel')->getRootDir().'/../src/';
$controllerDir = $rootPath.$bundleNamespace.'/Controller';
$files = scandir($controllerDir);
foreach ($files as $file) {
list($filename, $ext) = explode('.', $file);
if ($ext != 'php') continue;
$class = $bundleNamespace.'\\Controller\\'.$filename;
$reflectedClass = new \ReflectionClass($class);
foreach ($reflectedClass->getMethods() as $reflectedMethod) {
// the annotations
$annotations = $annotationReader->getMethodAnnotations($reflectedMethod);
}
}
}