Bread::Board::LazyLoader::Supersite - loads the proper IOC root with your Bread::Board setup
version 0.14
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();
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.
MyApp::Site::cz->root
MyApp::Site::sk->root
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.
MyApp::IOC->root
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.
MyApp::Site::*
MyApp::Site::Core
use Bread::Board::LazyLoader::Supersite
use Bread::Board::LazyLoader::Supersite %params;
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
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
$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.
env_var
site
Used either like site => $module or site => { prefix => $module_prefix, filter => $name_filter_re }.
site => $module
site => { prefix => $module_prefix, filter => $name_filter_re }
Dispatches the root to appropriate module. There may be just one.
root
Roman Daniel <roman@daniel.cz>
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.
To install Bread::Board::LazyLoader, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Bread::Board::LazyLoader
CPAN shell
perl -MCPAN -e shell install Bread::Board::LazyLoader
For more information on module installation, please visit the detailed CPAN module installation guide.