2014-12-28 20 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() –

相關問題