Skip to content

Commit ac1aa96

Browse files
Jean FradetJean Fradet
authored andcommitted
Modified global_aliases to be stored in default bolt forms config
1 parent 941ca3c commit ac1aa96

File tree

1 file changed

+49
-7
lines changed

1 file changed

+49
-7
lines changed

src/BoltFormsConfig.php

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,27 @@ private function getAdditionalFormConfigs(): array
7373
$boltFormsRoot = explode('.yaml', $mainConfigPath)[0];
7474
$extrasConfigPath = $boltFormsRoot . DIRECTORY_SEPARATOR;
7575

76-
$global_aliases = file_get_contents($boltFormsRoot.'-global.yaml');
76+
// Share global_aliases with other configs
77+
$defaultConfig;
78+
$global_aliases;
79+
80+
try {
81+
$defaultConfig = file_get_contents($boltFormsRoot.'.yaml');
82+
}
83+
catch (ParseException $e) {
84+
throw new ParseException(sprintf('Default BoltForm Config Not Found'));
85+
}
86+
87+
$global_aliases = $this->getFormGlobals($defaultConfig);
7788

78-
if (is_null($global_aliases) ) {$global_aliases = '';}
7989
$finder = new Finder();
80-
8190
$finder->files()->in($extrasConfigPath)->name('*.yaml');
82-
91+
92+
// Exit if no sub_files str
8393
if (! $finder->hasResults()) {
8494
return [];
8595
}
96+
8697
$result = [];
8798

8899
foreach ($finder as $file) {
@@ -97,11 +108,42 @@ private function getAdditionalFormConfigs(): array
97108
} catch (ParseException $e) {
98109
$e;
99110
throw new ParseException(sprintf('Error detected on form %s: %s', $formName, $e->getMessage()));
100-
}
101-
111+
}
102112
unset($result[$formName]['global_aliases']);
103113
}
104-
114+
105115
return $result;
106116
}
117+
118+
private function getFormGlobals(string $defaultFormContents): string
119+
{
120+
$start = strpos($defaultFormContents, 'global_aliases');
121+
122+
if ($start === false) return '';
123+
124+
$end;
125+
$commented_out;
126+
127+
//Check if commented out
128+
if ($start === 0):
129+
$commented_out = false;
130+
else:
131+
// Check to see if a disabled through commenting (previous 5 char)
132+
$rewind_check_start_pos = ($start > 5)? $start - 5 : 0;
133+
$scan_str = substr($defaultFormContents,$rewind_check_start_pos, $start);
134+
135+
$comment_found = strpos($scan_str, '#'.-1); //Note: actually a position, not a bool, reverse searched
136+
$commented_out = ($comment_found !== false && $comment_found < strpos($scan_str, "\n".-1)); //ie: # found and no inbetween \n
137+
endif;
138+
139+
if ($commented_out) return '';
140+
141+
// This is used to find the first instance of a line which starts with neither a whitespace or comment (ie the end of the global aliases, if starting just after the g (start + 1))
142+
preg_match("/^[^\s\#]/m", $defaultFormContents."", $matches, PREG_OFFSET_CAPTURE, $start+1);
143+
if (sizeof($matches) == 0) return '';
144+
145+
$end = $matches[0][1];
146+
$rt_str = substr($defaultFormContents,$start,$end);
147+
return $rt_str;
148+
}
107149
}

0 commit comments

Comments
 (0)