https://github.com/artspb/php-generator
Simple PHP code generator on Kotlin builders
https://github.com/artspb/php-generator
Last synced: about 1 month ago
JSON representation
Simple PHP code generator on Kotlin builders
- Host: GitHub
- URL: https://github.com/artspb/php-generator
- Owner: artspb
- License: apache-2.0
- Created: 2016-06-14T21:29:43.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2018-02-24T16:59:10.000Z (about 7 years ago)
- Last Synced: 2025-02-05T11:17:07.028Z (3 months ago)
- Language: Kotlin
- Size: 114 KB
- Stars: 1
- Watchers: 0
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# php-generator
Simple PHP code generator on Kotlin builders[](https://travis-ci.org/artspb/php-generator)
This project is a work in progress.
# Input
```kotlin
import me.artspb.php.generator.model.phpfun main(args: Array) = dir("/path/to/file/") {
file("file.php") {
php {comment { +"normal comment" }
comment {
+"normal"
+"multiline"
+"comment"
}
comment(symbol = "#") { +"sharp comment" }
comment(symbol = "#") {
+"sharp"
+"multiline"
+"comment"
}
comment(true) { +"delimited comment" }
comment(true) {
+"delimited"
+"multiline"
+"comment"
}namespace("NS1\\NS2") {
_class("NamespaceClass") {}
function("namespaceFoo") {}
const("CONSTANT1") {
""""1""""
}
+"""define("NS1\\NS2\\CONSTANT2", "2");"""
}namespace("NS") {
use { _as("""NS1\NS2\NamespaceClass""", "Clazz") }
use("NS1\\", "function") { +"NS2\\namespaceFoo" }
use("NS1\\", "const") {
+"NS2\\CONSTANT1"
_as("NS2\\CONSTANT2", "CNST")
}function("foo") {
returnType("int")
parameter("bar", "string")
parameter("baz") {
"0"
}
}_for("\$i = 0", "\$i < 10", "\$i++") {
+"echo \$i;"
}foreach("array()", value = "\$value") {
+"echo \$value;"
}foreach("""["key" => "value"]""", key = "\$key", value = "\$value") {
+"""echo "${'$'}key = ${'$'}value";"""
}_try {
+"throw new \\Exception();"
}
catch("\\Exception", "\$e") {
+"var_dump(\$e);"
}
finally {
+"""echo "finally";"""
}_interface("QuxInterface") {
method("foo") {
returnType("string")
parameter("bar", "string")
parameter("baz") {
"0"
}
}
method("bar") {}
const("CONSTANT") {
""""VALUE""""
}
}trait("FooTrait") {
method("foo") {}
}trait("BarTrait") {
method("foo") {}
}_class("FooClass", "abstract") {
implements("QuxInterface")
use("FooTrait") {
_as("foo", "private")
_as("foo", "bar")
_as("foo", "private", "bar")
}
use("FooTrait", "BarTrait") {
insteadof("FooTrait::foo", "BarTrait")
}
const("CNST", "private") {
"0"
}
}_class("BarClass") {
extends("FooClass")phpdoc {
param("string", "bar")
param("int", "baz")
_return("string")
throws("\\Exception")
}
method("foo", "public") {
returnType("string")
parameter("bar", "string")
parameter("baz") {
"0"
}
}method("bar") {}
property("qux");
const("TMP_CNST") {
"100500"
}
}}
}
}
}.create()
```# Output
> /path/to/file/file.php
```php
"value"] as $key => $value) {
echo "$key = $value";
}
try {
throw new \Exception();
}
catch (\Exception $e) {
var_dump($e);
}
finally {
echo "finally";
}
interface QuxInterface {
function foo(string $bar, $baz = 0): string;
function bar();
const CONSTANT = "VALUE";
}
trait FooTrait {
function foo() {
}
}
trait BarTrait {
function foo() {
}
}
abstract class FooClass implements QuxInterface {
use FooTrait {
foo as private;
foo as bar;
foo as private bar;
}
use FooTrait, BarTrait {
FooTrait::foo insteadof BarTrait;
}
private const CNST = 0;
}
class BarClass extends FooClass {
/**
* @param string $bar
* @param int $baz
* @return string
* @throws \Exception
*/
public function foo(string $bar, $baz = 0): string {
}
function bar() {
}
var $qux;
const TMP_CNST = 100500;
}
}
```