line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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
|
|
|
|
|
|
|
our $VERSION = '0.55'; |
11
|
|
|
|
|
|
|
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
|
|
|
1
|
|
322
|
use Nile::Base; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
6
|
|
119
|
|
|
|
|
|
|
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
120
|
1
|
|
|
1
|
|
5345
|
use Moose; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
4
|
|
121
|
1
|
|
|
1
|
|
1477
|
use MooseX::Declare; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
5
|
|
122
|
1
|
|
|
1
|
|
1523
|
use MooseX::MethodAttributes; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
6
|
|
123
|
|
|
|
|
|
|
|
124
|
1
|
|
|
1
|
|
1779
|
use Import::Into; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
25
|
|
125
|
1
|
|
|
1
|
|
3
|
use Module::Runtime qw(use_module); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
9
|
|
126
|
|
|
|
|
|
|
|
127
|
1
|
|
|
1
|
|
37
|
no warnings 'redefine'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
35
|
|
128
|
1
|
|
|
1
|
|
4
|
no strict 'refs'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
266
|
|
129
|
|
|
|
|
|
|
# disable the auto immutable feature of Moosex::Declare, or use class Nile::Home is mutable {...} |
130
|
0
|
|
|
0
|
|
0
|
*{"MooseX::Declare::Syntax::Keyword::Class" . '::' . "auto_make_immutable"} = sub { 0 }; |
131
|
|
|
|
|
|
|
#around auto_make_immutable => sub { 0 }; |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
our @EXPORT_MODULES = ( |
134
|
|
|
|
|
|
|
Moose => [], |
135
|
|
|
|
|
|
|
utf8 => [], |
136
|
|
|
|
|
|
|
'Nile::Say' => [], |
137
|
|
|
|
|
|
|
'MooseX::Declare' => [], |
138
|
|
|
|
|
|
|
'MooseX::MethodAttributes' => [], |
139
|
|
|
|
|
|
|
); |
140
|
|
|
|
|
|
|
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
141
|
|
|
|
|
|
|
sub import { |
142
|
|
|
|
|
|
|
|
143
|
2
|
|
|
2
|
|
5
|
my ($class, @args) = @_; |
144
|
|
|
|
|
|
|
|
145
|
2
|
|
|
|
|
6
|
my ($caller, $script) = caller; |
146
|
|
|
|
|
|
|
|
147
|
2
|
|
|
|
|
2
|
my $package = __PACKAGE__; |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
# ignore calling from child import |
150
|
2
|
50
|
|
|
|
8
|
return if ($class ne $package); |
151
|
|
|
|
|
|
|
|
152
|
2
|
|
|
|
|
8
|
my @modules = @EXPORT_MODULES; |
153
|
|
|
|
|
|
|
|
154
|
2
|
|
|
|
|
6
|
while (@modules) { |
155
|
10
|
|
|
|
|
9961
|
my $module = shift @modules; |
156
|
10
|
50
|
|
|
|
24
|
my $imports = ref($modules[0]) eq 'ARRAY' ? shift @modules : []; |
157
|
10
|
|
|
|
|
33
|
use_module($module)->import::into($caller, @{$imports}); |
|
10
|
|
|
|
|
243
|
|
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
{ |
161
|
1
|
|
|
1
|
|
5
|
no strict 'refs'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
258
|
|
|
2
|
|
|
|
|
8006
|
|
162
|
2
|
|
|
|
|
2
|
@{"${caller}::ISA"} = ($package, @{"${caller}::ISA"}); |
|
2
|
|
|
|
|
122
|
|
|
2
|
|
|
|
|
9
|
|
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
|
|
|
|
|
|
|
sub setting { |
197
|
|
|
|
|
|
|
|
198
|
0
|
|
|
0
|
0
|
|
my ($self, $plugin) = @_; |
199
|
|
|
|
|
|
|
|
200
|
0
|
|
0
|
|
|
|
$plugin ||= caller(); |
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
|
0
|
0
|
|
|
|
if (!exists $app->config->get("plugin")->{$plugin} && exists $app->config->get("plugin")->{ucfirst($plugin)}) { |
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
|
0
|
|
|
|
|
return wantarray ? %{$setting} : $setting; |
|
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; |