public function testSubmit() { $presenter = $this->presenterFactory->createPresenter('Mask'); $presenter->autoCanonicalize = FALSE; $presenter->run(new \Nette\Application\Request('Mask', 'POST', array('do' => 'form-submit'), array('mask' => '124 asd', 'regex' => '235 wes'))); /** @var \Form $form */ $form = $presenter['form']; $this->assertTrue($form->isSubmitted()); $this->assertFalse($form->hasErrors()); $this->assertSame(array('mask' => '124 asd', 'regex' => '235 wes'), $form->getValues(TRUE)); }
/** * @return void */ public function processRequest(Request $request) { process: if (count($this->requests) > self::$maxLoop) { throw new ApplicationException('Too many loops detected in application life cycle.'); } $this->requests[] = $request; $this->onRequest($this, $request); if (!$request->isMethod($request::FORWARD) && !strcasecmp($request->getPresenterName(), $this->errorPresenter)) { throw new BadRequestException('Invalid request. Presenter is not achievable.'); } try { $this->presenter = $this->presenterFactory->createPresenter($request->getPresenterName()); } catch (InvalidPresenterException $e) { throw count($this->requests) > 1 ? $e : new BadRequestException($e->getMessage(), 0, $e); } $this->onPresenter($this, $this->presenter); $response = $this->presenter->run(clone $request); if ($response instanceof Responses\ForwardResponse) { $request = $response->getRequest(); goto process; } elseif ($response) { $this->onResponse($this, $response); $response->send($this->httpRequest, $this->httpResponse); } }
/** * @return \Venne\Notifications\AdminModule\EmailPresenter */ private function createPresenter() { $presenter = $this->presenterFactory->createPresenter('Admin:Notifications:Email'); if ($presenter instanceof EmailPresenter) { return $presenter; } throw new InvalidStateException('Presenter must be instance of \'Venne\\Notification\\AdminModule\\EmailPresenter\'.'); }
public function testJsAndNormal() { $presenter = $this->presenterFactory->createPresenter('Date'); $presenter->autoCanonicalize = FALSE; $presenter->run(new \Nette\Application\Request('Date', 'POST', array('do' => 'form-submit'), array('date' => array('js' => '30.7.2015 14:00', 'day' => 27, 'month' => 7, 'year' => 2015, 'hours' => '14', 'minutes' => '00')))); /** @var \Form $form */ $form = $presenter['form']; $this->assertTrue($form->isSubmitted()); $this->assertFalse($form->hasErrors()); $this->assertSame(strtotime('27.7.2015 14:00'), $form->values['date']); }
/** * @return void */ public function processRequest(Request $request) { if (count($this->requests) > self::$maxLoop) { throw new ApplicationException('Too many loops detected in application life cycle.'); } $this->requests[] = $request; $this->onRequest($this, $request); $this->presenter = $this->presenterFactory->createPresenter($request->getPresenterName()); $response = $this->presenter->run($request); if ($response instanceof Responses\ForwardResponse) { $this->processRequest($response->getRequest()); } elseif ($response) { $this->onResponse($this, $response); $response->send($this->httpRequest, $this->httpResponse); } }
/** * @param string $view email template * @param User $user * @param NULL|array $args template variables * * @throws Exception from Latte\Engine * @throws SmtpException from Mailer */ public function send($view, User $user, array $args = []) { if ($this->orm->unsubscribes->getByEmail($user->email)) { // Last line of defense. Make sure unsubscribed users // really dont receive any email. This should however // be handled before the task is queued. $this->logger->addAlert("Email to '{$user->email}' not send, user unsubscribed. Find out why it was queued"); return; } $msg = new Message(); $msg->setFrom('Khanova škola <*****@*****.**>'); $msg->addReplyTo('Markéta Matějíčková <*****@*****.**>'); $msg->addTo($user->email, $user->name); $token = Unsubscribe::createFromUser($user); $token->emailType = $view; $this->orm->tokens->attach($token); $this->orm->flush(); $args['recipient'] = $user; $args['email'] = $msg; $args['unsubscribe'] = (object) ['token' => $token, 'code' => $token->getUnsafe()]; $args['baseUrl'] = rtrim($this->baseUrl, '/'); $latte = new Engine(); /** @var Presenters\Token $presenter */ $presenter = $this->factory->createPresenter('Token'); $presenter->autoCanonicalize = FALSE; $ref = new \ReflectionProperty(Presenter::class, 'globalParams'); $ref->setAccessible(TRUE); $ref->setValue($presenter, []); $latte->addFilter('token', function (Token $token, $unsafe) use($presenter, $view) { return $presenter->link('//Token:', ['token' => $token->toString($unsafe), 'utm_campaign' => "email-{$view}"]); }); $latte->addFilter('vocative', function ($phrase) { return $this->inflection->inflect($phrase, 5); }); $template = $latte->renderToString($this->getTemplate($view), $args); $msg->setHtmlBody($template); $this->mailer->send($msg); $this->logger->addInfo('Email send', ['view' => $view, 'email' => $user->email]); }
/** * Return presenter * @param string $presenter - etc. 'Front:GoodsChange:Goods' * @param bool $autoCanonicalize * @return \Nette\Application\IPresenter */ public function getPresenter($presenter, $autoCanonicalize = FALSE) { $presenter = $this->presenterFactory->createPresenter($presenter); $presenter->autoCanonicalize = $autoCanonicalize; return $presenter; }
/** * Dispatch a HTTP request to a front controller. * * @return void */ public function run() { $request = null; $repeatedError = false; do { try { if (count($this->requests) > self::$maxLoop) { throw new ApplicationException('Too many loops detected in application life cycle.'); } if (!$request) { $this->onStartup($this); $request = $this->router->match($this->httpRequest); if (!$request instanceof Request) { $request = null; throw new BadRequestException('No route for HTTP request.'); } if (strcasecmp($request->getPresenterName(), $this->errorPresenter) === 0) { throw new BadRequestException('Invalid request. Presenter is not achievable.'); } } $this->requests[] = $request; $this->onRequest($this, $request); // Instantiate presenter $presenterName = $request->getPresenterName(); try { $this->presenter = $this->presenterFactory->createPresenter($presenterName); } catch (InvalidPresenterException $e) { throw new BadRequestException($e->getMessage(), 404, $e); } $this->presenterFactory->getPresenterClass($presenterName); $request->setPresenterName($presenterName); $request->freeze(); // Execute presenter $response = $this->presenter->run($request); if ($response) { $this->onResponse($this, $response); } // Send response if ($response instanceof Responses\ForwardResponse) { $request = $response->getRequest(); continue; } elseif ($response instanceof IResponse) { $response->send($this->httpRequest, $this->httpResponse); } break; } catch (\Exception $e) { // fault barrier $this->onError($this, $e); if (!$this->catchExceptions) { $this->onShutdown($this, $e); throw $e; } if ($repeatedError) { $e = new ApplicationException('An error occurred while executing error-presenter', 0, $e); } if (!$this->httpResponse->isSent()) { $this->httpResponse->setCode($e instanceof BadRequestException ? $e->getCode() : 500); } if (!$repeatedError && $this->errorPresenter) { $repeatedError = true; if ($this->presenter instanceof UI\Presenter) { try { $this->presenter->forward(":{$this->errorPresenter}:", array('exception' => $e)); } catch (AbortException $foo) { $request = $this->presenter->getLastCreatedRequest(); } } else { $request = new Request($this->errorPresenter, Request::FORWARD, array('exception' => $e)); } // continue } else { // default error handler if ($e instanceof BadRequestException) { $code = $e->getCode(); } else { $code = 500; Nette\Diagnostics\Debugger::log($e, Nette\Diagnostics\Debugger::ERROR); } require __DIR__ . '/templates/error.phtml'; break; } } } while (1); $this->onShutdown($this, isset($e) ? $e : null); }
/** * @param string $name * @return IPresenter|UI\Presenter */ public function createPresenter($name) { return $this->presenterFactory->createPresenter($name); }
public function testCreatePresenterFailes() { $this->setExpectedException(InvalidPresenterException::class); $this->presenterFactory->createPresenter('Some'); }
/** * Dispatch a HTTP request to a front controller. * @return void */ public function run() { $request = NULL; $repeatedError = FALSE; do { try { if (count($this->requests) > self::$maxLoop) { throw new ApplicationException('Too many loops detected in application life cycle.'); } if (!$request) { $this->onStartup($this); $request = $this->router->match($this->httpRequest); if (!$request instanceof Request) { $request = NULL; throw new BadRequestException('No route for HTTP request.'); } if (strcasecmp($request->getPresenterName(), $this->errorPresenter) === 0) { throw new BadRequestException('Invalid request. Presenter is not achievable.'); } } $this->requests[] = $request; $this->onRequest($this, $request); // Instantiate presenter $presenterName = $request->getPresenterName(); try { $this->presenter = $this->presenterFactory->createPresenter($presenterName); } catch (InvalidPresenterException $e) { throw new BadRequestException($e->getMessage(), 404, $e); } $this->presenterFactory->getPresenterClass($presenterName); $request->setPresenterName($presenterName); $request->freeze(); // Execute presenter $response = $this->presenter->run($request); if ($response) { $this->onResponse($this, $response); } // Send response if ($response instanceof Responses\ForwardResponse) { $request = $response->getRequest(); continue; } elseif ($response instanceof IResponse) { $response->send($this->httpRequest, $this->httpResponse); } break; } catch (\Exception $e) { $this->onError($this, $e); if ($repeatedError) { $e = new ApplicationException("An error occurred while executing error-presenter '{$this->errorPresenter}'.", 0, $e); } if ($repeatedError || !$this->catchExceptions) { $this->onShutdown($this, $e); throw $e; } $repeatedError = TRUE; $this->errorPresenter = $this->errorPresenter ?: 'Nette:Error'; if (!$this->httpResponse->isSent()) { $this->httpResponse->setCode($e instanceof BadRequestException ? $e->getCode() : 500); } if ($this->presenter instanceof UI\Presenter) { try { $this->presenter->forward(":{$this->errorPresenter}:", array('exception' => $e)); } catch (AbortException $foo) { $request = $this->presenter->getLastCreatedRequest(); } } else { $request = new Request($this->errorPresenter, Request::FORWARD, array('exception' => $e)); } // continue } } while (1); $this->onShutdown($this, isset($e) ? $e : NULL); }