File tree Expand file tree Collapse file tree 2 files changed +55
-7
lines changed
src/Bridges/ApplicationDI Expand file tree Collapse file tree 2 files changed +55
-7
lines changed Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff 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+
5383class 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
86121Assert::same ([
87122 'LoremIpsumMacros ' ,
88123 'IpsumLoremMacros ' ,
124+ 'NonStaticMacrosFactory::install ' ,
125+ 'NonStaticMacrosFactory::create ' ,
89126 'FooMacros ' ,
90127], Notes::fetch ());
You can’t perform that action at this time.
0 commit comments