Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/" }
}
}
3 changes: 1 addition & 2 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 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