public function load(Module $containingModule, ModuleList $modules)
    {
        if (null !== $this->state) {
            throw new AlreadyLoadedException('The mapping is already loaded.');
        }

        $filesystemPaths = array();
        $pathMappings = array();
        $loadErrors = array();

        foreach ($this->pathReferences as $relativePath) {
            $loadError = null;

            try {
                $absolutePath = $this->makeAbsolute($relativePath, $containingModule, $modules);
                $this->assertFileExists($absolutePath, $relativePath, $containingModule);

                $filesystemPaths[] = $absolutePath;
            } catch (NoSuchModuleException $loadError) {
            } catch (FileNotFoundException $loadError) {
            }

            if ($loadError) {
                $loadErrors[] = $loadError;
            }
        }

        foreach ($filesystemPaths as $filesystemPath) {
            $pathMappings[$filesystemPath] = $this->repositoryPath;

            if (!is_dir($filesystemPath)) {
                continue;
            }

            $prefixLength = strlen($filesystemPath);
            $directoryEntries = iterator_to_array(new RecursiveIteratorIterator(
                new RecursiveDirectoryIterator(
                    $filesystemPath,
                    RecursiveDirectoryIterator::CURRENT_AS_PATHNAME | RecursiveDirectoryIterator::SKIP_DOTS
                ),
                RecursiveIteratorIterator::SELF_FIRST
            ));

            // RecursiveDirectoryIterator is not guaranteed to sort its results,
            // so sort them here
            // We need to sort in the loop and not at the very end because the
            // order of the $filesystemPaths should be kept in $pathMappings
            ksort($directoryEntries);

            foreach ($directoryEntries as $nestedFilesystemPath) {
                $pathMappings[$nestedFilesystemPath] = substr_replace($nestedFilesystemPath, $this->repositoryPath, 0, $prefixLength);
            }
        }

        $this->repositoryPaths = array_unique($pathMappings);
        $this->filesystemPaths = $filesystemPaths;
        $this->pathMappings = $pathMappings;
        $this->loadErrors = $loadErrors;
        $this->containingModule = $containingModule;

        sort($this->repositoryPaths);

        $this->refreshState();
    }