QBit::Application - base class for create applications.
It union all project models.
timelog
Initialization application.
It is done:
Set options ApplicationPath and FrameworkPath
Read all configs
Install die handler if needed
Set default locale
Initialization accessors (see "set_accessors")
Preload accessors if needed
No arguments.
Example:
my $app = Application->new(); # Application based on QBit::Application
Set accessors. Initialization accessors - one of the steps in sub "init". If you used set_accessors after init, call sub init_accessors.
You can use standard way for set accessors
use Application::Model::Users accessor => 'users'; use QBit::Application::Model::RBAC::DB accessor => 'rbac', models => {db => 'app_db'};
But use this method preferable.
Reserved keys:
accessor
package
models
init
app_pkg
Arguments:
%accessors - Accessors (type: hash). Keys is accessor name, values is options for import.
__PACKAGE__->set_accessors( app_db => { package => 'Application::Model::DB', # key "package" required (Package name) }, rbac => { package => 'QBit::Application::Model::RBAC::DB', models => { # key "models" redefine accessors into rbac db => 'app_db' }, }, ); # or run time $app->set_accessors(...); $app->init_accessors(); #after $app->app_db; # returns object of a class "Application::Model::DB" $app->rbac; # returns object of a class "QBit::Application::Model::RBAC::DB" $app->rbac->db; # returns object of a class "Application::Model::DB", but into package used "QBit::Application::Model::DB::RBAC"
Initialization accessors. Used after calling set_accessors in run time a code
$app->set_accessors(...); $app->init_accessors();
Set options in config
%opts - Options (type: hash)
__PACKAGE__->config_opts(param_name => 'Param'); # later in your code: my $param = $app->get_option('param_name'); # 'Param'
Set a file in config queue. The configuration is read in sub "init". In the same place are set the settings ApplicationPath and FrameworkPath.
QBit::Application options:
locales - type: hash
locales => { ru => {name => 'Русский', code => 'ru_RU', default => 1}, en => {name => 'English', code => 'en_GB'}, },
preload_accessors - type: int, values: 1/0 (1 - preload accessors, 0 - lazy load, default: 0)
install_die_handler - type: int, values: 1/0 (1 - set die handler qbit::Exceptions::die_handler, default: 0)
timelog_class - type: string, values: QBit::TimeLog::XS/QBit::TimeLog (default: QBit::TimeLog - this is not a production solution, in production use XS version)
locale_domain - type: string, value: <your domain> (used in set_locale for Locale::Messages::textdomain, default: 'application')
find_app_mem_cycle - type: int, values: 1/0 (1 - find memory cycle in post_run, used Devel::Cycle, default: 0)
QBit::WebInterface options:
error_dump_dir - type: string, value: <your path for error dumps>
salt - type: string, value: <your salt> (used for generate csrf token)
TemplateCachePath - type: string, value: <your path for template cache> (default: "/tmp")
show_timelog - type: int, values: 1/0 (1 - view timelog in html footer, default: 0)
TemplateIncludePaths - type: array of a string: value: [<your path for templates>]
already used: - <project_path>/templates # project_path = $self->get_option('ApplicationPath') - <framework_path>/QBit/templates # framework_path = $self->get_option('FrameworkPath')
QBit::WebInterface::Routing options:
controller_class - type: string, value: <your controller class> (default: QBit::WebInterface::Controller)
use_base_routing - type: int, values: 1/0 (1 - also use routing from QBit::WebInterface::Controller, 0 - only use routing from QBit::WebInterface::Routing)
$filename - Config name (type: string)
__PACKAGE__->use_config('Application.cfg'); # or __PACKAGE__->use_config('Application.json'); # later in your code: my preload_accessors = $app->get_option('preload_accessors');
read config by path or name from folder "configs".
> tree ./Project Project ├── configs │ └── Application.cfg └── lib └── Application.pm
Formats:
cfg - perl code
> cat ./configs/Application.cfg preload_accessors => 1, timelog_class => 'QBit::TimeLog::XS', locale_domain => 'domain.local', TemplateIncludePaths => ['${ApplicationPath}lib/QBit/templates'],
json - json format
> cat ./configs/Application.json { "preload_accessors" : 1, "timelog_class" : "QBit::TimeLog::XS", "locale_domain" : "domain.local", "TemplateIncludePaths" : ["${ApplicationPath}lib/QBit/templates"] }
Return value: Options (type: ref of a hash)
my $config = $app->read_config('Application.cfg');
Returns option value by name
$name - Option name (type: string)
$default - Default value
Return value: Option value
my $salt = $app->get_option('salt', 's3cret'); my $stash = $app->get_option('stash', {});
Set option value by name.
$value - Option value
$app->set_option('salt', 's3cret'); $app->set_option('stash', {key => 'val'});
set or get current user
$user - hash ref
Return value: hash ref
my $user = {id => 1}; $cur_user = $app->cur_user($user); # set current user # if use rbac # {id => 1, roles => {3 => {id => 3, name => 'ROLE 3', description => 'ROLE 3'}}, rights => ['RIGHT1', 'RIGHT2']} # or # {id => 1} $cur_user = $app->cur_user(); # return current user or {} $app->cur_user({}); # remove current user
set rights for current user
$rights - array ref
$app->set_cur_user_rights([qw(RIGHT1 RIGHT2)]);
revoke rights for current user
$app->revoke_cur_user_rights([qw(RIGHT1 RIGHT2)]);
refresh rights for current user
my $cur_user_id = $app->cur_user()->{'id'}; $app->rbac->set_user_role($cur_user_id, 3); # role_id = 3 $app->refresh_rights();
Returns all models.
Return value: $models - ref of a hash
Examples:
my $models = $app->get_models(); # $models = { # users => 'Application::Model::Users', # ... # }
Returns all registered rights
Return value: ref of a hash
my $registered_rights = $app->get_registered_rights(); # $registered_rights = { # view_all => { # name => 'Right to view all elements', # group => 'elemets' # }, # ... # }
Returns all registered right groups.
Return value: $registered_right_groups - ref of a hash
my $registered_right_groups = $app->get_registered_right_groups(); # $registered_right_groups = { # elements => 'Elements', # }
Check rights for current user.
@rights - array of strings or array ref
Return value: boolean
$app->check_rights('RIGHT1', 'RIGHT2'); # TRUE if has rights 'RIGHT1' and 'RIGHT2' $app->check_rights(['RIGHT1', 'RIGHT2']); # TRUE if has rights 'RIGHT1' or 'RIGHT2'
Set locale for Application.
$locale_id - type: string, values: from config (key "locales")
$app->set_app_locale('ru');
Set temporary locale.
Return value: $tmp_locale - object QBit::Application::_Utils::TmpLocale
my $tmp_locale = $app->set_tmp_app_locale('ru'); #restore locale undef($tmp_locale);
Add temporary rights.
@rights - Rights (type: array of a string)
Return value: $tmp_rights - object QBit::Application::_Utils::TmpRights
my $tmp_rights = $app->add_tmp_rights('view_all', 'edit_all'); #restore rights undef($tmp_rights);
Called before the request is processed.
Resets current user
Refresh options
Resets timelog
Call "pre_run" for models
$app->pre_run();
Called after the request is processed.
Call "post_run" for models
Finish timelog
Call "process_timelog"
Find memory cycles and call "process_mem_cycles" if needed
$app->post_run();
Process memory cycles
$cycles - Cycles. (result: Devel::Cycle::find_cycle)
Return value: $text - info (type: string)
Process time log. Empty method.
To install QBit::Application, copy and paste the appropriate command in to your terminal.
cpanm
cpanm QBit::Application
CPAN shell
perl -MCPAN -e shell install QBit::Application
For more information on module installation, please visit the detailed CPAN module installation guide.