Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'feature/accepted-model-controller-plugin' of git://gith…

…ub.com/basz/zf2 into basz-feature/accepted-model-controller-plugin
  • Loading branch information...
commit a577a448f42640bb618387e94a4df2b194c379fa 2 parents 59711fd + f6b8690
@Freeaqingme Freeaqingme authored
View
4 library/Zend/Http/Header/AbstractAccept.php
@@ -303,6 +303,8 @@ public function match($matchAgainst)
foreach ($matchAgainst as $right) {
if ($right->type == '*' || $left->type == '*') {
if ($res = $this->matchAcceptParams($left, $right)) {
+ $res->matchedAgainst = $right;
+
return $res;
}
}
@@ -314,6 +316,8 @@ public function match($matchAgainst)
$right->format == '*' || $left->format == '*')))
{
if ($res = $this->matchAcceptParams($left, $right)) {
+ $res->matchedAgainst = $right;
+
return $res;
}
}
View
132 library/Zend/Mvc/Controller/Plugin/AcceptedModel.php
@@ -0,0 +1,132 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_Mvc
+ */
+
+namespace Zend\Mvc\Controller\Plugin;
+
+use Zend\Http\Request;
+use Zend\Mvc\Controller\Plugin\AbstractPlugin;
+use Zend\View\Model\ModelInterface;
+use Zend\Mvc\InjectApplicationEventInterface;
+use Zend\Mvc\MvcEvent;
+
+/**
+ * @category Zend
+ * @package Zend_Mvc
+ * @subpackage Controller
+ */
+class AcceptedModel extends AbstractPlugin
+{
+ protected $event;
+ protected $request;
+ protected $detected;
+
+ /**
+ * Default match string to match against. Could be set 'somewhere'
+ *
+ * @var String
+ */
+ protected $defaultMatchArray;
+
+ /**
+ * Detects an appropriate model for view.
+ *
+ * @return ModelInterface
+ */
+ public function __invoke(array $matchAgainst = null, $returnMatchedAcceptTypeOnly = false)
+ {
+ $this->detected = 'Zend\View\Model\ViewModel';
+ $request = $this->getRequest();
+ $headers = $request->getHeaders();
+
+
+ if ((!$matchAgainst && !$this->defaultMatchString) || !$headers->has('accept')) {
+ return new $this->detected;
+ }
+
+ if (!$matchAgainst) {
+ $matchAgainst = $this->defaultMatchString;
+ }
+
+ $matchAgainstString = '';
+ foreach ($matchAgainst as $modelName => $modelStrings) {
+ $modelName = str_replace('\\', '|', $modelName);
+ foreach ((array) $modelStrings as $modelString) {
+ $matchAgainstString .= $modelString
+ . '; _internalViewModel="' . $modelName . '", ';
+ }
+ }
+
+ /** @var $accept \Zend\Http\Header\Accept */
+ $accept = $headers->get('Accept');
+ if (($res = $accept->match($matchAgainstString)) === false) {
+ return new $this->detected;
+ }
+
+ if ($returnMatchedAcceptTypeOnly) {
+ return $res;
+ }
+
+ $modelName = $res->matchedAgainst->params['_internalViewModel'];
+ $modelName = str_replace('|', '\\', $modelName);
+
+ return new $modelName;
+ }
+
+ /**
+ * Get the request
+ *
+ * @return Request
+ * @throws Exception\DomainException if unable to find request
+ */
+ protected function getRequest()
+ {
+ if ($this->request) {
+ return $this->request;
+ }
+
+ $event = $this->getEvent();
+ $request = $event->getRequest();
+ if (!$request instanceof Request) {
+ throw new Exception\DomainException('AcceptedModel plugin requires event compose a request');
+ }
+ $this->request = $request;
+
+ return $this->request;
+ }
+
+ /**
+ * Get the event
+ *
+ * @return MvcEvent
+ * @throws Exception\DomainException if unable to find event
+ */
+ protected function getEvent()
+ {
+ if ($this->event) {
+ return $this->event;
+ }
+
+ $controller = $this->getController();
+ if (!$controller instanceof InjectApplicationEventInterface) {
+ throw new Exception\DomainException('AcceptedModel plugin requires a controller that implements InjectApplicationEventInterface');
+ }
+
+ $event = $controller->getEvent();
+ if (!$event instanceof MvcEvent) {
+ $params = $event->getParams();
+ $event = new MvcEvent();
+ $event->setParams($params);
+ }
+ $this->event = $event;
+
+ return $this->event;
+ }
+
+}
View
1  library/Zend/Mvc/Controller/PluginManager.php
@@ -33,6 +33,7 @@ class PluginManager extends AbstractPluginManager
* @var array
*/
protected $invokableClasses = array(
+ 'acceptedmodel' => 'Zend\Mvc\Controller\Plugin\AcceptedModel',
'flashmessenger' => 'Zend\Mvc\Controller\Plugin\FlashMessenger',
'forward' => 'Zend\Mvc\Controller\Plugin\Forward',
'layout' => 'Zend\Mvc\Controller\Plugin\Layout',
View
58 tests/ZendTest/Mvc/Controller/Plugin/AcceptedModelTest.php
@@ -0,0 +1,58 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_Mvc
+ */
+
+namespace ZendTest\Mvc\Controller\Plugin;
+
+use Zend\Mvc\Controller\Plugin\AcceptedModel;
+use Zend\Http\Request;
+use Zend\Mvc\MvcEvent;
+use Zend\Http\Header\Accept;
+use ZendTest\Mvc\Controller\TestAsset\SampleController;
+
+/**
+ * @category Zend
+ * @package Zend_Mvc
+ * @subpackage UnitTests
+ */
+class AcceptedModelTest extends \PHPUnit_Framework_TestCase
+{
+ public function setUp()
+ {
+ $this->request = new Request();
+
+ $event = new MvcEvent();
+ $event->setRequest($this->request);
+ $this->event = $event;
+
+ $this->controller = new SampleController();
+ $this->controller->setEvent($event);
+
+ /** @var Zend\Mvc\Controller\Plugin\AcceptedModel */
+ $this->plugin = $this->controller->plugin('acceptedModel');
+ }
+
+ public function test_TestingTheTestSetup()
+ {
+ $plugin = $this->plugin;
+ $header = Accept::fromString('Accept: text/plain; q=0.5, text/html, text/xml; q=0, text/x-dvi; q=0.8, text/x-c');
+ $this->request->getHeaders()->addHeader($header);
+ $result = $plugin(array(
+ 'Zend\View\Model\JsonModel' => array(
+ 'application/json',
+ 'application/javascript'
+ ),
+ 'Zend\View\Model\FeedModel' => array(
+ 'application/rss+xml',
+ 'application/atom+xml'
+ ),
+ 'Zend\View\Model\ViewModel' => '*/*'
+ ));
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.