Skip to content

Commit 31d1a0a

Browse files
committed
Support abstract methods
1 parent 2927ba1 commit 31d1a0a

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

src/dbpp/Database.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PDO;
88
use ReflectionClass;
99
use ReflectionException;
10+
use ReflectionMethod;
1011
use ReflectionNamedType;
1112
use ReflectionProperty;
1213

@@ -21,10 +22,32 @@ public final function __construct(PDO $pdo) {
2122
try {
2223
$class = new ReflectionClass($propertyType->getName());
2324
if($class->isSubclassOf(Dao::class)){
24-
$this->{$property->getName()} = $class->newInstance($pdo);
25+
$this->{$property->getName()} = $this->createDao($pdo, $class);
2526
}
2627
} catch (ReflectionException) {}
2728
}
2829
}
2930
}
31+
32+
private function createDao(PDO $pdo, ReflectionClass $class): Dao {
33+
$tempName = $class->name."Impl".time();
34+
$abstractMethods = $class->getMethods(ReflectionMethod::IS_ABSTRACT);
35+
$classDef = "class $tempName extends $class->name{";
36+
foreach($abstractMethods as $method) {
37+
$argsDef = [];
38+
$argsDefArray = [];
39+
foreach ($method->getParameters() as $parameter) {
40+
$argsDef[] = ($parameter->getType() ?: "") . "$" .$parameter->name;
41+
$argsDefArray[] = "\"$parameter->name\"=>\$$parameter->name";
42+
}
43+
44+
$classDef .= "public function $method->name(".implode(",", $argsDef)."):{$method->getReturnType()}{".
45+
"return \$this->__call(\"$method->name\", [".implode(",", $argsDefArray)."]);}";
46+
}
47+
48+
$classDef .= "}";
49+
eval($classDef);
50+
51+
return new $tempName($pdo);
52+
}
3053
}

0 commit comments

Comments
 (0)