| 1 | #! /bin/false | ||
| 2 | |||
| 3 | # Copyright (C) 2016-2018 Guido Flohr <guido.flohr@cantanea.com>, | ||
| 4 | # all rights reserved. | ||
| 5 | |||
| 6 | # This program is free software: you can redistribute it and/or modify | ||
| 7 | # it under the terms of the GNU General Public License as published by | ||
| 8 | # the Free Software Foundation; either version 3 of the License, or | ||
| 9 | # (at your option) any later version. | ||
| 10 | |||
| 11 | # This program is distributed in the hope that it will be useful, | ||
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 14 | # GNU General Public License for more details. | ||
| 15 | |||
| 16 | # You should have received a copy of the GNU General Public License | ||
| 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 18 | |||
| 19 | package Qgoda::PluginLoader::TT2::Filter; | ||
| 20 | $Qgoda::PluginLoader::TT2::Filter::VERSION = 'v0.9.2'; | ||
| 21 | use strict; | ||
| 22 | |||
| 23 | use Locale::TextDomain qw(qgoda); | ||
| 24 | use Template::Plugin::Filter; | ||
| 25 | |||
| 26 | use Qgoda::Util qw(empty perl_class perl_identifier); | ||
| 27 | |||
| 28 | my $singleton; | ||
| 29 | |||
| 30 | sub new { | ||
| 31 | my ($class) = @_; | ||
| 32 | |||
| 33 | return $singleton if $singleton; | ||
| 34 | |||
| 35 | $singleton = { | ||
| 36 | __modules => {} | ||
| 37 | }; | ||
| 38 | |||
| 39 | bless $singleton, $class; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub namespace { | ||
| 43 | my ($self, $plugin_data) = @_; | ||
| 44 | |||
| 45 | die __x("Field '{field}' missing in 'package.json'.\n", | ||
| 46 | field => 'qgoda.module') | ||
| 47 | if !exists $plugin_data->{module}; | ||
| 48 | die __x("Invalid module name '{name}'.\n", name => $plugin_data->{module}) | ||
| 49 | if !perl_class $plugin_data->{module}; | ||
| 50 | |||
| 51 | return 'Qgoda::TT2::Plugin::' . $plugin_data->{module}; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub addPlugin { | ||
| 55 | my ($self, $plugin_data) = @_; | ||
| 56 | |||
| 57 | my $class_name = $self->namespace($plugin_data); | ||
| 58 | |||
| 59 | my $module_name = $class_name; | ||
| 60 | $module_name =~ s{(?:::|\')}{/}g; | ||
| 61 | $module_name .= '.pm'; | ||
| 62 | |||
| 63 | $self->{__modules}->{$module_name} = $plugin_data; | ||
| 64 | |||
| 65 | die __x("Field '{field}' missing in 'package.json'.\n", | ||
| 66 | field => 'qgoda.entry') | ||
| 67 | if !exists $plugin_data->{entry}; | ||
| 68 | my $entry = $plugin_data->{entry}; | ||
| 69 | die __x("Invalid entry point '{entry}'.\n", entry => $entry) | ||
| 70 | if !perl_identifier $entry; | ||
| 71 | |||
| 72 | no strict 'refs'; | ||
| 73 | |||
| 74 | *{"${class_name}::init"} = sub { | ||
| 75 | my ($self) = @_; | ||
| 76 | |||
| 77 | $self->{_DYNAMIC} = 1; | ||
| 78 | $self->install_filter($plugin_data->{entry}); | ||
| 79 | |||
| 80 | $plugin_data->{plugger}->compile->(); | ||
| 81 | |||
| 82 | return $self; | ||
| 83 | }; | ||
| 84 | |||
| 85 | *{"${class_name}::filter"} = sub { | ||
| 86 | my (\$self, \$text, \$config, \$args) = \@_; | ||
| 87 | |||
| 88 | return $plugin_data->{entry}(\$self, \$text, \$config, \$args); | ||
| 89 | }; | ||
| 90 | |||
| 91 | @{"${class_name}::ISA"} = 'Template::Plugin::Filter'; | ||
| 92 | |||
| 93 | return $self; | ||
| 94 | } | ||
| 95 | |||
| 96 | sub Qgoda::PluginLoader::TT2::Filter::INC { | ||
| 97 | my ($self, $filename) = @_; | ||
| 98 | |||
| 99 | return if !exists $self->{__modules}->{$filename}; | ||
| 100 | |||
| 101 | my $data = 'use strict; 1;'; | ||
| 102 | |||
| 103 | open my $fh, '<', \$data; | ||
| 104 | |||
| 105 | return $fh; | ||
| 106 | } | ||
| 107 | |||
| 108 | unshift @INC, Qgoda::PluginLoader::TT2::Filter->new; |