|
@@ -1,8 +1,9 @@
|
|
|
<?php
|
|
<?php
|
|
|
|
|
+
|
|
|
namespace SixShop\News\Service;
|
|
namespace SixShop\News\Service;
|
|
|
|
|
|
|
|
-use FFI;
|
|
|
|
|
use Exception;
|
|
use Exception;
|
|
|
|
|
+use FFI;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* FFI Service Wrapper for the News extension's Go library.
|
|
* FFI Service Wrapper for the News extension's Go library.
|
|
@@ -19,8 +20,8 @@ class NewsFfiService
|
|
|
*/
|
|
*/
|
|
|
private function __construct()
|
|
private function __construct()
|
|
|
{
|
|
{
|
|
|
- try {
|
|
|
|
|
- $cdef = "
|
|
|
|
|
|
|
+
|
|
|
|
|
+ $cdef = "
|
|
|
// Initialization function
|
|
// Initialization function
|
|
|
char* Initialize(char* host, char* user, char* password, char* dbname, char* charset, int port);
|
|
char* Initialize(char* host, char* user, char* password, char* dbname, char* charset, int port);
|
|
|
|
|
|
|
@@ -40,36 +41,32 @@ class NewsFfiService
|
|
|
char* IncrementNewsViews(int id);
|
|
char* IncrementNewsViews(int id);
|
|
|
";
|
|
";
|
|
|
|
|
|
|
|
- $libPath = __DIR__ . '/../../ffi/lib_news.so';
|
|
|
|
|
|
|
+ $libPath = __DIR__ . '/../../ffi/lib_news.so';
|
|
|
|
|
+ if (!file_exists($libPath)) {
|
|
|
|
|
+ $libPath = root_path('runtime') . 'lib_news.so';
|
|
|
if (!file_exists($libPath)) {
|
|
if (!file_exists($libPath)) {
|
|
|
throw new Exception("FFI library not found at: $libPath. Please run 'make' in the ffi directory.");
|
|
throw new Exception("FFI library not found at: $libPath. Please run 'make' in the ffi directory.");
|
|
|
}
|
|
}
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- $this->ffi = FFI::cdef($cdef, $libPath);
|
|
|
|
|
-
|
|
|
|
|
- // Dynamically initialize the Go module with config from the environment
|
|
|
|
|
- $dbConfig = config('database.connections.mysql');
|
|
|
|
|
-
|
|
|
|
|
- $resultJson = FFI::string($this->ffi->Initialize(
|
|
|
|
|
- $dbConfig['hostname'],
|
|
|
|
|
- $dbConfig['username'],
|
|
|
|
|
- $dbConfig['password'],
|
|
|
|
|
- $dbConfig['database'],
|
|
|
|
|
- $dbConfig['charset'],
|
|
|
|
|
- $dbConfig['hostport']
|
|
|
|
|
- ));
|
|
|
|
|
-
|
|
|
|
|
- $result = json_decode($resultJson, true);
|
|
|
|
|
- if (!isset($result['status']) || $result['status'] !== 'ok') {
|
|
|
|
|
- $error = $result['error'] ?? 'unknown initialization error';
|
|
|
|
|
- throw new Exception("Failed to initialize Go FFI database: " . $error);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ $this->ffi = FFI::cdef($cdef, $libPath);
|
|
|
|
|
+
|
|
|
|
|
+ // Dynamically initialize the Go module with config from the environment
|
|
|
|
|
+ $dbConfig = config('database.connections.mysql');
|
|
|
|
|
+
|
|
|
|
|
+ $resultJson = FFI::string($this->ffi->Initialize(
|
|
|
|
|
+ $dbConfig['hostname'],
|
|
|
|
|
+ $dbConfig['username'],
|
|
|
|
|
+ $dbConfig['password'],
|
|
|
|
|
+ $dbConfig['database'],
|
|
|
|
|
+ $dbConfig['charset'],
|
|
|
|
|
+ $dbConfig['hostport']
|
|
|
|
|
+ ));
|
|
|
|
|
|
|
|
- } catch (Exception $e) {
|
|
|
|
|
- // Handle FFI loading errors gracefully
|
|
|
|
|
- // In a real application, you'd log this error.
|
|
|
|
|
- // For now, we can re-throw it to make debugging easier.
|
|
|
|
|
- throw new Exception("Failed to initialize News FFI Service: " . $e->getMessage());
|
|
|
|
|
|
|
+ $result = json_decode($resultJson, true);
|
|
|
|
|
+ if (!isset($result['status']) || $result['status'] !== 'ok') {
|
|
|
|
|
+ $error = $result['error'] ?? 'unknown initialization error';
|
|
|
|
|
+ throw new Exception("Failed to initialize Go FFI database: " . $error);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|