Skip to content

Commit

Permalink
feat(blueprints): add ability to create blueprint vars #24
Browse files Browse the repository at this point in the history
  • Loading branch information
Awilum committed Jun 11, 2021
1 parent c2df3d9 commit fbcc17e
Showing 1 changed file with 70 additions and 20 deletions.
90 changes: 70 additions & 20 deletions app/Models/Blueprints.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,9 @@ public function render(string $id, array $values = [], array $vars = []): void
{
$blueprint = flextype('blueprints')->fetch($id)->toArray();

$this->processEmitter($blueprint);
$this->processActions($blueprint);
$processVars = $this->processVars($blueprint, $vars);
$this->processEmitter($blueprint, $processVars);
$this->processActions($blueprint, $processVars);

echo flextype('twig')
->getEnvironment()
Expand All @@ -389,7 +390,7 @@ public function render(string $id, array $values = [], array $vars = []): void
'values' => $values,
'query' => $_GET,
'blocks' => flextype('registry')->get('plugins.blueprints.settings.blocks'),
], $vars));
], $processVars));
}

/**
Expand All @@ -405,8 +406,9 @@ public function render(string $id, array $values = [], array $vars = []): void
*/
public function renderFromArray(array $blueprint, array $values = [], array $vars = []): void
{
$this->processEmitter($blueprint);
$this->processActions($blueprint);
$processVars = $this->processVars($blueprint, $vars);
$this->processEmitter($blueprint, $processVars);
$this->processActions($blueprint, $processVars);

echo flextype('twig')
->getEnvironment()
Expand All @@ -417,7 +419,7 @@ public function renderFromArray(array $blueprint, array $values = [], array $var
'values' => $values,
'query' => $_GET,
'blocks' => flextype('registry')->get('plugins.blueprints.settings.blocks'),
], $vars));
], $processVars));
}

/**
Expand Down Expand Up @@ -545,7 +547,7 @@ public function getCacheID(string $id): string
*
* @access private
*/
private function processEmitter($blueprint): void
private function processEmitter(array $blueprint, array $vars = []): void
{
// Emmit events
if (isset($blueprint['emitter']['emit'])) {
Expand All @@ -557,48 +559,46 @@ private function processEmitter($blueprint): void
// Register listeners
if (isset($blueprint['emitter']['addListener'])) {
foreach ($blueprint['emitter']['addListener'] as $key => $event) {
flextype('emitter')->addListener($event['name'], function() use ($event) {

flextype('emitter')->addListener($event['name'], function() use ($event, $vars) {
dump($vars);
// Get event vars
$eventVars = [];
if (isset($event['properties']['vars'])) {
foreach ($event['properties']['vars'] as $key => $var) {

$varType = isset($var['type']) ? $var['type'] : 'string';
switch ($varType) {
case 'array':
if (is_iterable($var['value'])) {

array_walk_recursive($var['value'], function(&$value, $key) {
$value = strings(flextype('twig')->fetchFromString($value, []))->trim()->toString();
$value = strings(flextype('twig')->fetchFromString($value, $vars))->trim()->toString();
});

$eventVars[$var['name']] = $var['value'];

} else {
$value = htmlspecialchars_decode(flextype('twig')->fetchFromString(trim($var['value']), []));
$value = htmlspecialchars_decode(flextype('twig')->fetchFromString(trim($var['value']), $vars));
$eventVars[$var['name']] = flextype('serializers')->json()->decode($value);
}
break;
case 'bool':
$eventVars[$var['name']] = strings(flextype('twig')->fetchFromString($var['value'], []))->trim()->toBoolean();
$eventVars[$var['name']] = strings(flextype('twig')->fetchFromString($var['value'], $vars))->trim()->toBoolean();
break;
case 'float':
$eventVars[$var['name']] = strings(flextype('twig')->fetchFromString($var['value'], []))->trim()->toFloat();
$eventVars[$var['name']] = strings(flextype('twig')->fetchFromString($var['value'], $vars))->trim()->toFloat();
break;
case 'int':
$eventVars[$var['name']] = strings(flextype('twig')->fetchFromString($var['value'], []))->trim()->toInteger();
$eventVars[$var['name']] = strings(flextype('twig')->fetchFromString($var['value'], $vars))->trim()->toInteger();
break;
case 'string':
default:
$eventVars[$var['name']] = strings(flextype('twig')->fetchFromString($var['value'], []))->trim()->toString();
$eventVars[$var['name']] = strings(flextype('twig')->fetchFromString($var['value'], $vars))->trim()->toString();
break;
}
}
}

if (isset($event['properties']['value'])) {
strings(flextype('twig')->fetchFromString($event['properties']['value'], $eventVars))->trim()->echo();
strings(flextype('twig')->fetchFromString($event['properties']['value'], arrays($eventVars)->merge($vars)->toArray()))->trim()->echo();
}
});
}
Expand All @@ -614,7 +614,7 @@ private function processEmitter($blueprint): void
*
* @access private
*/
private function processActions($blueprint): void
private function processActions(array $blueprint, array $vars = []): void
{
if (isset($blueprint['actions'])) {

Expand All @@ -626,7 +626,6 @@ private function processActions($blueprint): void
$properties = array_values($action['properties']['vars']);
foreach ($properties as $key => $var) {
$type = isset($var['type']) ? $var['type'] : 'string';
$vars = [];
switch ($type) {
case 'array':
if (is_iterable($var['value'])) {
Expand Down Expand Up @@ -666,4 +665,55 @@ private function processActions($blueprint): void
flextype('emitter')->emit('onBlueprintsAfterProcessedActions');
}
}

/**
* Process vars for blueprint
*
* @param array $blueprint Blueprint array.
*
* @return void
*
* @access private
*/
private function processVars(array $blueprint, array $vars): array
{
if (isset($blueprint['vars'])) {
$processVars = [];
foreach ($blueprint['vars'] as $key => $var) {
$varType = isset($var['type']) ? $var['type'] : 'string';
switch ($varType) {
case 'array':
if (is_iterable($var['value'])) {

array_walk_recursive($var['value'], function(&$value, $key) {
$value = strings(flextype('twig')->fetchFromString($value, $vars))->trim()->toString();
});

$processVars[$var['name']] = $var['value'];

} else {
$value = htmlspecialchars_decode(flextype('twig')->fetchFromString(trim($var['value']), $vars));
$processVars[$var['name']] = flextype('serializers')->json()->decode($value);
}
break;
case 'bool':
$processVars[$var['name']] = strings(flextype('twig')->fetchFromString($var['value'], $vars))->trim()->toBoolean();
break;
case 'float':
$processVars[$var['name']] = strings(flextype('twig')->fetchFromString($var['value'], $vars))->trim()->toFloat();
break;
case 'int':
$processVars[$var['name']] = strings(flextype('twig')->fetchFromString($var['value'], $vars))->trim()->toInteger();
break;
case 'string':
default:
$processVars[$var['name']] = strings(flextype('twig')->fetchFromString($var['value'], $vars))->trim()->toString();
break;
}
}
return arrays($vars)->merge($processVars)->toArray();
} else {
return $vars;
}
}
}

0 comments on commit fbcc17e

Please sign in to comment.