Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ jobs:
with:
php-version: 8.3

- name: Set up dummy env for unit tests
run: |
touch .env && echo "APP_ENV=local" >> .env

- name: Install librdkafka and rdkafka extension
run: |
sudo apt-get install -y librdkafka-dev
Expand Down
7 changes: 4 additions & 3 deletions src/Libraries/StorageDrivers/MinioAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,15 @@ public function move(string $path, string $newpath): bool
/**
* Get all files in a directory
*
* @param string $dirpath path of the directory
* @param string $dirpath path of the directory
* @param bool $recursive whether to get files recursively or not
*
* @return bool
*/
public function allFiles(string $dirpath): array
public function allFiles(string $dirpath, bool $recursive = false): array
{
try {
$paths = $this->listContents($dirpath);
$paths = $this->listContents($dirpath, $recursive);
return $paths;
} catch (\Throwable $th) {
throw $th;
Expand Down
5 changes: 3 additions & 2 deletions src/Libraries/StorageDrivers/NFS.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ public function delete(string $filepath): bool
if (!is_file($filepath)) {
return true;
}
if (!exec("rm $filepath")) {
exec("rm $filepath", $output, $exit_code);
if ($exit_code != 0) {
throw new RuntimeException("Failed to delete $filepath");
}
return true;
Expand Down Expand Up @@ -175,7 +176,7 @@ public function securelinkFolder(string $dirpath): array
*
* @return void
*/
private function createDirectory(string $dirpath): void
public function createDirectory(string $dirpath): void
{
try {
if (!is_dir($dirpath)) {
Expand Down
2 changes: 2 additions & 0 deletions src/Logs/TraitLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public function info(array $data): void
*/
private function getEmbeddedInfo(array &$data): void
{
$data['taskname'] = env('TASK_NAME');
$data['hostname'] = gethostname();
if ($meta = $this->context->get(Metadata::class)) {
if ($meta instanceof Metadata) {
if (isset($meta->req_id)) {
Expand Down
86 changes: 86 additions & 0 deletions src/Providers/MinioStorageServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

/**
* PHP version 8
*
* @category Library
* @package Providers
* @author Made Mas Adi Winata <m45adiwinata@gmail.com>
* @license https://mit-license.org/ MIT License
* @version GIT: 0.3.1
* @link https://github.com/spotlibs
*/

declare(strict_types=1);

namespace Spotlibs\PhpLib\Providers;

use Aws\S3\S3Client;
use Illuminate\Support\ServiceProvider;
use League\Flysystem\AwsS3v3\AwsS3Adapter;
use Illuminate\Support\Facades\Storage;
use Spotlibs\PhpLib\Libraries\StorageDrivers\MinioAdapter;

/**
* MinioStorageServiceProvider
*
* Service provider for MinIO storage
*
* @category StandardService
* @package ServiceProvider
* @author Made Mas Adi Winata <m45adiwinata@gmail.com>
* @license https://mit-license.org/ MIT License
* @link https://github.com/spotlibs
*/
class MinioStorageServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
Storage::extend('minio', function ($app, $config) {
// Client for internal Docker network communication
$client = new S3Client([
'credentials' => [
'key' => $config["key"],
'secret' => $config["secret"]
],
'region' => $config["region"],
'version' => "latest",
'bucket_endpoint' => false,
'use_path_style_endpoint' => true,
'endpoint' => $config["endpoint"], // e.g., http://minio:9000
]);

$adapter = new AwsS3Adapter($client, $config["bucket"]);

// Public client for browser-accessible URLs
$publicClient = new S3Client([
'credentials' => [
'key' => $config['key'],
'secret' => $config['secret']
],
'region' => $config['region'],
'version' => 'latest',
'use_path_style_endpoint' => true,
'endpoint' => $config['url'] ?? $config['endpoint'], // e.g., http://localhost:9000
]);

// Return your custom MinioAdapter (which extends Filesystem)
return new MinioAdapter($adapter, $config, $publicClient);
});
}

/**
* Register the application services.
*
* @return void
*/
public function register()
{

}
}
14 changes: 11 additions & 3 deletions tests/Logs/LogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class LogTest extends TestCase
{
private function setContext(): void
{
putenv('APP_ENV=local');
$meta = new Metadata();
$meta->req_id = '123123';
$meta->identifier = 'spotlibs-unittest';
Expand All @@ -23,15 +22,14 @@ private function setContext(): void

private function setContext2(): void
{
putenv('APP_ENV=local');
$meta = new Metadata();
$meta->task_id = '123123';
$meta->identifier = 'spotlibs-unittest';
$context = app(Context::class);
$context->set(Metadata::class, $meta);
}

private string $expected = '{"test":"let me know","traceID":"123123","identifier":"spotlibs-unittest"}';
private string $expected = '{"test":"let me know","taskname":null,"hostname":"--- IGNORE ---","traceID":"123123","identifier":"spotlibs-unittest"}';

public function testCreateRuntimeErrorLog()
{
Expand All @@ -44,6 +42,7 @@ public function testCreateRuntimeErrorLog()
$temp = explode(':: ', $logDetail);
$logLevel = $temp[0];
$logMessage = trim($temp[1]);
$logMessage = json_encode($this->removeHostname(json_decode($logMessage, true)));
$this->assertEquals('ERROR', $logLevel);
$this->assertEquals($this->expected, $logMessage);
}
Expand All @@ -58,6 +57,7 @@ public function testCreateRuntimeInfoLog()
$temp = explode(':: ', $logDetail);
$logLevel = $temp[0];
$logMessage = trim($temp[1]);
$logMessage = json_encode($this->removeHostname(json_decode($logMessage, true)));
$this->assertEquals('INFO', $logLevel);
$this->assertEquals($this->expected, $logMessage);
}
Expand All @@ -72,6 +72,7 @@ public function testCreateRuntimeWarningLog()
$temp = explode(':: ', $logDetail);
$logLevel = $temp[0];
$logMessage = trim($temp[1]);
$logMessage = json_encode($this->removeHostname(json_decode($logMessage, true)));
$this->assertEquals('WARNING', $logLevel);
$this->assertEquals($this->expected, $logMessage);
}
Expand All @@ -86,6 +87,7 @@ public function testCreateActivityInfoLog()
$temp = explode(':: ', $logDetail);
$logLevel = $temp[0];
$logMessage = trim($temp[1]);
$logMessage = json_encode($this->removeHostname(json_decode($logMessage, true)));
$this->assertEquals('INFO', $logLevel);
$this->assertEquals($this->expected, $logMessage);
}
Expand All @@ -100,7 +102,13 @@ public function testCreateWorkerInfoLog()
$temp = explode(':: ', $logDetail);
$logLevel = $temp[0];
$logMessage = trim($temp[1]);
$logMessage = json_encode($this->removeHostname(json_decode($logMessage, true)));
$this->assertEquals('INFO', $logLevel);
$this->assertEquals($this->expected, $logMessage);
}
private function removeHostname(array $logData): array
{
$logData['hostname'] = '--- IGNORE ---';
return $logData;
}
}
Loading