private function addResource($path, Resource $resource) { // Don't modify resources attached to other repositories if ($resource->isAttached()) { $resource = clone $resource; } $basePath = '/' === $path ? $path : $path . '/'; // Read children before attaching the resource to this repository $children = $resource->listChildren(); $resource->attachTo($this, $path); // Add the resource before adding its children, so that the array // stays sorted $this->resources[$path] = $resource; foreach ($children as $name => $child) { $this->addResource($basePath . $name, $child); } }