public static function getNearestChildByPath(Document\Hardlink $hardlink, $path)
{
if ($hardlink->getChildrenFromSource() && $hardlink->getSourceDocument()) {
$hardlinkRealPath = preg_replace("@^" . preg_quote($hardlink->getRealFullPath()) . "@", $hardlink->getSourceDocument()->getRealFullPath(), $path);
$pathes = [];
$pathes[] = "/";
$pathParts = explode("/", $hardlinkRealPath);
$tmpPathes = [];
foreach ($pathParts as $pathPart) {
$tmpPathes[] = $pathPart;
$t = implode("/", $tmpPathes);
if (!empty($t)) {
$pathes[] = $t;
}
}
$pathes = array_reverse($pathes);
foreach ($pathes as $p) {
$hardLinkedDocument = Document::getByPath($p);
if ($hardLinkedDocument instanceof Document) {
$hardLinkedDocument = self::wrap($hardLinkedDocument);
$hardLinkedDocument->setHardLinkSource($hardlink);
$_path = $path != "/" ? $_path = dirname($p) : $p;
$_path = str_replace("\\", "/", $_path);
// windows patch
$_path .= $_path != "/" ? "/" : "";
$_path = preg_replace("@^" . preg_quote($hardlink->getSourceDocument()->getRealPath()) . "@", $hardlink->getRealPath(), $_path);
$hardLinkedDocument->setPath($_path);
return $hardLinkedDocument;
}
}
}
}