Skip to content

Commit ff9dd67

Browse files
committed
Adding Java transformer, logging feature
1 parent 800de67 commit ff9dd67

9 files changed

+273
-3
lines changed

logs/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.*

src/Core/Database/Connections/Connector.php

+17
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44

55
use Coco\SourceWatcher\Core\Row;
66
use Coco\SourceWatcher\Core\SourceWatcherException;
7+
use Coco\SourceWatcher\Utils\FileUtils;
78
use Coco\SourceWatcher\Utils\Internationalization;
89
use Doctrine\DBAL\Connection;
910
use Doctrine\DBAL\DriverManager;
1011
use Exception;
12+
use Monolog\Handler\StreamHandler;
13+
use Monolog\Logger;
1114

1215
/**
1316
* Class Connector
@@ -16,6 +19,18 @@
1619
*/
1720
abstract class Connector
1821
{
22+
private Logger $logger;
23+
24+
public function __construct ()
25+
{
26+
$this->logger = new Logger( "Connector" );
27+
28+
$streamPath = FileUtils::file_build_path( __DIR__, "..", "..", "..", "..", "logs",
29+
"Connector" . "-" . gmdate( "Y-m-d-H-i-s", time() ) . "-" . getmypid() . ".txt" );
30+
31+
$this->logger->pushHandler( new StreamHandler( $streamPath ), Logger::DEBUG );
32+
}
33+
1934
protected string $driver = "";
2035

2136
protected array $connectionParameters = [];
@@ -100,6 +115,8 @@ public function insert ( Row $row ) : int
100115

101116
$connection->close();
102117
} catch ( Exception $exception ) {
118+
$this->logger->debug( $exception->getMessage() );
119+
103120
throw new SourceWatcherException( Internationalization::getInstance()->getText( Connector::class,
104121
"Unexpected_Error" ), 0, $exception );
105122
}

src/Core/Loaders/DatabaseLoader.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Coco\SourceWatcher\Core\Loader;
77
use Coco\SourceWatcher\Core\Row;
88
use Coco\SourceWatcher\Core\SourceWatcherException;
9+
use Exception;
910

1011
/**
1112
* Class DatabaseLoader
@@ -16,11 +17,14 @@ class DatabaseLoader extends Loader
1617
{
1718
/**
1819
* @param Row $row
19-
* @throws SourceWatcherException
2020
*/
2121
public function load ( Row $row )
2222
{
23-
$this->insert( $row );
23+
try {
24+
$this->insert( $row );
25+
} catch ( Exception $exception ) {
26+
echo $exception->getMessage() . PHP_EOL;
27+
}
2428
}
2529

2630
/**
@@ -34,7 +38,8 @@ protected function insert ( Row $row ) : void
3438
}
3539

3640
if ( !( $this->output instanceof DatabaseOutput ) ) {
37-
throw new SourceWatcherException( sprintf( "The output must be an instance of %s", DatabaseOutput::class ) );
41+
throw new SourceWatcherException( sprintf( "The output must be an instance of %s",
42+
DatabaseOutput::class ) );
3843
}
3944

4045
if ( $this->output->getOutput() == null ) {
+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace Coco\SourceWatcher\Core\Transformers;
4+
5+
use Coco\SourceWatcher\Core\SourceWatcherException;
6+
7+
class JavaTransformerArgument
8+
{
9+
private string $type;
10+
11+
private string $columnValue;
12+
private string $stringValue;
13+
private string $mixedKey;
14+
private string $mixedVal;
15+
16+
/**
17+
* JavaTransformerArgument constructor.
18+
*
19+
* @param array $options
20+
* @throws SourceWatcherException
21+
*/
22+
public function __construct ( array $options )
23+
{
24+
switch ( $options["type"] ) {
25+
case JavaTransformerArgumentType::ARG_TYPE_COLUMN:
26+
$this->columnValue = $options["columnValue"];
27+
break;
28+
case JavaTransformerArgumentType::ARG_TYPE_STRING:
29+
$this->stringValue = $options["stringValue"];
30+
break;
31+
case JavaTransformerArgumentType::ARG_TYPE_MIXED:
32+
$this->mixedKey = $options["mixedKey"];
33+
$this->mixedVal = $options["mixedVal"];
34+
break;
35+
default:
36+
throw new SourceWatcherException( "Type not supported" );
37+
}
38+
39+
$this->type = $options["type"];
40+
}
41+
42+
/**
43+
* @return mixed|string
44+
*/
45+
public function getType () : string
46+
{
47+
return $this->type;
48+
}
49+
50+
/**
51+
* @return mixed|string
52+
*/
53+
public function getColumnValue () : string
54+
{
55+
return $this->columnValue;
56+
}
57+
58+
/**
59+
* @return mixed|string
60+
*/
61+
public function getStringValue () : string
62+
{
63+
return $this->stringValue;
64+
}
65+
66+
/**
67+
* @return mixed|string
68+
*/
69+
public function getMixedKey () : string
70+
{
71+
return $this->mixedKey;
72+
}
73+
74+
/**
75+
* @return mixed|string
76+
*/
77+
public function getMixedVal () : string
78+
{
79+
return $this->mixedVal;
80+
}
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Coco\SourceWatcher\Core\Transformers;
4+
5+
class JavaTransformerArgumentType
6+
{
7+
const ARG_TYPE_COLUMN = "arg_type_column";
8+
const ARG_TYPE_STRING = "arg_type_string";
9+
const ARG_TYPE_MIXED = "arg_type_mixed";
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Coco\SourceWatcher\Core\Transformers;
4+
5+
class JavaTransformerResultType
6+
{
7+
const RESULT_TYPE_JSON = "result_type_json";
8+
}

src/Utils/FileUtils.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Coco\SourceWatcher\Utils;
4+
5+
/**
6+
* Class FileUtils
7+
*
8+
* @package Coco\SourceWatcher\Utils
9+
*/
10+
class FileUtils
11+
{
12+
public static function file_build_path ( ...$segments ) : string
13+
{
14+
return join( DIRECTORY_SEPARATOR, $segments );
15+
}
16+
17+
/**
18+
* // getenv("HOME") isn't set on windows and generates a Notice.
19+
*
20+
* @return string
21+
*/
22+
public static function getUserHomePath () : string
23+
{
24+
$home = getenv( "HOME" );
25+
26+
if ( empty( $home ) ) {
27+
if ( !empty( $_SERVER["HOMEDRIVE"] ) && !empty( $_SERVER["HOMEPATH"] ) ) {
28+
// home on windows
29+
$home = $_SERVER["HOMEDRIVE"] . $_SERVER["HOMEPATH"];
30+
}
31+
}
32+
33+
return empty( $home ) ? "" : $home;
34+
}
35+
}

src/Utils/TextUtils.php

+16
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
/**
66
* Class TextUtils
7+
*
78
* @package Coco\SourceWatcher\Utils
89
*/
910
class TextUtils
@@ -40,4 +41,19 @@ public function textToPascalCase ( string $word ) : string
4041
// Make the new word the combination of the given word parts
4142
return implode( "", $wordParts );
4243
}
44+
45+
public function fromCamelToSnakeCase ( string $word ) : string
46+
{
47+
$pattern = '!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!';
48+
preg_match_all( $pattern, $word, $matches );
49+
$ret = $matches[0];
50+
51+
foreach ( $ret as &$match ) {
52+
$match = $match == strtoupper( $match ) ?
53+
strtolower( $match ) :
54+
lcfirst( $match );
55+
}
56+
57+
return implode( '_', $ret );
58+
}
4359
}

0 commit comments

Comments
 (0)