Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

better detection + matched against addition

  • Loading branch information...
commit 7ed457581e28a2f35c84b08e83f211d6d85633a3 1 parent 62122e7
@basz basz 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
54 library/Zend/Mvc/Controller/Plugin/AcceptedModel.php
@@ -31,37 +31,55 @@ class AcceptedModel extends AbstractPlugin
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($cached = true)
+ public function __invoke(array $matchAgainst = null, $returnMatchedAcceptTypeOnly = false)
{
- if ($cached && $this->detected != null) {
- return new $this->detected;
- }
-
$this->detected = 'Zend\View\Model\ViewModel';
$request = $this->getRequest();
$headers = $request->getHeaders();
- if (!$headers->has('accept')) {
+
+ 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');
- foreach ($accept->getPrioritized() as $acceptPart) {
- if (in_array($acceptPart->getTypeString(), array('application/json', 'application/javascript'))) {
- $this->detected = 'Zend\View\Model\JsonModel';
- break;
- }
- if (in_array($acceptPart->getTypeString(), array('application/rss+xml', 'application/atom+xml'))) {
- $this->detected = 'Zend\View\Model\FeedModel';
- break;
- }
- }
-
- return new $this->detected;
+ if (($res = $accept->match($matchAgainstString)) === false) {
+ return new $this->detected;
+ }
+
+ if ($returnMatchedAcceptTypeOnly) {
+ return $res;
+ }
+
+ //@todo return actual instance, make sure to preserve \
+ $modelName = $res->matchedAgainst->params['_internalViewModel'];
+
+ return str_replace('|', '\\', $modelName);
}
/**
Please sign in to comment.
Something went wrong with that request. Please try again.