ogboot/src/OgBootBundle/Service/CurlRequestService.php

74 lines
2.3 KiB
PHP

<?php
// src/OgBootBundle/Service/CurlRequestService.php
namespace App\OgBootBundle\Service;
use Exception;
use Psr\Log\LoggerInterface;
class CurlRequestService
{
private LoggerInterface $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
public function convertMaskToCIDR($mask)
{
$bits = 0;
$mask = explode(".", $mask);
foreach ($mask as $octet) {
$bits += strlen(str_replace("0", "", decbin($octet)));
}
return $bits;
}
public function callOgLive($parameter)
{
// Ruta completa al script oglivecli
$ogLiveCliPath = sprintf("%s/bin/oglivecli", dirname(dirname(dirname(__DIR__))));
// Dividir el parámetro en acción y argumentos
$args = array_map('trim', explode(' ', $parameter));
$action = array_shift($args);
// Registrar la acción y los argumentos
$this->logger->debug('Action: ' . $action);
$this->logger->debug('Arguments: ' . json_encode($args));
// Limpiar los argumentos de comillas innecesarias
$cleanedArgs = array_map(function ($arg) {
return trim($arg, '\'\"');
}, $args);
// Construir el comando final sin añadir comillas alrededor de cada elemento
$commandToRun = $ogLiveCliPath . ' ' . $action . ' ' . implode(' ', $cleanedArgs);
// Registrar el comando para depuración
$this->logger->debug('Command: ' . $commandToRun);
// Ejecutar el comando, capturando la salida y el código de salida
$output = [];
$exitCode = null;
exec($commandToRun, $output, $exitCode);
// Unir la salida en una sola cadena y registrar en el logger
$outputString = implode("\n", $output);
$this->logger->debug('Output: ' . $outputString);
$this->logger->debug('Exit Code: ' . $exitCode);
// Decodificar la salida JSON si es posible
$decodedOutput = json_decode($outputString, true);
$this->logger->debug('Decoded Output: ' . print_r($decodedOutput, true));
return [
'output' => $decodedOutput, // Retorna la salida decodificada (JSON)
'exitCode' => $exitCode // Retorna el código de salida del comando
];
}
}