From 151d3c895d72f977e7606d72117036e64e9b3161 Mon Sep 17 00:00:00 2001 From: "Dolf Schimmel (Freeaqingme)" Date: Sun, 17 Jun 2012 03:39:17 +0200 Subject: [PATCH 1/6] Cleaning up --- src/Strategy/JsonStrategy.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Strategy/JsonStrategy.php b/src/Strategy/JsonStrategy.php index 08095bb0..aedce678 100644 --- a/src/Strategy/JsonStrategy.php +++ b/src/Strategy/JsonStrategy.php @@ -96,7 +96,6 @@ public function detach(EventManagerInterface $events) */ public function selectRenderer(ViewEvent $e) { -// return $this->renderer; $model = $e->getModel(); if ($model instanceof Model\JsonModel) { From 067594d35ab46a43f8ac69c291826049bdb1c194 Mon Sep 17 00:00:00 2001 From: Dolf Schimmel Date: Sat, 14 Jul 2012 04:04:58 +0200 Subject: [PATCH 2/6] Fixed strategy BC break --- src/Strategy/FeedStrategy.php | 19 ++++++++----------- src/Strategy/JsonStrategy.php | 21 ++++++++++----------- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/src/Strategy/FeedStrategy.php b/src/Strategy/FeedStrategy.php index 7cfc5c6c..b43b4fe0 100644 --- a/src/Strategy/FeedStrategy.php +++ b/src/Strategy/FeedStrategy.php @@ -100,17 +100,14 @@ public function selectRenderer(ViewEvent $e) $headers = $request->getHeaders(); if ($headers->has('accept')) { $accept = $headers->get('accept'); - foreach ($accept->getPrioritized() as $mediaType) { - if (0 === strpos($mediaType, 'application/rss+xml')) { - // application/rss+xml Accept header found - $this->renderer->setFeedType('rss'); - return $this->renderer; - } - if (0 === strpos($mediaType, 'application/atom+xml')) { - // application/atom+xml Accept header found - $this->renderer->setFeedType('atom'); - return $this->renderer; - } + if ($accept->match('application/rss+xml')) { + $this->renderer->setFeedType('rss'); + return $this->renderer; + } + + if ($accept->match('application/atom+xml')) { + $this->renderer->setFeedType('atom'); + return $this->renderer; } } diff --git a/src/Strategy/JsonStrategy.php b/src/Strategy/JsonStrategy.php index 2a11f7d7..67fa7912 100644 --- a/src/Strategy/JsonStrategy.php +++ b/src/Strategy/JsonStrategy.php @@ -99,18 +99,17 @@ public function selectRenderer(ViewEvent $e) $headers = $request->getHeaders(); if ($headers->has('accept')) { $accept = $headers->get('Accept'); - foreach ($accept->getPrioritized() as $mediaType) { - if (0 === strpos($mediaType, 'application/json')) { - // application/json Accept header found - return $this->renderer; - } - if (0 === strpos($mediaType, 'application/javascript')) { - // application/javascript Accept header found - if (false != ($callback = $request->getQuery()->get('callback'))) { - $this->renderer->setJsonpCallback($callback); - } - return $this->renderer; + if ($accept->match('application/json')) { + // application/json Accept header found + return $this->renderer; + } + + if ($accept->match('application/javascript')) { + // application/javascript Accept header found + if (false != ($callback = $request->getQuery()->get('callback'))) { + $this->renderer->setJsonpCallback($callback); } + return $this->renderer; } } From 91342b184a1c08761ef4f60864db759ee67e230b Mon Sep 17 00:00:00 2001 From: Dolf Schimmel Date: Sat, 14 Jul 2012 04:31:34 +0200 Subject: [PATCH 3/6] CS --- src/Strategy/PhpRendererStrategy.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Strategy/PhpRendererStrategy.php b/src/Strategy/PhpRendererStrategy.php index 86c0374f..95b11bf9 100644 --- a/src/Strategy/PhpRendererStrategy.php +++ b/src/Strategy/PhpRendererStrategy.php @@ -130,7 +130,7 @@ public function selectRenderer(ViewEvent $e) * Populates the content of the response object from the view rendering * results. * - * @param ViewEvent $e + * @param ViewEvent $e * @return void */ public function injectResponse(ViewEvent $e) From 7917cc40d8821cd5fe736b7ff006c0fc3e4d0771 Mon Sep 17 00:00:00 2001 From: Dolf Schimmel Date: Sat, 14 Jul 2012 18:50:58 +0200 Subject: [PATCH 4/6] Improved zend\view\strategy accept header matching --- src/Strategy/FeedStrategy.php | 4 ++-- src/Strategy/JsonStrategy.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Strategy/FeedStrategy.php b/src/Strategy/FeedStrategy.php index b43b4fe0..b2456862 100644 --- a/src/Strategy/FeedStrategy.php +++ b/src/Strategy/FeedStrategy.php @@ -100,12 +100,12 @@ public function selectRenderer(ViewEvent $e) $headers = $request->getHeaders(); if ($headers->has('accept')) { $accept = $headers->get('accept'); - if ($accept->match('application/rss+xml')) { + if ($accept->hasMediaType('application/rss+xml')) { $this->renderer->setFeedType('rss'); return $this->renderer; } - if ($accept->match('application/atom+xml')) { + if ($accept->hasMediaType('application/atom+xml')) { $this->renderer->setFeedType('atom'); return $this->renderer; } diff --git a/src/Strategy/JsonStrategy.php b/src/Strategy/JsonStrategy.php index 67fa7912..0be5be68 100644 --- a/src/Strategy/JsonStrategy.php +++ b/src/Strategy/JsonStrategy.php @@ -99,12 +99,12 @@ public function selectRenderer(ViewEvent $e) $headers = $request->getHeaders(); if ($headers->has('accept')) { $accept = $headers->get('Accept'); - if ($accept->match('application/json')) { + if ($accept->hasMediaType('application/json')) { // application/json Accept header found return $this->renderer; } - if ($accept->match('application/javascript')) { + if ($accept->hasMediaType('application/javascript')) { // application/javascript Accept header found if (false != ($callback = $request->getQuery()->get('callback'))) { $this->renderer->setJsonpCallback($callback); From 4dc27f83ef1d3ebae04e891a7d9fad34ea322873 Mon Sep 17 00:00:00 2001 From: Dolf Schimmel Date: Sat, 14 Jul 2012 19:57:03 +0200 Subject: [PATCH 5/6] Fixing view strategy priorization --- src/Strategy/FeedStrategy.php | 6 ++++-- src/Strategy/JsonStrategy.php | 7 +++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Strategy/FeedStrategy.php b/src/Strategy/FeedStrategy.php index b2456862..cccb2f60 100644 --- a/src/Strategy/FeedStrategy.php +++ b/src/Strategy/FeedStrategy.php @@ -100,12 +100,14 @@ public function selectRenderer(ViewEvent $e) $headers = $request->getHeaders(); if ($headers->has('accept')) { $accept = $headers->get('accept'); - if ($accept->hasMediaType('application/rss+xml')) { + + $match = $accept->match('application/rss+xml, application/atom+xml'); + if ($match && $match->getTypeString() == 'application/rss+xml') { $this->renderer->setFeedType('rss'); return $this->renderer; } - if ($accept->hasMediaType('application/atom+xml')) { + if ($match && $match->getTypeString() == 'application/atom+xml') { $this->renderer->setFeedType('atom'); return $this->renderer; } diff --git a/src/Strategy/JsonStrategy.php b/src/Strategy/JsonStrategy.php index 0be5be68..744d1128 100644 --- a/src/Strategy/JsonStrategy.php +++ b/src/Strategy/JsonStrategy.php @@ -99,12 +99,15 @@ public function selectRenderer(ViewEvent $e) $headers = $request->getHeaders(); if ($headers->has('accept')) { $accept = $headers->get('Accept'); - if ($accept->hasMediaType('application/json')) { + + $match = $accept->match('application/json, application/javascript'); + + if ($match && $match->getFormat() == 'json') { // application/json Accept header found return $this->renderer; } - if ($accept->hasMediaType('application/javascript')) { + if ($match) { // application/javascript Accept header found if (false != ($callback = $request->getQuery()->get('callback'))) { $this->renderer->setJsonpCallback($callback); From 79b036aa4ad321d138660a69b2c34a92481ce38a Mon Sep 17 00:00:00 2001 From: Dolf Schimmel Date: Sat, 14 Jul 2012 20:03:44 +0200 Subject: [PATCH 6/6] refactoring view strategy priorization --- src/Strategy/FeedStrategy.php | 28 +++++++++++++++------------- src/Strategy/JsonStrategy.php | 32 ++++++++++++++++---------------- 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/src/Strategy/FeedStrategy.php b/src/Strategy/FeedStrategy.php index cccb2f60..cca82db2 100644 --- a/src/Strategy/FeedStrategy.php +++ b/src/Strategy/FeedStrategy.php @@ -98,23 +98,25 @@ public function selectRenderer(ViewEvent $e) } $headers = $request->getHeaders(); - if ($headers->has('accept')) { - $accept = $headers->get('accept'); + if (!$headers->has('accept')) { + return; + } - $match = $accept->match('application/rss+xml, application/atom+xml'); - if ($match && $match->getTypeString() == 'application/rss+xml') { - $this->renderer->setFeedType('rss'); - return $this->renderer; - } + $accept = $headers->get('accept'); + if (($match = $accept->match('application/rss+xml, application/atom+xml')) == false) { + return; + } - if ($match && $match->getTypeString() == 'application/atom+xml') { - $this->renderer->setFeedType('atom'); - return $this->renderer; - } + if ($match->getTypeString() == 'application/rss+xml') { + $this->renderer->setFeedType('rss'); + return $this->renderer; + } + + if ($match->getTypeString() == 'application/atom+xml') { + $this->renderer->setFeedType('atom'); + return $this->renderer; } - // Not matched! - return; } /** diff --git a/src/Strategy/JsonStrategy.php b/src/Strategy/JsonStrategy.php index 744d1128..9d7c8206 100644 --- a/src/Strategy/JsonStrategy.php +++ b/src/Strategy/JsonStrategy.php @@ -97,27 +97,27 @@ public function selectRenderer(ViewEvent $e) } $headers = $request->getHeaders(); - if ($headers->has('accept')) { - $accept = $headers->get('Accept'); + if (!$headers->has('accept')) { + return; + } - $match = $accept->match('application/json, application/javascript'); - if ($match && $match->getFormat() == 'json') { - // application/json Accept header found - return $this->renderer; - } + $accept = $headers->get('Accept'); + if (($match = $accept->match('application/json, application/javascript')) == false) { + return; + } - if ($match) { - // application/javascript Accept header found - if (false != ($callback = $request->getQuery()->get('callback'))) { - $this->renderer->setJsonpCallback($callback); - } - return $this->renderer; - } + if ($match->getFormat() == 'json') { + // application/json Accept header found + return $this->renderer; + } + + // application/javascript Accept header found + if (false != ($callback = $request->getQuery()->get('callback'))) { + $this->renderer->setJsonpCallback($callback); } - // Not matched! - return; + return $this->renderer; } /**