Last active
January 5, 2021 15:45
-
-
Save kobus1998/1ba1722575fcbd068ba9c2069886de73 to your computer and use it in GitHub Desktop.
code generation
This file contains hidden or 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
<?php | |
$namespace = "App"; | |
$classname = "Kernel"; | |
$extends = "\\Symfony\\Kernel"; | |
$implements = [ | |
"\\App\\Contract\\KernelContract" | |
]; | |
$properties = [ | |
[ | |
"name" => 'root', | |
'privacy' => 'protected', | |
'static' => false | |
] | |
]; | |
$methods = [ | |
[ | |
'privacy' => 'public', | |
"name" => "__construct", | |
'static' => false, | |
"params" => [ | |
[ | |
"type" => "string", | |
"name" => "root" | |
] | |
], | |
'content' => 'echo $root;' | |
] | |
]; | |
$result = []; | |
$result[] = '<?php'; | |
$result[] = ''; | |
$result[] = 'namespace ' . $namespace . ';'; | |
$result[] = ''; | |
$classline = 'class ' . $classname . ' extends ' . $extends . ' implements '; | |
foreach ($implements as $implementation) { | |
$classline .= $implementation . ', '; | |
} | |
$result[] = rtrim($classline, ', '); | |
$result[] = "{"; | |
foreach($properties as $property) { | |
$result[] = "\t" . $property['privacy'] . ($property['static'] ? ' static ' : ' ') . '$' . $property['name'] . ';'; | |
$result[] = "\t"; | |
} | |
foreach ($methods as $index => $method) { | |
$sLine = "\t{$method['privacy']}" . ($method['static'] ? ' static' : '') . ' function ' . $method['name'] . '('; | |
foreach ($method['params'] as $param) { | |
$sLine .= "{$param['type']} \${$param['name']}"; | |
} | |
$sLine .= ")"; | |
$result[] = $sLine; | |
$result[] = "\t{"; | |
if ($method['content']) { | |
$result[] = "\t\t{$method['content']}"; | |
} | |
$result[] = "\t}"; | |
if ($index != count($methods) - 1) { | |
$result[] = ''; | |
} | |
} | |
$result[] = "}"; | |
$result[] = ''; | |
echo implode("\n", $result); | |
/* result | |
<?php | |
namespace App; | |
class Kernel extends \Symfony\Kernel implements \App\Contract\KernelContract | |
{ | |
protected $root; | |
public function __construct(string $root) | |
{ | |
echo $root; | |
} | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment