toReflection() public static method

public static toReflection ( $callable ) : ReflectionMethod | ReflectionFunction
return ReflectionMethod | ReflectionFunction
示例#1
0
 public function validate(array $controls = NULL)
 {
     foreach ($this->beforeValidate ?: [] as $handler) {
         $params = Callback::toReflection($handler)->getParameters();
         $values = isset($params[1]) ? $this->getValues($params[1]->isArray()) : NULL;
         $this->values = Callback::invoke($handler, $this, $values);
     }
     parent::validate($controls);
 }
示例#2
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;
		}
	}
示例#3
0
 public function fireEvents()
 {
     /** @var ObjectForm|UI\Form $this */
     if (!($submittedBy = $this->isSubmitted())) {
         return;
     }
     foreach ($this->onReceiveData ?: array() as $handler) {
         $params = Nette\Utils\Callback::toReflection($handler)->getParameters();
         Nette\Utils\Callback::invoke($handler, $this);
     }
     $this->validate();
     if ($this->object !== NULL) {
         $this->getObjectMapper()->save($this->object, $this);
     }
     if ($submittedBy instanceof Nette\Forms\ISubmitterControl) {
         if ($this->isValid()) {
             $submittedBy->onClick($submittedBy);
         } else {
             $submittedBy->onInvalidClick($submittedBy);
         }
     }
     if ($this->object !== NULL) {
         $validateControls = $submittedBy instanceof Nette\Forms\ISubmitterControl ? $submittedBy->getValidationScope() : NULL;
         if ($validateControls === NULL || count($validateControls) > 0) {
             $this->getViolationsMapper()->validateContainer($this->object, $this, $validateControls);
         }
     }
     if ($this->onSuccess) {
         foreach ($this->onSuccess as $handler) {
             if (!$this->isValid()) {
                 $this->onError($this);
                 break;
             }
             $params = Nette\Utils\Callback::toReflection($handler)->getParameters();
             $values = isset($params[1]) ? $this->getValues($params[1]->isArray()) : NULL;
             Nette\Utils\Callback::invoke($handler, $this, $values);
         }
     } elseif (!$this->isValid()) {
         $this->onError($this);
     }
     $this->onSubmit($this);
 }
示例#4
0
 /** @return string|NULL */
 private function resolveEntityClass($entity, $recursive = [])
 {
     $entity = $this->normalizeEntity($entity instanceof Statement ? $entity->getEntity() : $entity);
     if (is_array($entity)) {
         if (($service = $this->getServiceName($entity[0])) || $entity[0] instanceof Statement) {
             $entity[0] = $this->resolveEntityClass($entity[0], $recursive);
             if (!$entity[0]) {
                 return;
             } elseif (isset($this->definitions[$service]) && $this->definitions[$service]->getImplement()) {
                 // @Implement::create
                 return $entity[1] === 'create' ? $this->resolveServiceClass($service, $recursive) : NULL;
             }
         }
         try {
             $reflection = Nette\Utils\Callback::toReflection($entity[0] === '' ? $entity[1] : $entity);
             $refClass = $reflection instanceof \ReflectionMethod ? $reflection->getDeclaringClass() : NULL;
         } catch (\ReflectionException $e) {
         }
         if (isset($e) || $refClass && (!$reflection->isPublic() || $refClass->isTrait() && !$reflection->isStatic())) {
             $name = array_slice(array_keys($recursive), -1);
             throw new ServiceCreationException(sprintf("Factory '%s' used in service '%s' is not callable.", Nette\Utils\Callback::toString($entity), $name[0]));
         }
         return PhpReflection::getReturnType($reflection);
     } elseif ($service = $this->getServiceName($entity)) {
         // alias or factory
         if (Strings::contains($service, '\\')) {
             // @\Class
             return ltrim($service, '\\');
         }
         return $this->definitions[$service]->getImplement() ?: $this->resolveServiceClass($service, $recursive);
     } elseif (is_string($entity)) {
         if (!class_exists($entity) || !(new ReflectionClass($entity))->isInstantiable()) {
             $name = array_slice(array_keys($recursive), -1);
             throw new ServiceCreationException("Class {$entity} used in service '{$name['0']}' not found or is not instantiable.");
         }
         return ltrim($entity, '\\');
     }
 }
