diff Lib/IMPL/Web/Handler/ViewSelector.pm @ 229:47f77e6409f7

heavily reworked the resource model of the web application: *some ResourcesContraact functionality moved to Resource +Added CustomResource *Corrected action handlers
author sergey
date Sat, 29 Sep 2012 02:34:47 +0400
parents d6e2ea24af08
children 3cebcf6fdb9b
line wrap: on
line diff
--- a/Lib/IMPL/Web/Handler/ViewSelector.pm	Thu Sep 13 17:55:01 2012 +0400
+++ b/Lib/IMPL/Web/Handler/ViewSelector.pm	Sat Sep 29 02:34:47 2012 +0400
@@ -4,67 +4,68 @@
 use IMPL::lang qw(:declare :constants);
 
 use IMPL::declare {
-	require => {
-		NotAcceptable => 'IMPL::Web::NotAcceptableException',
-		ViewResult => 'IMPL::Web::Application::ViewResult'
-	},
-	base => {
-		'IMPL::Object' => undef,
-		'IMPL::Object::Autofill' => '@_',
-		'IMPL::Object::Serializable' => undef
-	}
+    require => {
+        NotAcceptable => 'IMPL::Web::NotAcceptableException',
+        HttpResponse  => 'IMPL::Web::HttpResponse'
+      },
+      base => [
+        'IMPL::Object'               => undef,
+        'IMPL::Object::Autofill'     => '@_',
+        'IMPL::Object::Serializable' => undef
+      ],
+      props => [
+        views    => PROP_ALL | PROP_LIST,
+        fallback => PROP_ALL,
+        types    => PROP_ALL
+      ]
 };
 
-BEGIN {
-	public property views => PROP_ALL | PROP_LIST;
-	public property fallback => PROP_ALL;
-	public property types => PROP_ALL;
-}
-
 sub Invoke {
-	my ($this,$action,$next) = @_;
-	
-	my $result = $next ? $next->($action) : undef;
-	
-	my $model;
-	
-	if( eval { $result->isa(ViewResult) } ) {
-	
-		my $handler;
-		my $path = $action->query->path_info;
-		
-		if ($this->types and $path =~ m/\.(\w+)$/) {
-			my $forced;	
-			if ($forced = $this->types->{$1} and $action->query->Accept($forced) ) {
-				($handler) = grep eval { $_->can('contentType') } && $_->contentType eq $forced, $this->views;
-			}
-		}
-		
-		if (not $handler) {
-		
-			my @handlers =
-		        sort {
-		            $b->{preference} <=> $a->{preference}
-		        } map {
-		            {
-		            	handler => $_,
-		                preference =>
-		                    eval { $_->can('contentType') } ? $action->query->Accept($_->contentType) : 0
-		            }
-				} $this->views;
-		
-		    my $info = shift @handlers;
-		    $handler = $info ? $info->{handler} : undef;
-	    
-		}
-	    
-	    die NotAcceptable->new(map { eval {$_->can('contentType') } ? $_->contentType : () } $this->views )
-	        unless $handler;
-	        
-	    return $handler->Invoke($action,sub { $result });
-	} else {
-		return $result;
-	}
+    my ( $this, $action, $next ) = @_;
+
+    my $result = $next ? $next->($action) : undef;
+
+    my $model;
+
+    return $result if eval { $result->isa(HttpResponse) };
+
+    my $handler;
+    my $path = $action->pathInfo;
+
+    if ( $this->types and $path =~ m/\.(\w+)$/ ) {
+        my $forced;
+        if ( $forced = $this->types->{$1} and $action->query->Accept($forced) )
+        {
+            ($handler) =
+              grep eval { $_->can('contentType') }
+              && $_->contentType eq $forced, $this->views;
+        }
+    }
+
+    if ( not $handler ) {
+
+        my @handlers =
+          sort { $b->{preference} <=> $a->{preference} } map {
+            {
+                handler    => $_,
+                preference => eval { $_->can('contentType') }
+                ? $action->query->Accept( $_->contentType )
+                : 0
+            }
+          } $this->views;
+
+        my $info = shift @handlers;
+        $handler = $info ? $info->{handler} : undef;
+
+    }
+
+    die NotAcceptable->new(
+        map {
+            eval { $_->can('contentType') } ? $_->contentType : ()
+        } $this->views
+    ) unless $handler;
+
+    return $handler->Invoke( $action, sub { $result } );
 }
 
 1;
@@ -96,4 +97,4 @@
 
 Хеш с соотвествием между расширением и типом содержимого, для подсказки при выборе представления.
 
-=cut
\ No newline at end of file
+=cut