|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Coco\SourceWatcher\Core\Transformers; |
|
4
|
|
|
|
|
5
|
|
|
use Coco\SourceWatcher\Core\Row; |
|
6
|
|
|
use Coco\SourceWatcher\Core\Transformer; |
|
7
|
|
|
use Coco\SourceWatcher\Utils\FileUtils; |
|
8
|
|
|
use Monolog\Handler\StreamHandler; |
|
9
|
|
|
use Monolog\Logger; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class JavaTransformer |
|
13
|
|
|
* |
|
14
|
|
|
* @package Coco\SourceWatcher\Core\Transformers |
|
15
|
|
|
*/ |
|
16
|
|
|
class JavaTransformer extends Transformer |
|
17
|
|
|
{ |
|
18
|
|
|
protected string $classpath = ""; |
|
19
|
|
|
protected string $classname = ""; |
|
20
|
|
|
protected array $arguments = []; |
|
21
|
|
|
protected string $resultType = ""; |
|
22
|
|
|
|
|
23
|
|
|
protected array $availableOptions = [ "classpath", "classname", "arguments", "resultType" ]; |
|
24
|
|
|
|
|
25
|
|
|
private Logger $logger; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct () |
|
28
|
|
|
{ |
|
29
|
|
|
$this->logger = new Logger( "JavaTransformer" ); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
private function getArguments ( Row $row ) : string |
|
33
|
|
|
{ |
|
34
|
|
|
$arguments = ""; |
|
35
|
|
|
|
|
36
|
|
|
foreach ( $this->arguments as $currentArgument ) { |
|
37
|
|
|
if ( $currentArgument->getType() == JavaTransformerArgumentType::ARG_TYPE_COLUMN ) { |
|
38
|
|
|
$arguments .= " " . $row[$currentArgument->getColumnValue()]; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
if ( $currentArgument->getType() == JavaTransformerArgumentType::ARG_TYPE_STRING ) { |
|
42
|
|
|
$arguments .= " " . $currentArgument->getStringValue(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
if ( $currentArgument->getType() == JavaTransformerArgumentType::ARG_TYPE_MIXED ) { |
|
46
|
|
|
$arguments .= " " . $currentArgument->getMixedKey() . "=" . $row[$currentArgument->getMixedVal()]; |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return trim( $arguments ); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
private function getCommand ( Row $row ) : string |
|
54
|
|
|
{ |
|
55
|
|
|
return "java -cp " . $this->classpath . " " . $this->classname . " " . $this->getArguments( $row ); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
private bool $logHasBeenSet = false; |
|
59
|
|
|
|
|
60
|
|
|
private function setLogHandler () : void |
|
61
|
|
|
{ |
|
62
|
|
|
if ( !$this->logHasBeenSet ) { |
|
63
|
|
|
$streamPath = FileUtils::file_build_path( __DIR__, "..", "..", "..", "logs", |
|
64
|
|
|
$this->classname . "-" . gmdate( "Y-m-d-H-i-s", time() ) . "-" . getmypid() . ".txt" ); |
|
65
|
|
|
$this->logger->pushHandler( new StreamHandler( $streamPath ), Logger::DEBUG ); |
|
|
|
|
|
|
66
|
|
|
$this->logHasBeenSet = true; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function transform ( Row $row ) |
|
71
|
|
|
{ |
|
72
|
|
|
$this->setLogHandler(); |
|
73
|
|
|
|
|
74
|
|
|
$command = $this->getCommand( $row ); |
|
75
|
|
|
$this->logger->debug( $command ); |
|
76
|
|
|
|
|
77
|
|
|
exec( $command, $output, $returnValue ); |
|
78
|
|
|
|
|
79
|
|
|
$this->logger->debug( $returnValue ); |
|
80
|
|
|
|
|
81
|
|
|
if ( $returnValue == 0 ) { |
|
82
|
|
|
if ( $this->resultType == JavaTransformerResultType::RESULT_TYPE_JSON ) { |
|
83
|
|
|
$this->logger->debug( $output[0] ); |
|
84
|
|
|
|
|
85
|
|
|
$array = json_decode( $output[0], true ); |
|
86
|
|
|
|
|
87
|
|
|
if ( !empty( $array ) && is_array( $array ) ) { |
|
88
|
|
|
foreach ( $array as $key => $val ) { |
|
89
|
|
|
$row->set( $key, $val ); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
} else { |
|
94
|
|
|
echo "returnValue = $returnValue for " . print_r( $row, true ) . PHP_EOL; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.