示例#5
0
 /**
  * Performs the server side validation.
  * @param  IControl[]
  * @return void
  */
 public function validate(array $controls = NULL)
 {
     foreach ($controls === NULL ? $this->getComponents() : $controls as $control) {
         $control->validate();
     }
     if ($this->onValidate !== NULL) {
         if (!is_array($this->onValidate) && !$this->onValidate instanceof \Traversable) {
             throw new Nette\UnexpectedValueException('Property Form::$onValidate must be array or Traversable, ' . gettype($this->onValidate) . ' given.');
         }
         foreach ($this->onValidate as $handler) {
             $params = Nette\Utils\Callback::toReflection($handler)->getParameters();
             $values = isset($params[1]) ? $this->getValues($params[1]->isArray()) : NULL;
             Nette\Utils\Callback::invoke($handler, $this, $values);
         }
     }
     $this->validated = TRUE;
 }
示例#6
0
 /**
  * Fires submit/click events.
  * @return void
  */
 public function fireEvents()
 {
     if (!$this->isSubmitted()) {
         return;
     } elseif (!$this->getErrors()) {
         $this->validate();
     }
     if ($this->submittedBy instanceof ISubmitterControl) {
         if ($this->isValid()) {
             $this->submittedBy->onClick($this->submittedBy);
         } else {
             $this->submittedBy->onInvalidClick($this->submittedBy);
         }
     }
     if (!$this->isValid()) {
         $this->onError($this);
     } elseif ($this->onSuccess !== NULL) {
         if (!is_array($this->onSuccess) && !$this->onSuccess instanceof \Traversable) {
             throw new Nette\UnexpectedValueException('Property Form::$onSuccess must be array or Traversable, ' . gettype($this->onSuccess) . ' given.');
         }
         foreach ($this->onSuccess as $handler) {
             $params = Nette\Utils\Callback::toReflection($handler)->getParameters();
             $values = isset($params[1]) ? $this->getValues($params[1]->isArray()) : NULL;
             Nette\Utils\Callback::invoke($handler, $this, $values);
             if (!$this->isValid()) {
                 $this->onError($this);
                 break;
             }
         }
     }
     $this->onSubmit($this);
 }
示例#7
0
 /**
  * Calls method using autowiring.
  * @return mixed
  */
 public function callMethod($function, array $args = array())
 {
     return call_user_func_array($function, Helpers::autowireArguments(Nette\Utils\Callback::toReflection($function), $args, $this));
 }
示例#8
0
 /**
  * Performs the server side validation.
  * @param  IControl[]
  * @return void
  */
 public function validate(array $controls = NULL)
 {
     foreach ($controls === NULL ? $this->getComponents() : $controls as $control) {
         $control->validate();
     }
     foreach ($this->onValidate ?: [] as $handler) {
         $params = Nette\Utils\Callback::toReflection($handler)->getParameters();
         $values = isset($params[1]) ? $this->getValues($params[1]->isArray()) : NULL;
         Nette\Utils\Callback::invoke($handler, $this, $values);
     }
     $this->validated = TRUE;
 }
示例#9
0
文件: Container.php 项目: Northys/di
 /**
  * Calls method using autowiring.
  * @return mixed
  */
 public function callMethod(callable $function, array $args = [])
 {
     return $function(...Helpers::autowireArguments(Nette\Utils\Callback::toReflection($function), $args, $this));
 }
示例#10
0
文件: MapAPI.php 项目: jasin755/maps
 /**
  * Send markers to template as JSON
  * @internal
  */
 public function handleMarkers()
 {
     foreach ($this->onMarkers as $handler) {
         $params = Nette\Utils\Callback::toReflection($handler)->getParameters();
         Nette\Utils\Callback::invoke($handler, $this, $params);
     }
     $this->getPresenter()->sendResponse(new JsonResponse((array) $this->markers));
 }
示例#11
0
 /**
  * Caches results of function/method calls.
  * @param  mixed
  * @param  array  dependencies
  * @return Closure
  */
 public function wrap($function, array $dependencies = NULL)
 {
     $cache = $this;
     return function () use($cache, $function, $dependencies) {
         $key = array(Callback::toReflection($function), func_get_args());
         $data = $cache->load($key);
         if ($data === NULL) {
             $data = $cache->save($key, Callback::invokeArgs($function, $key[1]), $dependencies);
         }
         return $data;
     };
 }
示例#12
0
 private function resolveClass($name, $recursive = array())
 {
     if (isset($recursive[$name])) {
         throw new ServiceCreationException('Circular reference detected for services: ' . implode(', ', array_keys($recursive)) . '.');
     }
     $recursive[$name] = TRUE;
     $def = $this->definitions[$name];
     $factory = $def->factory->entity;
     if ($def->class) {
         return $def->class;
     } elseif (is_array($factory)) {
         // method calling
         if ($service = $this->getServiceName($factory[0])) {
             if (Strings::contains($service, '\\')) {
                 // @\Class
                 $factory[0] = $service;
             } else {
                 $factory[0] = $this->resolveClass($service, $recursive);
                 if (!$factory[0]) {
                     return;
                 }
                 if ($this->definitions[$service]->implement && $factory[1] === 'create') {
                     return $def->class = $factory[0];
                 }
             }
         }
         if (!is_callable($factory)) {
             throw new ServiceCreationException("Factory '" . Nette\Utils\Callback::toString($factory) . "' is not callable.");
         }
         try {
             $reflection = Nette\Utils\Callback::toReflection($factory);
         } catch (\ReflectionException $e) {
             throw new ServiceCreationException("Missing factory '" . Nette\Utils\Callback::toString($factory) . "'.");
         }
         $def->class = preg_replace('#[|\\s].*#', '', $reflection->getAnnotation('return'));
         if ($def->class && $reflection instanceof \ReflectionMethod) {
             $def->class = Reflection\AnnotationsParser::expandClassName($def->class, $reflection->getDeclaringClass());
         }
     } elseif ($service = $this->getServiceName($factory)) {
         // alias or factory
         if (!$def->implement) {
             $def->autowired = FALSE;
         }
         if (Strings::contains($service, '\\')) {
             // @\Class
             return $def->class = $service;
         }
         if ($this->definitions[$service]->implement) {
             $def->autowired = FALSE;
         }
         return $def->class = $this->definitions[$service]->implement ?: $this->resolveClass($service, $recursive);
     } else {
         return $def->class = $factory;
         // class name
     }
 }
示例#13
0
文件: Form.php 项目: bombush/NatsuCon
 /**
  * Fires submit/click events.
  * @return void
  */
 public function fireEvents()
 {
     if (!$this->isSubmitted()) {
         return;
     } elseif (!$this->getErrors()) {
         $this->validate();
     }
     if ($this->submittedBy instanceof ISubmitterControl) {
         if ($this->isValid()) {
             $this->submittedBy->onClick($this->submittedBy);
         } else {
             $this->submittedBy->onInvalidClick($this->submittedBy);
         }
     }
     if (!$this->isValid()) {
         $this->onError($this);
     } elseif ($this->onSuccess) {
         foreach ($this->onSuccess as $handler) {
             $params = Nette\Utils\Callback::toReflection($handler)->getParameters();
             $values = isset($params[1]) ? $this->getValues($params[1]->isArray()) : NULL;
             Nette\Utils\Callback::invoke($handler, $this, $values);
             if (!$this->isValid()) {
                 $this->onError($this);
                 break;
             }
         }
     }
     $this->onSubmit($this);
 }
示例#14
0
 private function renderListeners($ids)
 {
     static $addIcon;
     if (empty($addIcon)) {
         $addIcon = '<img width="18" height="18" src="data:image/png;base64,' . base64_encode(file_get_contents(__DIR__ . '/add.png')) . '" title="Listener" />';
     }
     $registeredClasses = $this->getClassMap();
     $h = 'htmlspecialchars';
     $shortFilename = function (Nette\Reflection\GlobalFunction $refl) {
         $title = '.../' . basename($refl->getFileName()) . ':' . $refl->getStartLine();
         if ($editor = Tracy\Helpers::editorUri($refl->getFileName(), $refl->getStartLine())) {
             return sprintf(' defined at <a href="%s">%s</a>', htmlspecialchars($editor), $title);
         }
         return ' defined at ' . $title;
     };
     $s = '';
     foreach ($ids as $id) {
         if (is_callable($id)) {
             $s .= '<tr><td width=18>' . $addIcon . '</td><td><pre class="nette-dump"><span class="nette-dump-object">' . Callback::toString($id) . ($id instanceof \Closure ? $shortFilename(Callback::toReflection($id)) : '') . '</span></span></th></tr>';
             continue;
         }
         if (!$this->sl->isCreated($id) && ($class = array_search($id, $registeredClasses, TRUE))) {
             $s .= '<tr><td width=18>' . $addIcon . '</td><td><pre class="nette-dump"><span class="nette-dump-object">' . $h(Nette\Reflection\ClassType::from($class)->getName()) . '</span></span></th></tr>';
         } else {
             try {
                 $s .= '<tr><td width=18>' . $addIcon . '</td><td>' . self::dumpToHtml($this->sl->getService($id)) . '</th></tr>';
             } catch (\Exception $e) {
                 $s .= "<tr><td colspan=2>Service {$id} cannot be loaded because of exception<br><br>\n" . (string) $e . '</td></th>';
             }
         }
     }
     return $s;
 }
