NAME

Bread::Board::LazyLoader::Supersite - loads the proper IOC root with your Bread::Board setup

VERSION

version 0.14

SYNOPSIS

    package MyApp::IOC;
    use strict;
    use warnings;

    use Bread::Board::LazyLoader::SuperSite 
        env_var => 'MY_APP_SITE',
        site => {
            prefix => 'My::Site',
            filter => qr{^[a-z]}
        };

    # in scripts, psgi apps

    use MyApp::IOC;

    my $root = MyApp::Root->root;
    $root->fetch('Scripts/MyApp-Web')->get()->run();

DESCRIPTION

This module creates a single proxy subroutine for IOC root, which may be loaded from different modules (for example national specific).

Better with example:

We have two instances of our application, czech and slovak, with IOC roots implemented in MyApp::Site::cz->root and MyApp::Site::sk->root. In each instance of our app only one this modules is installed.

Most of the scripts (tests, psgi files, ...) referencing the IOC root are not nationally specific, so we prefer them to use some common name.

Having defined "dispatcher" ioc module like this:

   package MyApp::IOC;
   use strict;

   use Bread::Board::LazyLoader::SuperSite 
        site => {
                prefix => 'MyApp::Site',
                filter => qr{^[a-z]},
        };

   1;

We can use MyApp::IOC->root uniformly to get our IOC root of the application, which returns either MyApp::Site::cz->root or MyApp::Site::sk->root depending on site.

Import looks through all MyApp::Site::* installed modules and tries to find one with next part starting with lowercase letter (lowercase, so that our base IOC module MyApp::Site::Core is not found). There must be exactly one such module or use Bread::Board::LazyLoader::Supersite fails.

import parameters

   use Bread::Board::LazyLoader::Supersite %params;
env_var=NAME

The content of environment variable (if set) is used as site module (the one with root method). There may be more than one modules separated by semicolon inside env var.

With

    package MyApp::IOC;
    use strict;
    use warnings;

    use Bread::Board::LazyLoader::SuperSite env_var => 'MY_APP_SITE';

and

    MY_APP_SITE='MyApp::Site::Sandbox;MyApp::Site::cz'

then

   MyApp::IOC->root

returns

   MyApp::Site::Sandbox->root( MyApp::Site::cz->root )

Environment variable may even contain paths:

    MY_APP_SITE="$HOME/app/sandbox.ioc;MyApp::Site::cz"

With $HOME/app/sandbox.ioc

    use Bread::Board;
    sub {
        my $c = shift;

        # $c here is MyApp::Site::cz->root

        container $c => as {
                service dbh => some_mocked_dbh();
        };
    };

If env_var option is used (and the appropriate variable set), it has priority over site option.

site

Used either like site => $module or site => { prefix => $module_prefix, filter => $name_filter_re }.

Dispatches the root to appropriate module. There may be just one.

AUTHOR

Roman Daniel <roman@daniel.cz>

COPYRIGHT AND LICENSE

This software is copyright (c) 2016 by Roman Daniel.

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