Nile/Plugin.pm

filter: Statementstmt 78.3% 47 / 60 Branchbran 40.0% 4 / 10 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 83.3% 10 / 12 Podpod 0.0% 0 / 1 totaltotal 65.5% 61 / 93 CC 8 SCAR 22.3
CC
8
Cov
67%
setting
30.0
import
4.0
__ANON__
1.0
CRAP 9.3
« Nile/DBI/Table.pm ↑ Summary Nile/Plugin/Object.pm »
1 # Copyright Infomation
2 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3 # Author : Dr. Ahmed Amin Elsheshtawy, Ph.D.
4 # Website: https://github.com/mewsoft/Nile, http://www.mewsoft.com
5 # Email : mewsoft@cpan.org, support@mewsoft.com
6 # Copyrights (c) 2014-2015 Mewsoft Corp. All rights reserved.
7 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 package Nile::Plugin;
9  
10 1 our $VERSION = '0.55';
11 1 our $AUTHORITY = 'cpan:MEWSOFT';
12  
13 =pod
14  
15 =encoding utf8
16  
17 =head1 NAME
18  
19 Nile::Plugin - Plugin base class for the Nile framework.
20  
21 =head1 SYNOPSIS
22
23 =head1 DESCRIPTION
24  
25 Nile::Plugin - Plugin base class for the Nile framework.
26  
27 This module is the base class for plugins. You include it by using it which also makes itself as a parent class for the plugin and inherts
28 the method setting which has the plugin setting loaded automatically from the config files.
29  
30 Creating your first plugin C<Hello> is simple like that, just create a module file called C<Hello.pm> in the folder C<Nile/Plugin> and
31 put the following code in it:
32  
33 package Nile::Plugin::Hello;
34
35 our $VERSION = '0.55';
36
37 # this also extends Nile::Plugin, the plugin base class
38 use Nile::Plugin;
39
40 # optional our alternative for sub new {} called automaticall on object creation
41 sub main {
42  
43 my ($self, $arg) = @_;
44
45 # plugin settings from config files section
46 my $setting = $self->setting();
47 #same as
48 #my $setting = $self->setting("hello");
49
50 # get app context
51 my $app = $self->app;
52  
53 # good to setup hooks here
54 # run this hook after the "start" method
55 $app->hook->after_start( sub {
56 my ($me, @args) = @_;
57 #...
58 });
59
60 }
61
62 sub welcome {
63 my ($self) = @_;
64 return "Hello world";
65 }
66  
67 1;
68  
69 Then inside other modules or plugins you can access this plugin as
70
71 # get the plugin object
72 $hello = $app->plugin->hello;
73
74 # or
75 $hello = $app->plugin("Hello");
76
77 # if plugin name has sub modules
78 my $redis = $app->plugin("Cache::Redis");
79
80 # call plugin method
81 say $app->plugin->hello->welcome;
82  
83 # in general, you access plugins like this:
84 $app->plugin->your_plugin_name->your_plugin_method([args]);
85  
86 Plugins will be loaded automatically on the first time it is used and can be load on application startup in the C<init> method:
87  
88 $app->init({
89 plugin => [ qw(hello) ],
90 });
91  
92 Plugins also can be loaded on application startup by setting the C<autoload> variable in the plugin configuration in the
93 config files.
94  
95 Example of plugin configuration to auto load on application startup:
96  
97 <plugin>
98  
99 <hello>
100 <autoload>1</autoload>
101 </hello>
102  
103 </plugin>
104  
105 At the plugin load, the plugin optional method C<main> will be called automatically if it exists, this is an alternative for the method C<new>.
106  
107 Inside the plugin methods, you access the application context by the injected method C<app> and you use it in this way:
108  
109 my $app = $self->app;
110 $app->request->param("name");
111 ...
112 $app->config->get("email");
113  
114 Plugins that setup C<hooks> must be set to autoload on startup for hooks to work as expected.
115  
116 =cut
117  
118 1 ▶use Nile::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
119 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
120 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
121 1 ▶use MooseX::Declare;
Subroutine BEGIN: called CC=1 SCAR=0.0
122 1 ▶use MooseX::MethodAttributes;
Subroutine BEGIN: called CC=1 SCAR=0.0
123  
124 1 ▶use Import::Into;
Subroutine BEGIN: called CC=1 SCAR=0.0
125 1 ▶use Module::Runtime qw(use_module);
Subroutine BEGIN: called CC=1 SCAR=0.0
126  
127 1 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
128 1 ▶no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
129 # disable the auto immutable feature of Moosex::Declare, or use class Nile::Home is mutable {...}
130 0 ▶*{"MooseX::Declare::Syntax::Keyword::Class" . '::' . "auto_make_immutable"} = sub { 0 };
Subroutine __ANON__: not called CC=1 SCAR=0.4
131 #around auto_make_immutable => sub { 0 };
132  
133 1 our @EXPORT_MODULES = (
134 Moose => [],
135 utf8 => [],
136 'Nile::Say' => [],
137 'MooseX::Declare' => [],
138 'MooseX::MethodAttributes' => [],
139 );
140 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
141 2 ▶sub import {
Subroutine import: called CC=4 SCAR=13.9
142  
143 2 my ($class, @args) = @_;
144  
145 2 my ($caller, $script) = caller;
146  
147 2 my $package = __PACKAGE__;
148
149 # ignore calling from child import
150 2 ▶ return if ($class ne $package);
Branch: 1 branch
BranchTrueFalseTotal
if $class ne $package 0 2 2
151  
152 2 my @modules = @EXPORT_MODULES;
153  
154 2 ▶ while (@modules) {
Branch: 1 branch
BranchTrueFalseTotal
if (@modules) 10 2 12
155 10 my $module = shift @modules;
156 10 ▶ my $imports = ref($modules[0]) eq 'ARRAY' ? shift @modules : [];
Branch: 1 branch
BranchTrueFalseTotal
ref $modules[0] eq 'ARRAY' ? : 10 0 10
157 10 use_module($module)->import::into($caller, @{$imports});
158 }
159  
160 {
161 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
162 2 @{"${caller}::ISA"} = ($package, @{"${caller}::ISA"});
163 }
164  
165 }
166 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
167 =head2 setting()
168
169 # inside plugin classes, return current plugin class config settings
170 my $setting = $self->setting();
171 my %setting = $self->setting();
172  
173 #
174 # inside plugin classes, return specific plugin class config settings
175 my $setting = $self->setting("email");
176 my %setting = $self->setting("email");
177  
178 Returns plugin class settings from configuration files loaded.
179  
180 Helper plugin settings in config files must be in inside the plugin tag. The plugin class name can be lower case tag, so plugin C<Email> can be C<email>.
181  
182 Exampler settings for C<email> and C<cache> plugins class below:
183  
184 <plugin>
185 <email>
186 <transport>Sendmail</transport>
187 <sendmail>/usr/sbin/sendmail</sendmail>
188 </email>
189 <cache>
190 <autoload>1</autoload>
191 </cache>
192 </plugin>
193  
194 =cut
195  
196 0 ▶sub setting {
Subroutine setting: not called CC=5 SCAR=34.0
Pod undocumented
197  
198 0 my ($self, $plugin) = @_;
199  
200 0 ▶ $plugin ||= caller();
Condition0%0/3
$plugin ||= caller()
l!l&&r!l&&!r
000
MC/DC0%0/2
$plugin ||= caller()
$plugin caller()
Truth table0%0/3
A ||= B
A= $pluginB= caller()
ABresult
1X1
011
000
201
202 #$plugin =~ s/^(.*):://;
203  
204 0 $plugin =~ s/^Nile::Plugin:://;
205 0 $plugin =~ s/::/_/g;
206  
207 0 $plugin = lc($plugin);
208  
209 0 my $app = $self->app;
210
211 # access plugin name as "email" or "Email"
212 0 ▶ if (!exists $app->config->get("plugin")->{$plugin} && exists $app->config->get("plugin")->{ucfirst($plugin)}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $app->config->get("plugin")->{$plugin} and exists $app->config->get("plugin")->{ucfirst $plugin}) 0 0 0
Condition0%0/3
not exists $app->config->get("plugin")->{$plugin} and exists $app->config->get("plugin")->{ucfirst $plugin}
!ll&&!rl&&r
000
MC/DC0%0/2
not exists $app->config->get("plugin")->{$plugin} and exists $app->config->get("plugin")->{ucfirst $plugin}
not exists $app->config->get("plugin")->{$plugin} exists $app->config->get("plugin")->{ucfirst $plugin}
Truth table0%0/3
not(A) and B
A= not exists $app->config->get("plugin")->{$plugin}B= exists $app->config->get("plugin")->{ucfirst $plugin}
ABresult
0X0
100
111
213 0 $plugin = ucfirst($plugin);
214 }
215
216 0 my $setting = $app->config->get("plugin")->{$plugin};
217  
218 0 delete $setting->{autoload};
219  
220 0 ▶ return wantarray ? %{$setting} : $setting;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
221 }
222 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
223  
224 =pod
225  
226 =head1 Bugs
227  
228 This project is available on github at L<https://github.com/mewsoft/Nile>.
229  
230 =head1 HOMEPAGE
231  
232 Please visit the project's homepage at L<https://metacpan.org/release/Nile>.
233  
234 =head1 SOURCE
235  
236 Source repository is at L<https://github.com/mewsoft/Nile>.
237  
238 =head1 SEE ALSO
239  
240 See L<Nile> for details about the complete framework.
241  
242 =head1 AUTHOR
243  
244 Ahmed Amin Elsheshtawy, احمد امين الششتاوى <mewsoft@cpan.org>
245 Website: http://www.mewsoft.com
246  
247 =head1 COPYRIGHT AND LICENSE
248  
249 Copyright (C) 2014-2015 by Dr. Ahmed Amin Elsheshtawy احمد امين الششتاوى mewsoft@cpan.org, support@mewsoft.com,
250 L<https://github.com/mewsoft/Nile>, L<http://www.mewsoft.com>
251  
252 This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
253  
254 =cut
255  
256 1 1;
« Nile/DBI/Table.pm ↑ Summary Nile/Plugin/Object.pm »