[Templates-cvs] cvs commit: TT3/lib/Template Context.pm

cvs@template-toolkit.org cvs@template-toolkit.org
Sun, 12 Dec 2004 16:12:46 +0000


cvs         04/12/12 16:12:45

  Modified:    lib/Template Context.pm
  Log:
  * added init_resources() and removed all reference to compilers
    which are now defined along with the other resources
  
  Revision  Changes    Path
  1.9       +59 -12    TT3/lib/Template/Context.pm
  
  Index: Context.pm
  ===================================================================
  RCS file: /template-toolkit/TT3/lib/Template/Context.pm,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- Context.pm	2004/12/10 18:52:04	1.8
  +++ Context.pm	2004/12/12 16:12:45	1.9
  @@ -16,7 +16,7 @@
   #   modify it under the same terms as Perl itself.
   #
   # REVISION
  -#   $Id: Context.pm,v 1.8 2004/12/10 18:52:04 abw Exp $
  +#   $Id: Context.pm,v 1.9 2004/12/12 16:12:45 abw Exp $
   #
   #========================================================================
   
  @@ -25,7 +25,6 @@
   use strict;
   use warnings;
   use Template::Resources;
  -use Template::Compilers;
   use Template::Provider;
   use Template::Resource::Template;
   use Template::Scope;
  @@ -39,13 +38,12 @@
   use constant FETCH => 'fetch';
   use constant GET   => 'get';
   
  -our $VERSION   = sprintf("%d.%02d", q$Revision: 1.8 $ =~ /(\d+)\.(\d+)/);
  +our $VERSION   = sprintf("%d.%02d", q$Revision: 1.9 $ =~ /(\d+)\.(\d+)/);
   our $DEBUG     = 0 unless defined $DEBUG;
   our $ERROR     = '';
   our $THROW     = 'context';
   our $UTILS     = 'Template::Utils';
   our $RESOURCES = 'Template::Resources' unless defined $RESOURCES;
  -our $COMPILERS = 'Template::Compilers' unless defined $COMPILERS;
   our $TEMPLATES = 'Template::Provider'  unless defined $TEMPLATES;
   our $EXCEPTION = 'Template::Exception' unless defined $EXCEPTION;
   
  @@ -66,14 +64,9 @@
       # copy all keys in
       @$self{ keys %$config } = values %$config;
   
  -    $self->{ resources } = $config->{ resources } 
  -        || $self->pkgvar( RESOURCES => $RESOURCES );
  +    $self->init_resources($config) || return;
   
  -    $self->debug("created resource: $self->{ resources }\n") if $DEBUG;
  -
  -    $self->{ compilers } = $config->{ compilers } 
  -        || $self->pkgvar( COMPILERS => $COMPILERS );
  -
  +    # hack to make template_path work until we fix option handling properly
       $self->{ templates } = $config->{ templates } || do {
           my $templates = $self->pkgvar( TEMPLATES => $TEMPLATES );
           $templates->new($extract->{ template })
  @@ -85,6 +78,60 @@
   }
   
   
  +sub init_resources {
  +    my ($self, $config) = @_;
  +
  +    $self->debug("init_resources(", $self->dump_hash($config), ")\n") if $DEBUG;
  +
  +    # fetch $RESOURCES package var and look for 'resources' in config
  +    my $pkgres = $self->pkgvar( RESOURCES => $RESOURCES );
  +    my $cfgres = $config->{ resources };
  +
  +    if ($cfgres) {
  +        # handle the user-provided 'resources' config option
  +        if (UNIVERSAL::isa($cfgres, $pkgres)) {
  +            # a Template::Resources object or class
  +            if (ref $cfgres) {
  +                # a resources object is used as it is
  +                return $self->{ resources } = $cfgres;
  +            }
  +            else {
  +                # a resources class name is used to create a new object
  +                # but we don't want to confuse it with a 'resources'
  +                # option that points back to itself, so out it goes
  +                delete $config->{ resources };
  +                return $self->{ resource } = $cfgres->new($config)
  +                    || $self->error($cfgres->error());
  +            }
  +        }
  +        elsif (ref $cfgres eq 'HASH') {
  +            # a hash array of options which we pass to the object
  +            # created from the default $RESOURCES class, along with 
  +            # all the other config data
  +            $self->debug("creating new $pkgres resources\n") if $DEBUG;
  +            return $self->{ resources } = $pkgres->new($config)
  +                || $self->error($cfgres->error());
  +        }
  +        else {
  +            return $self->error("invalid resources option: $cfgres");
  +        }
  +    }
  +    elsif (defined $cfgres) {
  +        # defined but false, e.g. resources => 0, disables all 
  +        # resources, so do nothing but return true to indicate success
  +        return 1;
  +    }
  +    else {
  +        # no resources defined in config, so create a default
  +        # set using the class name defined in $RESOURCES pkg var
  +        return $self->{ resources } = $pkgres->new($config)
  +            || $self->error($pkgres->error());
  +    }
  +}
  +
  +
  +
  +
   # not sure if we need all these... how about an AUTOLOAD that walks
   # the right chains?
   
  @@ -224,7 +271,7 @@
   
   =head1 VERSION
   
  -$Revision: 1.8 $
  +$Revision: 1.9 $
   
   =head1 COPYRIGHT