示例#15
0
 public function fireEvents()
 {
     /** @var EntityForm|UI\Form $this */
     if (!($submittedBy = $this->isSubmitted())) {
         return;
     }
     $this->validate();
     if ($this->isValid()) {
         $this->getEntityMapper()->save($this->entity, $this);
     }
     $this->getViolationsMapper()->validateContainer($this, $this->entity);
     if ($submittedBy instanceof Nette\Forms\ISubmitterControl) {
         if ($this->isValid()) {
             $submittedBy->onClick($submittedBy);
         } else {
             $submittedBy->onInvalidClick($submittedBy);
         }
     }
     if ($this->onSuccess) {
         foreach ($this->onSuccess as $handler) {
             if (!$this->isValid()) {
                 $this->onError($this);
                 break;
             }
             $params = Nette\Utils\Callback::toReflection($handler)->getParameters();
             $values = isset($params[1]) ? $this->getValues($params[1]->isArray()) : NULL;
             Nette\Utils\Callback::invoke($handler, $this, $values);
         }
     } elseif (!$this->isValid()) {
         $this->onError($this);
     }
     $this->onSubmit($this);
 }
示例#16
0
 private function resolveClass($name, $recursive = array())
 {
     if (isset($recursive[$name])) {
         throw new ServiceCreationException(sprintf('Circular reference detected for services: %s.', implode(', ', array_keys($recursive))));
     }
     $recursive[$name] = TRUE;
     $def = $this->definitions[$name];
     $factory = $def->factory->entity;
     if ($def->class) {
         return $def->class;
     } elseif (is_array($factory)) {
         // method calling
         if ($service = $this->getServiceName($factory[0])) {
             if (Strings::contains($service, '\\')) {
                 // @\Class
                 $factory[0] = $service;
             } else {
                 $factory[0] = $this->resolveClass($service, $recursive);
                 if (!$factory[0]) {
                     return;
                 }
                 if ($this->definitions[$service]->implement && $factory[1] === 'create') {
                     return $def->class = $factory[0];
                 }
             }
         }
         try {
             $reflection = Nette\Utils\Callback::toReflection($factory);
         } catch (\ReflectionException $e) {
         }
         if (isset($e) || !is_callable($factory)) {
             throw new ServiceCreationException(sprintf("Factory '%s' used in service '%s' is not callable.", Nette\Utils\Callback::toString($factory), $name));
         }
         $def->class = preg_replace('#[|\\s].*#', '', $reflection->getAnnotation('return'));
         if ($def->class && $reflection instanceof \ReflectionMethod) {
             $def->class = Reflection\AnnotationsParser::expandClassName($tmp = $def->class, $reflection->getDeclaringClass());
             if ($tmp !== $def->class && $tmp[0] !== '\\' && class_exists($tmp)) {
                 $def->class = $tmp;
                 trigger_error("You should use @return \\{$tmp}' in {$reflection}.", E_USER_WARNING);
             }
         }
     } elseif ($service = $this->getServiceName($factory)) {
         // alias or factory
         if (!$def->implement) {
             $def->autowired = FALSE;
         }
         if (Strings::contains($service, '\\')) {
             // @\Class
             return $def->class = $service;
         }
         if ($this->definitions[$service]->implement) {
             $def->autowired = FALSE;
         }
         return $def->class = $this->definitions[$service]->implement ?: $this->resolveClass($service, $recursive);
     } else {
         return $def->class = $factory;
         // class name
     }
 }