GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — ProcessGraphQL/ProcessGraphQLConfig.php at master · gioqw/ProcessGraphQL Expand file tree/ Copy pathProcessGraphQLConfig.phpMore file actions 303 lines (268 loc) · 10.1 KB / Copy pathProcessGraphQLConfig.phpFile metadata and controls303 lines (268 loc) · 10.1 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 <?php namespace ProcessWire; use ProcessWire\GraphQL\Utils; use ProcessWire\GraphQL\Type\PageType; use ProcessWire\GraphQL\Type\FileType; use ProcessWire\GraphQL\Type\ImageType; require_once $this->config->paths->site . 'modules/ProcessGraphQL/vendor/autoload.php'; class ProcessGraphQLConfig extends Moduleconfig { const generatorName = 'graphql_generate_pages'; public function getDefaults() { return array( /** * Sets the max for ProcessWire's limit selector field. * @var integer */ 'maxLimit' => 50, /** * An array of template names that will be concidered for schema generation. * @var array */ 'legalTemplates' => [], /** * An array of field names that will be considered for schema generation. * @var array */ 'legalFields' => [], /** * An array of built-in Page field names that will be considered for schema * generation. * @var array */ 'legalPageFields' => [ 'created', 'modified', 'url', 'id', 'name', 'httpUrl', 'template', ], /** * An array of built-in PageFile field names that will be considered for * schema createtion. * @var array */ 'legalPageFileFields' => [ 'url', 'httpUrl', 'description', ], /** * An array of built-in PageImage field names that will be considered for * schema createtion. * @var array */ 'legalPageImageFields' => [ 'width', 'height', 'variations', ], /** * The `me` query field. Allows the user to query her credentials. * @var boolean */ 'meQuery' => true, /** * The `login` & `logout` fields. Provides authentication methods. * @var boolean */ 'authQuery' => true, ); } /** * Checks if the given name is compatible with GraphQL. * @param string $name The name against the check will be performed. * @return boolean True if the name is compatible, false otherwise. */ public static function isLegalTemplateName($name) { if (!$name) return false; if (preg_match('/^[_A-Za-z][-_0-9A-Za-z]*$/', $name) !== 1) return false; // the GraphQL naming requirement if (strpos($name, '__') === 0) return false; // the names with `__` prefix are reserved by GraphQL if (strpos($name, '--') === 0) return false; // as we change the `-` symbols to `_` for field names the `--` becomes `__` and it also reserved by GraphQL if (in_array($name, Utils::getReservedWords())) return false; // some words that used now and might be for future return true; } public function generatePages() { $pageNames = [ 'graphql' => 'GraphQL', 'graphiql' => 'GraphiQL', ]; // copy the template files to site/templates folder try { $from = $this->config->paths->ProcessGraphQL . 'templates/'; $to = $this->config->paths->templates; $this->files->copy($from, $to); $this->message(sprintf($this->_('Created template files: %s'), 'graphql.php & graphiql.php')); } catch(\Exception $e) { $this->error($e->getMessage()); } foreach($pageNames as $name => $title) { // create the templates try { $template = $this->wire(new Template()); $template->name = $name; $fieldgroup = $this->wire(new Fieldgroup()); $fieldgroup->name = $template->name; if ($this->fields->get('title') instanceof Field) $fieldgroup->add('title'); $fieldgroup->save(); $template->fieldgroup = $fieldgroup; $template->roles = array($this->roles->getGuestRole()->id); $template->noAppendTemplateFile = true; $template->noPrependTemplateFile = true; $template->save(); $this->message(sprintf($this->_('Added template and fieldgroup: %s'), $name)); } catch(\Exception $e) { $this->error($e->getMessage()); continue; } // create the pages try { $p = $this->wire(new Page()); $p->template = $this->templates->get($name); $p->name = $name; $p->parent = $this->pages->get(1); // root page $p->title = $title; $p->save(); $this->message(sprintf($this->_('Created page: %s'), $name)); } catch(\Exception $e) { $this->error($e->getMessage()); continue; } } } public function readyToGeneratePages() { $generatorName = self::generatorName; if ($this->input->post->$generatorName) $this->generatePages(); if (is_file($this->config->paths->templates . 'graphql.php')) return false; if (is_file($this->config->paths->templates . 'graphiql.php')) return false; if ($this->templates->get('graphql') instanceof Template) return false; if ($this->templates->get('graphiql') instanceof Template) return false; if (!$this->pages->get('/graphql/') instanceof NullPage) return false; if (!$this->pages->get('/graphiql/') instanceof NullPage) return false; return true; } /** * Build module configuration. * @return InputFields ProcessWire Inputfields form. */ public function getInputFields() { $inputfields = parent::getInputFields(); // legalTemplates $f = $this->modules->get('InputfieldAsmSelect'); $f->optionColumns = 4; $f->attr('name', 'legalTemplates'); $f->label = 'Templates'; $f->description = 'Choose which page templates you want to be served via GraphQL api.'; $gotDisabledFields = false; foreach (\ProcessWire\wire('templates') as $template) { // skip repeater templates if (Utils::isRepeaterTemplate($template)) { continue; } $attributes = []; if (!self::isLegalTemplateName($template->name)) { $attributes['disabled'] = true; $gotDisabledFields = true; } $label = $template->flags & Template::flagSystem ? "{$template->name} (system)" : $template->name; $f->addOption($template->name, $label, $attributes); } $notes = "Please be careful with what you are exposing to the public. Choosing templates marked as `system` can lead to security vulnerabilities."; if ($gotDisabledFields) { $notes .= PHP_EOL; $notes .= "The template is disabled if it's name is incompatible or reserved for ProcessGraphQL module."; } $f->notes = $notes; $inputfields->add($f); // legalFields $f = $this->modules->get('InputfieldAsmSelect'); $f->optionColumns = 4; $f->attr('name', 'legalFields'); $f->label = 'Fields'; $f->description = 'Choose which fields you want to be served via GraphQL api.'; $f->notes = 'Please be careful with what you are exposing to the public. Choosing fields marked as `system` can to lead security vulnerabilities.'; foreach (\ProcessWire\wire('fields')->find("name!=pass") as $field) { if ($field->type instanceof FieldtypeFieldsetOpen) continue; if ($field->type instanceof FieldtypeFieldsetClose) continue; if ($field->type instanceof FieldtypeFieldsetTabOpen) continue; $f->addOption($field->name, $field->flags & Field::flagSystem ? "{$field->name} (system)" : $field->name); } $inputfields->add($f); // legalPageFields $f = $this->modules->get('InputfieldAsmSelect'); $f->optionColumns = 4; $f->attr('name', 'legalPageFields'); $f->label = 'Page Fields'; $f->description = 'Choose which built in `Page` fields you want to be served via GraphQL api.'; $f->notes = 'Be careful with fields like `parents` & `children` that will allow user to construct deeply nested queries that might be very expensive for your server to fulfill.'; foreach (PageType::getBuiltInFields() as $field) { $f->addOption($field['name']); } $inputfields->add($f); // legalPageFileFields $f = $this->modules->get('InputfieldAsmSelect'); $f->optionColumns = 4; $f->attr('name', 'legalPageFileFields'); $f->label = 'PageFile Fields'; $f->description = 'Choose which built in `PageFile` fields you want to be served via GraphQL api.'; $f->notes = 'These fields are also inherited by `PageImage`.'; foreach (FileType::getBuiltInFields() as $field) { $f->addOption($field['name']); } $inputfields->add($f); // legalPageImageFields $f = $this->modules->get('InputfieldAsmSelect'); $f->optionColumns = 4; $f->attr('name', 'legalPageImageFields'); $f->label = 'PageImage Fields'; $f->description = 'Choose which built in `PageImage` fields you want to be served via GraphQL api.'; foreach (ImageType::getBuiltInFields() as $field) { $f->addOption($field['name']); } $inputfields->add($f); // GRAPHQL PAGES GENERATOR if ($this->readyToGeneratePages()) { $fSet = $this->modules->get('InputfieldFieldset'); $fSet->label = 'GraphQL Pages Generator'; $fSet->collapsed = Inputfield::collapsedYes; // graphql template name $f = $this->modules->get('InputfieldSubmit'); $f->label = 'Generate'; $f->description = 'Generates the graphql and graphiql templates, template files and pages.'; $f->attr('name', self::generatorName); $f->attr('value', 'Generate'); $fSet->add($f); $inputfields->add($fSet); } // maxLimit $f = $this->modules->get('InputfieldInteger'); $f->attr('name', 'maxLimit'); $f->label = 'Max Limit'; $f->description = 'Set the maximum value for `limit` selector field for GraphQL api.'; $f->required = true; $inputfields->add($f); // meQuery $f = $this->modules->get('InputfieldCheckbox'); $f->attr('name', 'meQuery'); $f->label = 'me Query'; $f->columnWidth = 50; $desc = "Adds `me` query field. Allows user to query her credentials."; $f->description = $desc; $inputfields->add($f); // authQuery $f = $this->modules->get('InputfieldCheckbox'); $f->attr('name', 'authQuery'); $f->label = 'login/logout Query'; $f->columnWidth = 50; $desc = "Adds `login` & `logout` fields. Allows users to authenticate via GraphQL API."; $f->description = $desc; $inputfields->add($f); $inputfields->add($f); return $inputfields; } } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |