5
5
class JasperPHP
6
6
{
7
7
protected $ executable = 'jasperstarter ' ; //executable jasperstarter
8
- protected $ path_executable = __DIR__ . ' /../JasperStarter/bin/ ' ; //Path to executable
8
+ protected $ path_executable;
9
9
protected $ the_command ;
10
10
protected $ redirect_output ;
11
11
protected $ background ;
@@ -16,11 +16,12 @@ class JasperPHP
16
16
17
17
function __construct ($ resource_dir = false )
18
18
{
19
+ $ this ->path_executable = __DIR__ . '/../JasperStarter/bin ' ; //Path to executable
19
20
if (strtoupper (substr (PHP_OS , 0 , 3 )) === 'WIN ' )
20
- $ this ->windows = true ;
21
+ $ this ->windows = true ;
21
22
22
23
if (!$ resource_dir ) {
23
- $ this ->resource_directory = __DIR__ . '/../../../vendor/lavela/phpjasper /src/JasperStarter/bin ' ;
24
+ $ this ->resource_directory = __DIR__ . '/../../../vendor/cossou/jasperphp /src/JasperStarter/bin ' ;
24
25
} else {
25
26
if (!file_exists ($ resource_dir ))
26
27
throw new \Exception ('Invalid resource directory. ' , 1 );
@@ -40,7 +41,7 @@ public static function __callStatic($method, $parameters)
40
41
41
42
public function compile ($ input_file , $ output_file = false , $ background = true , $ redirect_output = true )
42
43
{
43
- if (is_null ($ input_file ) || empty ($ input_file ))
44
+ if (is_null ($ input_file ) || empty ($ input_file ))
44
45
throw new \Exception ('No input file ' , 1 );
45
46
46
47
$ command = ($ this ->windows ) ? $ this ->executable : './ ' . $ this ->executable ;
@@ -49,31 +50,29 @@ public function compile($input_file, $output_file = false, $background = true, $
49
50
50
51
$ command .= "\"$ input_file \"" ;
51
52
52
- if ( $ output_file !== false )
53
+ if ( $ output_file !== false )
53
54
$ command .= ' -o ' . "\"$ output_file \"" ;
54
55
55
- $ this ->redirect_output = $ redirect_output ;
56
- $ this ->background = $ background ;
57
- $ this ->the_command = $ command ;
56
+ $ this ->redirect_output = $ redirect_output ;
57
+ $ this ->background = $ background ;
58
+ $ this ->the_command = $ command ;
58
59
59
60
return $ this ;
60
61
}
61
62
62
63
public function process ($ input_file , $ output_file = false , $ format = array ('pdf ' ), $ parameters = array (), $ db_connection = array (), $ background = true , $ redirect_output = true )
63
64
{
64
- if (is_null ($ input_file ) || empty ($ input_file ))
65
+ if (is_null ($ input_file ) || empty ($ input_file ))
65
66
throw new \Exception ('No input file ' , 1 );
66
67
67
- if ( is_array ($ format ) )
68
- {
69
- foreach ($ format as $ key )
70
- {
71
- if ( !in_array ($ key , $ this ->formats ))
68
+ if (is_array ($ format )) {
69
+ foreach ($ format as $ key ) {
70
+ if (!in_array ($ key , $ this ->formats ))
72
71
throw new \Exception ('Invalid format! ' , 1 );
73
72
}
74
73
} else {
75
- if ( !in_array ($ format , $ this ->formats ))
76
- throw new \Exception ('Invalid format! ' , 1 );
74
+ if ( !in_array ($ format , $ this ->formats ))
75
+ throw new \Exception ('Invalid format! ' , 1 );
77
76
}
78
77
79
78
$ command = ($ this ->windows ) ? $ this ->executable : './ ' . $ this ->executable ;
@@ -82,78 +81,75 @@ public function process($input_file, $output_file = false, $format = array('pdf'
82
81
83
82
$ command .= "\"$ input_file \"" ;
84
83
85
- if ( $ output_file !== false )
84
+ if ( $ output_file !== false )
86
85
$ command .= ' -o ' . "\"$ output_file \"" ;
87
86
88
- if ( is_array ($ format ) )
87
+ if ( is_array ($ format ))
89
88
$ command .= ' -f ' . join (' ' , $ format );
90
89
else
91
90
$ command .= ' -f ' . $ format ;
92
91
/*
93
92
// Resources dir
94
93
$command .= " -r " . $this->resource_directory;
95
94
*/
96
- if ( count ($ parameters ) > 0 )
97
- {
95
+ if (count ($ parameters ) > 0 ) {
98
96
$ command .= ' -P ' ;
99
97
100
- foreach ($ parameters as $ key => $ value )
101
- {
98
+ foreach ($ parameters as $ key => $ value ) {
102
99
$ param = $ key . '=" ' . $ value . '" ' ;
103
- $ command .= " " .$ param . " " ;
100
+ $ command .= " " . $ param . " " ;
104
101
}
105
102
106
103
}
107
104
108
- if ( count ($ db_connection ) > 0 )
109
- {
105
+ if (count ($ db_connection ) > 0 ) {
110
106
$ command .= ' -t ' . $ db_connection ['driver ' ];
111
107
112
- if (isset ($ db_connection ['username ' ]))
108
+ if (isset ($ db_connection ['username ' ]))
113
109
$ command .= " -u " . $ db_connection ['username ' ];
114
110
115
- if ( isset ($ db_connection ['password ' ]) && !empty ($ db_connection ['password ' ]) )
111
+ if ( isset ($ db_connection ['password ' ]) && !empty ($ db_connection ['password ' ]))
116
112
$ command .= ' -p ' . $ db_connection ['password ' ];
117
113
118
- if ( isset ($ db_connection ['host ' ]) && !empty ($ db_connection ['host ' ]) )
114
+ if ( isset ($ db_connection ['host ' ]) && !empty ($ db_connection ['host ' ]))
119
115
$ command .= ' -H ' . $ db_connection ['host ' ];
120
116
121
- if ( isset ($ db_connection ['database ' ]) && !empty ($ db_connection ['database ' ]) )
117
+ if ( isset ($ db_connection ['database ' ]) && !empty ($ db_connection ['database ' ]))
122
118
$ command .= ' -n ' . $ db_connection ['database ' ];
123
119
124
- if ( isset ($ db_connection ['port ' ]) && !empty ($ db_connection ['port ' ]) )
120
+ if ( isset ($ db_connection ['port ' ]) && !empty ($ db_connection ['port ' ]))
125
121
$ command .= ' --db-port ' . $ db_connection ['port ' ];
126
122
127
- if ( isset ($ db_connection ['jdbc_driver ' ]) && !empty ($ db_connection ['jdbc_driver ' ]) )
123
+ if ( isset ($ db_connection ['jdbc_driver ' ]) && !empty ($ db_connection ['jdbc_driver ' ]))
128
124
$ command .= ' --db-driver ' . $ db_connection ['jdbc_driver ' ];
129
125
130
- if ( isset ($ db_connection ['jdbc_url ' ]) && !empty ($ db_connection ['jdbc_url ' ]) )
126
+ if ( isset ($ db_connection ['jdbc_url ' ]) && !empty ($ db_connection ['jdbc_url ' ]))
131
127
$ command .= ' --db-url ' . $ db_connection ['jdbc_url ' ];
132
128
133
- if ( isset ($ db_connection ['jdbc_dir ' ]) && !empty ($ db_connection ['jdbc_dir ' ]) )
129
+ if (isset ($ db_connection ['jdbc_dir ' ]) && !empty ($ db_connection ['jdbc_dir ' ]))
134
130
$ command .= ' --jdbc-dir ' . $ db_connection ['jdbc_dir ' ];
135
131
136
- if ( isset ($ db_connection ['db_sid ' ]) && !empty ($ db_connection ['db_sid ' ]) )
132
+ if (isset ($ db_connection ['db_sid ' ]) && !empty ($ db_connection ['db_sid ' ]))
137
133
$ command .= ' --db-sid ' . $ db_connection ['db_sid ' ];
138
134
139
- if ( isset ($ db_connection ['xml_xpath ' ]) )
135
+ if (isset ($ db_connection ['xml_xpath ' ]))
140
136
$ command .= ' --xml-xpath ' . $ db_connection ['xml_xpath ' ];
141
137
142
- if ( isset ($ db_connection ['data_file ' ]) )
138
+ if (isset ($ db_connection ['data_file ' ]))
143
139
$ command .= ' --data-file ' . $ db_connection ['data_file ' ];
144
140
145
141
}
146
142
147
- $ this ->redirect_output = $ redirect_output ;
148
- $ this ->background = $ background ;
149
- $ this ->the_command = $ command ;
143
+ $ this ->redirect_output = $ redirect_output ;
144
+ $ this ->background = $ background ;
145
+ $ this ->the_command = $ command ;
150
146
151
147
return $ this ;
152
148
}
153
149
154
150
public function list_parameters ($ input_file )
155
151
{
156
- if (is_null ($ input_file ) || empty ($ input_file ))
152
+ if (is_null ($ input_file ) || empty ($ input_file ))
157
153
throw new \Exception ('No input file ' , 1 );
158
154
159
155
$ command = ($ this ->windows ) ? $ this ->executable : './ ' . $ this ->executable ;
@@ -175,22 +171,22 @@ public function output()
175
171
public function execute ($ run_as_user = false )
176
172
{
177
173
178
- if ( $ run_as_user !== false && strlen ($ run_as_user > 0 ) && !$ this ->windows )
174
+ if ( $ run_as_user !== false && strlen ($ run_as_user > 0 ) && !$ this ->windows )
179
175
$ this ->the_command = 'su -u ' . $ run_as_user . " -c \"" . $ this ->the_command . "\"" ;
180
176
181
177
$ output = array ();
182
178
$ return_var = 0 ;
183
179
184
- if (is_dir ($ this ->path_executable )){
180
+ if (is_dir ($ this ->path_executable )) {
185
181
chdir ($ this ->path_executable );
186
182
exec ($ this ->the_command , $ output , $ return_var );
187
183
} else {
188
184
throw new \Exception ('Invalid resource directory. ' , 1 );
189
185
}
190
186
191
- if ($ return_var != 0 )
187
+ if ($ return_var != 0 )
192
188
throw new \Exception ('Your report has an error and couldn \'t be processed!\ Try to output the command using the function `output();` and run it manually in the console. ' , 1 );
193
-
189
+
194
190
return $ output ;
195
191
}
196
- }
192
+ }
0 commit comments