/** * Generates and checks presenter class name. * * @param string presenter name * @return string class name * @throws Application\InvalidPresenterException */ public function getPresenterClass(&$name) { if (isset($this->cache[$name])) { return $this->cache[$name]; } if (!is_string($name) || !Nette\Utils\Strings::match($name, '#^[a-zA-Z\\x7f-\\xff][a-zA-Z0-9\\x7f-\\xff:]*\\z#')) { throw new Application\InvalidPresenterException("Presenter name must be alphanumeric string, '{$name}' is invalid."); } $classes = $this->formatPresenterClasses($name); if (!$classes) { throw new Application\InvalidPresenterException("Cannot load presenter '{$name}', no applicable mapping found."); } $class = $this->findValidClass($classes); if (!$class) { throw new Application\InvalidPresenterException("Cannot load presenter '{$name}', none of following classes were found: " . implode(', ', $classes)); } $reflection = new Nette\Reflection\ClassType($class); $class = $reflection->getName(); if (!$reflection->implementsInterface('Nette\\Application\\IPresenter')) { throw new Application\InvalidPresenterException("Cannot load presenter '{$name}', class '{$class}' is not Nette\\Application\\IPresenter implementor."); } if ($reflection->isAbstract()) { throw new Application\InvalidPresenterException("Cannot load presenter '{$name}', class '{$class}' is abstract."); } return $this->cache[$name] = $class; }
/** * Returns all classes implementing specified interface * * @param string interface name * @return array of fully qualified class names (with namespace) */ public function getAllClassesImplementing($interfaceName) { $cacheKey = array(); $cacheKey[] = 'implementing'; $cacheKey[] = $interfaceName; return $this->getAllClassesHelper($cacheKey, function ($className) use($interfaceName) { $class = new Nette\Reflection\ClassType($className); return $class->implementsInterface($interfaceName) && $interfaceName != $className; }); }
/** * @param string * @param callable * @param string */ public function addImporter($id, $factory, $class) { if (isset($this->factories[$id])) { throw new \NetteAddons\InvalidStateException("Importer '{$id}' is already registered"); } if (!is_callable($factory)) { throw new \NetteAddons\InvalidArgumentException('Factory is not callable.'); } $classRef = new ClassType($class); if (!$classRef->implementsInterface('NetteAddons\\Model\\IAddonImporter')) { throw new \NetteAddons\InvalidArgumentException("Class '{$class}' does not implement IAddonImporter."); } $this->factories[$id] = $factory; $this->classes[$id] = $class; }
public function beforeCompile() { $builder = $this->getContainerBuilder(); $definitions = $builder->getDefinitions(); $templateFactory = $builder->getDefinition("templateFactory"); foreach ($definitions as $definition) { if (!$definition->factory || !class_exists($definition->factory->entity)) { continue; } $classReflection = new Nette\Reflection\ClassType($definition->factory->entity); if ($classReflection->implementsInterface("HotelQuickly\\Factory\\IControlFactory")) { $definition->addSetup("setTemplateFactory", array($templateFactory)); } } }
/** * Generates and checks presenter class name. * @param string presenter name * @return string class name * @throws InvalidPresenterException */ public function getPresenterClass(&$name) { if (isset($this->cache[$name])) { list($class, $name) = $this->cache[$name]; return $class; } if (!is_string($name) || !Nette\Utils\Strings::match($name, '#^[a-zA-Z\\x7f-\\xff][a-zA-Z0-9\\x7f-\\xff:]*\\z#')) { throw new InvalidPresenterException("Presenter name must be alphanumeric string, '{$name}' is invalid."); } $class = $this->formatPresenterClass($name); if (!class_exists($class)) { // internal autoloading $file = $this->formatPresenterFile($name); if (is_file($file) && is_readable($file)) { call_user_func(function () use($file) { require $file; }); } if (!class_exists($class)) { throw new InvalidPresenterException("Cannot load presenter '{$name}', class '{$class}' was not found in '{$file}'."); } } $reflection = new Nette\Reflection\ClassType($class); $class = $reflection->getName(); if (!$reflection->implementsInterface('Nette\\Application\\IPresenter')) { throw new InvalidPresenterException("Cannot load presenter '{$name}', class '{$class}' is not Nette\\Application\\IPresenter implementor."); } if ($reflection->isAbstract()) { throw new InvalidPresenterException("Cannot load presenter '{$name}', class '{$class}' is abstract."); } // canonicalize presenter name $realName = $this->unformatPresenterClass($class); if ($name !== $realName) { if ($this->caseSensitive) { throw new InvalidPresenterException("Cannot load presenter '{$name}', case mismatch. Real name is '{$realName}'."); } else { $this->cache[$name] = array($class, $realName); $name = $realName; } } else { $this->cache[$name] = array($class, $realName); } return $class; }
/** * @param callable $begin {int $count} * @param callable $tick {} * @return int */ public function populate(callable $begin = NULL, callable $tick = NULL) { $counter = 0; /** @var Repository[] $repos */ $repos = []; foreach ($this->orm->getRepositoryClasses() as $class) { $repo = $this->orm->getRepository($class); $cn = $repo->getEntityClassName(); $classes = is_array($cn) ? $cn : [$cn]; foreach ($classes as $entity) { $ref = new ClassType($entity); if (!$ref->implementsInterface(IIndexable::class)) { continue 2; } } $repos[] = $repo; } $total = 0; foreach ($repos as $repo) { $total += $repo->findAll()->count(); } $begin($total); foreach ($repos as $repo) { $rows = []; /** @var Entity|IIndexable $entity */ foreach ($repo->findAll() as $entity) { $data = $entity->getIndexData(); if ($data === FALSE) { continue; } $rows[$entity->id] = $data; $tick(); $counter++; } if ($rows && isset($entity)) { $this->es->addToIndexBulk($entity->getShortEntityName(), $rows); } } return $counter; }
/** * @param ClassType $class * @return bool */ private function isInjectableService(ClassType $class) { return $class->implementsInterface(IInjectableService::class); }
private function registerExtension($extension) { $builder = $this->containerBuilder; // Statement if ($extension instanceof Statement) { $class = $extension->getEntity(); } elseif (is_string($extension) && Strings::startsWith($extension, '@')) { $class = $builder->getDefinition($builder->getServiceName($extension))->getClass(); } elseif (is_string($extension)) { $class = $extension; $extension = new Statement($extension); } else { throw new UnexpectedTypeException("Extension has to be string or Statement, given " . (is_object($extension) ? get_class($extension) : gettype($extension)) . "."); } if (!class_exists($class)) { throw new InvalidArgumentException("Form extension class {$class} not found."); } $reflection = new ClassType($class); $matched = FALSE; if ($reflection->implementsInterface('NForms\\Builder\\Components\\IBeforeCreateContainer')) { $this->beforeCreateContainerOpSet->addSetup('add', array($extension)); $matched = TRUE; } if ($reflection->implementsInterface('NForms\\Builder\\Components\\IContainerFactory')) { $this->containerFactories->addSetup('add', array($extension)); $matched = TRUE; } if ($reflection->implementsInterface('NForms\\Builder\\Components\\IAfterCreateContainer')) { $this->afterCreateContainerOpSet->addSetup('add', array($extension)); $matched = TRUE; } if ($reflection->implementsInterface('NForms\\Builder\\Components\\IBeforeCreateControl')) { $this->beforeCreateControlOpSet->addSetup('add', array($extension)); $matched = TRUE; } if ($reflection->implementsInterface('NForms\\Builder\\Components\\IControlFactory')) { $this->controlFactories->addSetup('add', array($extension)); $matched = TRUE; } if ($reflection->implementsInterface('NForms\\Builder\\Components\\IAfterCreateControl')) { $this->afterCreateControlOpSet->addSetup('add', array($extension)); $matched = TRUE; } if ($reflection->implementsInterface('NForms\\Mapping\\IControlMapper')) { $this->controlMapperSet->addSetup('add', array($extension)); $matched = TRUE; } if (!$matched) { throw new UnexpectedTypeException("Extension does not implement any of the expected interface. Given: '" . get_class($extension) . "'." . "Expected interfaces: IBeforeCreateContainer, IContainerFactory, IAfterCreateContainer, IBeforeCreateControl, IControlFactory, IAfterCreateControl, IControlMapper."); } return $this; }
/** * @param array $dirs * @param array $interfaces * @return array */ private function findByInterfaces(array $dirs, array $interfaces) { $loader = $this->createLoader(); $loader->addDirectory($dirs); $loader->rebuild(); $loader->register(); $classes = []; foreach (array_keys($loader->getIndexedClasses()) as $class) { // Skip not existing class if (!class_exists($class, TRUE)) { continue; } // Detect by reflection $ct = new ClassType($class); // Skip abstract if ($ct->isAbstract()) { continue; } // Does class implement one of given interface foreach ($interfaces as $interface) { if ($ct->implementsInterface($interface)) { $classes[] = $ct->getName(); } } } return $classes; }