|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
| 3 | + |
| 4 | +require_once '../vendor/autoload.php'; |
| 5 | + |
| 6 | +$loader = new Twig_Loader_Filesystem('templates'); |
| 7 | +$twig = new Twig_Environment($loader); |
| 8 | +$template = $twig->loadTemplate('endpoint.twig'); |
| 9 | + |
| 10 | +$counter = 0; |
| 11 | + |
| 12 | + |
| 13 | +function getApiPath() |
| 14 | +{ |
| 15 | + $path = dirname(__FILE__).'/elasticsearch/rest-api-spec/api/'; |
| 16 | + if (file_exists($path) !== true) { |
| 17 | + $path = dirname(__FILE__).'/elasticsearch/rest-api-spec/src/main/resources/rest-api-spec/api'; |
| 18 | + } |
| 19 | + return $path; |
| 20 | +} |
| 21 | + |
| 22 | + |
| 23 | +if ($handle = opendir(getApiPath())) { |
| 24 | + while (false !== ($entry = readdir($handle))) { |
| 25 | + if ($entry != "." && $entry != "..") { |
| 26 | + generateTemplate($entry, $template); |
| 27 | + } |
| 28 | + } |
| 29 | + closedir($handle); |
| 30 | +} |
| 31 | + |
| 32 | +function processURLPaths($data) |
| 33 | +{ |
| 34 | + $final = array(); |
| 35 | + |
| 36 | + $containsType = false; |
| 37 | + $containsIndex = false; |
| 38 | + foreach ($data['url']['paths'] as $path) { |
| 39 | + $params = array(); |
| 40 | + preg_match_all('/{(.*?)}/', $path, $params); |
| 41 | + $params = $params[1]; |
| 42 | + $count = count($params); |
| 43 | + $parsedPath = str_replace('}', '', $path); |
| 44 | + $parsedPath = str_replace('{', '$', $parsedPath); |
| 45 | + |
| 46 | + if (array_search('index', $params) !== false) { |
| 47 | + $containsIndex = true; |
| 48 | + } |
| 49 | + |
| 50 | + if (array_search('type', $params) !== false) { |
| 51 | + $containsType = true; |
| 52 | + } |
| 53 | + |
| 54 | + $duplicate = false; |
| 55 | + foreach ($final as $existing) { |
| 56 | + if ($existing['params'] === $params) { |
| 57 | + $duplicate = true; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + if ($duplicate !== true) { |
| 62 | + $final[] = array( |
| 63 | + 'path' => $path, |
| 64 | + 'parsedPath' => $parsedPath, |
| 65 | + 'params' => $params, |
| 66 | + 'count' => $count |
| 67 | + ); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /* |
| 72 | + foreach ($final as &$existing) { |
| 73 | + if ($containsIndex === true && array_search('index', $existing['params']) === false && array_search('type', $existing['params']) !== false) { |
| 74 | + $existing['parsedPath'] = '/_all'.$existing['parsedPath']; |
| 75 | + } |
| 76 | + } |
| 77 | + */ |
| 78 | + |
| 79 | + usort($final, function ($a, $b) { |
| 80 | + if ($a['count'] == $b['count']) { |
| 81 | + return 0; |
| 82 | + } |
| 83 | + |
| 84 | + return ($a['count'] > $b['count']) ? -1 : 1; |
| 85 | + }); |
| 86 | + |
| 87 | + return $final; |
| 88 | +} |
| 89 | + |
| 90 | +function getDefaultPath($path) |
| 91 | +{ |
| 92 | + if ($path['count'] === 0) { |
| 93 | + return $path['path']; |
| 94 | + } else { |
| 95 | + $final = str_replace('}', '', $path['path']); |
| 96 | + $final = str_replace('{', '$', $final); |
| 97 | + |
| 98 | + return $final; |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +function forbid($key, $value) |
| 103 | +{ |
| 104 | + $forbid = array( |
| 105 | + 'GET' => array( |
| 106 | + '/_nodes/hotthreads', |
| 107 | + '/_nodes/{node_id}/hotthreads', |
| 108 | + '/_nodes/{metric}' |
| 109 | + ), |
| 110 | + 'HEAD' => array(), |
| 111 | + 'PUT' => array( |
| 112 | + '/{index}/{type}/_mapping' |
| 113 | + ), |
| 114 | + 'POST' => array( |
| 115 | + '/_all/{type}/_bulk', |
| 116 | + '/_all/{type}/_mget' |
| 117 | + ), |
| 118 | + 'DELETE' => array( |
| 119 | + '/{index}/{type}', |
| 120 | + '/{index}/{type}/_mapping' |
| 121 | + ), |
| 122 | + 'QS' => array( |
| 123 | + 'operation_threading', |
| 124 | + 'field_data' |
| 125 | + ) |
| 126 | + ); |
| 127 | + |
| 128 | + if (isset($forbid['key']) && $forbid['key'] === $value) { |
| 129 | + return true; |
| 130 | + } else { |
| 131 | + return false; |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +function generateTemplate($path, $template) |
| 136 | +{ |
| 137 | + $ignoreList = array( |
| 138 | + 'index.json', 'bulk.json' |
| 139 | + ); |
| 140 | + |
| 141 | + if (array_search($path, $ignoreList) !== false) { |
| 142 | + return; |
| 143 | + } |
| 144 | + |
| 145 | + $path = getApiPath().DIRECTORY_SEPARATOR.$path; |
| 146 | + $json = file_get_contents($path); |
| 147 | + $data = json_decode($json, true); |
| 148 | + |
| 149 | + reset($data); |
| 150 | + $namespace = key($data); |
| 151 | + $data = $data[$namespace]; |
| 152 | + $namespace = explode('.', $namespace); |
| 153 | + |
| 154 | + $underscoreNamespace = array( |
| 155 | + 'get', |
| 156 | + 'put', |
| 157 | + 'post', |
| 158 | + 'delete', |
| 159 | + 'exists', |
| 160 | + 'update', |
| 161 | + 'create' |
| 162 | + ); |
| 163 | + |
| 164 | + $exceptions = array( |
| 165 | + 'delete_by_query', |
| 166 | + 'update_by_query', |
| 167 | + ); |
| 168 | + |
| 169 | + if (strpos($namespace[count($namespace)-1], '_')) { |
| 170 | + $temp = explode('_', $namespace[count($namespace)-1]); |
| 171 | + |
| 172 | + if (array_search($temp[0], $underscoreNamespace) !== false && array_search($namespace[count($namespace)-1], $exceptions) === false) { |
| 173 | + $namespace[count($namespace)-1] = $temp[1]; |
| 174 | + $namespace[] = $temp[0]; |
| 175 | + } else { |
| 176 | + $namespace[count($namespace)-1] = str_replace('_', '', $namespace[count($namespace)-1]); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + $data['url']['processed'] = processURLPaths($data); |
| 181 | + $data['url']['default'] = getDefaultPath($data['url']['processed'][count($data['url']['processed'])-1]); |
| 182 | + |
| 183 | + $renderVars = array( |
| 184 | + 'json' => $json, |
| 185 | + 'data' => $data, |
| 186 | + 'namespace' => $namespace, |
| 187 | + 'className' => ucfirst($namespace[count($namespace)-1]), |
| 188 | + ); |
| 189 | + |
| 190 | + $ret = $template->render($renderVars); |
| 191 | + |
| 192 | + $dir = './output/'.implode('/', array_map('ucfirst', array_splice($namespace, 0, count($namespace)-1))); |
| 193 | + |
| 194 | + if (substr($dir, -1) !== '/') { |
| 195 | + $dir .= '/'; |
| 196 | + } |
| 197 | + if (!file_exists($dir)) { |
| 198 | + echo 'making dir: '.$dir."\n\n"; |
| 199 | + $oldumask = umask(0); |
| 200 | + mkdir($dir, 0777, true); |
| 201 | + umask($oldumask); |
| 202 | + } |
| 203 | + |
| 204 | + echo $dir."\n\n"; |
| 205 | + $path = $dir.$renderVars['className'].'.php'; |
| 206 | + echo $path."\n\n"; |
| 207 | + |
| 208 | + file_put_contents($path, $ret); |
| 209 | +} |
0 commit comments