Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'weierophinney/feature/view-layer'
Browse files Browse the repository at this point in the history
  • Loading branch information
akrabat committed Feb 22, 2012
Show file tree
Hide file tree
Showing 87 changed files with 5,592 additions and 216 deletions.
48 changes: 5 additions & 43 deletions src/Helper/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
namespace Zend\View\Helper;

use Zend\Http\Response,
Zend\Json\Json as JsonFormatter,
Zend\Layout\Layout;
Zend\Json\Json as JsonFormatter;

/**
* Helper for simplifying JSON responses
Expand All @@ -38,28 +37,11 @@
*/
class Json extends AbstractHelper
{
/**
* @var Layout
*/
protected $layout;

/**
* @var Response
*/
protected $response;

/**
* Set the layout object
*
* @param Layout $layout
* @return Json
*/
public function setLayout(Layout $layout)
{
$this->layout = $layout;
return $this;
}

/**
* Set the response object
*
Expand All @@ -73,35 +55,15 @@ public function setResponse(Response $response)
}

/**
* Encode data as JSON, disable layouts, and set response header
*
* If $keepLayouts is true, does not disable layouts.
* Encode data as JSON and set response header
*
* @param mixed $data
* @param bool $keepLayouts
* NOTE: if boolean, establish $keepLayouts to true|false
* if array, admit params for Zend_Json::encode as enableJsonExprFinder=>true|false
* this array can contains a 'keepLayout'=>true|false
* that will not be passed to Zend_Json::encode method but will be used here
* @param array $jsonOptions Options to pass to JsonFormatter::encode()
* @return string|void
*/
public function __invoke($data, $keepLayouts = false)
public function __invoke($data, array $jsonOptions = array())
{
$options = array();
if (is_array($keepLayouts))
{
$options = $keepLayouts;
$keepLayouts = (array_key_exists('keepLayouts', $keepLayouts))
? $keepLayouts['keepLayouts']
: false;
unset($options['keepLayouts']);
}

$data = JsonFormatter::encode($data, null, $options);

if (!$keepLayouts && ($this->layout instanceof Layout)) {
$this->layout->disableLayout();
}
$data = JsonFormatter::encode($data, null, $jsonOptions);

if ($this->response instanceof Response) {
$headers = $this->response->headers();
Expand Down
78 changes: 58 additions & 20 deletions src/Helper/Layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,56 +24,94 @@
*/
namespace Zend\View\Helper;

use Zend\Layout\Layout as BaseLayout;
use Zend\View\Exception,
Zend\View\Model;

/**
* View helper for retrieving layout object
*
* @uses \Zend\Layout\Layout
* @uses \Zend\View\Helper\AbstractHelper
* @package Zend_View
* @subpackage Helper
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Layout extends AbstractHelper
{
/** @var BaseLayout */
protected $_layout;
/**
* @var ViewModel
*/
protected $viewModelHelper;

/**
* Get layout object
* Get layout template
*
* @return BaseLayout
* @return string
*/
public function getLayout()
{
if (null === $this->_layout) {
$this->_layout = new BaseLayout();
}

return $this->_layout;
$model = $this->getRoot();
return $model->getTemplate();
}

/**
* Set layout object
* Set layout template
*
* @param BaseLayout $layout
* @param string $template
* @return Layout
*/
public function setLayout(BaseLayout $layout)
public function setTemplate($template)
{
$this->_layout = $layout;
$model = $this->getRoot();
$model->setTemplate((string) $template);
return $this;
}

/**
* Return layout object
* Set layout template or retrieve "layout" view model
*
* If no arguments are given, grabs the "root" or "layout" view model.
* Otherwise, attempts to set the template for that view model.
*
* @return BaseLayout
* @param null|string $template
* @return Layout
*/
public function __invoke()
public function __invoke($template = null)
{
return $this->getLayout();
if (null === $template) {
return $this->getRoot();
}
return $this->setTemplate($template);
}

/**
* Get the root view model
*
* @return null|Model
*/
protected function getRoot()
{
$helper = $this->getViewModelHelper();
if (!$helper->hasRoot()) {
throw new Exception\RuntimeException(sprintf(
'%s: no view model currently registered as root in renderer',
__METHOD__
));
}
return $helper->getRoot();
}

/**
* Retrieve the view model helper
*
* @return ViewModel
*/
protected function getViewModelHelper()
{
if ($this->viewModelHelper) {
return $this->viewModelHelper;
}
$view = $this->getView();
$this->viewModelHelper = $view->plugin('view_model');
return $this->viewModelHelper;
}
}
142 changes: 142 additions & 0 deletions src/Helper/RenderChildModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_View
* @subpackage Helper
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

/**
* @namespace
*/
namespace Zend\View\Helper;

use Zend\View\Exception,
Zend\View\Model;

/**
* Helper for rendering child view models
*
* Finds children matching "capture-to" values, and renders them using the
* composed view instance.
*
* @package Zend_View
* @subpackage Helper
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class RenderChildModel extends AbstractHelper
{
/**
* @var Model Current view model
*/
protected $current;

/**
* @var ViewModel
*/
protected $viewModelHelper;

/**
* Invoke as a function
*
* Proxies to {render()}.
*
* @param string $child
* @return string
*/
public function __invoke($child)
{
return $this->render($child);
}

/**
* Render a model
*
* If a matching child model is found, it is rendered. If not, an empty
* string is returned.
*
* @param string $child
* @return string
*/
public function render($child)
{
$model = $this->findChild($child);
if (!$model) {
return '';
}

$current = $this->current;
$view = $this->getView();
$return = $view->render($model);
$helper = $this->getViewModelHelper();
$helper->setCurrent($current);
return $return;
}

/**
* Find the named child model
*
* Iterates through the current view model, looking for a child model that
* has a captureTo value matching the requested $child. If found, that child
* model is returned; otherwise, a boolean false is returned.
*
* @param string $child
* @return false|Model
*/
protected function findChild($child)
{
$this->current = $model = $this->getCurrent();
foreach ($model->getChildren() as $childModel) {
if ($childModel->captureTo() == $child) {
return $childModel;
}
}
return false;
}

/**
* Get the current view model
*
* @return null|Model
*/
protected function getCurrent()
{
$helper = $this->getViewModelHelper();
if (!$helper->hasCurrent()) {
throw new Exception\RuntimeException(sprintf(
'%s: no view model currently registered in renderer; cannot query for children',
__METHOD__
));
}
return $helper->getCurrent();
}

/**
* Retrieve the view model helper
*
* @return ViewModel
*/
protected function getViewModelHelper()
{
if ($this->viewModelHelper) {
return $this->viewModelHelper;
}
$view = $this->getView();
$this->viewModelHelper = $view->plugin('view_model');
return $this->viewModelHelper;
}
}
Loading

0 comments on commit 140c99f

Please sign in to comment.