Skip to content

Commit

Permalink
UIMacros: uses Latte providers uiControl and uiPresenter instead of $…
Browse files Browse the repository at this point in the history
…_control and $_presenter

$_control and $_presenter still exist for BC
  • Loading branch information
dg committed May 13, 2016
1 parent 8b93c3f commit c7531dc
Show file tree
Hide file tree
Showing 22 changed files with 128 additions and 128 deletions.
13 changes: 10 additions & 3 deletions src/Bridges/ApplicationLatte/TemplateFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,20 @@ public function createTemplate(UI\Control $control = NULL)
}

// default parameters
$template->control = $template->_control = $control;
$template->presenter = $template->_presenter = $presenter;
$template->control = $control;
$template->presenter = $presenter;
$template->user = $this->user;
$template->netteCacheStorage = $this->cacheStorage;
$template->baseUri = $template->baseUrl = $this->httpRequest ? rtrim($this->httpRequest->getUrl()->getBaseUrl(), '/') : NULL;
$template->basePath = preg_replace('#https?://[^/]+#A', '', $template->baseUrl);
$template->flashes = [];
$latte->addProvider('uiControl', $control);
$latte->addProvider('uiPresenter', $presenter);
$latte->addProvider('cachingStorage', $this->cacheStorage);

// back compatibility
$template->_control = $control;
$template->_presenter = $presenter;
$template->netteCacheStorage = $this->cacheStorage;

if ($presenter instanceof UI\Presenter && $presenter->hasFlashSession()) {
$id = $control->getParameterId('flash');
Expand Down
14 changes: 7 additions & 7 deletions src/Bridges/ApplicationLatte/UIMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ public function finalize()
return [
'if (Nette\Bridges\ApplicationLatte\UIRuntime::initialize($this, $this->blockQueue)) return;',
'',
$this->extends ? '' : '$this->parentName = $this->parentName ?: ($this->blocks && !$this->getReferringTemplate() && $this->params["_control"] instanceof Nette\Application\UI\Presenter
? $this->params["_control"]->findLayoutTemplateFile() : NULL);',
$this->extends ? '' : '$this->parentName = $this->parentName ?: ($this->blocks && !$this->getReferringTemplate() && isset($this->global->uiControl) && $this->global->uiControl instanceof Nette\Application\UI\Presenter
? $this->global->uiControl->findLayoutTemplateFile() : NULL);',
];
}

Expand All @@ -89,7 +89,7 @@ public function macroControl(MacroNode $node, PhpWriter $writer)
$param = substr($param, $param[0] === '[' ? 1 : 6, -1); // removes array() or []
}
return ($name[0] === '$' ? "if (is_object($name)) \$_tmp = $name; else " : '')
. '$_tmp = $_control->getComponent(' . $name . '); '
. '$_tmp = $this->global->uiControl->getComponent(' . $name . '); '
. 'if ($_tmp instanceof Nette\Application\UI\IRenderable) $_tmp->redrawControl(NULL, FALSE); '
. ($node->modifiers === '' ? "\$_tmp->$method($param)" : $writer->write("ob_start(function () {}); \$_tmp->$method($param); echo %modify(ob_get_clean())"));
}
Expand All @@ -104,7 +104,7 @@ public function macroLink(MacroNode $node, PhpWriter $writer)
{
$node->modifiers = preg_replace('#\|safeurl\s*(?=\||\z)#i', '', $node->modifiers);
return $writer->using($node, $this->getCompiler())
->write('echo %escape(%modify(' . ($node->name === 'plink' ? '$_presenter' : '$_control') . '->link(%node.word, %node.array?)))');
->write('echo %escape(%modify(' . ($node->name === 'plink' ? '$this->global->uiPresenter' : '$this->global->uiControl') . '->link(%node.word, %node.array?)))');
}


Expand All @@ -117,8 +117,8 @@ public function macroIfCurrent(MacroNode $node, PhpWriter $writer)
throw new CompileException("Modifiers are not allowed in {{$node->name}}");
}
return $writer->write($node->args
? 'if ($_presenter->isLinkCurrent(%node.word, %node.array?)) {'
: 'if ($_presenter->getLastCreatedRequestFlag("current")) {'
? 'if ($this->global->uiPresenter->isLinkCurrent(%node.word, %node.array?)) {'
: 'if ($this->global->uiPresenter->getLastCreatedRequestFlag("current")) {'
);
}

