Workflow

 view release on metacpan or  search on metacpan

lib/Workflow/Factory.pm  view on Meta::CPAN

sub import {
    my $class = shift;

    $class = ref $class || $class;    # just in case
    my $package = caller;
    if ( defined $_[0] && $_[0] eq 'FACTORY' ) {
        shift;
        my $instance;

        my $import_target = $package . '::FACTORY';
        no strict 'refs';
        unless ( defined &{$import_target} ) {
            *{$import_target} = sub {
                return $instance if $instance;
                $instance = _initialize_instance($class);
                return $instance;
            };
        }
    }
    $class->SUPER::import(@_);
}

require Workflow;
require Workflow::Action;
require Workflow::Condition;
require Workflow::Condition::Negated;
require Workflow::Config;
require Workflow::Context;
require Workflow::Persister;
require Workflow::State;
require Workflow::Validator;

my $DEFAULT_INITIAL_STATE = 'INITIAL';

my @FIELDS = qw(config_callback);

__PACKAGE__->mk_accessors(@FIELDS);

sub new {
    my $proto = shift;
    my $class = ref $proto || $proto;

    workflow_error "Please call 'instance()' or import the 'FACTORY' object ",
        "to get the '$class' object rather than instantiating a ",
        "new one directly.";
}

sub instance {
    my $proto = shift;
    my $class = ref $proto || $proto;

    return _initialize_instance($class);
}

sub _initialize_instance {
    my ($class) = @_;

    unless ( $INSTANCES{$class} ) {
        $log->debug( "Creating empty instance of '$class' factory for ",
                     "singleton use" );
        my $instance = bless {} => $class;
        $instance->init();
        $INSTANCES{$class} = $instance;
    }
    return $INSTANCES{$class};
}

sub _delete_instance {
    my ($class) = @_;

    if ( $INSTANCES{$class} ) {
        $log->debug( "Deleting instance of '$class' factory." );
        delete $INSTANCES{$class};
    } else {
        $log->debug( "No instance of '$class' factory found." );
    }

    return;
}

my %CONFIG = ( 'Workflow::Config' => 1 );

sub _add_config_from_files {
    my ($self, $method, $type, $config) = @_;

    foreach my $item ( @{ $config } ) {
        $self->$method(
            Workflow::Config->parse_all_files( $type, $item )
            );
    }

    return;
}

sub _add_config_from_file {
    my ($self, $method, $type, $config) = @_;

    if ( ref $config eq 'ARRAY' ) {
        $self->_add_config_from_files( $method, $type, $config );
    }
    else {
        $self->_add_config_from_files( $method, $type, [ $config ] );
    }

    return;
}

sub add_config_from_file {
    my ( $self, %params ) = @_;
    return unless ( scalar keys %params );

    _check_config_keys(%params);

    foreach my $type ( sort keys %params ) {
        $self->log->debug(
            sub { "Using '$type' configuration file(s): " .
                      join( ', ', _flatten( $params{$type} ) ) } );
    }

    $self->log->debug( "Adding condition configurations..." );
    $self->_add_config_from_file( \&_add_condition_config,



( run in 0.265 second using v1.01-cache-2.11-cpan-2b0bae70ee8 )