Skip to content

Commit

Permalink
Merge branch 'developer' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
geekcom authored Mar 8, 2017
2 parents 9bb6be1 + f959145 commit 3e2754a
Show file tree
Hide file tree
Showing 121 changed files with 418 additions and 231 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ php:
- '5.6'
- '7.0'
- '7.1'
- hhvm
- hhvm
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ use JasperPHP\JasperPHP;
$input = __DIR__ . '/vendor/geekcom/phpjasper/examples/hello_world_params.jrxml';

$jasper = new JasperPHP;
$output = $jasper->list_parameters($input)->execute();
$output = $jasper->listParameters($input)->execute();

foreach($output as $parameter_description)
print $parameter_description . '<pre>';
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 1 addition & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
"phpunit/phpunit": "4.8.*"
},
"autoload": {
"psr-0": {
"JasperPHP": "src/"
}
"psr-4": { "JasperPHP\\": "src/" }
}
}
2 changes: 1 addition & 1 deletion docs/de_DE/LESEN-MICH.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ use JasperPHP\JasperPHP;
$input = __DIR__ . '/vendor/geekcom/phpjasper/examples/hello_world_params.jrxml';

$jasper = new JasperPHP;
$output = $jasper->list_parameters($input)->execute();
$output = $jasper->listParameters($input)->execute();

foreach($output as $parameter_description)
print $parameter_description . '<pre>';
Expand Down
2 changes: 1 addition & 1 deletion docs/pt_BR/LEIA-ME_pt_BR.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ use JasperPHP\JasperPHP;
$input = __DIR__ . '/vendor/geekcom/phpjasper/examples/hello_world_params.jrxml';

$jasper = new JasperPHP;
$output = $jasper->list_parameters($input)->execute();
$output = $jasper->listParameters($input)->execute();

foreach($output as $parameter_description)
print $parameter_description . '<pre>';
Expand Down
5 changes: 2 additions & 3 deletions examples/example.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php

require __DIR__ . '/../src/JasperPHP/JasperPHP.php';
require __DIR__ . '/../vendor/autoload.php';

$outputPdfRtf = __DIR__;
$jasperFile = __DIR__ . '/hello_world.jasper';
Expand All @@ -20,7 +19,7 @@
)->execute();

