Skip to content

Commit edb904f

Browse files
committed
Remove Settings class in favor of Utils & Config.
1 parent 55879a7 commit edb904f

File tree

13 files changed

+672
-83
lines changed

13 files changed

+672
-83
lines changed

ProcessGraphQLConfig.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php namespace ProcessWire;
22

3-
use \ProcessWire\GraphQL\Type\InterfaceType\PageInterfaceType;
4-
use \ProcessWire\GraphQL\Type\InterfaceType\PageFileInterfaceType;
5-
use \ProcessWire\GraphQL\Settings;
3+
use ProcessWire\GraphQL\Type\InterfaceType\PageInterfaceType;
4+
use ProcessWire\GraphQL\Type\InterfaceType\PageFileInterfaceType;
5+
use ProcessWire\GraphQL\Utils;
66

77
require_once $this->config->paths->site . 'modules/ProcessGraphQL/vendor/autoload.php';
88

@@ -37,7 +37,7 @@ public static function isLegalTemplateName($name)
3737
if (preg_match('/^[_A-Za-z][-_0-9A-Za-z]*$/', $name) !== 1) return false; // the GraphQL naming requirement
3838
if (strpos($name, '__') === 0) return false; // the names with `__` prefix are reserved by GraphQL
3939
if (strpos($name, '--') === 0) return false; // as we change the `-` symbols to `_` for field names the `--` becomes `__` and it also reserved by GraphQL
40-
if (in_array($name, Settings::getReservedWords())) return false; // some words that used now and might be for future
40+
if (in_array($name, Utils::getReservedWords())) return false; // some words that used now and might be for future
4141
return true;
4242
}
4343

@@ -127,4 +127,4 @@ public function getInputFields()
127127
return $inputfields;
128128
}
129129

