Eidolon::Core::Loader - Eidolon driver loader.


Eidolon documentation Contained in the Eidolon distribution.

Index


Code Index:

NAME

Top

Eidolon::Core::Loader - Eidolon driver loader.

SYNOPSIS

Top

This code must be placed in one of your application controllers. For example, in lib/Example/Controller/Default.pm:

    sub default : Default
    {
        my ($r, $tpl);

        $r   = Eidolon::Core::Registry->get_instance;
        $tpl = $r->loader->get_object("Eidolon::Driver::Template");

        if ($tpl) 
        {
            $tpl->parse("index.tpl");
            $tpl->render;
        }
    }

DESCRIPTION

Top

The Eidolon::Core::Loader package is the central part of Eidolon abstraction layer. It provides the unified interface for manipulations with vital application objects (drivers). You can load a new driver and gain access to loaded drivers with this package.

The driver being loaded must be inherited from Eidolon::Driver class to pass driver validation procedure.

The object of Eidolon::Core::Loader is mounted in application registry as $r->loader, so it can be used later by any application component.

METHODS

Top

new()

Class constructor.

load($class, @params)

Loads a $class driver and initializes it with given @params (if any).

get_object($class)

Returns an object of the loaded driver. If driver with given $class isn't loaded undef will be returned.

SEE ALSO

Top

Eidolon, Eidolon::Application, Eidolon::Driver, Eidolon::Core::Exceptions

LICENSE

Top

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

AUTHOR

Top

Anton Belousov, <abel@cpan.org>

COPYRIGHT

Top


Eidolon documentation Contained in the Eidolon distribution.

package Eidolon::Core::Loader;
# ==============================================================================
#
#   Eidolon
#   Copyright (c) 2009, Atma 7
#   ---
#   Eidolon/Core/Loader.pm - driver loader
#
# ==============================================================================

use Eidolon::Driver::Exceptions;
use warnings;
use strict;

our $VERSION  = "0.02"; # 2009-05-13 22:32:29

# ------------------------------------------------------------------------------
# \% new()
# constructor
# ------------------------------------------------------------------------------
sub new
{
    my ($class, $self);

    $class = shift;

    # class attributes
    $self = { "type_cache" => {} };

    bless $self, $class;
    
    return $self;
}

# ------------------------------------------------------------------------------
# load($class, @params)
# load a driver
# ------------------------------------------------------------------------------
sub load
{
    my ($self, $class, @params, $r);

    ($self, $class, @params) = @_;
    $r    = Eidolon::Core::Registry->get_instance;
    
    # loading driver
    eval "require $class";
    throw CoreError::Compile($@) if $@;
    
    throw CoreError::Loader::InvalidDriver($class) if !$class->isa("Eidolon::Driver");
    throw CoreError::Loader::AlreadyLoaded($class) if exists $self->{"type_cache"}->{$class};

    $self->{"type_cache"}->{$class} = $class->new( @params );
}

# ------------------------------------------------------------------------------
# \% get_object($class)
# get driver object
# ------------------------------------------------------------------------------
sub get_object
{
    my ($self, $class) = @_;

    # find object by class
    return $self->{"type_cache"}->{$class} if (exists $self->{"type_cache"}->{$class});

    # find driver by parent class
    foreach (keys %{ $self->{"type_cache"} })
    {
        return $self->{"type_cache"}->{$_} if ($_->isa($class));
    }

    return undef;
}

1;

__END__