Skip to content

Commit

Permalink
Merge pull request #201 from GDATASoftwareAG/php/remove-monolog
Browse files Browse the repository at this point in the history
Php/remove monolog
  • Loading branch information
ata-no-one committed Mar 30, 2023
2 parents 22825e9 + d3fb931 commit 4a094d4
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 1,337 deletions.
16 changes: 0 additions & 16 deletions php/.devcontainer/Dockerfile

This file was deleted.

50 changes: 20 additions & 30 deletions php/.devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,39 +1,29 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.245.2/containers/php
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/php
{
"name": "PHP",
"build": {
"dockerfile": "Dockerfile",
"args": {
// Update VARIANT to pick a PHP version: 8, 8.1, 8.0, 7, 7.4
// Append -bullseye or -buster to pin to an OS version.
// Use -bullseye variants on local on arm64/Apple Silicon.
"VARIANT": "8",
"NODE_VERSION": "lts/*"
}
},
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/php:0-8.2",
// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},

// Configure tool-specific properties.
// "customizations": {},

// Use 'forwardPorts' to make a list of ports inside the container available locally.
"forwardPorts": [8080],

// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "sudo chmod a+x \"$(pwd)\" && sudo rm -rf /var/www/html && sudo ln -s \"$(pwd)\" /var/www/html"

// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
//"remoteUser": "root",
"customizations": {
// Configure properties specific to VS Code.
"vscode": {
// Set *default* container specific settings.json values on container create.
"settings": {
"php.validate.executablePath": "/usr/local/bin/php"
},
// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"xdebug.php-debug",
"bmewburn.vscode-intelephense-client",
"onecentlin.php-productive-pack"
"recca0120.vscode-phpunit"
]
}
},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
"forwardPorts": [
8080
],
// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "sudo chmod a+x \"$(pwd)\" && sudo rm -rf /var/www/html && sudo ln -s \"$(pwd)\" /var/www/html"
// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "vscode"
}
}
}
21 changes: 1 addition & 20 deletions php/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"name": "Listen for Xdebug",
"type": "php",
"request": "launch",
"port": 9003
"port": 9000
},
{
"name": "Launch currently open script",
Expand All @@ -24,25 +24,6 @@
"XDEBUG_MODE": "debug,develop",
"XDEBUG_CONFIG": "client_port=${port}"
}
},
{
"name": "Launch Built-in web server",
"type": "php",
"request": "launch",
"runtimeArgs": [
"-dxdebug.mode=debug",
"-dxdebug.start_with_request=yes",
"-S",
"localhost:0"
],
"program": "",
"cwd": "${workspaceRoot}",
"port": 9003,
"serverReadyAction": {
"pattern": "Development Server \\(http://localhost:([0-9]+)\\) started",
"uriFormat": "http://localhost:%s",
"action": "openExternally"
}
}
]
}
1 change: 1 addition & 0 deletions php/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"phpunit.phpunit": "/workspaces/vaas/php/tests/vaas/vendor/bin/phpunit",
"phpunit.php": "/usr/local/bin/php",
"php.debug.ideKey": "vsc",
"php.debug.executablePath": "/usr/local/bin/php"
}
90 changes: 42 additions & 48 deletions php/src/vaas/Vaas.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
use Exception;
use GuzzleHttp\Client as HttpClient;
use JsonMapper;
use Monolog\Formatter\JsonFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use VaasSdk\Exceptions\TimeoutException;
use VaasSdk\Exceptions\UploadFailedException;
use VaasSdk\Exceptions\VaasAuthenticationException;
Expand All @@ -20,6 +17,7 @@
use VaasSdk\Message\VerdictResponse;
use VaasSdk\Message\VerdictRequestForUrl;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use VaasSdk\Message\VaasVerdict;

class Vaas
Expand All @@ -31,32 +29,23 @@ class Vaas
private LoggerInterface $_logger;
private HttpClient $_httpClient;


/**
*/
public function __construct(?string $vaasUrl, ?LoggerInterface $logger = null)
{
$this->_httpClient = new HttpClient();
if ($logger == null) {
$monoLogger = new Logger("VaaS");

$streamHandler = new StreamHandler(
fopen('php://stdout', 'w'),
Logger::INFO
);
$streamHandler->setFormatter(new JsonFormatter());
$monoLogger->pushHandler($streamHandler);
$this->_logger = $monoLogger;
} else {
if ($logger != null)
$this->_logger = $logger;
}
else
$this->_logger = new NullLogger();
$this->_logger->debug("Url: " . $vaasUrl);
$this->_vaasUrl = $vaasUrl;
}

