diff --git a/.gitignore b/.gitignore
index 4f38912..f3f8b3f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
.idea
vendor
+src/webview/build/*
composer.lock
diff --git a/README.md b/README.md
index 9b0e2e2..0ad3767 100644
--- a/README.md
+++ b/README.md
@@ -57,4 +57,7 @@ $webview->bind('getList', function ($seq, $req, $context) use ($webview, &$list)
-To build the library, run **src/webview/build.sh**
+## Building
+For prerequisites, read (The link)[https://github.com/webview/webview#prerequisites]
+
+To build the library, run **src/webview/build.sh** on Unix-based systems, **src/webview/build.bat** on Windows
\ No newline at end of file
diff --git a/examples/bindings/main.php b/examples/bindings/main.php
index 1717e11..9c2c3fd 100755
--- a/examples/bindings/main.php
+++ b/examples/bindings/main.php
@@ -23,11 +23,8 @@
}
return [];
-});
-
-$webview->bind('getList', function ($seq, $req, $context) use ($webview, &$list) {
+})->bind('getList', function ($seq, $req, $context) use ($webview, &$list) {
$webview->returnValue($seq, 0, $list);
-});
-$webview->run();
-$webview->destroy();
+})->run()
+ ->destroy();
diff --git a/examples/helloworld/main.php b/examples/helloworld/main.php
index c41fe07..c403d5e 100755
--- a/examples/helloworld/main.php
+++ b/examples/helloworld/main.php
@@ -8,7 +8,6 @@
$webview = new WebView('Php WebView', 480, 320, WindowSizeHint::HINT_NONE, true);
-$webview->setHTML('
Hello World ');
-
-$webview->run();
-$webview->destroy();
+$webview->setHTML(' Hello World ')
+ ->run()
+ ->destroy();
diff --git a/src/Exceptions/OsException.php b/src/Exceptions/OsException.php
new file mode 100644
index 0000000..63cd6da
--- /dev/null
+++ b/src/Exceptions/OsException.php
@@ -0,0 +1,16 @@
+ffi = FFI::cdef($headerContent, __DIR__ . '/build/webview_php_ffi.so');
- $this->webview = $this->ffi->webview_create((int)$this->debug, null);
- $this->setValues();
- } catch (FFI\Exception $e) {
- echo $e->getMessage();
- }
+ $headerContent = file_get_contents($this->baseDir . DIRECTORY_SEPARATOR . 'webview_php.h');
+ $this->ffi = FFI::cdef($headerContent, $this->getDefaultLibraryFile());
+ $this->webview = $this->ffi->webview_create((int)$this->debug, null);
}
/**
@@ -55,10 +68,11 @@ public function getTitle(): string
/**
* @param string $title
*/
- public function setTitle(string $title): void
+ public function setTitle(string $title): self
{
$this->title = $title;
- $this->setValues();
+
+ return $this;
}
/**
@@ -72,10 +86,11 @@ public function getWidth(): int
/**
* @param int $width
*/
- public function setWidth(int $width): void
+ public function setWidth(int $width): self
{
$this->width = $width;
- $this->setValues();
+
+ return $this;
}
/**
@@ -92,7 +107,6 @@ public function getHeight(): int
public function setHeight(int $height): void
{
$this->height = $height;
- $this->setValues();
}
/**
@@ -106,10 +120,11 @@ public function getHint(): WindowSizeHint
/**
* @param WindowSizeHint $hint
*/
- public function setHint(WindowSizeHint $hint): void
+ public function setHint(WindowSizeHint $hint): self
{
$this->hint = $hint;
- $this->setValues();
+
+ return $this;
}
/**
@@ -120,23 +135,21 @@ public function isDebug(): bool
return $this->debug;
}
- private function setValues(): void
- {
- $this->ffi->webview_set_title($this->webview, $this->title);
- $this->ffi->webview_set_size($this->webview, $this->width, $this->height, $this->hint->value);
- }
-
- public function setHTML(string $html): void
+ public function setHTML(string $html): self
{
$this->ffi->webview_set_html($this->webview, $html);
+
+ return $this;
}
- public function returnValue($seq, $req, object|array $value): void
+ public function returnValue($seq, $req, object|array $value): self
{
$this->ffi->webview_return($this->webview, $seq, $req, json_encode($value));
+
+ return $this;
}
- public function bind($name, Closure $function, ?Context $context = null): void
+ public function bind($name, Closure $function, ?Context $context = null): self
{
$newFunction = function ($seq, $req, $args) use ($context, $function) {
$value = $function($seq, json_decode($req), $context);
@@ -145,40 +158,79 @@ public function bind($name, Closure $function, ?Context $context = null): void
}
};
$this->ffi->webview_bind($this->webview, $name, $newFunction, null);
+
+ return $this;
}
- public function unbind($name): void
+ public function unbind($name): self
{
$this->ffi->webview_unbind($this->webview, $name);
+
+ return $this;
}
- public function eval(string $js): void
+ public function eval(string $js): self
{
$this->ffi->webview_eval($this->webview, $js);
+
+ return $this;
}
- public function init(string $js): void
+ public function init(string $js): self
{
$this->ffi->webview_init($this->webview, $js);
+
+ return $this;
}
- public function navigate(string $url): void
+ public function navigate(string $url): self
{
$this->ffi->webview_navigate($this->webview, $url);
+
+ return $this;
}
- public function run(): void
+ public function run(): self
{
+ $this->ffi->webview_set_title($this->webview, $this->title);
+ $this->ffi->webview_set_size($this->webview, $this->width, $this->height, $this->hint->value);
$this->ffi->webview_run($this->webview);
+
+ return $this;
}
- public function destroy(): void
+ public function destroy(): self
{
$this->ffi->webview_destroy($this->webview);
+
+ return $this;
}
- public function terminate(): void
+ public function terminate(): self
{
$this->ffi->webview_terminate($this->webview);
+
+ return $this;
+ }
+
+ /**
+ * @return string
+ * @throws OsException
+ */
+ private function getDefaultLibraryFile(): string
+ {
+ if ($this->libraryFile !== null) {
+ return $this->libraryFile;
+ }
+
+ $this->libraryFile = match (PHP_OS_FAMILY) {
+ 'Linux' => $this->baseDir . '/build/linux/webview_php_ffi.so',
+ 'Darwin' => $this->baseDir . '/build/macos/webview_php_ffi.dylib',
+ 'Windows' => $this->baseDir . '\build\windows\webview_php_ffi.dll',
+ default => throw OsException::OsNotSupported(),
+ };
+
+
+ return $this->libraryFile;
}
}
\ No newline at end of file
diff --git a/src/build/linux/webview_php_ffi.o b/src/build/linux/webview_php_ffi.o
new file mode 100644
index 0000000..35c4ead
Binary files /dev/null and b/src/build/linux/webview_php_ffi.o differ
diff --git a/src/build/webview_php_ffi.so b/src/build/macos/webview_php_ffi.dylib
similarity index 100%
rename from src/build/webview_php_ffi.so
rename to src/build/macos/webview_php_ffi.dylib
diff --git a/src/build/windows/WebView2Loader.dll b/src/build/windows/WebView2Loader.dll
new file mode 100644
index 0000000..23ee281
Binary files /dev/null and b/src/build/windows/WebView2Loader.dll differ
diff --git a/src/build/windows/webview.obj b/src/build/windows/webview.obj
new file mode 100644
index 0000000..e3041f6
Binary files /dev/null and b/src/build/windows/webview.obj differ
diff --git a/src/build/windows/webview_php_ffi.dll b/src/build/windows/webview_php_ffi.dll
new file mode 100644
index 0000000..1e77313
Binary files /dev/null and b/src/build/windows/webview_php_ffi.dll differ
diff --git a/src/webview/build.bat b/src/webview/build.bat
new file mode 100644
index 0000000..7dec8ec
--- /dev/null
+++ b/src/webview/build.bat
@@ -0,0 +1,73 @@
+@echo off
+setlocal
+
+echo Prepare directories...
+set script_dir=%~dp0
+set script_dir=%script_dir:~0,-1%
+set src_dir=%script_dir%
+set build_dir=%script_dir%\build
+mkdir "%build_dir%"
+
+echo Webview directory: %src_dir%
+echo Build directory: %build_dir%
+
+:: If you update the nuget package, change its version here
+set nuget_version=1.0.1150.38
+echo Using Nuget Package microsoft.web.webview2.%nuget_version%
+if not exist "%script_dir%\microsoft.web.webview2.%nuget_version%" (
+ nuget.exe install Microsoft.Web.Webview2 -Version %nuget_version% -OutputDirectory %script_dir%
+ echo Nuget package installed
+)
+
+echo Looking for vswhere.exe...
+set "vswhere=%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe"
+if not exist "%vswhere%" set "vswhere=%ProgramFiles%\Microsoft Visual Studio\Installer\vswhere.exe"
+if not exist "%vswhere%" (
+ echo ERROR: Failed to find vswhere.exe
+ exit /b 1
+)
+echo Found %vswhere%
+
+echo Looking for VC...
+for /f "usebackq tokens=*" %%i in (`"%vswhere%" -latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath`) do (
+ set vc_dir=%%i
+)
+if not exist "%vc_dir%\Common7\Tools\vsdevcmd.bat" (
+ echo ERROR: Failed to find VC tools x86/x64
+ exit /b 1
+)
+echo Found %vc_dir%
+
+:: 4100: unreferenced formal parameter
+set warning_params=/W4 /wd4100
+
+:: build dlls if not found
+if not exist "%src_dir%\dll\x64\webview.dll" (
+ mkdir "%src_dir%\dll\x86"
+ mkdir "%src_dir%\dll\x64"
+ copy "%script_dir%\microsoft.web.webview2.%nuget_version%\build\native\x64\WebView2Loader.dll" "%src_dir%\dll\x64"
+ copy "%script_dir%\microsoft.web.webview2.%nuget_version%\build\native\x86\WebView2Loader.dll" "%src_dir%\dll\x86"
+
+ call "%vc_dir%\Common7\Tools\vsdevcmd.bat" -arch=x86 -host_arch=x64
+
+ echo Building webview.dll ^(x86^)
+ cl %warning_params% ^
+ /D "WEBVIEW_API=__declspec(dllexport)" ^
+ /I "%script_dir%\microsoft.web.webview2.%nuget_version%\build\native\include" ^
+ /std:c++17 /EHsc "/Fo%build_dir%"\ ^
+ "%src_dir%\webview.cc" /link /DLL "/OUT:%src_dir%\dll\x86\webview_php_ffi.dll" || exit /b
+
+ call "%vc_dir%\Common7\Tools\vsdevcmd.bat" -arch=x64 -host_arch=x64
+ echo Building webview.dll ^(x64^)
+ cl %warning_params% ^
+ /D "WEBVIEW_API=__declspec(dllexport)" ^
+ /I "%script_dir%\microsoft.web.webview2.%nuget_version%\build\native\include" ^
+ /std:c++17 /EHsc "/Fo%build_dir%"\ ^
+ "%src_dir%\webview.cc" /link /DLL "/OUT:%src_dir%\dll\x64\webview_php_ffi.dll" || exit /b
+)
+if not exist "%build_dir%\webview_php_ffi.dll" (
+ copy "%src_dir%\dll\x64\webview_php_ffi.dll" %build_dir%
+)
+if not exist "%build_dir%\WebView2Loader.dll" (
+ copy "%script_dir%\microsoft.web.webview2.%nuget_version%\build\native\x64\WebView2Loader.dll" "%build_dir%"
+)
\ No newline at end of file
diff --git a/src/webview/build.sh b/src/webview/build.sh
index bd8bb1e..8c0e4db 100755
--- a/src/webview/build.sh
+++ b/src/webview/build.sh
@@ -9,11 +9,13 @@ CFLAGS="-std=c99 $FLAGS"
if [ "$(uname)" = "Darwin" ]; then
CXXFLAGS="-DWEBVIEW_COCOA -std=c++11 $FLAGS -framework WebKit"
CXXSHARED="-DWEBVIEW_COCOA -std=c++11 $FLAGS -framework WebKit -fPIC -O3 -shared"
+ OUTPUT_NAME = "webview_php_ffi.dylib"
else
- CXXFLAGS="-DWEBVIEW_GTK -std=c++11 $FLAGS $(pkg-config --cflags --libs gtk+-3.0 webkit2gtk-4.0)"
- CXXSHARED=""
+ CXXFLAGS="-DWEBVIEW_GTK -std=c++11 $FLAGS $(pkg-config --cflags --libs gtk+-3.0 webkit2gtk-4.0) -fPIC"
+ CXXSHARED=" -fPIC -03 -shared -DWEBVIEW_GTK -std=c++11 $FLAGS $(pkg-config --cflags --libs gtk+-3.0 webkit2gtk-4.0) -fPIC -O3 -shared"
+ OUTPUT_NAME = "webview_php_ffi.so"
fi
c++ -c $CXXFLAGS webview.cc -o build/webview.o
cc -c webview_php.c $CFLAGS -o build/webview_php_ffi.o
-c++ build/webview_php_ffi.o build/webview.o $CXXSHARED -o ../build/webview_php_ffi.so
\ No newline at end of file
+c++ build/webview_php_ffi.o build/webview.o $CXXSHARED -o build/$OUTPUT_NAME
\ No newline at end of file
diff --git a/src/webview/build/webview.o b/src/webview/build/webview.o
deleted file mode 100644
index b19bb5e..0000000
Binary files a/src/webview/build/webview.o and /dev/null differ
diff --git a/src/webview/build/webview_php_ffi.o b/src/webview/build/webview_php_ffi.o
deleted file mode 100644
index 8118cab..0000000
Binary files a/src/webview/build/webview_php_ffi.o and /dev/null differ