从 Symfony 2 中的自定义命令读取控制器自定义注释
Read controllers custom annotations from a custom Command in Symfony 2
我已经创建了自定义注释来生成 JSON 文件(对于 NodeRed,如果你问......)我正在通过虚拟控制器中的虚拟方法成功测试它们。
我想将所有这些移植到 自定义 Sf 命令,其工作是读取我的包中的所有控制器注释并获得相同的结果(又名创建 JSON 个文件)。
我怎样才能做到这一点?使用查找器循环遍历 XxxxController.php 个文件会是一个不错的选择吗? 还是我野心太大了? :p
注解示例:
/**
* @NodeRedFlows(
* triggerBy="testFlow", options={"interval":"45"}
* )
*/
public function indexAction() { /*...*/ }
抱歉,要 post 更多代码并不容易,因为我有整个 reader class、注释 class 和另一个 class 创建 JSON 流量基于 triggerBy="testFlow"
id。
底线:*
我希望能够从命令而不是在我的控制器中创建我的 JSON 流文件(用于测试)。
加载所有已在 Symfony 中指定路由的控制器操作(参见 this and this)。
然后为每个找到的控制器操作加载注释:
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 );
}
}
更新:
如果您需要所有控制器方法,包括那些没有 @Route
注释的方法,那么我会按照您在问题中的建议进行操作:
// 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);
}
}
}
我已经创建了自定义注释来生成 JSON 文件(对于 NodeRed,如果你问......)我正在通过虚拟控制器中的虚拟方法成功测试它们。
我想将所有这些移植到 自定义 Sf 命令,其工作是读取我的包中的所有控制器注释并获得相同的结果(又名创建 JSON 个文件)。
我怎样才能做到这一点?使用查找器循环遍历 XxxxController.php 个文件会是一个不错的选择吗? 还是我野心太大了? :p
注解示例:
/**
* @NodeRedFlows(
* triggerBy="testFlow", options={"interval":"45"}
* )
*/
public function indexAction() { /*...*/ }
抱歉,要 post 更多代码并不容易,因为我有整个 reader class、注释 class 和另一个 class 创建 JSON 流量基于 triggerBy="testFlow"
id。
底线:*
我希望能够从命令而不是在我的控制器中创建我的 JSON 流文件(用于测试)。
加载所有已在 Symfony 中指定路由的控制器操作(参见 this and this)。
然后为每个找到的控制器操作加载注释:
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 );
}
}
更新:
如果您需要所有控制器方法,包括那些没有 @Route
注释的方法,那么我会按照您在问题中的建议进行操作:
// 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);
}
}
}