Browse Source

runCommand function added that run the amqp:remote command

Guillermo Espinoza 6 years ago
parent
commit
0b6408bd09
1 changed files with 40 additions and 4 deletions
  1. 40 4
      Controller/RESTController.php

+ 40 - 4
Controller/RESTController.php

@@ -15,7 +15,9 @@ use Symfony\Bundle\FrameworkBundle\Controller\Controller;
 use Symfony\Component\Form\Form;
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpFoundation\Response;
-
+use Symfony\Bundle\FrameworkBundle\Console\Application;
+use Symfony\Component\Console\Input\ArgvInput;
+use Symfony\Component\Console\Output\BufferedOutput;
 use Voryx\RESTGeneratorBundle\Controller\VoryxController;
 
 abstract class RESTController extends VoryxController
@@ -81,7 +83,7 @@ abstract class RESTController extends VoryxController
                 } else {
                     foreach ($filters as $field => $value) {
                         if ($orWhere) {
-                            if (is_numeric($value)) {                              
+                            if (is_numeric($value)) {
                                 $criteria->orWhere($criteria->expr()->in("$field", (is_array($value) ? $value : [$value])));
                             } else {
                                 $value = urldecode($value);
@@ -94,9 +96,9 @@ abstract class RESTController extends VoryxController
                                 $value = urldecode($value);
                                 $criteria->andWhere($criteria->expr()->contains("$field", "$value"));
                             }
-                        }                    
+                        }
                     }
-                    
+
                     if (!is_null($paramFetcher->get('limit'))) {
                         $criteria->setMaxResults($paramFetcher->get('limit'));
                     }
@@ -367,4 +369,38 @@ abstract class RESTController extends VoryxController
         }
     }
 
+    /**
+     * Corro el comando para crear el device por AMQP
+     * @global kernel $kernel
+     *
+     * @param string $name
+     * @param array $cmd_args
+     *
+     * @return string
+     */
+    public function runCommand($name, $cmd_args = array())
+    {
+        $kernel = $this->get('kernel');
+
+        $application = new Application($kernel);
+        $application->setAutoExit(false);
+
+        $args = [
+            '',
+            'amqp:remote',
+            $name,
+            '--route=' . getenv("AMQP_KEY"),
+        ];
+        foreach ($cmd_args as $cmd_arg) {
+            $args[] = "--args={$cmd_arg}";
+        }
+        $input = new ArgvInput($args);
+
+        $output = new BufferedOutput();
+
+        $application->run($input, $output);
+
+        return $output->fetch();
+    }
+
 }