File tree Expand file tree Collapse file tree 1 file changed +24
-1
lines changed
Expand file tree Collapse file tree 1 file changed +24
-1
lines changed Original file line number Diff line number Diff line change 77use PDO ;
88use ReflectionClass ;
99use ReflectionException ;
10+ use ReflectionMethod ;
1011use ReflectionNamedType ;
1112use 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}
You can’t perform that action at this time.
0 commit comments