private function addComponentFactories(array $factories) { $builder = $this->getContainerBuilder(); foreach ($factories as $component => $factory) { if (strpos($factory, '::') === FALSE) { $factory .= '::' . self::DEFAULT_FACTORY_METHOD; } if ($builder->hasDefinition($name = $this->prefix('registry.' . $component))) { $definition = $builder->getDefinition($name); } else { $definition = $builder->addDefinition($name)->addTag(self::TAG_COMPONENT_REGISTRY, $component)->setAutowired(FALSE); } list($class, $method) = explode('::', $factory); if (!Method::from($class, $method)->isStatic()) { $factory = '@' . $factory; } $definition->setFactory($factory); } }
/** * @author Jiří Šifalda * @param string * @return \Nette\Application\UI\Multiplier|\Nette\ComponentModel\IComponent */ protected function createComponent($name) { $method = 'createComponent' . ucfirst($name); if (method_exists($this, $method)) { $this->checkRequirements($this->getReflection()->getMethod($method)); if (\Nette\Reflection\Method::from($this, $method)->hasAnnotation('multiple')) { $presenter = $this; return new \Nette\Application\UI\Multiplier(function ($id) use($presenter, $method) { $defaultArgs = array($presenter, $id); return call_user_func_array(array($presenter, $method), $defaultArgs); }); # in PHP 5.4 factory for multiplied component can be protected # return new UI\Multiplier(function ($id) use ($name) { # return $this->$method($this, $id, $this->getDataset($name)); # }); } } return parent::createComponent($name); }
/** * @return Method|NULL */ public function getConstructor() { return ($ref = parent::getConstructor()) ? Method::from($this->getName(), $ref->getName()) : NULL; }
/** * Is a method callable? It means class is instantiable and method has * public visibility, is non-static and non-abstract. * @param string method name * @return bool */ public function hasCallableMethod($method) { $class = $this->getName(); $cache =& self::$mcCache[strtolower($class . ':' . $method)]; if ($cache === NULL) { try { $cache = FALSE; $rm = Nette\Reflection\Method::from($class, $method); $cache = $this->isInstantiable() && $rm->isPublic() && !$rm->isAbstract() && !$rm->isStatic(); } catch (\ReflectionException $e) { } } return $cache; }
public function testGetFailedExpression_evaluatorReturnsFalse_returnsExpressionThatFailed() { $request = \Mockery::mock(Request::class); $this->evaluator->shouldReceive('evaluate')->andReturn(false); $methodReflection = Method::from(TestDerivedClass::class, 'testMethod'); $result = $this->checker->checkRequirement($methodReflection, $request); $this->assertFalse($result); $this->assertEquals('method', (string) $this->checker->getFailedExpression()); }
/** * Generates link. If links points to @secure annotated signal handler method, additonal * parameter preventing changing parameters will be added. * * @param string * @param array|mixed $args * @return string */ public function link($destination, $args = array()) { if (!is_array($args)) { $args = func_get_args(); array_shift($args); } $link = parent::link($destination, $args); $lastRequest = $this->getPresenter()->getLastCreatedRequest(); // bad link if ($lastRequest === NULL) { return $link; } // not a signal if (substr($destination, -1) !== '!') { return $link; } // signal must lead to this presenter if ($this->getPresenter()->getName() !== $lastRequest->getPresenterName()) { return $link; } $destination = str_replace(':', '-', $destination); if (strpos($destination, '-') !== FALSE) { $pos = strrpos($destination, '-'); $signal = substr($destination, $pos + 1, -1); $component = substr($destination, 0, $pos); $component = $this->getComponent($component); } else { $signal = substr($destination, 0, -1); $component = $this; } // only components if (!$component instanceof PresenterComponent) { return $link; } $method = $component->formatSignalMethod($signal); $reflection = Method::from($component, $method); // does not have annotation if (!$reflection->hasAnnotation('secured')) { return $link; } $origParams = $lastRequest->getParameters(); $protectedParams = array(); foreach ($reflection->getParameters() as $param) { if ($param->isOptional()) { continue; } $protectedParams[$param->name] = $origParams[$component->getParameterId($param->name)]; } $uniqueId = $this->getUniqueId(); if (empty($uniqueId)) { $paramName = $component->getParameterId('__sec'); } else { $paramName = substr($component->getParameterId('__sec'), strlen($uniqueId) + 1); } $args[$paramName] = $this->createSecureHash($protectedParams); return parent::link($destination, $args); }
/** * Converts list of arguments to named parameters. * @param string class name * @param string method name * @param array arguments * @param array supplemental arguments * @return void * @throws InvalidLinkException */ private static function argsToParams($class, $method, &$args, $supplemental = array()) { static $cache; $params =& $cache[strtolower($class . ':' . $method)]; if ($params === NULL) { $params = Reflection\Method::from($class, $method)->getDefaultParameters(); } $i = 0; foreach ($params as $name => $def) { if (array_key_exists($i, $args)) { $args[$name] = $args[$i]; unset($args[$i]); $i++; } elseif (array_key_exists($name, $args)) { // continue with process } elseif (array_key_exists($name, $supplemental)) { $args[$name] = $supplemental[$name]; } else { continue; } if ($def === NULL) { if ((string) $args[$name] === '') { $args[$name] = NULL; // value transmit is unnecessary } } else { settype($args[$name], gettype($def)); if ($args[$name] === $def) { $args[$name] = NULL; } } } if (array_key_exists($i, $args)) { $method = Reflection\Method::from($class, $method)->getName(); throw new InvalidLinkException("Passed more parameters than method {$class}::{$method}() expects."); } }
/** * @param \MyTester\Job $job * @return string */ protected function runJob(Job $job) { $jobName = $this->getJobName(\Nette\Reflection\Method::from($job->callback[0], $job->callback[1])); Environment::$currentJob = $jobName; if (!$job->skip) { $this->setUp(); } $job->execute(); if (!$job->skip) { $this->tearDown(); } Environment::$currentJob = ""; switch ($job->result) { case "passed": return "."; break; case "skipped": return "s"; break; case "failed": return "F"; break; } return ""; }
getPersistentComponents($class=NULL){$class=$class===NULL?$this->getName():$class;$components=&self::$pcCache[$class];if($components!==NULL){return$components;}$components=array();if(is_subclass_of($class,'Nette\Application\UI\Presenter')){foreach(call_user_func(array($class,'getPersistentComponents'),$class)as$name=>$meta){if(is_string($meta)){$name=$meta;}$components[$name]=array('since'=>$class);}$components=$this->getPersistentComponents(get_parent_class($class))+$components;}return$components;}function hasCallableMethod($method){$class=$this->getName();$cache=&self::$mcCache[strtolower($class.':'.$method)];if($cache===NULL)try{$cache=FALSE;$rm=Nette\Reflection\Method::from($class,$method);$cache=$this->isInstantiable()&&$rm->isPublic()&&!$rm->isAbstract()&&!$rm->isStatic();}catch(\ReflectionException$e){}return$cache;}static
/** * @param Property $prop * @throws MissingServiceException * @throws UnexpectedValueException */ private function resolveProperty(Property $prop) { $type = $this->resolveAnnotationClass($prop, $prop->getAnnotation('var'), 'var'); $metadata = ['value' => NULL, 'type' => $type]; if (($args = (array) $prop->getAnnotation('autowire')) && !empty($args['factory'])) { $factoryType = $this->resolveAnnotationClass($prop, $args['factory'], 'autowire'); if (!$this->findByTypeForProperty($factoryType)) { throw new MissingServiceException("Factory of type \"{$factoryType}\" not found for {$prop} in annotation @autowire.", $prop); } $factoryMethod = Method::from($factoryType, 'create'); $createsType = $this->resolveAnnotationClass($factoryMethod, $factoryMethod->getAnnotation('return'), 'return'); if ($createsType !== $type) { throw new UnexpectedValueException("The property {$prop} requires {$type}, but factory of type {$factoryType}, that creates {$createsType} was provided.", $prop); } unset($args['factory']); $metadata['arguments'] = array_values($args); $metadata['factory'] = $this->findByTypeForProperty($factoryType); } elseif (!$this->findByTypeForProperty($type)) { throw new MissingServiceException("Service of type \"{$type}\" not found for {$prop} in annotation @var.", $prop); } // unset property to pass control to __set() and __get() unset($this->{$prop->getName()}); $this->autowireProperties[$prop->getName()] = $metadata; }
/** * Checks if given method has a given annotation * * @param string $method * @param string $annotation * @return bool */ protected function hasMethodAnnotation($method, $annotation) { if (!$this->getReflection()->hasMethod($method)) { return FALSE; } $rm = Method::from($this->getReflection()->getName(), $method); return $rm->hasAnnotation($annotation); }
/** * Gains possible return type from the `return` method annotation. * * @param string $className * @param string $methodName * @return array */ public static function getReturnTypes($className, $methodName) { $methodName = Method::from($className, $methodName); list($types) = preg_split('~\\s~', $methodName->getAnnotation('return'), 2); return explode('|', $types); }