Skip to content

Commit 8574627

Browse files
committed
Resolve lambdas before beginning filter chains.
1 parent f2b50b0 commit 8574627

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/Mustache/Compiler.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -570,18 +570,23 @@ private function variable($id, $filters, $escape, $level)
570570
if (!(%s)) {
571571
throw new Mustache_Exception_UnknownFilterException(%s);
572572
}
573-
$value = call_user_func($filter, $value);%s
573+
$value = call_user_func($filter, %s);%s
574574
';
575+
const FILTER_FIRST_VALUE = '$this->resolveValue($value, $context)';
576+
const FILTER_VALUE = '$value';
575577

576578
/**
577579
* Generate Mustache Template variable filtering PHP source.
578580
*
581+
* If the initial $value is a lambda it will be resolved before starting the filter chain.
582+
*
579583
* @param string[] $filters Array of filters
580584
* @param int $level
585+
* @param bool $first (default: false)
581586
*
582587
* @return string Generated filter PHP source
583588
*/
584-
private function getFilters(array $filters, $level)
589+
private function getFilters(array $filters, $level, $first = true)
585590
{
586591
if (empty($filters)) {
587592
return '';
@@ -593,8 +598,9 @@ private function getFilters(array $filters, $level)
593598
$findArg = $this->getFindMethodArgs($method);
594599
$callable = $this->getCallable('$filter');
595600
$msg = var_export($name, true);
601+
$value = $first ? self::FILTER_FIRST_VALUE : self::FILTER_VALUE;
596602

597-
return sprintf($this->prepare(self::FILTER, $level), $method, $filter, $findArg, $callable, $msg, $this->getFilters($filters, $level));
603+
return sprintf($this->prepare(self::FILTER, $level), $method, $filter, $findArg, $callable, $msg, $value, $this->getFilters($filters, $level, false));
598604
}
599605

600606
const LINE = '$buffer .= "\n";';

test/Mustache/Test/FiveThree/Functional/FiltersTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,13 @@ public function lambdaFiltersData()
203203
);
204204

205205
$data = array(
206+
'noop' => function ($value) {
207+
return $value;
208+
},
206209
'people' => $people,
210+
'people_lambda' => function () use ($people) {
211+
return $people;
212+
},
207213
'first_name' => function ($arr) {
208214
return $arr[0]->name;
209215
},
@@ -225,6 +231,8 @@ public function lambdaFiltersData()
225231
array('{{% FILTERS }}{{ people | last_name }}', $data, 'Charles'),
226232
array('{{% FILTERS }}{{ people | all_names }}', $data, 'Albert, Betty, Charles'),
227233
array('{{% FILTERS }}{{# people | first_person }}{{ name }}{{/ people }}', $data, 'Albert'),
234+
array('{{% FILTERS }}{{# people_lambda | first_person }}{{ name }}{{/ people_lambda }}', $data, 'Albert'),
235+
array('{{% FILTERS }}{{# people_lambda | noop | first_person }}{{ name }}{{/ people_lambda }}', $data, 'Albert'),
228236
);
229237
}
230238
}

0 commit comments

Comments
 (0)