Call to an undefined method Nette\Application\UI\Template::getLatte()

MKI-Miro
Člen | 261
+
0
-

Ahojte

Mam takto mailsender

<?php

declare(strict_types=1);

namespace App\Helpers;

use Nette;
use Nette\Application\LinkGenerator;
use Nette\Application\UI\TemplateFactory ;
use Nette\Application\UI\Template;
use Nette\Mail\Message;
use App\Language\Translator;

class MailSender
{
    public function __construct(
        private LinkGenerator $linkGenerator,
        private TemplateFactory $templateFactory,
        private Translator $translator
    ) {
    }

    private function createTemplate(): Template
    {
        $template = $this->templateFactory->createTemplate();
        $template->getLatte()->addProvider('uiControl', $this->linkGenerator);

        return $template;
    }

Dostavam ale chybu v phpstan

Call to an undefined method Nette\Application\UI\Template::getLatte().

Ked sa pozriem do frameowrku tak getLatte() sa nachadza v

Nette\Bridges\ApplicationLatte\Template implements Nette\Application\UI\Template

Nette\Application\UI\Template nema metodu getLatte()

Isiel som podla https://doc.nette.org/en/mail

Robim niekde chybu?

Dakujem

David Grudl
Nette Core | 8108
+
0
-

Místo Nette\Application\UI\TemplateFactory tam dej Nette\Bridges\ApplicationLatte\TemplateFactory. V nette/application @​dev je tam správný hint.

MKI-Miro
Člen | 261
+
0
-

ten ale tie vrati UI\Template

public function createTemplate(UI\Control $control = null, string $class = null): UI\Template
David Grudl
Nette Core | 8108
+
0
-

V dev verzi je tam /** @return Template */ místo UI\Template.

MKI-Miro
Člen | 261
+
0
-

niesom si isty teda ci dobre pozeram

https://github.com/…eFactory.php

/** @return Template */
	public function createTemplate(UI\Control $control = null, string $class = null): UI\Template
	{

cize /** @return Template */ tam sice je ale navratova hodnota je UI\Template

ci zle to chapem?

Editoval MKI-Miro (11. 1. 2021 11:10)

David Grudl
Nette Core | 8108
+
+1
-

PhpStan by měl brát přednostně ten @return. Navratova hodnota tam musi byt jina kvuli starým verzím PHP.

MKI-Miro
Člen | 261
+
0
-

Mam nainstalovane najnovsie verzie balickov a mailsender vyzera takto:

<?php

declare(strict_types=1);

namespace App\Helpers;

use Nette\Application\LinkGenerator;
use Nette\Bridges\ApplicationLatte\TemplateFactory;
use Nette\Bridges\ApplicationLatte\Template;
use Nette\Mail\Message;
use App\Language\Translator;

class MailSender
{
    public function __construct(
        private LinkGenerator $linkGenerator,
        private TemplateFactory $templateFactory,
        private Translator $translator
    ) {
    }

    private function createTemplate(): Template
    {
        $template = $this->templateFactory->createTemplate();
        $template->getLatte()->addProvider('uiControl', $this->linkGenerator);

        return $template;
    }

    /**
     * @param string $templateFileName
     * @param array<string, mixed> $params
     */
    public function createEmail(string $templateFileName, array $params): Message
    {
        $template = $this->createTemplate();
        $template->setTranslator($this->translator);

        $html = $template->renderToString(__DIR__ . DIRECTORY_SEPARATOR . 'email-templates' . DIRECTORY_SEPARATOR . $templateFileName . '.latte', $params);

        $mail = new Message();
        $mail->setHtmlBody($html);

        return $mail;
    }

    public function createEmptyEmail(): Message
    {
        $mail = new Message();

        return $mail;
    }
}

pricom dostavam chybu

Service 'mailSender' (type of App\Helpers\MailSender): Service of type Nette\Bridges\ApplicationLatte\TemplateFactory needed by $templateFactory in __construct() not found. Did you add it to configuration file?

ked ale vymeni use ApplicationLatte za

use Nette\Application\UI\TemplateFactory;
use Nette\Application\UI\Template;

tak to funguje.

V com moze byt problem?

dakujem

Martk
Člen | 652
+
0
-

@MKI-Miro Měl bys injectovat interface Nette\Application\UI\ITemplateFactory

Editoval Martk (27. 1. 2021 13:19)

MKI-Miro
Člen | 261
+
0
-

no neviem ci sa nato pozeram zle ale ked pouzijem
use Nette\Application\UI\TemplateFactory;

tak funkcia $this->templateFactory->createTemplate()
vracia Nette\Application\UI\Template

a ta neobsahuje funkciu getLatte()

tak je mi to aj celkom zahodou ze ako to moze fungovat

David Matějka
Moderator | 6445
+
0
-

interface tu metodu neobsahuje, implementace ano.

obvykle tam v takovych pripadech dam assert

assert($template instanceof \Nette\Bridges\ApplicationLatte\Template);
MKI-Miro
Člen | 261
+
0
-

Ahojte

podobnu chybu dostavam aj v presentery

Cannot call method setTranslator() on Nette\Application\UI\Template|stdClass
$this->template->setTranslator($this->translator);

pricom

class Translator implements \Nette\Localization\Translator

Ako to teda správne napísať?

ďakujem