|
| 1 | +<?php |
| 2 | + |
| 3 | +use Packaged\Helpers\Strings; |
| 4 | + |
| 5 | +require dirname(__DIR__) . '/vendor/autoload.php'; |
| 6 | + |
| 7 | +$outputDir = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'src' |
| 8 | + . DIRECTORY_SEPARATOR . 'Generated' . DIRECTORY_SEPARATOR . 'V1'; |
| 9 | +if(!file_exists($outputDir)) |
| 10 | +{ |
| 11 | + mkdir($outputDir, 0777, true); |
| 12 | +} |
| 13 | +$files = glob(__DIR__ . DIRECTORY_SEPARATOR . 'tmp_webhooks/*.json'); |
| 14 | +$mutations = ['UPDATE' => [], 'CREATE' => []]; |
| 15 | +$mutationCount = 0; |
| 16 | + |
| 17 | +function filenameToClass($file) |
| 18 | +{ |
| 19 | + return ucfirst($file); |
| 20 | +} |
| 21 | + |
| 22 | +foreach($files as $file) |
| 23 | +{ |
| 24 | + $json = json_decode(file_get_contents($file)); |
| 25 | + $filename = basename($file, '.json'); |
| 26 | + $className = filenameToClass($json->name ?? $filename); |
| 27 | + |
| 28 | + $src = []; |
| 29 | + $src[] = '<?php'; |
| 30 | + $src[] = 'namespace Fident\\Webhooks\\Generated\\V1;'; |
| 31 | + $src[] = ''; |
| 32 | + $src[] = 'use Fident\\Webhooks\\WebhookFoundation;'; |
| 33 | + $src[] = ''; |
| 34 | + if(isset($json->description)) |
| 35 | + { |
| 36 | + $src[] = '/**'; |
| 37 | + $src[] = ' * ' . $json->description; |
| 38 | + $src[] = ' */'; |
| 39 | + } |
| 40 | + $src[] = 'class ' . $className . ' extends WebhookFoundation'; |
| 41 | + $src[] = '{'; |
| 42 | + |
| 43 | + $setters = []; |
| 44 | + |
| 45 | + foreach($json->properties as $property => $propertyDefinition) |
| 46 | + { |
| 47 | + if(isset($propertyDefinition->enum)) |
| 48 | + { |
| 49 | + foreach($propertyDefinition->enum as $enum) |
| 50 | + { |
| 51 | + $src[] = ' const ' |
| 52 | + . strtoupper(str_replace(' ', '_', Strings::splitOnCamelCase($property . '_' . $enum))) |
| 53 | + . ' = "' . addslashes($enum) . '";'; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + $docBlock = []; |
| 58 | + if(isset($propertyDefinition->description)) |
| 59 | + { |
| 60 | + $docBlock[] = ' * ' . $propertyDefinition->description; |
| 61 | + $docBlock[] = ' *'; |
| 62 | + } |
| 63 | + |
| 64 | + if(isset($propertyDefinition->type)) |
| 65 | + { |
| 66 | + if($propertyDefinition->type == "array" && isset($propertyDefinition->items)) |
| 67 | + { |
| 68 | + if(isset($propertyDefinition->items->{'$ref'})) |
| 69 | + { |
| 70 | + $ref = $propertyDefinition->items->{'$ref'}; |
| 71 | + $class = filenameToClass(basename($ref, '.json')); |
| 72 | + $docBlock[] = ' * @var ' . $class . '[]'; |
| 73 | + $setters[$property] = "$class::manyFromSource(\$value)"; |
| 74 | + } |
| 75 | + else if(isset($propertyDefinition->items->type)) |
| 76 | + { |
| 77 | + $docBlock[] = ' * @var ' . $propertyDefinition->items->type . '[]'; |
| 78 | + } |
| 79 | + } |
| 80 | + else |
| 81 | + { |
| 82 | + $docBlock[] = ' * @var ' . $propertyDefinition->type; |
| 83 | + } |
| 84 | + } |
| 85 | + else if(isset($propertyDefinition->{'$ref'})) |
| 86 | + { |
| 87 | + $ref = $propertyDefinition->{'$ref'}; |
| 88 | + $class = filenameToClass(basename($ref, '.json')); |
| 89 | + $docBlock[] = ' * @var ' . $class; |
| 90 | + $setters[$property] = "$class::fromSource(\$value)"; |
| 91 | + } |
| 92 | + |
| 93 | + if(!empty($docBlock)) |
| 94 | + { |
| 95 | + $src[] = ' /**'; |
| 96 | + $src = array_merge($src, $docBlock); |
| 97 | + $src[] = ' */'; |
| 98 | + } |
| 99 | + |
| 100 | + $src[] = ' public $' . $property . ';'; |
| 101 | + $src[] = ''; |
| 102 | + } |
| 103 | + |
| 104 | + if(!empty($setters)) |
| 105 | + { |
| 106 | + $src[] = ' protected function _set($property, $value)'; |
| 107 | + $src[] = ' {'; |
| 108 | + foreach($setters as $property => $setter) |
| 109 | + { |
| 110 | + $src[] = ' if($property == \'' . $property . '\')'; |
| 111 | + $src[] = ' {'; |
| 112 | + $src[] = ' $this->' . $property . ' = ' . $setter . ';'; |
| 113 | + $src[] = ' return;'; |
| 114 | + $src[] = ' }'; |
| 115 | + $src[] = ''; |
| 116 | + } |
| 117 | + $src[] = ' parent::_set($property, $value);'; |
| 118 | + $src[] = ' }'; |
| 119 | + } |
| 120 | + |
| 121 | + $src[] = '}'; |
| 122 | + $src[] = ''; |
| 123 | + |
| 124 | + $outputFile = $outputDir . DIRECTORY_SEPARATOR . $className . '.php'; |
| 125 | + $data = implode(PHP_EOL, $src); |
| 126 | + |
| 127 | + $exists = file_exists($outputFile); |
| 128 | + if(!$exists || file_get_contents($outputFile) != $data) |
| 129 | + { |
| 130 | + file_put_contents($outputFile, $data); |
| 131 | + $mutations[$exists ? 'UPDATE' : 'CREATE'][] = $className; |
| 132 | + $mutationCount++; |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +if($mutationCount > 0) |
| 137 | +{ |
| 138 | + $file = fopen('v1.changelog.md', 'a+'); |
| 139 | + if($file) |
| 140 | + { |
| 141 | + fwrite($file, "####Build Process @ " . date("Y-m-d H:i:s") . PHP_EOL); |
| 142 | + foreach(['CREATE' => 'New Classes', 'UPDATE' => 'Updated Classes'] as $state => $message) |
| 143 | + { |
| 144 | + |
| 145 | + if(!empty($mutations[$state])) |
| 146 | + { |
| 147 | + fwrite($file, "####$message" . PHP_EOL); |
| 148 | + foreach($mutations[$state] as $class) |
| 149 | + { |
| 150 | + fwrite($file, "- $class" . PHP_EOL); |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + fwrite($file, PHP_EOL); |
| 156 | + fclose($file); |
| 157 | + } |
| 158 | +} |
0 commit comments