diff Lib/IMPL/Web/View/TTDocument.pm @ 305:b5d5793f348e

TTView refactoring, still experiencing memory leaks
author sergey
date Wed, 17 Apr 2013 18:00:19 +0400
parents 2ff513227cb4
children 2da2564f115d
line wrap: on
line diff
--- a/Lib/IMPL/Web/View/TTDocument.pm	Mon Apr 15 07:44:50 2013 +0400
+++ b/Lib/IMPL/Web/View/TTDocument.pm	Wed Apr 17 18:00:19 2013 +0400
@@ -9,6 +9,7 @@
 
 use IMPL::declare {
     require => {
+    	TTRegistry => 'IMPL::Web::View::TTRegistry',
         TTFactory => 'IMPL::Web::View::TTFactory',
         TTControl =>  'IMPL::Web::View::TTControl',
         Loader => 'IMPL::Code::Loader'
@@ -41,54 +42,16 @@
     my ($this,$args) = @_;
 
     $args ||= {};
-
-    my %controls;
-    my $require;
-    my $documentContext;
     
-    my $self = $this;
+    $this->context->localise(); # localise stash
+    my $documentContext = _clone_context( $this->context ); # create a new context
     
-    $require = sub {
-        my $control = shift;
-        
-        unless($self) {
-            carp("Cant load control $control outside the document rendering process");
-            return;
-        }
-        
-        if (my $factory = $controls{$control}) {
-            return $factory;
-        } else {
-            my $path = $control;
-            
-            if ( my $template = $self->loader->template($path) ) {
-                
-                $documentContext->localise();
-                my $ctx = _clone_context($documentContext);
-                $documentContext->delocalise();
-               
-                $factory = new IMPL::Web::View::TTFactory(
-                    $template,
-                    $ctx,
-                    join( '/', splice( @{[split(/\//,$path)]}, 0, -1 ) ),
-                    $require
-                );
-                
-                $controls{$control} = $factory;
-                            
-                return $factory;
-    
-            } else {
-                die new IMPL::KeyNotFoundException($control);
-            }
-        }
-    };
-    
-    $this->context->localise();
-    $documentContext = _clone_context( $this->context );
-    
-    $this->context->stash->set(require => $require);
-    $this->context->stash->set(document => sub { $self });
+    my $registry = TTRegistry->new($this->loader, $documentContext);
+
+    $this->context->stash->update({
+    	require => sub { $registry->Require(shift) },
+    	document => $this
+    });
     
     my $text = eval {
     
@@ -122,10 +85,8 @@
     
     my $e = $@;
 
-    undef $require;
-    undef $documentContext;    
-    undef %controls;
-    undef $self;
+    $registry->Dispose();
+    undef $registry;    
     $this->context->delocalise();
     
     if ($e) {