function AttributeClassLoader::addRoute
Parameters
RouteAttribute $attr or an object that exposes a similar interface:
1 call to AttributeClassLoader::addRoute()
- AttributeClassLoader::load in vendor/
symfony/ routing/ Loader/ AttributeClassLoader.php
File
-
vendor/
symfony/ routing/ Loader/ AttributeClassLoader.php, line 151
Class
- AttributeClassLoader
- AttributeClassLoader loads routing information from a PHP class and its methods.
Namespace
Symfony\Component\Routing\LoaderCode
protected function addRoute(RouteCollection $collection, object $attr, array $globals, \ReflectionClass $class, \ReflectionMethod $method) : void {
if ($attr->getEnv() && $attr->getEnv() !== $this->env) {
return;
}
$name = $attr->getName() ?? $this->getDefaultRouteName($class, $method);
$name = $globals['name'] . $name;
$requirements = $attr->getRequirements();
foreach ($requirements as $placeholder => $requirement) {
if (\is_int($placeholder)) {
throw new \InvalidArgumentException(\sprintf('A placeholder name must be a string (%d given). Did you forget to specify the placeholder key for the requirement "%s" of route "%s" in "%s::%s()"?', $placeholder, $requirement, $name, $class->getName(), $method->getName()));
}
}
$defaults = array_replace($globals['defaults'], $attr->getDefaults());
$requirements = array_replace($globals['requirements'], $requirements);
$options = array_replace($globals['options'], $attr->getOptions());
$schemes = array_unique(array_merge($globals['schemes'], $attr->getSchemes()));
$methods = array_unique(array_merge($globals['methods'], $attr->getMethods()));
$host = $attr->getHost() ?? $globals['host'];
$condition = $attr->getCondition() ?? $globals['condition'];
$priority = $attr->getPriority() ?? $globals['priority'];
$path = $attr->getLocalizedPaths() ?: $attr->getPath();
$prefix = $globals['localized_paths'] ?: $globals['path'];
$paths = [];
if (\is_array($path)) {
if (!\is_array($prefix)) {
foreach ($path as $locale => $localePath) {
$paths[$locale] = $prefix . $localePath;
}
}
elseif ($missing = array_diff_key($prefix, $path)) {
throw new \LogicException(\sprintf('Route to "%s" is missing paths for locale(s) "%s".', $class->name . '::' . $method->name, implode('", "', array_keys($missing))));
}
else {
foreach ($path as $locale => $localePath) {
if (!isset($prefix[$locale])) {
throw new \LogicException(\sprintf('Route to "%s" with locale "%s" is missing a corresponding prefix in class "%s".', $method->name, $locale, $class->name));
}
$paths[$locale] = $prefix[$locale] . $localePath;
}
}
}
elseif (\is_array($prefix)) {
foreach ($prefix as $locale => $localePrefix) {
$paths[$locale] = $localePrefix . $path;
}
}
else {
$paths[] = $prefix . $path;
}
foreach ($method->getParameters() as $param) {
if (isset($defaults[$param->name]) || !$param->isDefaultValueAvailable()) {
continue;
}
foreach ($paths as $locale => $path) {
if (preg_match(\sprintf('/\\{%s(?:<.*?>)?\\}/', preg_quote($param->name)), $path)) {
if (\is_scalar($defaultValue = $param->getDefaultValue()) || null === $defaultValue) {
$defaults[$param->name] = $defaultValue;
}
elseif ($defaultValue instanceof \BackedEnum) {
$defaults[$param->name] = $defaultValue->value;
}
break;
}
}
}
foreach ($paths as $locale => $path) {
$route = $this->createRoute($path, $defaults, $requirements, $options, $host, $schemes, $methods, $condition);
$this->configureRoute($route, $class, $method, $attr);
if (0 !== $locale) {
$route->setDefault('_locale', $locale);
$route->setRequirement('_locale', preg_quote($locale));
$route->setDefault('_canonical_route', $name);
$collection->add($name . '.' . $locale, $route, $priority);
}
else {
$collection->add($name, $route, $priority);
}
}
}