Expand All @@ -132,7 +132,7 @@ public function macroExtends(MacroNode $node, PhpWriter $writer)
if ($node->modifiers || $node->parentNode || $node->args !== 'auto') {
return FALSE;
}
return $writer->write('$this->parentName = $this->params["_presenter"]->findLayoutTemplateFile();');
return $writer->write('$this->parentName = $this->global->uiPresenter->findLayoutTemplateFile();');
}


Expand Down
17 changes: 13 additions & 4 deletions src/Bridges/ApplicationLatte/UIRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,28 @@ class UIRuntime
{
use Nette\StaticClass;


/**
* @return bool
*/
public static function initialize(Latte\Template $template, $blockQueue)
{
// snippet support
// back compatiblity
$params = $template->getParameters();
if (!$template->getParentName() && !empty($params['_control']->snippetMode)) {
if (empty($template->global->uiControl) && isset($params['_control'])) {
trigger_error('Replace template variable $_control with provider: $latte->addProvider("uiControl", ...)', E_USER_DEPRECATED);
$template->global->uiControl = $params['_control'];
}
if (empty($template->global->uiPresenter) && isset($params['_presenter'])) {
trigger_error('Replace template variable $_presenter with provider: $latte->addProvider("uiPresenter", ...)', E_USER_DEPRECATED);
$template->global->uiPresenter = $params['_presenter'];
}

// snippet support
if (!$template->getParentName() && !empty($template->global->uiControl->snippetMode)) {
$tmp = $template;
while (in_array($tmp->getReferenceType(), ['extends', 'include', NULL], TRUE) && ($tmp = $tmp->getReferringTemplate()));
if (!$tmp) {
self::renderSnippets($params['_control'], $blockQueue, $params);
self::renderSnippets($template->global->uiControl, $blockQueue, $params);
return TRUE;
}
};
Expand Down
39 changes: 10 additions & 29 deletions tests/Bridges.Latte/Template.getParentName().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,60 +18,41 @@ class MockPresenter extends Nette\Application\UI\Presenter

$latte = new Latte\Engine;
$latte->setLoader(new Latte\Loaders\StringLoader);
$latte->addProvider('uiControl', new MockPresenter);
UIMacros::install($latte->getCompiler());

$template = $latte->createTemplate(
'',
['_control' => new MockPresenter]
);
$template = $latte->createTemplate('');
$template->prepare();
Assert::null($template->getParentName());

$template = $latte->createTemplate(
'{block}...{/block}',
['_control' => new MockPresenter]
);
$template = $latte->createTemplate('{block}...{/block}');
$template->prepare();
Assert::null($template->getParentName());

$template = $latte->createTemplate(
'{block name}...{/block}',
['_control' => new MockPresenter]
);
$template = $latte->createTemplate('{block name}...{/block}');
$template->prepare();
Assert::same('layout.latte', $template->getParentName());

$template = $latte->createTemplate(
'{extends "file.latte"} {block name}...{/block}',
['_control' => new MockPresenter]
);
$template = $latte->createTemplate('{extends "file.latte"} {block name}...{/block}');
$template->prepare();
Assert::same('file.latte', $template->getParentName());

$template = $latte->createTemplate(
'{extends "file.latte"}',
['_control' => new MockPresenter]
);
$template = $latte->createTemplate('{extends "file.latte"}');
$template->prepare();
Assert::same('file.latte', $template->getParentName());

$template = $latte->createTemplate(
'{extends $file} {block name}...{/block}',
['_control' => new MockPresenter, 'file' => 'file.latte']
['file' => 'file.latte']
);
$template->prepare();
Assert::same('file.latte', $template->getParentName());

$template = $latte->createTemplate(
'{extends none}',
['_control' => new MockPresenter]
);
$template = $latte->createTemplate('{extends none}');
$template->prepare();
Assert::null($template->getParentName());

$template = $latte->createTemplate(
'{extends auto}',
['_presenter' => new MockPresenter]
);
$latte->addProvider('uiPresenter', new MockPresenter);
$template = $latte->createTemplate('{extends auto}');
$template->prepare();
Assert::same('layout.latte', $template->getParentName());
2 changes: 1 addition & 1 deletion tests/Bridges.Latte/UIMacros.control.2.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ $latte = new Latte\Engine;
$latte->setLoader(new Latte\Loaders\StringLoader);
UIMacros::install($latte->getCompiler());

$params['_control'] = new MockComponent;
$latte->addProvider('uiControl', new MockComponent);
$params['form'] = new MockControl;
$params['name'] = 'form';

Expand Down
18 changes: 9 additions & 9 deletions tests/Bridges.Latte/UIMacros.control.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ $compiler = new Latte\Compiler;
UIMacros::install($compiler);

// {control ...}
Assert::match('<?php %a% $_control->getComponent("form"); %a%->render() ?>', $compiler->expandMacro('control', 'form', '')->openingCode);
Assert::match('<?php %a% $_control->getComponent("form"); %a%->render(); echo call_user_func($this->filters->filter, %a%) ?>', $compiler->expandMacro('control', 'form', 'filter')->openingCode);
Assert::match('<?php if (is_object($form)) %a% else %a% $_control->getComponent($form); %a%->render() ?>', $compiler->expandMacro('control', '$form', '')->openingCode);
Assert::match('<?php %a% $_control->getComponent("form"); %a%->renderType() ?>', $compiler->expandMacro('control', 'form:type', '')->openingCode);
Assert::match('<?php %a% $_control->getComponent("form"); %a%->{"render$type"}() ?>', $compiler->expandMacro('control', 'form:$type', '')->openingCode);
Assert::match('<?php %a% $_control->getComponent("form"); %a%->renderType(\'param\') ?>', $compiler->expandMacro('control', 'form:type param', '')->openingCode);
Assert::match('<?php %a% $_control->getComponent("form"); %a%->renderType([\'param\' => 123]) ?>', $compiler->expandMacro('control', 'form:type param => 123', '')->openingCode);
Assert::match('<?php %a% $_control->getComponent("form"); %a%->renderType([\'param\' => 123]) ?>', $compiler->expandMacro('control', 'form:type, param => 123', '')->openingCode);
Assert::match('<?php %a% $_control->getComponent("form"); %a%->render(); echo call_user_func($this->filters->striptags, %a%) ?>', $compiler->expandMacro('control', 'form', 'striptags')->openingCode);
Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->render() ?>', $compiler->expandMacro('control', 'form', '')->openingCode);
Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->render(); echo call_user_func($this->filters->filter, %a%) ?>', $compiler->expandMacro('control', 'form', 'filter')->openingCode);
Assert::match('<?php if (is_object($form)) %a% else %a% $this->global->uiControl->getComponent($form); %a%->render() ?>', $compiler->expandMacro('control', '$form', '')->openingCode);
Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->renderType() ?>', $compiler->expandMacro('control', 'form:type', '')->openingCode);
Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->{"render$type"}() ?>', $compiler->expandMacro('control', 'form:$type', '')->openingCode);
Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->renderType(\'param\') ?>', $compiler->expandMacro('control', 'form:type param', '')->openingCode);
Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->renderType([\'param\' => 123]) ?>', $compiler->expandMacro('control', 'form:type param => 123', '')->openingCode);
Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->renderType([\'param\' => 123]) ?>', $compiler->expandMacro('control', 'form:type, param => 123', '')->openingCode);
Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->render(); echo call_user_func($this->filters->striptags, %a%) ?>', $compiler->expandMacro('control', 'form', 'striptags')->openingCode);
4 changes: 2 additions & 2 deletions tests/Bridges.Latte/UIMacros.link.2.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ $latte = new Latte\Engine;
$latte->setLoader(new Latte\Loaders\StringLoader);
UIMacros::install($latte->getCompiler());

$params['_control'] = new MockControl;
$params['_presenter'] = new MockPresenter;
$latte->addProvider('uiControl', new MockControl);
$latte->addProvider('uiPresenter', new MockPresenter);
$params['action'] = 'login';
$params['arr'] = ['link' => 'login', 'param' => 123];

Expand Down
24 changes: 12 additions & 12 deletions tests/Bridges.Latte/UIMacros.link.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ $compiler->setContentType($compiler::CONTENT_TEXT);
UIMacros::install($compiler);

// {link ...}
Assert::same('<?php echo $_control->link("p") ?>', $compiler->expandMacro('link', 'p', '')->openingCode);
Assert::same('<?php echo call_user_func($this->filters->filter, $_control->link("p")) ?>', $compiler->expandMacro('link', 'p', 'filter')->openingCode);
Assert::same('<?php echo $_control->link("p:a") ?>', $compiler->expandMacro('link', 'p:a', '')->openingCode);
Assert::same('<?php echo $_control->link($dest) ?>', $compiler->expandMacro('link', '$dest', '')->openingCode);
Assert::same('<?php echo $_control->link($p:$a) ?>', $compiler->expandMacro('link', '$p:$a', '')->openingCode);
Assert::same('<?php echo $_control->link("$p:$a") ?>', $compiler->expandMacro('link', '"$p:$a"', '')->openingCode);
Assert::same('<?php echo $_control->link("p:a") ?>', $compiler->expandMacro('link', '"p:a"', '')->openingCode);
Assert::same('<?php echo $_control->link(\'p:a\') ?>', $compiler->expandMacro('link', "'p:a'", '')->openingCode);

Assert::same('<?php echo $_control->link("p", [\'param\']) ?>', $compiler->expandMacro('link', 'p param', '')->openingCode);
Assert::same('<?php echo $_control->link("p", [\'param\' => 123]) ?>', $compiler->expandMacro('link', 'p param => 123', '')->openingCode);
Assert::same('<?php echo $_control->link("p", [\'param\' => 123]) ?>', $compiler->expandMacro('link', 'p, param => 123', '')->openingCode);
Assert::same('<?php echo $this->global->uiControl->link("p") ?>', $compiler->expandMacro('link', 'p', '')->openingCode);
Assert::same('<?php echo call_user_func($this->filters->filter, $this->global->uiControl->link("p")) ?>', $compiler->expandMacro('link', 'p', 'filter')->openingCode);
Assert::same('<?php echo $this->global->uiControl->link("p:a") ?>', $compiler->expandMacro('link', 'p:a', '')->openingCode);
Assert::same('<?php echo $this->global->uiControl->link($dest) ?>', $compiler->expandMacro('link', '$dest', '')->openingCode);
Assert::same('<?php echo $this->global->uiControl->link($p:$a) ?>', $compiler->expandMacro('link', '$p:$a', '')->openingCode);
Assert::same('<?php echo $this->global->uiControl->link("$p:$a") ?>', $compiler->expandMacro('link', '"$p:$a"', '')->openingCode);
Assert::same('<?php echo $this->global->uiControl->link("p:a") ?>', $compiler->expandMacro('link', '"p:a"', '')->openingCode);
Assert::same('<?php echo $this->global->uiControl->link(\'p:a\') ?>', $compiler->expandMacro('link', "'p:a'", '')->openingCode);

Assert::same('<?php echo $this->global->uiControl->link("p", [\'param\']) ?>', $compiler->expandMacro('link', 'p param', '')->openingCode);
Assert::same('<?php echo $this->global->uiControl->link("p", [\'param\' => 123]) ?>', $compiler->expandMacro('link', 'p param => 123', '')->openingCode);
Assert::same('<?php echo $this->global->uiControl->link("p", [\'param\' => 123]) ?>', $compiler->expandMacro('link', 'p, param => 123', '')->openingCode);
4 changes: 2 additions & 2 deletions tests/Bridges.Latte/UIMacros.renderSnippets.extends.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class TestPresenter extends Nette\Application\UI\Presenter
{
$latte = new Latte\Engine;
UIMacros::install($latte->getCompiler());
$params['_control'] = $this;
return $latte->renderToString(__DIR__ . '/templates/snippets.extends.latte', $params);
$latte->addProvider('uiControl', $this);
return $latte->renderToString(__DIR__ . '/templates/snippets.extends.latte');
}
}

Expand Down
8 changes: 4 additions & 4 deletions tests/Bridges.Latte/UIMacros.renderSnippets.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class InnerControl extends Nette\Application\UI\Control
{
$latte = new Latte\Engine;
UIMacros::install($latte->getCompiler());
$params['_presenter'] = $this->getPresenter();
$params['_control'] = $this;
$latte->addProvider('uiPresenter', $this->getPresenter());
$latte->addProvider('uiControl', $this);
$params['say'] = 'Hello';
$latte->render(__DIR__ . '/templates/snippet-included.latte', $params);
}
Expand All @@ -39,8 +39,8 @@ class TestPresenter extends Nette\Application\UI\Presenter
{
$latte = new Latte\Engine;
UIMacros::install($latte->getCompiler());
$params['_control'] = $this;
$latte->render(__DIR__ . '/templates/snippet-include.latte', $params);
$latte->addProvider('uiControl', $this);
$latte->render(__DIR__ . '/templates/snippet-include.latte');
}
}

Expand Down
12 changes: 6 additions & 6 deletions tests/Bridges.Latte/UIMacros.renderSnippets2.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class InnerControl extends Nette\Application\UI\Control
$latte = new Latte\Engine;
$latte->setLoader(new Latte\Loaders\StringLoader);
UIMacros::install($latte->getCompiler());
$params['_presenter'] = $this->getPresenter();
$params['_control'] = $this;
$latte->addProvider('uiPresenter', $this->getPresenter());
$latte->addProvider('uiControl', $this);
$params['say'] = 'Hello';
$latte->render('{snippet testA}{$say}{/snippet}', $params);
}
Expand All @@ -35,8 +35,8 @@ class InnerControl extends Nette\Application\UI\Control
$latte = new Latte\Engine;
$latte->setLoader(new Latte\Loaders\StringLoader);
UIMacros::install($latte->getCompiler());
$params['_presenter'] = $this->getPresenter();
$params['_control'] = $this;
$latte->addProvider('uiPresenter', $this->getPresenter());
$latte->addProvider('uiControl', $this);
$params['say'] = 'world';
$latte->render('{snippet testB}{$say}{/snippet}', $params);
}
Expand All @@ -57,8 +57,8 @@ class TestPresenter extends Nette\Application\UI\Presenter
$latte = new Latte\Engine;
$latte->setLoader(new Latte\Loaders\StringLoader);
UIMacros::install($latte->getCompiler());
$params['_control'] = $this;
$latte->render('', $params);
$latte->addProvider('uiControl', $this);
$latte->render('');
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Bridges.Latte/UIMacros.renderSnippets3.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class TestPresenter extends Nette\Application\UI\Presenter
{
$latte = new Latte\Engine;
UIMacros::install($latte->getCompiler());
$params['_control'] = $this;
$latte->render(__DIR__ . '/templates/snippetArea-include.latte', $params);
$latte->addProvider('uiControl', $this);
$latte->render(__DIR__ . '/templates/snippetArea-include.latte');
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Bridges.Latte/UIMacros.renderSnippets4.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class TestPresenter extends Nette\Application\UI\Presenter
{
$latte = new Latte\Engine;
UIMacros::install($latte->getCompiler());
$params['_control'] = $this;
$latte->render(__DIR__ . '/templates/snippets.includeblock.latte', $params);
$latte->addProvider('uiControl', $this);
$latte->render(__DIR__ . '/templates/snippets.includeblock.latte');
}
}

Expand Down
3 changes: 2 additions & 1 deletion tests/Bridges.Latte/UIMacros.snippet1.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class MockControl
$latte = new Latte\Engine;
UIMacros::install($latte->getCompiler());
$latte->setLoader(new Latte\Loaders\StringLoader);
$latte->addProvider('uiControl', new MockControl);

Assert::match(<<<EOD
<div>
Expand All @@ -35,4 +36,4 @@ EOD
{/snippet}
</div>
EOD
, ['_control' => new MockControl]));
));
3 changes: 2 additions & 1 deletion tests/Bridges.Latte/UIMacros.snippet2.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ class MockControl
$latte = new Latte\Engine;
UIMacros::install($latte->getCompiler());
$latte->setLoader(new Latte\Loaders\StringLoader);
$latte->addProvider('uiControl', new MockControl);

Assert::match(<<<EOD
<p><div id="">hello</div> world</p>
EOD
, $latte->renderToString(<<<EOD
<p>{snippet abc}hello{/snippet} world</p>
EOD
, ['_control' => new MockControl]));
));
6 changes: 3 additions & 3 deletions tests/Bridges.Latte/UIMacros.snippet3.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ class MockControl
$latte = new Latte\Engine;
UIMacros::install($latte->getCompiler());
$latte->setLoader(new Latte\Loaders\StringLoader);
$params['_control'] = new MockControl;
$latte->addProvider('uiControl', new MockControl);

Assert::match(<<<EOD
<p id="">hello</p>
EOD
, $latte->renderToString(<<<EOD
<p n:inner-snippet="abc">hello</p>
EOD
, $params));
));


Assert::match(<<<EOD
Expand All @@ -39,7 +39,7 @@ EOD
, $latte->renderToString(<<<EOD
<p n:snippet="abc">hello</p>
EOD
, $params));
));


Assert::error(function () use ($latte) {
Expand Down
Loading

1 comment on commit c7531dc

@f3l1x
Copy link
Member

@f3l1x f3l1x commented on c7531dc May 13, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow! Wow! Wow! 👍

Please sign in to comment.