-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added PyClass, Support Python class as parent for PHP
- Loading branch information
Showing
9 changed files
with
154 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ Makefile | |
/CMakeFiles | ||
/.settings | ||
__pycache__ | ||
__phpy_proxy__ | ||
/vendor | ||
/venv | ||
/.pytest_cache | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
use phpy\PyClass; | ||
|
||
|
||
#[parent('Animal', 'animal')] | ||
class Dog extends PyClass | ||
{ | ||
function __construct(string $name, int $age) | ||
{ | ||
parent::__construct($name, $age); | ||
$this->self()->color = 'black'; | ||
} | ||
|
||
function speak(string $name): void | ||
{ | ||
echo "Dog $name, color: {$this->self()->color}, speak: wang wang wang\n"; | ||
$this->super()->speak('dog'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
require dirname(__DIR__, 2) . '/vendor/autoload.php'; | ||
require dirname(__DIR__, 2) . '/lib/phpy/PyClass.php'; | ||
require __DIR__ . '/Dog.php'; | ||
|
||
PyCore::import('sys')->path->append('.'); | ||
|
||
$dog = new Dog('dog', 1); | ||
$dog->speak('hello'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
|
||
namespace phpy; | ||
|
||
use PyCore; | ||
use PyObject; | ||
use ReflectionClass; | ||
use ReflectionMethod; | ||
|
||
class PyClass | ||
{ | ||
static private string $_proxyPath = ''; | ||
static private ?PyObject $_sys = null; | ||
protected ?PyObject $_self = null; | ||
protected ?PyObject $_super = null; | ||
protected string $_proxyClass; | ||
private string $_proxyFile; | ||
private string $_class; | ||
|
||
function __construct() | ||
{ | ||
if (!self::$_sys) { | ||
self::$_sys = PyCore::import('sys'); | ||
} | ||
if (!self::$_proxyPath) { | ||
self::setProxyPath(getcwd() . '/__phpy_proxy__'); | ||
} | ||
|
||
$this->_class = get_class($this); | ||
$this->_proxyClass = strtolower(str_replace('\\', '_', $this->_class)); | ||
$this->_proxyFile = self::$_proxyPath . '/' . $this->_proxyClass . '.py'; | ||
|
||
if (!is_file($this->_proxyFile)) { | ||
$this->makeProxy(); | ||
} | ||
PyCore::import($this->_proxyClass)->{$this->_proxyClass}($this); | ||
if (func_num_args() > 0) { | ||
$this->super()->__init__(...func_get_args()); | ||
} | ||
} | ||
|
||
private function makeProxy() | ||
{ | ||
$ref = new ReflectionClass($this); | ||
$refParents = $ref->getAttributes('parent'); | ||
|
||
$import = ''; | ||
$parents = []; | ||
|
||
if (empty($refParents)) { | ||
$parents[] = 'object'; | ||
} else { | ||
foreach ($refParents as $refParent) { | ||
$parent = $refParent->getArguments(); | ||
if (count($parent) == 1) { | ||
$parents[] = $parent[0]; | ||
} else { | ||
[$class, $package] = $parent; | ||
$import .= 'import ' . $package . PHP_EOL; | ||
$parents[] = $package . '.' . $class; | ||
} | ||
} | ||
} | ||
|
||
$refMethods = $ref->getMethods(ReflectionMethod::IS_PUBLIC); | ||
$methods = []; | ||
foreach ($refMethods as $method) { | ||
$modifiers = $method->getModifiers(); | ||
if (str_starts_with($method->name, '__') or $modifiers & ReflectionMethod::IS_STATIC) { | ||
continue; | ||
} | ||
$refParams = $method->getParameters(); | ||
$params = []; | ||
foreach ($refParams as $param) { | ||
$params[] = $param->name; | ||
} | ||
$methods[] = [ | ||
'name' => $method->name, | ||
'argv' => implode(', ', $params), | ||
]; | ||
} | ||
ob_start(); | ||
include __DIR__ . '/proxy.tpl'; | ||
$content = ob_get_clean(); | ||
$dir = dirname($this->_proxyFile); | ||
if (!is_dir($dir)) { | ||
mkdir($dir, 0777, true); | ||
} | ||
file_put_contents($this->_proxyFile, $content); | ||
} | ||
|
||
protected function super() | ||
{ | ||
return $this->_super; | ||
} | ||
|
||
protected function self() | ||
{ | ||
return $this->_self; | ||
} | ||
|
||
static function setProxyPath(string $path): void | ||
{ | ||
self::$_proxyPath = $path; | ||
self::$_sys->path->append($path); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?=$import?> | ||
|
||
class <?=$this->_proxyClass?>(<?=implode(', ', $parents)?>): | ||
def __init__(self, _this): | ||
self.__this = _this | ||
_this.set('_super', super()) | ||
_this.set('_self', self) | ||
|
||
<?php foreach($methods as $m):?> | ||
def <?=$m['name']?>(self, <?=$m['argv']?>): | ||
self.__this.call('<?=$m['name']?>', <?=$m['argv']?>) | ||
|
||
<?php endforeach; ?> |