HummingBirdAnimeClient/src/Aviat/Ion/View.php

137 lines
2.0 KiB
PHP
Raw Normal View History

2015-09-17 23:11:18 -04:00
<?php
2015-11-16 11:40:01 -05:00
/**
* Ion
*
* Building blocks for web development
*
* @package Ion
* @author Timothy J. Warren
* @copyright Copyright (c) 2015 - 2016
2015-11-16 11:40:01 -05:00
* @license MIT
*/
2015-09-17 23:11:18 -04:00
namespace Aviat\Ion;
use Aviat\Ion\Di\ContainerInterface;
2015-10-12 14:27:20 -04:00
use Aviat\Ion\Type\StringType;
2015-09-17 23:11:18 -04:00
/**
* Base view response class
*/
2015-09-17 23:11:18 -04:00
abstract class View {
use Di\ContainerAware;
2016-03-03 16:53:17 -05:00
use StringWrapper;
2015-09-17 23:11:18 -04:00
/**
* HTTP response Object
*
* @var Zend\Diactoros\Response
2015-09-17 23:11:18 -04:00
*/
2016-03-03 16:53:17 -05:00
public $response;
/**
* Redirect response object
*
* @var Zend\Diactoros\RedirectResponse
*/
protected $redirectResponse;
2015-09-17 23:11:18 -04:00
/**
* Response mime type
*
* @var string
*/
protected $contentType = '';
/**
* String of response to be output
*
2015-10-12 14:27:20 -04:00
* @var StringType
2015-09-17 23:11:18 -04:00
*/
2015-10-01 16:02:51 -04:00
protected $output;
2015-09-17 23:11:18 -04:00
2016-01-04 10:53:03 -05:00
/**
* If the view has sent output via
* __toString or send method
*
* @var boolean
*/
protected $hasRendered = FALSE;
2016-01-04 10:53:03 -05:00
2015-09-17 23:11:18 -04:00
/**
* Constructor
*
* @param ContainerInterface $container
*/
public function __construct(ContainerInterface $container)
{
$this->setContainer($container);
$this->response = $container->get('response');
2016-03-03 16:53:17 -05:00
$this->redirectResponse = NULL;
2015-09-17 23:11:18 -04:00
}
/**
* Send output to client
*/
public function __destruct()
{
2016-01-04 10:53:03 -05:00
if ( ! $this->hasRendered)
{
$this->send();
}
2016-01-08 15:53:50 -05:00
}
2016-01-04 10:53:03 -05:00
/**
* Return rendered output
*
* @return string
*/
public function __toString()
{
$this->hasRendered = TRUE;
2016-01-04 10:53:03 -05:00
return $this->getOutput();
2015-09-17 23:11:18 -04:00
}
/**
* Set the output string
*
* @param string $string
* @return View
*/
public function setOutput($string)
{
$this->response->getBody()->write($string);
2015-10-01 16:02:51 -04:00
2015-09-17 23:11:18 -04:00
return $this;
}
/**
* Append additional output
*
* @param string $string
* @return View
*/
public function appendOutput($string)
{
2016-03-03 16:53:17 -05:00
return $this->setOutput($string);
2015-09-17 23:11:18 -04:00
}
/**
* Get the current output string
*
* @return string
*/
public function getOutput()
{
return $this->response->getBody()->__toString();
2016-01-08 15:53:50 -05:00
}
/**
* Send output to client
*
* @return void
*/
abstract public function send();
2015-09-17 23:11:18 -04:00
}
// End of View.php