Esempio n. 1
0
 /**
  * @return Nette\Application\IResponse
  */
 public function run(Application\Request $request)
 {
     $this->request = $request;
     $httpRequest = $this->context->getByType('Nette\\Http\\IRequest');
     if (!$httpRequest->isAjax() && ($request->isMethod('get') || $request->isMethod('head'))) {
         $refUrl = clone $httpRequest->getUrl();
         $url = $this->context->getService('router')->constructUrl($request, $refUrl->setPath($refUrl->getScriptPath()));
         if ($url !== NULL && !$httpRequest->getUrl()->isEqual($url)) {
             return new Responses\RedirectResponse($url, Http\IResponse::S301_MOVED_PERMANENTLY);
         }
     }
     $params = $request->getParameters();
     if (!isset($params['callback'])) {
         throw new Application\BadRequestException("Parameter callback is missing.");
     }
     $params['presenter'] = $this;
     $callback = new Nette\Callback($params['callback']);
     $response = $callback->invokeArgs(Application\UI\PresenterComponentReflection::combineArgs($callback->toReflection(), $params));
     if (is_string($response)) {
         $response = array($response, array());
     }
     if (is_array($response)) {
         if ($response[0] instanceof \SplFileInfo) {
             $response = $this->createTemplate('Nette\\Templating\\FileTemplate')->setParameters($response[1])->setFile($response[0]);
         } else {
             $response = $this->createTemplate('Nette\\Templating\\Template')->setParameters($response[1])->setSource($response[0]);
         }
     }
     if ($response instanceof Nette\Templating\ITemplate) {
         return new Responses\TextResponse($response);
     } else {
         return $response;
     }
 }
Esempio n. 2
0
 /**
  * @param App\Request $request
  * @return App\IResponse
  */
 public function run(App\Request $request)
 {
     $this->request = $request;
     $this->startup();
     if (!$this->startupCheck) {
         $class = (new \ReflectionClass($this))->getMethod('startup')->getDeclaringClass()->getName();
         throw new Nette\InvalidStateException("'{$class}::startup()' or its descendant does not call parent method");
     }
     try {
         $rm = new \ReflectionMethod($this, $this->getAction());
     } catch (\ReflectionException $e) {
     }
     if (isset($e) || $rm->isAbstract() || $rm->isStatic() || !$rm->isPublic()) {
         throw new App\BadRequestException("Method '{$request->getMethod()}' not allowed", 405);
     }
     $params = $this->getParameters();
     $args = App\UI\PresenterComponentReflection::combineArgs($rm, $params);
     $response = $rm->invokeArgs($this, $args);
     if ($response === null) {
         $response = new Responses\NullResponse();
     } elseif (!$response instanceof App\IResponse) {
         throw new Nette\InvalidStateException("Action '{$this->getAction(true)}' does not return instance of Nette\\Application\\IResponse");
     }
     return $response;
 }
 /**
  * @param string
  */
 public function signalReceived($signal)
 {
     $methodName = sprintf('handle%s', \Nette\Utils\Strings::firstUpper($signal));
     if (!method_exists($this, $methodName)) {
         throw new \Nette\Application\UI\BadSignalException(sprintf('Method %s does not exist', $methodName));
     }
     $presenterComponentReflection = new PresenterComponentReflection(get_called_class());
     $methodReflection = $presenterComponentReflection->getMethod($methodName);
     $args = $presenterComponentReflection->combineArgs($methodReflection, $this->params);
     $methodReflection->invokeArgs($this, $args);
 }
Esempio n. 4
0
	/**
	 * @return Nette\Application\IResponse
	 */
	public function run(Application\Request $request)
	{
		$this->request = $request;

		if ($this->httpRequest && $this->router && !$this->httpRequest->isAjax() && ($request->isMethod('get') || $request->isMethod('head'))) {
			$refUrl = clone $this->httpRequest->getUrl();
			$url = $this->router->constructUrl($request, $refUrl->setPath($refUrl->getScriptPath()));
			if ($url !== NULL && !$this->httpRequest->getUrl()->isEqual($url)) {
				return new Responses\RedirectResponse($url, Http\IResponse::S301_MOVED_PERMANENTLY);
			}
		}

		$params = $request->getParameters();
		if (!isset($params['callback'])) {
			throw new Application\BadRequestException('Parameter callback is missing.');
		}
		$params['presenter'] = $this;
		$callback = $params['callback'];
		$reflection = Nette\Utils\Callback::toReflection(Nette\Utils\Callback::check($callback));
		$params = Application\UI\PresenterComponentReflection::combineArgs($reflection, $params);

		if ($this->context) {
			foreach ($reflection->getParameters() as $param) {
				if ($param->getClassName()) {
					unset($params[$param->getPosition()]);
				}
			}

			$params = Nette\DI\Helpers::autowireArguments($reflection, $params, $this->context);
			$params['presenter'] = $this;
		}

		$response = call_user_func_array($callback, $params);

		if (is_string($response)) {
			$response = array($response, array());
		}
		if (is_array($response)) {
			list($templateSource, $templateParams) = $response;
			$response = $this->createTemplate()->setParameters($templateParams);
			if (!$templateSource instanceof \SplFileInfo) {
				$response->getLatte()->setLoader(new Latte\Loaders\StringLoader);
			}
			$response->setFile($templateSource);
		}
		if ($response instanceof Application\UI\ITemplate) {
			return new Responses\TextResponse($response);
		} else {
			return $response;
		}
	}
Esempio n. 5
0
 /**
  * Calls public method if exists
  *
  * @param  string
  * @param  array
  *
  * @return bool  does method exist?
  */
 protected function tryCall($method, array $params)
 {
     $rc = $this->getReflection();
     if ($rc->hasMethod($method)) {
         $rm = $rc->getMethod($method);
         if ($rm->isPublic() && !$rm->isAbstract() && !$rm->isStatic()) {
             return $rm->invokeArgs($this, Application\UI\PresenterComponentReflection::combineArgs($rm, $params));
         }
     }
     return FALSE;
 }
Esempio n. 6
0
 /**
  * Calls public method if exists.
  * @param  string
  * @param  array
  * @return bool  does method exist?
  */
 protected function tryCall($method, array $params, $class = null, $dryRun = false)
 {
     if (func_num_args() == 2) {
         $class = $this;
     }
     //$rc = $class->getReflection();
     $rc = new Nette\Application\UI\PresenterComponentReflection(get_class($class));
     if ($rc->hasMethod($method)) {
         $rm = $rc->getMethod($method);
         if ($rm->isPublic() && !$rm->isAbstract() && !$rm->isStatic()) {
             $this->checkRequirements($rm);
             if (!$dryRun) {
                 $rm->invokeArgs($class, $rc->combineArgs($rm, $params));
             }
             return TRUE;
         }
     }
     return FALSE;
 }