Skip to content

Instantly share code, notes, and snippets.

@kobus1998
Last active January 5, 2021 15:45
Show Gist options
  • Save kobus1998/1ba1722575fcbd068ba9c2069886de73 to your computer and use it in GitHub Desktop.
Save kobus1998/1ba1722575fcbd068ba9c2069886de73 to your computer and use it in GitHub Desktop.
code generation
<?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