/**
*/
public function Connect(
string $token,
?LoggerInterface $logger = null,
?VaasConnection $vaasConnection = null
) {
if (isset($vaasConnection)) {
Expand All @@ -66,23 +55,11 @@ public function Connect(
}
$webSocket = $this->_vaasConnection->GetConnectedWebsocket();

if ($logger == null) {
$monoLogger = new Logger("VaaS");

$streamHandler = new StreamHandler(
fopen('php://stdout', 'w'),
Logger::INFO
);
$streamHandler->setFormatter(new JsonFormatter());
$monoLogger->pushHandler($streamHandler);
$this->_logger = $monoLogger;
} else {
$this->_logger = $logger;
}
$authRequest = new AuthRequest($token);
$webSocket->send(json_encode($authRequest));
$authResponse = $this->_waitForAuthResponse();
$this->_logger->debug("Authenticated: " . json_encode($authResponse));
if ($this->_logger != null)
$this->_logger->debug("Authenticated: " . json_encode($authResponse));
$this->_vaasConnection->SessionId = $authResponse->session_id;
}

Expand All @@ -99,7 +76,8 @@ public function Connect(
*/
public function ForSha256(string $hashString, string $uuid = null): VaasVerdict
{
$this->_logger->debug("ForSha256", ["Sha256" => $hashString]);
if ($this->_logger != null)
$this->_logger->debug("ForSha256", ["Sha256" => $hashString]);

$sha256 = Sha256::TryFromString($hashString);

Expand All @@ -118,7 +96,8 @@ public function ForSha256(string $hashString, string $uuid = null): VaasVerdict
*/
public function ForUrl(string $url, string $uuid = null): VaasVerdict
{
$this->_logger->debug("ForUrl", ["URL:" => $url]);
if ($this->_logger != null)
$this->_logger->debug("ForUrl", ["URL:" => $url]);

return new VaasVerdict($this->_verdictResponseForUrl($url, $uuid));
}
Expand All @@ -139,14 +118,17 @@ public function ForUrl(string $url, string $uuid = null): VaasVerdict
*/
public function ForFile(string $path, bool $upload = true, string $uuid = null): VaasVerdict
{
$this->_logger->debug("ForFile", ["File" => $path]);
if ($this->_logger != null)
$this->_logger->debug("ForFile", ["File" => $path]);

$sha256 = Sha256::TryFromFile($path);
$this->_logger->debug("Calculated Hash", ["Sha256" => $sha256]);
if ($this->_logger != null)
$this->_logger->debug("Calculated Hash", ["Sha256" => $sha256]);

$verdictResponse = $this->_verdictResponseForSha256($sha256, $uuid);
if ($verdictResponse->verdict == Verdict::UNKNOWN && $upload === true) {
$this->_logger->debug("UploadToken", ["UploadToken" => $verdictResponse->upload_token]);
if ($this->_logger != null)
$this->_logger->debug("UploadToken", ["UploadToken" => $verdictResponse->upload_token]);
$fileContent = file_get_contents($path);
$response = $this->_httpClient->put($verdictResponse->url, [
'body' => $fileContent,
Expand All @@ -170,7 +152,8 @@ public function ForFile(string $path, bool $upload = true, string $uuid = null):
private function _waitForAuthResponse(): AuthResponse
{
$websocket = $this->_vaasConnection->GetConnectedWebsocket();
$this->_logger->debug("WaitForAuthResponse");
if ($this->_logger != null)
$this->_logger->debug("WaitForAuthResponse");

$start_time = time();

Expand All @@ -188,15 +171,17 @@ private function _waitForAuthResponse(): AuthResponse
}

if ($result != null) {
$this->_logger->debug("Result", json_decode($result, true));
if ($this->_logger != null)
$this->_logger->debug("Result", json_decode($result, true));

$resultObject = json_decode($result);
if ($resultObject->kind == Kind::AUTH_RESPONSE) {
$authResponse = (new JsonMapper())->map(
$resultObject,
new AuthResponse()
);
$this->_logger->debug($result);
if ($this->_logger != null)
$this->_logger->debug($result);
if ($authResponse->success === false) {
throw new VaasAuthenticationException($result);
}
Expand All @@ -206,7 +191,8 @@ private function _waitForAuthResponse(): AuthResponse
} catch (VaasAuthenticationException $e) {
throw $e;
} catch (Exception $e) {
$this->_logger->warning("Error", ["Error" => $e]);
if ($this->_logger != null)
$this->_logger->warning("Error", ["Error" => $e]);
}
sleep(1);
}
Expand All @@ -219,7 +205,8 @@ private function _waitForAuthResponse(): AuthResponse
*/
private function _waitForVerdict(string $guid): VerdictResponse
{
$this->_logger->debug("WaitForVerdict");
if ($this->_logger != null)
$this->_logger->debug("WaitForVerdict");
$start_time = time();

if (!isset($this->_vaasConnection)) {
Expand All @@ -235,11 +222,13 @@ private function _waitForVerdict(string $guid): VerdictResponse
try {
$result = $websocket->receive();
} catch (\WebSocket\TimeoutException $e) {
$this->_logger->debug("Read timeout, send ping");
if ($this->_logger != null)
$this->_logger->debug("Read timeout, send ping");
$websocket->ping();
}
if ($result != null) {
$this->_logger->debug("Result", json_decode($result, true));
if ($this->_logger != null)
$this->_logger->debug("Result", json_decode($result, true));
$resultObject = json_decode($result);
if (!isset($resultObject->guid) || !isset($resultObject->kind)) {
continue;
Expand All @@ -256,7 +245,8 @@ private function _waitForVerdict(string $guid): VerdictResponse
}
}
} catch (Exception $e) {
$this->_logger->warning("Error", ["Error" => $e]);
if ($this->_logger != null)
$this->_logger->warning("Error", ["Error" => $e]);
}
sleep(1);
}
Expand All @@ -269,7 +259,8 @@ private function _waitForVerdict(string $guid): VerdictResponse
*/
private function _verdictResponseForSha256(Sha256 $sha256, string $uuid = null): VerdictResponse
{
$this->_logger->debug("_verdictResponseForSha256");
if ($this->_logger != null)
$this->_logger->debug("_verdictResponseForSha256");

if (!isset($this->_vaasConnection)) {
throw new VaasInvalidStateException("connect() was not called");
Expand All @@ -279,7 +270,8 @@ private function _verdictResponseForSha256(Sha256 $sha256, string $uuid = null):
$request = new VerdictRequest(strtolower($sha256), $uuid, $this->_vaasConnection->SessionId);
$websocket->send(json_encode($request));

$this->_logger->debug("verdictResponse", ["VerdictResponse" => json_encode($request)]);
if ($this->_logger != null)
$this->_logger->debug("verdictResponse", ["VerdictResponse" => json_encode($request)]);

return $this->_waitForVerdict($request->guid);
}
Expand All @@ -291,7 +283,8 @@ private function _verdictResponseForSha256(Sha256 $sha256, string $uuid = null):
*/
private function _verdictResponseForUrl(string $url, string $uuid = null): VerdictResponse
{
$this->_logger->debug("_verdictResponseForUrl");
if ($this->_logger != null)
$this->_logger->debug("_verdictResponseForUrl");

if (!isset($this->_vaasConnection)) {
throw new VaasInvalidStateException("connect() was not called");
Expand All @@ -301,7 +294,8 @@ private function _verdictResponseForUrl(string $url, string $uuid = null): Verdi
$request = new VerdictRequestForUrl($url, $uuid, $this->_vaasConnection->SessionId);
$websocket->send(json_encode($request));

$this->_logger->debug("verdictResponse", ["VerdictResponse" => json_encode($request)]);
if ($this->_logger != null)
$this->_logger->debug("verdictResponse", ["VerdictResponse" => json_encode($request)]);

return $this->_waitForVerdict($request->guid);
}
Expand Down
2 changes: 1 addition & 1 deletion php/src/vaas/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"textalk/websocket": "^1.6",
"netresearch/jsonmapper": "^4.1",
"guzzlehttp/guzzle": "^7.5",
"monolog/monolog": "^3.2"
"psr/log": "^1.1 || ^2.0 || ^3.0"
},
"autoload": {
"psr-4": {
Expand Down

0 comments on commit 4a094d4

Please sign in to comment.