protected function storeVersion(PuliResource $resource)
{
$path = $resource->getPath();
// Newly inserted parent directories and the resource need to be
// sorted before we can correctly search references below
krsort($this->json);
// If a mapping exists for a sub-path of this resource
// (e.g. $path = /a, mapped sub-path = /a/b)
// we need to record the order, since by default sub-paths are
// preferred over super paths
$references = $this->searchReferences($path, self::NO_SEARCH_FILESYSTEM | self::NO_CHECK_FILE_EXISTS | self::INCLUDE_ANCESTORS | self::INCLUDE_NESTED);
// Filter virtual resources
$references = array_filter($references, function ($currentReferences) {
return array(null) !== $currentReferences;
});
// The $references contain:
// - any sub references (e.g. /a/b/c, /a/b/d)
// - the reference itself at $pos (e.g. /a/b)
// - non-null parent references (e.g. /a)
// (in that order, since long paths are sorted before short paths)
$pos = array_search($path, array_keys($references), true);
// We need to do three things:
// 1. If any parent mapping has an order defined, inherit that order
if ($pos + 1 < count($references)) {
// Inherit the parent order if necessary
if (!isset($this->json['_order'][$path])) {
$parentReferences = array_slice($references, $pos + 1);
$this->initWithParentOrder($path, $parentReferences);
}
// A parent order was inherited. Insert the path itself.
if (isset($this->json['_order'][$path])) {
$this->prependOrderEntry($path, $path);
}
}
// 2. If there are child mappings, insert the current path into their order
if ($pos > 0) {
$subReferences = array_slice($references, 0, $pos);
foreach ($subReferences as $subPath => $_) {
if (isset($this->json['_order'][$subPath])) {
continue;
}
if (isset($this->json['_order'][$path])) {
$this->json['_order'][$subPath] = $this->json['_order'][$path];
} else {
$this->initWithDefaultOrder($subPath, $path, $references);
}
}
// After initializing all order entries, insert the new one
foreach ($subReferences as $subPath => $_) {
$this->prependOrderEntry($subPath, $path);
}
}
}