Skip to content
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
vendor
src/webview/build/*
composer.lock
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,7 @@ $webview->bind('getList', function ($seq, $req, $context) use ($webview, &$list)

<img src="examples/bindings/binding.png" width="330px">

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
9 changes: 3 additions & 6 deletions examples/bindings/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

7 changes: 3 additions & 4 deletions examples/helloworld/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

$webview = new WebView('Php WebView', 480, 320, WindowSizeHint::HINT_NONE, true);

$webview->setHTML('<center> Hello World </center>');

$webview->run();
$webview->destroy();
$webview->setHTML('<center> Hello World </center>')
->run()
->destroy();
16 changes: 16 additions & 0 deletions src/Exceptions/OsException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace PhpWebView\Exceptions;

class OsException extends \Exception
{
public const OsNotSupportedCode = 1;

public static function OsNotSupported(): self
{
return new self(
"Os is not supported, Only Linux, MacOs and windows are only supported by default. You can compile yourself.",
self::OsNotSupportedCode
);
}
}
124 changes: 88 additions & 36 deletions src/Webview.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,42 @@

use Closure;
use FFI;
use PhpWebView\Exceptions\OsException;
use PhpWebView\FFI\Darwin;
use PhpWebView\FFI\Linux;
use PhpWebView\FFI\Os;
use PhpWebView\FFI\Windows;

class WebView
{
private $ffi;

private $webview;

/**
* @param string $title
* @param int $width
* @param int $height
* @param WindowSizeHint $hint
* @param string $baseDir
* @param string|null $libraryFile
* @param bool $debug
* @throws OsException
* @throws FFI\Exception
*/
public function __construct(
private string $title,
private int $width,
private int $height,
private WindowSizeHint $hint,
private bool $debug = false,
protected string $title,
protected int $width,
protected int $height,
protected WindowSizeHint $hint,
protected bool $debug = false,
protected string $baseDir = __DIR__,
protected ?string $libraryFile = null,
)
{
try {
$headerContent = file_get_contents(__DIR__ . '/webview_php.h');
$this->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);
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -92,7 +107,6 @@ public function getHeight(): int
public function setHeight(int $height): void
{
$this->height = $height;
$this->setValues();
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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);
Expand All @@ -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;
}
}
Binary file added src/build/linux/webview_php_ffi.o
Binary file not shown.
File renamed without changes.
Binary file added src/build/windows/WebView2Loader.dll
Binary file not shown.
Binary file added src/build/windows/webview.obj
Binary file not shown.
Binary file added src/build/windows/webview_php_ffi.dll
Binary file not shown.
73 changes: 73 additions & 0 deletions src/webview/build.bat
Original file line number Diff line number Diff line change
@@ -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%"
)
8 changes: 5 additions & 3 deletions src/webview/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
c++ build/webview_php_ffi.o build/webview.o $CXXSHARED -o build/$OUTPUT_NAME
Binary file removed src/webview/build/webview.o
Binary file not shown.
Binary file removed src/webview/build/webview_php_ffi.o
Binary file not shown.