130-
}
130+
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "dadish/ProcessGraphql",
33
"type": "processwire-plugin",
44
"license": "MIT",
5+
"description": "GraphQL for ProcessWire",
56
"authors": [
67
{
78
"name": "Nurguly Ashyrov",

src/Config.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace ProcessWire\GraphQL;
4+
5+
use ProcessWire\WireData;
6+
use ProcessWire\ProcessGraphQL;
7+
8+
class Config extends WireData {
9+
10+
protected $module;
11+
12+
public function __construct(ProcessGraphQL $module)
13+
{
14+
$this->module = $module;
15+
16+
// cache reference for useful ProcessWire API variables
17+
$apiVars = [
18+
'templates',
19+
'fields',
20+
'roles',
21+
'users',
22+
'pages',
23+
];
24+
foreach ($apiVars as $varName) {
25+
$this->$varName = \ProcessWire\wire($varName);
26+
}
27+
28+
// Assign Config to module so we can access it easily accross the module codebase.
29+
$module->Config = $this;
30+
}
31+
32+
public function get($key)
33+
{
34+
switch ($key) {
35+
case 'maxLimit':
36+
case 'fullWidthGraphiQL':
37+
case 'legalPageFields':
38+
case 'legalPageFileFields':
39+
return $this->module->$key;
40+
case 'legalTemplates':
41+
return $this->getLegalTemplates();
42+
case 'legalFields':
43+
return $this->getLegalFields();
44+
default:
45+
return parent::get($key);
46+
}
47+
}
48+
49+
protected function getLegalTemplates()
50+
{
51+
$legalTemplates = $this->module->legalTemplates;
52+
$templates = \ProcessWire\wire('templates')->find("name=" . implode('|', $legalTemplates));
53+
$user = \ProcessWire\wire('user');
54+
55+
// Wierd behavior with ProcessWire. $user->hasPermission() does not
56+
// work if you do not load the required roles beforehand.
57+
\ProcessWire\wire('roles')->find("");
58+
59+
foreach ($templates as $template) {
60+
// We serve only those that user has permission to view
61+
if (!$user->hasTemplatePermission('page-view', $template)) {
62+
$templates->remove($template);
63+
}
64+
}
65+
66+
return $templates;
67+
}
68+
69+
protected function getLegalFields()
70+
{
71+
$legalFields = $this->module->legalFields;
72+
$fields = \ProcessWire\wire('fields')->find("name=" . implode('|', $legalFields));
73+
return $fields;
74+
}
75+
76+
}

src/Schema.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use Youshido\GraphQL\Config\Schema\SchemaConfig;
44
use Youshido\GraphQL\Schema\AbstractSchema;
5-
use ProcessWire\GraphQL\Settings;
5+
use ProcessWire\GraphQL\Utils;
66
use ProcessWire\GraphQL\Field\Pages\PagesField;
77
use ProcessWire\GraphQL\Field\TemplatedPageArray\TemplatedPageArrayField;
88
use ProcessWire\GraphQL\Field\Debug\DbQueryCountField;
@@ -11,7 +11,6 @@
1111
use ProcessWire\GraphQL\Field\User\UserField;
1212
use ProcessWire\GraphQL\Field\Mutation\CreateTemplatedPage;
1313

14-
1514
class Schema extends AbstractSchema {
1615

1716
protected $fields = [];
@@ -27,7 +26,7 @@ public function build(SchemaConfig $config)
2726
$query->addField(new PagesField());
2827

2928
// $templates
30-
foreach (Settings::getLegalTemplates() as $template) {
29+
foreach (Utils::moduleConfig()->legalTemplates as $template) {
3130
$query->addField(new TemplatedPageArrayField($template));
3231
}
3332

@@ -50,7 +49,7 @@ public function build(SchemaConfig $config)
5049
$mutation = $config->getMutation();
5150

5251
// CreatePage
53-
foreach (Settings::getLegalTemplates() as $template) {
52+
foreach (Utils::moduleConfig()->legalTemplates as $template) {
5453
$mutation->addField(new CreateTemplatedPage($template));
5554
}
5655

src/Settings.php

Lines changed: 0 additions & 61 deletions
This file was deleted.

src/Type/InterfaceType/PageFileInterfaceType.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
use Youshido\GraphQL\Type\InterfaceType\AbstractInterfaceType;
66
use Youshido\GraphQL\Type\Scalar\StringType;
77
use Youshido\GraphQL\Type\Scalar\IntType;
8+
use ProcessWire\GraphQL\Utils;
89
use ProcessWire\Pageimage;
910
use ProcessWire\GraphQL\Type\Object\PageFileType;
1011
use ProcessWire\GraphQL\Type\Object\PageImageType;
11-
use ProcessWire\GraphQL\Settings;
1212

1313
class PageFileInterfaceType extends AbstractInterfaceType {
1414

@@ -25,7 +25,7 @@ public function getDescription()
2525
public function build($config)
2626
{
2727
$fields = self::getPageFileFields();
28-
$legalPageFileFields = Settings::getLegalPageFileFields();
28+
$legalPageFileFields = Utils::moduleConfig()->legalPageFileFields;
2929
foreach ($fields as $fieldName => $fieldConfig) {
3030
if (!in_array($fieldName, $legalPageFileFields)) continue;
3131
$config->addField($fieldName, $fieldConfig);

src/Type/InterfaceType/PageInterfaceType.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
namespace ProcessWire\GraphQL\Type\InterfaceType;
44

55
use Youshido\GraphQL\Type\InterfaceType\AbstractInterfaceType;
6+
use ProcessWire\GraphQL\Utils;
67
use ProcessWire\Field;
78
use ProcessWire\GraphQL\Type\Object\TemplatedPageType;
8-
use ProcessWire\GraphQL\Settings;
99

1010
class PageInterfaceType extends AbstractInterfaceType {
1111

@@ -22,7 +22,7 @@ public function getDescription()
2222
public function build($config)
2323
{
2424
$fields = self::getPageFields();
25-
$legalPageFields = Settings::getLegalPageFields();
25+
$legalPageFields = Utils::moduleConfig()->legalPageFields;
2626

2727
foreach ($fields as $fieldName => $fieldClassName) {
2828
if (!in_array($fieldName, $legalPageFields)) continue;
@@ -31,7 +31,7 @@ public function build($config)
3131
}
3232

3333
// add global fields too
34-
$legalFields = Settings::getLegalFields();
34+
$legalFields = Utils::moduleConfig()->legalFields;
3535
foreach ($legalFields as $field) {
3636
if ($field->flags & Field::flagGlobal) {
3737
$className = "\\ProcessWire\\GraphQL\\Field\\Page\\Fieldtype\\" . $field->type->className();

src/Type/InterfaceType/PaginatedArrayType.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Youshido\GraphQL\Type\InterfaceType\AbstractInterfaceType;
66
use Youshido\GraphQL\Type\Scalar\IntType;
7-
use ProcessWire\GraphQL\Settings;
7+
use ProcessWire\GraphQL\Utils;
88
use ProcessWire\GraphQL\Type\Scalar\SelectorType;
99
use ProcessWire\GraphQL\Type\Object\PageArrayType;
1010
use ProcessWire\GraphQL\Type\Object\TemplatedPageArrayType;
@@ -23,7 +23,7 @@ public function getDescription()
2323

2424
public function build($config)
2525
{
26-
$maxLimit = Settings::module()->maxLimit;
26+
$maxLimit = Utils::moduleConfig()->maxLimit;
2727
$config->addFields([
2828

2929
// \ProcessWire\PaginatedArray::getTotal()
@@ -68,7 +68,7 @@ public function resolveType($pageArray)
6868

6969
// if there is only one template selected then we can assume it is a TemplatedPageArray
7070
if (count($templateSelector->values) === 1) {
71-
$template = Settings::getLegalTemplates()->get($templateSelector->values[0]);
71+
$template = Utils::moduleConfig()->legalTemplates->get($templateSelector->values[0]);
7272
return new TemplatedPageArrayType($template);
7373
}
7474
return new PageArrayType();

src/Type/Object/TemplatedPageType.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
use Youshido\GraphQL\Type\Object\AbstractObjectType;
66
use ProcessWire\Template;
77
use ProcessWire\Field;
8+
use ProcessWire\GraphQL\Utils;
89
use ProcessWire\GraphQL\Type\InterfaceType\PageInterfaceType;
9-
use ProcessWire\GraphQL\Settings;
1010

1111
class TemplatedPageType extends AbstractObjectType {
1212

@@ -37,7 +37,7 @@ public function getDescription()
3737

3838
public function build($config)
3939
{
40-
$legalFields = Settings::getLegalFields();
40+
$legalFields = Utils::moduleConfig()->legalFields;
4141
$config->applyInterface(new PageInterfaceType());
4242
foreach ($this->template->fields as $field) {
4343
if (!$legalFields->has($field)) continue;

src/Type/Scalar/SelectorType.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use ProcessWire\Selectors;
88
use ProcessWire\Selector;
99
use ProcessWire\SelectorEqual;
10-
use ProcessWire\GraphQL\Settings;
10+
use ProcessWire\GraphQL\Utils;
1111

1212
class SelectorType extends StringType {
1313

@@ -29,7 +29,7 @@ public function serialize($selectors)
2929
$selectors = new Selectors($selectors);
3030

3131
// make sure the limit field is not greater than max allowed
32-
$maxLimit = Settings::module()->maxLimit;
32+
$maxLimit = Utils::moduleConfig()->maxLimit;
3333
$limitSelector = self::findSelectorByField($selectors, 'limit');
3434
if ($limitSelector) {
3535
if ($maxLimit < $limitSelector->value) $limitSelector->set('value', $maxLimit);
@@ -40,7 +40,7 @@ public function serialize($selectors)
4040

4141
// make sure to limit the search to legal templates only
4242
$templateSelector = self::findSelectorByField($selectors, 'template');
43-
$legalTemplates = Settings::getLegalTemplates();
43+
$legalTemplates = Utils::moduleConfig()->legalTemplates;
4444
$names = [];
4545

4646
if ($templateSelector instanceof Selector) {

0 commit comments

Comments
 (0)