// Listing Parameters of jasper file
$jasperParameters = $jasper->list_parameters($jrxmlParamsFile)->execute();
$jasperParameters = $jasper->listParameters($jrxmlParamsFile)->execute();
print "<h2>Listing Parameters of jasper file</h2>\n";
foreach($jasperParameters as $parameter_description) {
print "<pre>" . $parameter_description . "</pre>\n";
Expand Down
File renamed without changes.
14 changes: 14 additions & 0 deletions src/Exception/ErrorCommandExecutable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
namespace JasperPHP\Exception;
/**
* Class ErrorCommandExecutable
* @package JasperPHP\Exception
*/
class ErrorCommandExecutable extends \Exception
{
public function __construct($message = "", $code = 0, Exception $previous = null)
{
$message = '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.';
parent::__construct($message, $code, $previous);
}
}
21 changes: 21 additions & 0 deletions src/Exception/InvalidCommandExecutable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
namespace JasperPHP\Exception;
/**
* Class InvalidCommandExecutable
* @package JasperPHP\Exception
*/
class InvalidCommandExecutable extends \Exception
{

/**
* InvalidCommandExecutable constructor.
* @param string $message
* @param int $code
* @param Exception|null $previous
*/
public function __construct($message = "", $code = 0, Exception $previous = null)
{
$message = 'Cannot execute a blank command';
parent::__construct($message, $code, $previous);
}
}
14 changes: 14 additions & 0 deletions src/Exception/InvalidFormat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
namespace JasperPHP\Exception;
/**
* Class InvalidFormat
* @package JasperPHP\Exception
*/
class InvalidFormat extends \Exception
{

public function __construct($message = "", $code = 0, Exception $previous = null)
{
parent::__construct($message, $code, $previous);
}
}
21 changes: 21 additions & 0 deletions src/Exception/InvalidInputFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
namespace JasperPHP\Exception;
/**
* Class InvalidInputFile
* @package JasperPHP\Exception
*/
class InvalidInputFile extends \Exception
{

/**
* InvalidInputFile constructor.
* @param string $message
* @param int $code
* @param Exception|null $previous
*/
public function __construct($message = "", $code = 0, Exception $previous = null)
{
$message = 'No input file';
parent::__construct($message, $code, $previous);
}
}
22 changes: 22 additions & 0 deletions src/Exception/InvalidResourceDirectory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
namespace JasperPHP\Exception;
/**
* Class InvalidResourceDirectory
* @package JasperPHP\Exception
*/
class InvalidResourceDirectory extends \Exception
{

/**
* Invalid Resource Directory constructor.
*
* @param string $message
* @param int $code
* @param Exception|null $previous
*/
public function __construct($message = "", $code = 0, Exception $previous = null)
{
$message = 'Invalid resource directory';
parent::__construct($message, $code, $previous);
}
}
File renamed without changes.
215 changes: 215 additions & 0 deletions src/JasperPHP.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
<?php
namespace JasperPHP;
/**
* Class JasperPHP
* @package JasperPHP
*/
class JasperPHP
{

/**
* @var string
*/
protected $command;

/**
* @var string
*/
protected $executable;

/**
* @var string
*/
protected $path_executable;

/**
* @var bool
*/
protected $windows;

/**
* @var array
*/
protected $formats = ['pdf', 'rtf', 'xls', 'xlsx', 'docx', 'odt', 'ods', 'pptx', 'csv', 'html', 'xhtml', 'xml', 'jrprint'];

/**
* JasperPHP constructor
*/
public function __construct()
{
$this->executable = 'jasperstarter';
$this->path_executable = __DIR__ . '/../bin/jasperstarter/bin';
$this->windows = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? true : false;
}

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

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

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

return $this;
}


/**
* @param $input_file
* @param bool $output_file
* @param array $options
* @return $this
* @throws Exception\InvalidInputFile
* @throws Exception\InvalidFormat
*/
public function process($input_file, $output_file = false, $options = [])
{
$options = $this->parseProcessOptions($options);
if (!$input_file) {
throw new \JasperPHP\Exception\InvalidInputFile();
}
$this->validateFormat($options['format']);

$this->command = $this->windows ? $this->executable : './' . $this->executable;
if ($options['locale']) {
$this->command .= " --locale {$options['locale']}";
}

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

$this->command .= ' -f ' . join(' ', $options['format']);
if ($options['params']) {
$this->command .= ' -P ';
foreach ($options['params'] as $key => $value) {
$this->command .= " " . $key . '="' . $value . '" ' . " ";
}
}

return $this;
}

/**
*
* @param $options
* @return array
*/
protected function parseProcessOptions($options)
{
$defaultOptions = [
'format' => ['pdf'],
'params' => [],
'locale' => false,
'db_connection' => []
];

return array_merge($defaultOptions, $options);
}

/**
* @param $format
* @throws Exception\InvalidFormat
*/
protected function validateFormat($format)
{
if (!is_array($format)) {
$format = [$format];
}
foreach ($format as $value) {
if (!in_array($value, $this->formats)) {
throw new \JasperPHP\Exception\InvalidFormat();
}
}
}

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

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

return $this;
}

/**
* @param bool $user
* @return mixed
* @throws Exception\InvalidCommandExecutable
* @throws Exception\InvalidResourceDirectory
* @throws Exception\ErrorCommandExecutable
*/
public function execute($user = false)
{
$this->validateExecute();
$this->addUserToCommand($user);

$output = [];
$return_var = 0;

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

return $output;
}

/**
* @return string
*/
public function output()
{
return $this->command;
}

/**
* @param $user
*/
protected function addUserToCommand($user)
{
if ($user && !$this->windows) {
$this->command = 'su -u ' . $user . " -c \"" . $this->command . "\"";
}
}

/**
* @throws Exception\InvalidCommandExecutable
* @throws Exception\InvalidResourceDirectory
*/
protected function validateExecute()
{
if (!$this->command) {
throw new \JasperPHP\Exception\InvalidCommandExecutable();
}
if (!is_dir ($this->path_executable)) {
throw new \JasperPHP\Exception\InvalidResourceDirectory();
}

}

}
Loading

0 comments on commit 3e2754a

Please sign in to comment.