PageRenderTime 26ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Reporter.php

https://gitlab.com/matheusgarcez/jasperphp
PHP | 167 lines | 136 code | 12 blank | 19 comment | 4 complexity | b33e5c63068dba3db72e0e9126d713d9 MD5 | raw file
  1. <?php
  2. namespace JasperPHP;
  3. class Reporter
  4. {
  5. private $jasper;
  6. private $reportDirectory;
  7. private $connection = [];
  8. public function __construct($reportDir = "../../../../../reports/", $resourcesDir = false)
  9. {
  10. $this->reportDirectory = $reportDir;
  11. $this->jasper = new JasperPHP($resourcesDir);
  12. }
  13. public function connection(array $connection)
  14. {
  15. $this->connection = $connection;
  16. }
  17. private function compileReport($name)
  18. {
  19. $reportFile = $this->reportDirectory . "/" . $name;
  20. if (!file_exists($reportFile . ".jasper")) {
  21. $this->jasper->compile($reportFile . ".jrxml", $reportFile)->execute();
  22. }
  23. }
  24. /**
  25. * Generate a report with the specified name and parameters
  26. *
  27. * @param array [string]|string $files
  28. * @param array $parameters
  29. * @param string $format
  30. *
  31. * @return $this
  32. * @throws \Exception
  33. */
  34. public function generate($files, array $parameters = [], $format = "pdf")
  35. {
  36. $compiledFiles = [];
  37. // Compile report files
  38. if (is_array($files)) {
  39. // When multiple files
  40. foreach ($files as $parent => $filename) {
  41. // When multiple files with dependencies
  42. if (is_array($filename)) {
  43. /**
  44. * Loop through report dependencies
  45. *
  46. * @var string[] $filename
  47. */
  48. foreach ($filename as $dependency) {
  49. $compiledFiles[] = [$dependency, false];
  50. $this->compileReport($dependency);
  51. }
  52. // Compile the parent report after the dependencies
  53. $compiledFiles[] = [$parent, true];
  54. $this->compileReport($parent);
  55. } else {
  56. throw new \Exception("Multiple report generation isn't supported.");
  57. // $compiledFiles[] = [$filename, true];
  58. // $this->compileReport($filename);
  59. }
  60. }
  61. } else {
  62. // When only one file
  63. $compiledFiles[] = [$files, true];
  64. $this->compileReport($files);
  65. }
  66. // Check temporary directory
  67. // @codeCoverageIgnoreStart
  68. $cacheDirectory = $this->reportDirectory . "/tmp/";
  69. if (!file_exists($cacheDirectory) && !@mkdir($cacheDirectory, 0755) && !is_dir($cacheDirectory)) {
  70. throw new \Exception("Failed to create temporary folder.");
  71. }
  72. // @codeCoverageIgnoreEnd
  73. $hash = md5(base64_encode(random_bytes(64)));
  74. $tempFileName = $this->reportDirectory . "/tmp/" . $hash;
  75. $jasperCopy = $tempFileName . ".jasper";
  76. $mainReportFile = array_filter($compiledFiles, function ($value) {
  77. return $value[1] === true;
  78. });
  79. $reportFileName = end($mainReportFile);
  80. // Copy report file to prevent resource conflict
  81. $reportFile = $this->reportDirectory . "/" . $reportFileName[0];
  82. copy($reportFile . '.jasper', $jasperCopy);
  83. chmod($reportFile . '.jasper', 0775);
  84. $reportContent = null;
  85. try {
  86. // Generate jasper report
  87. $this->jasper->convert($jasperCopy, $tempFileName, [$format], $parameters, $this->connection)->execute();
  88. // Check generated report exists
  89. // @codeCoverageIgnoreStart
  90. if (!file_exists($tempFileName . "." . $format)) {
  91. throw new \Exception("Failed to generate the report with name '$reportFile'");
  92. }
  93. // @codeCoverageIgnoreEnd
  94. // Get report content
  95. $reportContent = file_get_contents($tempFileName . "." . $format);
  96. } catch (\Exception $ex) {
  97. if (strpos($ex->getMessage(), "expression for source text") !== false) {
  98. $params = $this->jasper->listParameters($jasperCopy)->execute();
  99. $paramCount = count($params);
  100. $required = "";
  101. $specifiedParamCount = 0;
  102. foreach ($params as $i => $param) {
  103. preg_match_all("/([a-z]{0,2}) (.*) (.*)/i", $param, $matches);
  104. $cleanNameMatch = trim($matches[2][0]);
  105. if (array_key_exists($cleanNameMatch, $parameters)) {
  106. $required .= "- {$cleanNameMatch} : {$matches[3][0]} ({$parameters[$cleanNameMatch]})";
  107. $specifiedParamCount++;
  108. } else {
  109. $required .= "- {$cleanNameMatch} : {$matches[3][0]}";
  110. }
  111. if (($paramCount - 1) !== $i) {
  112. $required .= "\n";
  113. }
  114. }
  115. if ($specifiedParamCount !== $paramCount) {
  116. throw new \Exception("Missing parameters:\n" . $required, 0, $ex);
  117. } else {
  118. throw $ex;
  119. }
  120. }
  121. throw $ex;
  122. } finally {
  123. unlink($jasperCopy);
  124. if (file_exists($tempFileName . "." . $format)) {
  125. unlink($tempFileName . "." . $format);
  126. }
  127. }
  128. // Return file content
  129. return $reportContent;
  130. }
  131. }