File Coverage

blib/lib/Catalyst/View/Graphics/Primitive.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package Catalyst::View::Graphics::Primitive;
2              
3 1     1   16528 use strict;
  1         2  
  1         34  
4 1     1   4 use warnings;
  1         2  
  1         25  
5              
6 1     1   456 use Class::Load ':all';
  1         24840  
  1         138  
7 1     1   7 use Scalar::Util 'blessed';
  1         1  
  1         41  
8              
9 1     1   221 use Catalyst::Exception;
  0            
  0            
10              
11             our $VERSION = '0.06';
12             our $AUTHORITY = 'cpan:GPHAT';
13              
14             use base 'Catalyst::View';
15              
16             sub process {
17             my $self = shift;
18             my $c = shift;
19             my @args = @_;
20              
21             my $content_type = $c->stash->{'graphics_primitive_content_type'}
22             || $self->{'content_type'};
23              
24             my $gp = $c->stash->{'graphics_primitive'};
25              
26             (defined $gp)
27             || die "No Graphics::Primitive to render";
28              
29             (blessed($gp) && $gp->isa('Graphics::Primitive::Component'))
30             || die "Bad graphics_primitive, must be an instance of Graphics::Primitive::Component";
31              
32             my $out = eval {
33              
34             my $dname = $c->stash->{'graphics_primitive_driver'}
35             || $self->{'driver'};
36             my $dclass = "Graphics::Primitive::Driver::$dname";
37             # If we've got a unary plus, assume they want the driver to be the
38             # name they gave, not a suffix.
39             if($dname =~ /^\+(.*)/) {
40             $dclass = $1;
41             }
42             my $meta = load_class($dclass);
43             unless(defined($meta)) {
44             die("Couldn't load driver: $dclass");
45             }
46              
47             my $dargs = $c->stash->{'graphics_primitive_driver_args'}
48             || $self->{'driver_args'} || {};
49             my $driver = $dclass->new($dargs);
50              
51             $driver->prepare($gp);
52             if($gp->can('layout_manager')) {
53             $gp->layout_manager->do_layout($gp);
54             }
55             $driver->finalize($gp);
56             $driver->draw($gp);
57              
58             $c->response->content_type($content_type);
59             $c->response->body($driver->data);
60             };
61             if ($@) {
62             die "Failed to render '$' as '$content_type' because: $@";
63             }
64             }
65              
66             1;
67             __END__
68             =head1 NAME
69              
70             Catalyst::View::Graphics::Primitive - A Catalyst View for Graphics::Primitive
71              
72             =head1 SYNOPSIS
73              
74             # lib/MyApp/View/GP.pm
75             package MyApp::View::GP
76             use base 'Catalyst::View::Graphics::Primitive';
77             1;
78              
79             # configure in lib/MyApp.pm
80             MyApp->config(
81             ...
82             'View::Graphics::Primitive' => {
83             driver => 'Cairo',
84             driver_args => { format => 'pdf' },
85             content_type => 'application/pdf'
86             }
87             )
88              
89             =head1 DESCRIPTION
90              
91             This is the L<Catalyst> view class for L<Graphics::Primitive>. Any components
92             created with Graphics::Primitive can be passed to this view and rendered via
93             whatever driver you desire. This view has a helper so you can create your
94             view in the standard Catalyst way with I<myapp_create.pl> (where I<myapp> is
95             replaced with your application name).
96              
97             =head1 CONFIGURATION
98              
99             The following configuration options can be set:
100              
101             =over 4
102              
103             =item I<content_type>
104              
105             Sets the default content type to put into the response.
106              
107             =item I<driver>
108              
109             Sets the default driver to use when rendering a component.
110              
111             =item I<driver_args>
112              
113             Sets the arguments to pass when insantiating the driver. A common example
114             for the Cairo driver would be:
115              
116             MyApp->config(
117             ...
118             'View::Graphics::Primitive' => {
119             driver => 'Cairo',
120             driver_args => { format => 'pdf' },
121             content_type => 'application/pdf'
122             }
123             );
124              
125             =back
126              
127             All of these options can be overridden at request time by prefixing the name
128             with C<graphics_primitive_> and setting the value in the stash. For example,
129             to override the driver, args and content type:
130              
131             $c->stash->{graphics_primitive_driver} = 'SomethingElse';
132             $c->stash->{graphics_primitive_driver_args} = { format => 'png' };
133             $c->stash->{graphics_primitive_content_type} = 'image/png';
134              
135             =head1 METHODS
136              
137             =head2 new
138              
139             The constructor for a new Graphics::Primitive view.
140              
141             =head2 process
142              
143             Renders the Graphics::Primitive::Component object stored in
144             C<< $c->stash->{graphics_primitive} >> using the driver specified in the
145             configuration or in C<< $c->stash->{graphics_primitive_driver} >> (for
146             runtime changes). The driver will instantiated using driver_args from the
147             configuration or C<< $c->stash->{graphics_primitive_driver_args} >>. The
148             component will then be moved through the Graphics::Primitive rendering
149             lifecycle as follows:
150              
151             $driver->prepare($comp);
152             $driver->finalize($comp);
153             if($comp->can('layout_manager')) {
154             $comp->layout_manager->do_layout($comp);
155             }
156             $driver->draw($comp);
157              
158             The result of C<draw> is then set as the body response. The content type is
159             set based on the C<content_type> configuration option or the value of
160             C<< $c->stash->{graphics_primitive_content_type} >>.
161              
162             =head1 AUTHOR
163              
164             Cory Watson, C<< <gphat@cpan.org> >>
165              
166             Infinity Interactive, L<http://www.iinteractive.com>
167              
168             =head1 ACKNOWLEDGEMENTS
169              
170             This module was inspired by L<Catalyst::View::GD> and L<Catalyst::View::TT>.
171              
172             =head1 BUGS
173              
174             Please report any bugs or feature requests to C<bug-geometry-primitive at rt.cpan.org>, or through
175             the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Geometry-Primitive>. I will be notified, and then you'll
176             automatically be notified of progress on your bug as I make changes.
177              
178             =head1 COPYRIGHT & LICENSE
179              
180             Copyright 2008 by Infinity Interactive, Inc.
181              
182             L<http://www.iinteractive.com>
183              
184             This program is free software; you can redistribute it and/or modify it
185             under the same terms as Perl itself.