| 1 | package MPMinus; # $Id: MPMinus.pm 224 2017-04-04 10:27:41Z minus $ | ||
| 2 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | |||
| 4 | =head1 NAME | ||
| 5 | |||
| 6 | MPMinus - mod_perl2 Web Application Framework | ||
| 7 | |||
| 8 | =head1 VERSION | ||
| 9 | |||
| 10 | Version 1.20 | ||
| 11 | |||
| 12 | =head1 SYNOPSIS | ||
| 13 | |||
| 14 | use MPMinus; | ||
| 15 | |||
| 16 | =head1 ABSTRACT | ||
| 17 | |||
| 18 | MPMinus - mod_perl2 Web Application Framework | ||
| 19 | |||
| 20 | =head1 DESCRIPTION | ||
| 21 | |||
| 22 | See C<README> file first and L<MPMinus::Manual> | ||
| 23 | |||
| 24 | =head1 METHODS | ||
| 25 | |||
| 26 | =over 8 | ||
| 27 | |||
| 28 | =item B<conf, config, get_conf, get_config> | ||
| 29 | |||
| 30 | my $project = $m->conf('project'); | ||
| 31 | |||
| 32 | Getting configuration value by name | ||
| 33 | |||
| 34 | =item B<disp, dispatcher> | ||
| 35 | |||
| 36 | my $disp = $m->disp; | ||
| 37 | |||
| 38 | Returns all Dispatcher records | ||
| 39 | |||
| 40 | =item B<drec, drecord, record> | ||
| 41 | |||
| 42 | my $d = $m->drec; | ||
| 43 | |||
| 44 | Returns current Dispatcher record. See L<MPMinus::Dispatcher> | ||
| 45 | |||
| 46 | =item B<get, get_node> | ||
| 47 | |||
| 48 | my $r = get('r'); | ||
| 49 | |||
| 50 | Getting node by name | ||
| 51 | |||
| 52 | =item B<m, glob> | ||
| 53 | |||
| 54 | # Used in the dependent packages | ||
| 55 | my $m = MPMinus->m; | ||
| 56 | |||
| 57 | # Used in the Apache handlers | ||
| 58 | my $m = shift; | ||
| 59 | |||
| 60 | Returns main MPMinus object | ||
| 61 | |||
| 62 | =item B<mysql, oracle, multistore> | ||
| 63 | |||
| 64 | my $mysql = $m->mysql; | ||
| 65 | my $oracle = $m->oracle; | ||
| 66 | my $mso = $m->multistore; | ||
| 67 | |||
| 68 | Getting mysql (L<MPMinus::Store::MySQL>), oracle (L<MPMinus::Store::Oracle>) or multistore | ||
| 69 | (L<MPMinus::Store::MultiStore>) objects | ||
| 70 | |||
| 71 | =item B<namespace> | ||
| 72 | |||
| 73 | my $namespace = $m->namespace; | ||
| 74 | |||
| 75 | Return current name space | ||
| 76 | |||
| 77 | =item B<r, req> | ||
| 78 | |||
| 79 | my $r = $m->r; | ||
| 80 | |||
| 81 | Returns Apache2::RequestRec object. See L<Apache2::RequestRec> | ||
| 82 | |||
| 83 | =item B<set, set_node> | ||
| 84 | |||
| 85 | Setting node by name | ||
| 86 | |||
| 87 | For example (in handler of MPM::foo::Handlers module): | ||
| 88 | |||
| 89 | # Set r as Apache2::RequestRec object | ||
| 90 | $m->set( r => $r ); | ||
| 91 | |||
| 92 | # Set mysql as MPMinus::Store::MySQL object | ||
| 93 | $m->set( mysql => new MPMinus::Store::MySQL( | ||
| 94 | -m => $m, | ||
| 95 | -attributes => {mysql_enable_utf8 => 1 | ||
| 96 | }) | ||
| 97 | ) unless $m->mysql; | ||
| 98 | |||
| 99 | # Set disp as MPMinus::Dispatcher object | ||
| 100 | $m->set( | ||
| 101 | disp => new MPMinus::Dispatcher($m->conf('project'),$m->namespace) | ||
| 102 | ) unless $m->disp; | ||
| 103 | |||
| 104 | # Initialising dispatcher record | ||
| 105 | my $record = $m->disp->get(-uri=>$m->conf('request_uri')); | ||
| 106 | $m->set(drec => $record); | ||
| 107 | |||
| 108 | =item B<set_conf, set_config> | ||
| 109 | |||
| 110 | $m->set_conf("LOCALHOST", $m->conf('http_host') =~ /localhost|workstation/ ? 1 : 0); | ||
| 111 | |||
| 112 | Setting configuration value | ||
| 113 | |||
| 114 | =back | ||
| 115 | |||
| 116 | =head1 HISTORY | ||
| 117 | |||
| 118 | See C<CHANGES> file | ||
| 119 | |||
| 120 | =head1 DEPENDENCIES | ||
| 121 | |||
| 122 | L<CTK>, L<Apache2>, L<TemplateM> | ||
| 123 | |||
| 124 | =head1 TO DO | ||
| 125 | |||
| 126 | See C<TODO> file | ||
| 127 | |||
| 128 | =head1 BUGS | ||
| 129 | |||
| 130 | * none noted | ||
| 131 | |||
| 132 | =head1 SEE ALSO | ||
| 133 | |||
| 134 | C<perl>, L<CTK>, L<CTK::Util>, L<TemplateM> | ||
| 135 | |||
| 136 | =head1 THANKS | ||
| 137 | |||
| 138 | Thanks to Dmitry Klimov for technical translating C<http://fla-master.com>. | ||
| 139 | |||
| 140 | =head1 AUTHOR | ||
| 141 | |||
| 142 | Sergey Lepenkov (Serz Minus) L<http://www.serzik.com> E<lt>minus@serzik.comE<gt> | ||
| 143 | |||
| 144 | =head1 COPYRIGHT | ||
| 145 | |||
| 146 | Copyright (C) 1998-2017 D&D Corporation. All Rights Reserved | ||
| 147 | |||
| 148 | =head1 LICENSE | ||
| 149 | |||
| 150 | This program is free software: you can redistribute it and/or modify | ||
| 151 | it under the terms of the GNU General Public License as published by | ||
| 152 | the Free Software Foundation, either version 3 of the License, or | ||
| 153 | (at your option) any later version. | ||
| 154 | |||
| 155 | This program is distributed in the hope that it will be useful, | ||
| 156 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 157 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 158 | GNU General Public License for more details. | ||
| 159 | |||
| 160 | See C<LICENSE> file | ||
| 161 | |||
| 162 | =cut | ||
| 163 | |||
| 164 | 1 | ▶ | use vars qw/ $VERSION $PATCH_20141100055 /; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 165 | $VERSION = "1.20"; | ||
| 166 | $PATCH_20141100055 = 0; | ||
| 167 | |||
| 168 | 1 | use base qw/ | |
| 169 | MPMinus::Configuration | ||
| 170 | MPMinus::Transaction | ||
| 171 | MPMinus::Util | ||
| 172 | MPMinus::Debug::Info | ||
| 173 | 1 | ▶ | /; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 174 | |||
| 175 | use Apache2::ServerUtil; | ||
| 176 | use Apache2::Connection; | ||
| 177 | |||
| 178 | use Carp; # qw/carp croak cluck confess/ | ||
| 179 | # carp -- ïðîñòî ïèøåì | ||
| 180 | # croak -- ïðîñòî ïèøåì è óáèâàåì | ||
| 181 | # cluck -- ïèøåì íî ñ ïîäðîáíîñòÿìè | ||
| 182 | # confess -- ïèøåì ñ ïîäðîáíîñòÿìè è óáèâàåì | ||
| 183 | |||
| 184 | our @ISA; | ||
| 185 | |||
| 186 | sub import { | ||
| 187 | my $class = shift; | ||
| 188 | my $callerp = caller(0); | ||
| 189 | if ($callerp =~ /^(.+)\:\:Handlers$/) { | ||
| 190 | my $pnamespace = $1; | ||
| 191 | push @ISA, $pnamespace unless grep {$_ eq $pnamespace} @ISA; | ||
| 192 | $class->new($callerp); | ||
| 193 | } | ||
| 194 | |||
| 195 | # Patch: http://osdir.com/ml/modperl.perl.apache.org/2014-11/msg00055.html | ||
| 196 | unless ($PATCH_20141100055) { | ||
| 197 | my $sver = _get_server_version(); | ||
| 198 | if ($sver && ($sver >= 2.04) && !Apache2::Connection->can('remote_ip')) { # Apache 2.4.x or larger | ||
| 199 | eval 'sub Apache2::Connection::remote_ip { return $_[0]->client_ip }'; | ||
| 200 | } | ||
| 201 | $PATCH_20141100055 = 1; | ||
| 202 | } | ||
| 203 | } | ||
| 204 | sub new { | ||
| 205 | my $class = shift; | ||
| 206 | my $caller = shift; | ||
| 207 | |||
| 208 | # Ïîëó÷àåì íàçâàíèå ïàêåòà | ||
| 209 | my $pnamespace = _search_pnamespace($caller || caller(0)); | ||
| 210 | no strict 'refs'; | ||
| 211 | |||
| 212 | my $self = bless { | ||
| 213 | namespace => $pnamespace, | ||
| 214 | }, $class; | ||
| 215 | |||
| 216 | # Ïðèñâàèâàåì ïîäñòðóêòóðå íîâûé îáúåêò | ||
| 217 | ${"${pnamespace}::glob"} = $self; | ||
| 218 | |||
| 219 | return ${"${pnamespace}::glob"}; | ||
| 220 | } | ||
| 221 | sub m { | ||
| 222 | # Âîçâðàùàåò óêàçàòåëü íà îáúåêò | ||
| 223 | my $self = shift; | ||
| 224 | my $caller = shift || caller(0); | ||
| 225 | my $pnamespace = _search_pnamespace($caller); | ||
| 226 | no strict 'refs'; | ||
| 227 | return ${"$pnamespace\:\:glob"}; | ||
| 228 | } | ||
| 229 | sub glob { goto &m }; | ||
| 230 | sub r { | ||
| 231 | # Ïîëó÷åíèå îáúåêòà (íîäû) çàïðîñà | ||
| 232 | my $self = shift; | ||
| 233 | return undef unless $self->{r}; | ||
| 234 | return $self->{r}; | ||
| 235 | } | ||
| 236 | sub req { goto &r }; | ||
| 237 | sub drec { | ||
| 238 | # Ïîëó÷åíèå ñòðîêè çàïèñè äèñïåò÷åðà | ||
| 239 | my $self = shift; | ||
| 240 | return undef unless $self->{drec}; | ||
| 241 | return $self->{drec}; | ||
| 242 | } | ||
| 243 | sub drecord { goto &drec }; | ||
| 244 | sub record { goto &drec }; | ||
| 245 | sub set_node { | ||
| 246 | # Äîáàâèòü íîäó ê ãëîáàëüíîìó ìàññèâó | ||
| 247 | my $self = shift; | ||
| 248 | my $node = shift; | ||
| 249 | my $data = shift; | ||
| 250 | $self->{$node} = $data; | ||
| 251 | } | ||
| 252 | sub set { goto &set_node }; | ||
| 253 | sub get_node { | ||
| 254 | # Ïðî÷èòàòü íîäó èç ãëîáàëüíîãî ìàññèâà | ||
| 255 | my $self = shift; | ||
| 256 | my $node = shift; | ||
| 257 | return $self->{$node}; | ||
| 258 | } | ||
| 259 | sub get { goto &get_node }; | ||
| 260 | sub mysql { | ||
| 261 | # âåðíóòü îáúåêò áä MySQL | ||
| 262 | my $self = shift; | ||
| 263 | return undef unless $self->{mysql}; | ||
| 264 | return $self->{mysql}; | ||
| 265 | } | ||
| 266 | sub oracle { | ||
| 267 | # âåðíóòü îáúåêò áä Oracle | ||
| 268 | my $self = shift; | ||
| 269 | return undef unless $self->{oracle}; | ||
| 270 | return $self->{oracle}; | ||
| 271 | } | ||
| 272 | sub multistore { | ||
| 273 | # âåðíóòü îáúåêò MultiStore | ||
| 274 | my $self = shift; | ||
| 275 | return undef unless $self->{multistore}; | ||
| 276 | return $self->{multistore}; | ||
| 277 | } | ||
| 278 | sub disp { | ||
| 279 | # âåðíóòü îáúåêò äèñïåò÷åðà disp | ||
| 280 | my $self = shift; | ||
| 281 | return undef unless $self->{disp}; | ||
| 282 | return $self->{disp}; | ||
| 283 | } | ||
| 284 | sub dispatcher { goto &disp }; | ||
| 285 | sub namespace { | ||
| 286 | my $self = shift; | ||
| 287 | return $self->{namespace}; | ||
| 288 | } | ||
| 289 | sub _search_pnamespace { | ||
| 290 | my $clr = shift; | ||
| 291 | my ($pn) = grep {$clr =~ /$_/ } @ISA; | ||
| 292 | croak("Missing 'use MPMinus' in module $clr\:\:Handlers") unless $pn; | ||
| 293 | return $pn; | ||
| 294 | } | ||
| 295 | sub _get_server_version { | ||
| 296 | return 0 unless $ENV{MOD_PERL}; | ||
| 297 | my $sver = Apache2::ServerUtil::get_server_banner() || ''; | ||
| 298 | $sver =~ s/^.+?\///; | ||
| 299 | if ($sver =~ /([0-9]+)\.([0-9]+)\.([0-9]+)/) { | ||
| 300 | return $1 + ($2/100) + ($3/10000); | ||
| 301 | } elsif ($sver =~ /([0-9]+)\.([0-9]+)/) { | ||
| 302 | return $1 + ($2/100); | ||
| 303 | } elsif ($sver =~ /([0-9]+)/) { | ||
| 304 | return $1; | ||
| 305 | } | ||
| 306 | return 0 | ||
| 307 | } | ||
| 308 | sub AUTOLOAD { | ||
| 309 | # Ýòî ñâîåãî ðîäà èíòåðôåéñ êî âñåì ñâîéñòâàì ÷åðåç îáúåêòíóþ ìîäåëü | ||
| 310 | # åñëè òàêîãî ñâîéñòâà íå îêàæèòñÿ, òî çíà÷èò ðóãàåìñÿ êàðïîì !! | ||
| 311 | my $self = shift; | ||
| 312 | our $AUTOLOAD; | ||
| 313 | my $AL = $AUTOLOAD; | ||
| 314 | my $ss = undef; | ||
| 315 | $ss = $1 if $AL=~/\:\:([^\:]+)$/; | ||
| 316 | if ($ss && $self->{$ss}) { | ||
| 317 | return $self->{$ss}; | ||
| 318 | } else { | ||
| 319 | carp("Can't find MPMinus node \"$ss\""); | ||
| 320 | } | ||
| 321 | return undef; | ||
| 322 | } | ||
| 323 | sub DESTROY { | ||
| 324 | my $self = shift; | ||
| 325 | return 1 unless $self && ref($self); | ||
| 326 | my $oo = $self->oracle; | ||
| 327 | my $mo = $self->mysql; | ||
| 328 | my $msoo = $self->multistore; | ||
| 329 | undef $oo; | ||
| 330 | undef $mo; | ||
| 331 | undef $msoo; | ||
| 332 | return 1; | ||
| 333 | } | ||
| 334 | |||
| 335 | 1; | ||