Created
January 2, 2015 17:27
-
-
Save marzocchi/2b7b89ba7866212b4cea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\Process\ProcessBuilder; | |
/** | |
* @author marzocchi | |
*/ | |
class RequestToCurlTranslator { | |
private $curl = 'curl'; | |
public function translate(Request $request) { | |
$args = [$this->curl]; | |
if ($request->getMethod() !== 'GET') { | |
$args[] = '-X' . $request->getMethod(); | |
} | |
$args[] = $request->getUri(); | |
if ($request->getMethod() === 'POST') { | |
xdebug_break(); | |
} | |
if ($this->hasContent($request)) { | |
switch ($request->headers->get('Content-Type')) { | |
case 'multipart/form-data': | |
break; | |
case 'application/x-www-form-urlencoded': | |
foreach($request->request->keys() as $key) { | |
$args[] = '-d' . $key . '=' . $request->request->get($key); | |
} | |
break; | |
default: | |
$args[] = '-d' . $request->getContent(); | |
break; | |
} | |
} | |
$builder = new ProcessBuilder($args); | |
$commandLine = $builder->getProcess()->getCommandLine(); | |
return $commandLine; | |
} | |
private function hasContent(Request $request) { | |
return in_array($request->getMethod(), ['PUT', 'POST']) || trim($request->getContent()) !== ''; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment