1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* Jasper report integration for PHP |
7
|
|
|
* |
8
|
|
|
* @link https://github.com/belgattitude/soluble-jasper |
9
|
|
|
* @author Vanvelthem Sébastien |
10
|
|
|
* @copyright Copyright (c) 2017 Vanvelthem Sébastien |
11
|
|
|
* @license MIT |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Soluble\Jasper\Context; |
15
|
|
|
|
16
|
|
|
use Soluble\Japha\Bridge\Adapter as BridgeAdapter; |
17
|
|
|
use Soluble\Japha\Interfaces\JavaObject; |
18
|
|
|
use Soluble\Jasper\Exception\InvalidArgumentException; |
19
|
|
|
use Soluble\Jasper\Report; |
20
|
|
|
|
21
|
|
|
class DefaultClassLoader |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var BridgeAdapter |
25
|
|
|
*/ |
26
|
|
|
private $ba; |
27
|
|
|
|
28
|
13 |
|
public function __construct(BridgeAdapter $ba) |
29
|
|
|
{ |
30
|
13 |
|
$this->ba = $ba; |
31
|
13 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return JavaObject Java('java.lang.ClassLoader') |
35
|
|
|
*/ |
36
|
1 |
|
public function getReportClassLoader(Report $report): JavaObject |
37
|
|
|
{ |
38
|
1 |
|
$reportPath = $report->getReportPath(); |
39
|
|
|
|
40
|
1 |
|
return $this->getClassLoader([$reportPath]); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string[] $paths paths that will be added to the classLoader |
45
|
|
|
* @param bool $checkPathExists default to true, will throw exception if path does not exists |
46
|
|
|
* |
47
|
|
|
* @return JavaObject Java('java.lang.ClassLoader') |
48
|
|
|
*/ |
49
|
13 |
View Code Duplication |
public function getClassLoader(array $paths, bool $checkPathExists = true): JavaObject |
50
|
|
|
{ |
51
|
13 |
|
$classLoaderPaths = []; |
52
|
13 |
|
foreach ($paths as $path) { |
53
|
13 |
|
if ($checkPathExists && !is_dir($path)) { |
54
|
1 |
|
throw new InvalidArgumentException( |
55
|
1 |
|
sprintf( |
56
|
1 |
|
'Cannot add path to classLoader, it does not exists: %s', |
57
|
1 |
|
$path |
58
|
|
|
) |
59
|
|
|
); |
60
|
|
|
} |
61
|
12 |
|
$classLoaderPaths[] = $this->ba->java('java.io.File', $path)->toUrl(); |
62
|
|
|
} |
63
|
|
|
|
64
|
12 |
|
$classLoaderPaths = array_unique($classLoaderPaths); |
65
|
|
|
|
66
|
12 |
|
return $this->ba->java('java.net.URLClassLoader', $classLoaderPaths); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|