Browse files

Merge branch '2.0'

Conflicts:
	composer.json
  • Loading branch information...
2 parents e544c98 + 84398f9 commit 8ee435f619ed955313c173b5498ca20d0543b659 @lsmith77 lsmith77 committed Jan 10, 2012
Showing with 85 additions and 21 deletions.
  1. +25 −0 Controller/Annotations/View.php
  2. +6 −3 EventListener/ViewResponseListener.php
  3. +15 −3 README.md
  4. +8 −8 Tests/View/ViewHandlerTest.php
  5. +28 −0 View/View.php
  6. +2 −7 View/ViewHandler.php
  7. +1 −0 composer.json
View
25 Controller/Annotations/View.php
@@ -20,6 +20,11 @@
class View extends Template
{
/**
+ * @var string
+ */
+ protected $templateVar;
+
+ /**
* Returns the annotation alias name.
*
* @return string
@@ -29,4 +34,24 @@ public function getAliasName()
{
return 'view';
}
+
+ /**
+ * Sets the template var name to be used for templating formats.
+ *
+ * @param string $templateVar
+ */
+ public function setTemplateVar($templateVar)
+ {
+ $this->templateVar = $templateVar;
+ }
+
+ /**
+ * Returns the template var name to be used for templating formats.
+ *
+ * @return string
+ */
+ public function getTemplateVar()
+ {
+ return $this->templateVar;
+ }
}
View
9 EventListener/ViewResponseListener.php
@@ -72,13 +72,16 @@ public function onKernelView(GetResponseForControllerResultEvent $event)
$view = $event->getControllerResult();
if (!$view instanceOf View) {
- if (!$request->attributes->get('_view')
- && !$this->container->getParameter('fos_rest.view_response_listener.force_view')
- ) {
+ $configuration = $request->attributes->get('_view');
+
+ if (!$configuration && !$this->container->getParameter('fos_rest.view_response_listener.force_view')) {
return;
}
$view = new View($view);
+ if ($configuration && $configuration->getTemplateVar()) {
+ $view->setTemplateVar($configuration->getTemplateVar());
+ }
}
if (null === $view->getFormat()) {
View
18 README.md
@@ -136,9 +136,8 @@ constructor.
As the purpose is to create a format-agnostic controller, data assigned to the ``View``
instance should ideally be an object graph, though any data type is acceptable. Note that when rendering
templating formats, the ``ViewHandler`` will wrap data types other than associative arrays in an
-associative array with the single key ``'data'``, which will become the variable name of the object in
-the respective template. If the view data is an instance of ``Symfony\Component\Form\FormInterface``,
-the key will be named ``'form'``.
+associative array with a single key (default ``'data'``), which will become the variable name of the
+object in the respective template.
There are also two specialized ``View`` classes for handling directs, one for redirecting
to an URL called ``RedirectView`` and one to redirect to a route called ``RouteRedirectView``.
@@ -340,7 +339,20 @@ class UsersController
}
}
```
+If ``@View()`` is used, the template variable name used to render templating formats can be
+configured (default ``'data'``):
+```php
+<?php
+
+/**
+ * @View(templateVar="users")
+ */
+public function getUsersAction()
+{
+ ...
+}
+```
See the following example code for more details:
https://github.com/liip/LiipHelloBundle/blob/master/Controller/ExtraController.php
View
16 Tests/View/ViewHandlerTest.php
@@ -165,18 +165,18 @@ public function testCreateResponseWithoutLocation($format, $expected, $createVie
$viewHandler->setContainer($container);
- $data = array('foo' => 'bar');
if ($form) {
- $form = $this->getMock('\Symfony\Component\Form\Form', array('createView', 'isValid', 'getChildren'), array(), '', false);
- $form
+ $data = $this->getMock('\Symfony\Component\Form\Form', array('createView', 'isValid', 'getChildren'), array(), '', false);
+ $data
->expects($this->exactly($createViewCalls))
->method('createView')
->will($this->returnValue(array('bla' => 'toto')));
- $form
+ $data
->expects($this->any())
->method('isValid')
->will($this->returnValue($formIsValid));
- $data['form'] = $form;
+ } else {
+ $data = array('foo' => 'bar');
}
$view = new View($data);
@@ -189,8 +189,8 @@ public static function createResponseWithoutLocationDataProvider()
return array(
'not templating aware no form' => array('json', array('foo' => 'bar')),
'templating aware no form' => array('html', array('foo' => 'bar')),
- 'templating aware and form' => array('html', array('foo' => 'bar', 'form' => array('bla' => 'toto')), 1, true, true),
- 'not templating aware and invalid form' => array('json', array('foo' => 'bar', 'form' => array(0 => 'error', 1 => 'error')), 0, false, true),
+ 'templating aware and form' => array('html', array('data' => array('bla' => 'toto')), 1, true, true),
+ 'not templating aware and invalid form' => array('json', array('data' => array(0 => 'error', 1 => 'error')), 0, false, true),
);
}
@@ -312,7 +312,7 @@ public function prepareTemplateParametersDataProvider()
'assoc array does not change' => array(array('foo' => 'bar'), array('foo' => 'bar')),
'array is wrapped as data key' => array(array('foo', 'bar'), array('data' => array('foo', 'bar'))),
'object is wrapped as data key' => array($object, array('data' => $object)),
- 'form is wrapped as form key' => array($form, array('form' => $formView))
+ 'form is wrapped as form key' => array($form, array('data' => $formView))
);
}
}
View
28 View/View.php
@@ -44,6 +44,11 @@ class View
/**
* @var string
*/
+ private $templateVar;
+
+ /**
+ * @var string
+ */
private $engine;
/**
@@ -85,6 +90,7 @@ public function __construct($data = null, $statusCode = null, array $headers = a
$this->data = $data;
$this->statusCode = $statusCode;
$this->headers = $headers;
+ $this->templateVar = 'data';
}
/**
@@ -158,6 +164,18 @@ public function setTemplate($template)
}
/**
+ * Sets template variable name to be used in templating formats
+ *
+ * @param string
+ */
+ public function setTemplateVar($templateVar)
+ {
+ $this->templateVar = $templateVar;
+
+ return $this;
+ }
+
+ /**
* set the engine
*
* @param $engine
@@ -252,6 +270,16 @@ public function getTemplate()
}
/**
+ * Get the template variable name.
+ *
+ * @param string
+ */
+ public function getTemplateVar()
+ {
+ return $this->templateVar;
+ }
+
+ /**
* get the engine
*
* @return string engine
View
9 View/ViewHandler.php
@@ -270,15 +270,10 @@ public function prepareTemplateParameters(View $view)
if (!is_array($data) || array_key_exists(0, $data)) {
if ($data instanceof FormInterface) {
- $data = array('form' => $data);
- } else {
- $data = array('data' => $data);
+ $data = $data->createView();
}
- }
- if (isset($data['form']) && $data['form'] instanceof FormInterface) {
- $data['form'] = $data['form']->createView();
- return $data;
+ $data = array($view->getTemplateVar() => $data);
}
return $data;
View
1 composer.json
@@ -22,6 +22,7 @@
"require": {
"php": ">=5.3.2",
+ "symfony/symfony": ">=2.1.0-dev",
"symfony/framework-bundle": ">=2.1.0-dev",
"sensio/framework-extra-bundle": "*",
"jms/serializer-bundle": "*"

0 comments on commit 8ee435f

Please sign in to comment.