public
Last active

multiplexer to json-ify any controller as well as call multiple separate controllers via one request

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
<?php
 
namespace Application\FooBundle\Controller;
 
class MultiplexController
{
/**
* Request
* @var Symfony\Component\HttpFoundation\Request
*/
protected $request;
 
/**
* Response
* @var Symfony\Component\HttpFoundation\Response
*/
protected $response;
 
/**
* Kernel
* @var Symfony\Component\HttpKernel\Kernel
*/
protected $kernel;
 
/**
* Router
* @var Symfony\Component\Routing\RouterInterface
*/
protected $router;
 
/**
* assoc array with config parameters
* @var array
*/
protected $params;
 
/**
* Constructor
*
* @param array $params assoc array with config parameters
* @param Symfony\Component\HttpFoundation\Request $request
* @param Symfony\Component\HttpFoundation\Response $response
* @param Symfony\Component\HttpKernel\Kernel $kernel
* @param Symfony\Component\Routing\RouterInterface $router
*/
public function __construct(array $params, $request, $response, $kernel, $router)
{
$this->params = $params;
$this->request = $request;
$this->response = $response;
$this->kernel = $kernel;
$this->router = $router;
}
 
/**
* Handle a single request
*
* @param array $request array contains 'uri', 'method' and 'query'
* @param string $id id value
* @return array contains 'id', 'status' and 'html'
*/
protected function handleRequest($request, $i)
{
if (empty($request['uri'])) {
throw new \InvalidArgumentException('no uri given for index: '.$i);
}
$request['uri'] = preg_replace('/^('.preg_quote($this->request->getScriptName(), '/').')?\//', '', $request['uri']);
 
if (empty($request['method'])) {
$request['method'] = 'get';
}
if (empty($request['parameters'])) {
$request['parameters'] = array();
}
 
$subRequest = $this->request->create($request['uri'], $request['method'], $request['parameters']);
if (false === ($parameters = $this->router->match($subRequest->getPathInfo()))) {
throw new \InvalidArgumentException('uri did not match a route for index: '.$i);
}
 
$subRequest->attributes->add($parameters);
$subResponse = $this->kernel->handle($subRequest, \Symfony\Component\HttpKernel\HttpKernelInterface::SUB_REQUEST);
if (substr($subResponse->getStatusCode(), 0, 1) === '3') {
$request = array(
'uri' => $subResponse->headers->get('location'),
'method' => 'get',
'parameters' => array(),
);
return $this->handleRequest($request, $i);
}
 
return array(
'id' => $i,
'status' => $subResponse->getStatusCode(),
'html' => $subResponse->getContent(),
);
}
 
/**
* Handle the index request
*
* @param string $_format the format to use in the response (json, html ..)
* @return Symfony\Component\HttpFoundation\Response
*/
public function indexAction($_format)
{
$requests = (array)$this->request->get('requests');
 
$content = array('response' => array());
foreach ($requests as $i => $request) {
if ($_format === 'html') {
$content['response'][] = $this->handleRequest($request, $i);
} else {
try {
$content['response'][] = $this->handleRequest($request, $i);
} catch (\Exception $e) {
// TODO: are our error messages safe to be returned?
$content['response'][] = array('id' => $i, 'status' => '500', 'html' => $e->getMessage());
}
}
}
 
$this->response->setStatusCode(200);
$content['status'] = 'success';
 
// TODO add xml?
switch ($_format) {
case 'json':
$content = json_encode($content);
break;
case 'html':
default:
$content = '<pre>'.var_export($content, true).'</pre>';
break;
}
 
$this->response->setContent($content);
 
return $this->response;
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.