Skip to content

Commit f9b52f2

Browse files
hrachdg
authored andcommitted
LatteExtension: added support for macro factories defined as DI services
1 parent 7b07c4e commit f9b52f2

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed

src/Bridges/ApplicationDI/LatteExtension.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ public function loadConfiguration()
5757
->setFactory(Nette\Bridges\ApplicationLatte\TemplateFactory::class);
5858

5959
foreach ($config['macros'] as $macro) {
60-
if (strpos($macro, '::') === FALSE && class_exists($macro)) {
61-
$macro .= '::install';
62-
}
6360
$this->addMacro($macro);
6461
}
6562

@@ -71,14 +68,28 @@ public function loadConfiguration()
7168

7269

7370
/**
74-
* @param callable
71+
* @param string
7572
* @return void
7673
*/
77-
public function addMacro(callable $macro)
74+
public function addMacro($macro)
7875
{
7976
$builder = $this->getContainerBuilder();
80-
$builder->getDefinition($this->prefix('latteFactory'))
81-
->addSetup('?->onCompile[] = function ($engine) { ' . $macro . '($engine->getCompiler()); }', ['@self']);
77+
$definition = $builder->getDefinition($this->prefix('latteFactory'));
78+
79+
if (isset($macro[0]) && $macro[0] === '@') {
80+
if (strpos($macro, '::') === FALSE) {
81+
$method = 'install';
82+
} else {
83+
list($macro, $method) = explode('::', $macro);
84+
}
85+
$definition->addSetup('?->onCompile[] = function ($engine) { ?->' . $method . '($engine->getCompiler()); }', ['@self', $macro]);
86+
87+
} else {
88+
if (strpos($macro, '::') === FALSE && class_exists($macro)) {
89+
$macro .= '::install';
90+
}
91+
$definition->addSetup('?->onCompile[] = function ($engine) { ' . $macro . '($engine->getCompiler()); }', ['@self']);
92+
}
8293
}
8394

8495
}

tests/Bridges.DI/LatteExtension.basic.phpt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,36 @@ class FooMacros extends Latte\Macros\MacroSet
5050
}
5151

5252

53+
class NonStaticMacrosFactory
54+
{
55+
56+
/** @var string */
57+
private $parameter;
58+
59+
60+
public function __construct($parameter)
61+
{
62+
$this->parameter = $parameter;
63+
}
64+
65+
66+
public function install(Latte\Compiler $compiler)
67+
{
68+
$macros = new Latte\Macros\MacroSet($compiler);
69+
$macros->addMacro('foo', 'foo ' . $this->parameter);
70+
Notes::add(get_class($this) . '::install');
71+
}
72+
73+
74+
public function create(Latte\Compiler $compiler)
75+
{
76+
$macros = new Latte\Macros\MacroSet($compiler);
77+
$macros->addMacro('foo2', 'foo ' . $this->parameter);
78+
Notes::add(get_class($this) . '::create');
79+
}
80+
}
81+
82+
5383
class AnotherExtension extends Nette\DI\CompilerExtension
5484
{
5585

@@ -69,6 +99,11 @@ latte:
6999
macros:
70100
- LoremIpsumMacros
71101
- IpsumLoremMacros::install
102+
- @macroFactory
103+
- @macroFactory::create
104+
105+
services:
106+
macroFactory: NonStaticMacrosFactory(foo)
72107
', 'neon'));
73108

74109
$compiler = new DI\Compiler;
@@ -86,5 +121,7 @@ $container->getService('nette.latteFactory')->create()->setLoader(new Latte\Load
86121
Assert::same([
87122
'LoremIpsumMacros',
88123
'IpsumLoremMacros',
124+
'NonStaticMacrosFactory::install',
125+
'NonStaticMacrosFactory::create',
89126
'FooMacros',
90127
], Notes::fetch());

0 commit comments

Comments
 (0)