2014-12-28 23 views
0

我创建了一个名为@Module的注释类和一个命令GenerateModulesCommand。我想要的是找到具有@Module注释的所有控制器操作。检索具有特定注释的所有控制器操作 - Symfony2

例子:

/** 
* 
* @Module(name='sidebar', enabled=true') 
*/ 
public function sidebarAction($name) { 
    $ape = new ArrayParamsExtension(); 
    return $this->render('ModuleManagerBundle:Default:sidebar.html.twig', $ape->getArrayParams($name)); 
} 

我希望能够看在@Module特定的属性(名称,启用,等等) 到目前为止,这是我从我的命令执行方法:

protected function execute(InputInterface $input, OutputInterface $output) { 
    $path = $this->getApplication()->getKernel()->locateResource('@ModuleManagerBundle'); 
    $driver = new PHPDriver($path); 
    $classes = $driver->getAllClassNames(); 

    foreach ($classes as $key => $class) { 
     $reader = new AnnotationReader(); 

     $annotationReader = new CachedReader(
       $reader, new ArrayCache() 
     ); 

     $reflClass = new ReflectionClass("\Controller\\" . $reportableClass); 
     $annotation = $annotationReader->getClassAnnotation(
       $reflClass, 'Custom_Annotation' 
     ); 
     if (is_null($annotation)) { 
      unset($classes[$key]); 
     } 
    } 


    $output->writeln($path); 
} 

我发现这个SOF代码,但我不知道如何搜索所有控制器类和内他们所有的动作..

回答

1

您可以在FUNC试试这个重刑

use Doctrine\Common\Annotations\AnnotationReader; 

你的函数

public function getControllersWithAnnotationModules() 
     { 
      $allAnnotations = new AnnotationReader(); 
      $controllers = array(); 
      foreach ($this->container->get('router')->getRouteCollection()->all() as $route) { 
       $defaults = $route->getDefaults(); 
       if (isset($defaults['_controller'])) { 
        $controllerAction = explode(':', $defaults['_controller']); 
        $controller = $controllerAction[0]; 
        if (!isset($controllers[$controller]) && class_exists($controller)) { 
         $controllers[$controller] = $controller; 
        } 


       } 
      } 
      $controllersWithModules = array(); 
      foreach($controllers as $controller){ 

       $reflectionClass = new \ReflectionClass($controller); 
       $module = $allAnnotations->getClassAnnotation($reflectionClass,'Acme\YourBundle\Module'); 
       if($module) 
        $controllersWithModules[] = $controller; 

      } 
      return $controllersWithModules ; 

     } 
+0

对不起,但我可以问你如何使用这个函数来获取“操作”有注释@Module?例如:indexAction等... –

+1

你可以在你的控制器中添加这个,不要忘记“使用”,然后用你的@module的命名空间替换这个“Acme \ YourBundle \ Module”,最后在你的indexAction中你可以调用这个函数$这个 - > getControllersWithAnnotationModules() –

相关问题