Skip to content

Instantly share code, notes, and snippets.

@Freeaqingme
Created July 18, 2012 22:01
Show Gist options
  • Save Freeaqingme/3139197 to your computer and use it in GitHub Desktop.
Save Freeaqingme/3139197 to your computer and use it in GitHub Desktop.
THIS IS PSEUDOCODE
<?php
// tHIS ENTIRE FILE IS PSEUDOCODE ONLY
class ConsoleStrategy implements ListenerAggregateInterface
{
/**
* @var \Zend\Stdlib\CallbackHandler[]
*/
protected $listeners = array();
/**
* @var JsonRenderer
*/
protected $renderer;
/**
* Constructor
*
* @param ConsoleRenderer $renderer
* @return void
*/
public function __construct(ConsoleRenderer $renderer)
{
$this->renderer = $renderer;
}
public function attach(EventManagerInterface $events, $priority = 1)
public function detach(EventManagerInterface $events)
/**
* Detect if we should use the JsonRenderer based on model type and/or
* Accept header
*
* @param ViewEvent $e
* @return null|JsonRenderer
*/
public function selectRenderer(ViewEvent $e)
{
$model = $e->getModel();
if ($model instanceof Model\ConsoleModel) {
return $this->renderer;
}
$request = $e->getRequest();
if (!$request instanceof CLIRequest) {
return $this->renderer;
}
}
public function injectResponse(ViewEvent $e)
}
class AcceptHeaderStrategy implements ListenerAggregateInterface
{
/**
* @var \Zend\Stdlib\CallbackHandler[]
*/
protected $listeners = array();
acceptStrategies = array(); // How to get those here? listeners? sth else?
public function attach(EventManagerInterface $events, $priority = 100)
public function detach(EventManagerInterface $events)
/**
* Detect if we should use the JsonRenderer based on model type and/or
* Accept header
*
* @param ViewEvent $e
* @return null|JsonRenderer
*/
public function selectRenderer(ViewEvent $e)
{
$request = $e->getRequest();
if (!$request instanceof HttpRequest) {
// Not an HTTP request; cannot autodetermine
return;
}
$headers = $request->getHeaders();
if (!$headers->has('accept')) {
return;
}
$fieldValueParts = new FieldValuePartCollection(); // Iterator that allows for equally named keys
foreach($this->acceptStrategies as $key => $acceptStrategy) {
foreach($acceptStrategy->getFieldValueParts() as $fieldValuePart) {
//todo array_walk ftw
$fieldValueParts->add($key, $fieldValuePart);
}
}
$accept = $headers->get('Accept');
if (($res = $accept->match($fieldValueParts) != false) {
$res->getMatchedFieldValuePartId()->getRenderer($e, $res);
}
return;
}
public function injectResponse(ViewEvent $e)
}
class AcceptHeaderStrategy\JsonStrategy
{
public $renderer;
public function __construct(Render $r) {
$this->renderer = $r;
}
public getFieldValueParts() {
return array(new FieldValuePart('type' => 'application', 'subtype' => 'javascript'));
}
public function getRenderer()
{
if ($this->renderer->hasJsonpCallback()) {
$headers->addHeaderLine('content-type', 'application/javascript');
} else {
$headers->addHeaderLine('content-type', 'application/json');
}
}
}
class AcceptHeaderStrategy\FeedStrategy
{
public getFieldValueParts() {
return array(new FieldValuePart('type' => 'application', 'subtype' => 'javascript', 'format' => 'xml'),
new FieldValuePart('type' => 'application', 'subtype' => 'atom', 'format' => 'xml'));
}
public function getRenderer()
{
//return the renderer as specified while constructing
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment