-
Notifications
You must be signed in to change notification settings - Fork 88
add cache block #357
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
add cache block #357
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -617,6 +617,42 @@ protected function _addDefaultModifiers() | |
|
|
||
| return preg_split($pattern, $value); | ||
| }; | ||
|
|
||
| $this->_actions['cache'] = [ | ||
| 'type' => self::BLOCK_COMPILER | ||
| ,'open' => | ||
| function ($tokens, $scope) { | ||
| $name = false; | ||
| $cname = $scope->tpl->parsePlainArg($tokens, $name); | ||
| $params = [ | ||
| 'lifetime'=>0, | ||
| 'options'=>[], | ||
| ]; | ||
| $params = $scope->tpl->parseParams($tokens, $params); | ||
|
|
||
| if (!$name) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Только static name
|
||
| throw new \RuntimeException("Invalid cache name"); | ||
| } | ||
|
|
||
| $scope['name'] = $cname; | ||
| $scope['params'] = $params; | ||
| $scope['var'] = $scope->tpl->tmpVar(); | ||
|
|
||
| return "{$scope['var']} = \$var[\"_modx\"]->cacheManager->get({$scope['name']},".Fenom\Compiler::toArray($scope['params']['options']).");\n | ||
| if(!{$scope['var']} ) { \n | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cache miss
Нужно что-то вроде: if ({$scope['var']} === false || {$scope['var']} === null) {(как обычно отдаёт miss у MODX |
||
| ob_start(); \n | ||
| "; | ||
| } | ||
| ,'close' => | ||
| function($tokens, $scope){ | ||
| return " | ||
| {$scope['var']} = ob_get_clean(); \n | ||
| \$var[\"_modx\"]->cacheManager->set({$scope['name']},{$scope['var']},{$scope['params']['lifetime']},".Fenom\Compiler::toArray($scope['params']['options']).");\n | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. set() + options — blocker
// $options is not used due to security reasons
return $this->cacheManager->set($key, $var, $lifetime);Здесь \$var[\"_modx\"]->cacheManager->set({$scope['name']}, {$scope['var']}, {$scope['params']['lifetime']});без 4-го аргумента. |
||
| }\n | ||
| echo {$scope['var']}; | ||
| "; | ||
| } | ||
| ]; | ||
|
|
||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
options default
Дефолт
'options' => []— PHP-массив, а FenomparseParamsобычно ждёт code-string'и. Для пустого дефолтаCompiler::toArray([])ещё ок, но приoptions=$fooвtoArray()попадёт строка выражения и foreach разберёт её по символам.Лучше: убрать
optionsсовсем, либо default как'options' => 'array()'и явно валидировать значение.