Skip to content

Commit

Permalink
Merge pull request #52 from PHPJasper/php7
Browse files Browse the repository at this point in the history
refactoring to improvements

Release Notes - PHPJasper - Version 2.1
========================================================
** Refactoring
    * strict types activate
    * add type declarations
________________________________________________________
  • Loading branch information
geekcom authored Jun 1, 2017
2 parents b5ab85e + 3e34d96 commit 42ce4e7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 29 deletions.
6 changes: 6 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
Release Notes - PHPJasper - Version 2.1
========================================================
** Refactoring
* strict types activate
* add type declarations
________________________________________________________
Release Notes - PHPJasper - Version 2.0
========================================================
** Improvement
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[![License](https://poser.pugx.org/geekcom/phpjasper/license)](https://packagist.org/packages/geekcom/phpjasper)

### Docs
[![Language-pt_BR](https://img.shields.io/badge/pt__BR-100%25-green.svg)](https://github.com/PHPJasper/phpjasper/blob/master/docs/pt_BR/LEIA-ME_pt_BR.md)
[![Language-pt_BR](https://img.shields.io/badge/pt__BR-100%25-green.svg)](https://github.com/PHPJasper/phpjasper/blob/2.0/docs/pt_BR/LEIA-ME_pt_BR.md)

### About
This package is the solution to compile and process JasperReports (.jrxml & .jasper files) just using PHP.
Expand Down
56 changes: 28 additions & 28 deletions src/PHPJasper.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace PHPJasper;

/**
Expand All @@ -22,7 +24,7 @@ class PHPJasper
/**
* @var string
*/
protected $path_executable;
protected $pathExecutable;

/**
* @var bool
Expand All @@ -40,46 +42,46 @@ class PHPJasper
public function __construct()
{
$this->executable = 'jasperstarter';
$this->path_executable = __DIR__ . '/../bin/jasperstarter/bin';
$this->pathExecutable = __DIR__ . '/../bin/jasperstarter/bin';
$this->windows = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? true : false;
}

/**
* @param $input_file
* @param bool $output_file
* @param string $input
* @param string $output optional
* @return $this
* @throws Exception\InvalidInputFile
*/
public function compile($input_file, $output_file = false)
public function compile(string $input, string $output = '')
{
if (!$input_file) {
if (!$input) {
throw new \PHPJasper\Exception\InvalidInputFile();
}

$this->command = $this->windows ? $this->executable : './' . $this->executable;
$this->command .= ' compile ';
$this->command .= "\"$input_file\"";
$this->command .= "\"$input\"";

if ($output_file !== false) {
$this->command .= ' -o ' . "\"$output_file\"";
if (!empty($output)) {
$this->command .= ' -o ' . "\"$output\"";
}

return $this;
}


/**
* @param $input_file
* @param bool $output_file
* @param string $input
* @param string $output
* @param array $options
* @return $this
* @throws Exception\InvalidInputFile
* @throws Exception\InvalidFormat
*/
public function process($input_file, $output_file = false, $options = [])
public function process(string $input, string $output, array $options = [])
{
$options = $this->parseProcessOptions($options);
if (!$input_file) {
if (!$input) {
throw new \PHPJasper\Exception\InvalidInputFile();
}
$this->validateFormat($options['format']);
Expand All @@ -90,10 +92,8 @@ public function process($input_file, $output_file = false, $options = [])
}

$this->command .= ' process ';
$this->command .= "\"$input_file\"";
if ($output_file !== false) {
$this->command .= ' -o ' . "\"$output_file\"";
}
$this->command .= "\"$input\"";
$this->command .= ' -o ' . "\"$output\"";

$this->command .= ' -f ' . join(' ', $options['format']);
if ($options['params']) {
Expand Down Expand Up @@ -130,10 +130,10 @@ public function process($input_file, $output_file = false, $options = [])

/**
*
* @param $options
* @param array $options
* @return array
*/
protected function parseProcessOptions($options)
protected function parseProcessOptions(array $options)
{
$defaultOptions = [
'format' => ['pdf'],
Expand Down Expand Up @@ -162,19 +162,19 @@ protected function validateFormat($format)
}

/**
* @param $input_file
* @param string $input
* @return $this
* @throws \Exception
*/
public function listParameters($input_file)
public function listParameters(string $input)
{
if (!$input_file) {
if (!$input) {
throw new \PHPJasper\Exception\InvalidInputFile();
}

$this->command = $this->windows ? $this->executable : './' . $this->executable;
$this->command .= ' list_parameters ';
$this->command .= "\"$input_file\"";
$this->command .= "\"$input\"";

return $this;
}
Expand All @@ -192,11 +192,11 @@ public function execute($user = false)
$this->addUserToCommand($user);

$output = [];
$return_var = 0;
$returnVar = 0;

chdir($this->path_executable);
exec($this->command, $output, $return_var);
if ($return_var !== 0) {
chdir($this->pathExecutable);
exec($this->command, $output, $returnVar);
if ($returnVar !== 0) {
throw new \PHPJasper\Exception\ErrorCommandExecutable();
}

Expand Down Expand Up @@ -230,7 +230,7 @@ protected function validateExecute()
if (!$this->command) {
throw new \PHPJasper\Exception\InvalidCommandExecutable();
}
if (!is_dir($this->path_executable)) {
if (!is_dir($this->pathExecutable)) {
throw new \PHPJasper\Exception\InvalidResourceDirectory();
}

Expand Down

0 comments on commit 42ce4e7

Please sign in to comment.