public function getRepository($className)
{
/** @var Repository $repo */
$repo = null;
$className = (string) $className;
if (array_key_exists($className, $this->aliases)) {
$className = $this->aliases[$className];
}
try {
$classMetadata = $this->getMapper()->loadMetadataForClass($className);
} catch (StorageException $e) {
throw new InvalidRepositoryException("Attempted to load repository for invalid class or alias: {$className}. Check that the class, alias or contenttype definition is correct.");
}
if (array_key_exists($classMetadata->getName(), $this->repositories)) {
$repoClass = $this->repositories[$classMetadata->getName()];
if (is_callable($repoClass)) {
$repo = call_user_func_array($repoClass, [$this, $classMetadata]);
} else {
$repo = new $repoClass($this, $classMetadata);
}
}
if ($repo === null) {
foreach ($this->aliases as $alias => $namespace) {
$full = str_replace($alias, $namespace, $className);
if (array_key_exists($full, $this->repositories)) {
$classMetadata = $this->getMapper()->loadMetadataForClass($full);
$repoClass = $this->repositories[$full];
$repo = new $repoClass($this, $classMetadata);
}
}
}
/*
* The metadata driver can also attempt to resolve an alias for us.
* For now we are hardcoding the link between a content entity and
* the content repository, but in time this should be a metadata level
* configuration.
*/
if ($repo === null && $this->getMapper()->resolveClassName($className) === 'Bolt\\Storage\\Entity\\Content') {
$repo = $this->getDefaultRepositoryFactory($classMetadata);
}
/*
* If the fetched metadata isn't mapped to a specific entity then we treat
* it as a generic Content repo
*/
if ($repo === null && in_array($className, $this->getMapper()->getUnmapped())) {
$repo = $this->getDefaultRepositoryFactory($classMetadata);
}
if ($repo === null) {
$repo = new Repository($this, $classMetadata);
}
if ($repo instanceof Repository\ContentRepository) {
/** @var ContentRepository $repo */
$repo->setLegacyService($this->legacyService);
}
return $repo;
}