elementId = $elementId; if (!in_array($elementId, self::SLOT_IDS)) { throw new ExceptionRuntimeInternal("$elementId is not a valid slot id. Valid ids are (" . ArrayUtility::formatAsString(self::SLOT_IDS) . ")."); } $this->contextPath = $contextPath; } public static function createFromElementId(string $elementId, WikiPath $contextPath = null): TemplateSlot { if ($contextPath === null) { $contextPath = ExecutionContext::getActualOrCreateFromEnv() ->getConfig() ->getDefaultContextPath(); } return new TemplateSlot($elementId, $contextPath); } public static function createFromPathName($pathNameWithoutExtension): TemplateSlot { return self::createFromElementId(self::getElementIdFromPathName($pathNameWithoutExtension)); } private static function getElementIdFromPathName($pathNameWithoutExtension): string { if ($pathNameWithoutExtension === SlotSystem::getPageHeaderSlotName()) { return self::PAGE_HEADER_ID; } if ($pathNameWithoutExtension === SlotSystem::getPageFooterSlotName()) { return self::PAGE_FOOTER_ID; } if ($pathNameWithoutExtension === SlotSystem::getSidebarName()) { return self::PAGE_SIDE_ID; } if ($pathNameWithoutExtension === SlotSystem::getMainSideSlotName()) { return self::MAIN_SIDE_ID; } if ($pathNameWithoutExtension === self::SLOT_MAIN_HEADER_PATH_NAME) { return self::MAIN_HEADER_ID; } if ($pathNameWithoutExtension === self::SLOT_MAIN_FOOTER_PATH_NAME) { return self::MAIN_FOOTER_ID; } throw new ExceptionRuntimeInternal("Internal: The markup name ($pathNameWithoutExtension) was unexpected, it's not a slot"); } public static function getPathNameFromElementId($elementId) { switch ($elementId) { case self::PAGE_HEADER_ID: return SlotSystem::getPageHeaderSlotName(); case self::PAGE_FOOTER_ID: return SlotSystem::getPageFooterSlotName(); case self::MAIN_CONTENT_ID: throw new ExceptionRuntimeInternal("Main content area is not a slot and does not have any last slot name"); case self::PAGE_SIDE_ID: return SlotSystem::getSidebarName(); case self::MAIN_SIDE_ID: return SlotSystem::getMainSideSlotName(); case self::MAIN_HEADER_ID: return self::SLOT_MAIN_HEADER_PATH_NAME; case self::MAIN_FOOTER_ID: return self::SLOT_MAIN_FOOTER_PATH_NAME; default: throw new ExceptionRuntimeInternal("Internal: The element ($elementId) was unexpected, it's not a slot"); } } /** * * @return WikiPath */ public function getDefaultSlotContentPath(): WikiPath { return WikiPath::createComboResource(":slot:{$this->getElementId()}.md"); } /** */ public function getLastFileNameForFragment() { $elementId = $this->getElementId(); return self::getPathNameFromElementId($elementId); } public function __toString() { return "Slot {$this->getElementId()} for {$this->contextPath}"; } /** * @throws ExceptionNotFound - if the area is not a slot or there is no path found */ private function getFragmentPath() { // Main content $requestedPath = $this->contextPath; if ($this->getElementId() === self::MAIN_CONTENT_ID) { return $requestedPath; } // Slot $contextExtension = $requestedPath->getExtension(); try { return FileSystems::closest($requestedPath, $this->getLastFileNameForFragment() . '.' . $contextExtension); } catch (ExceptionNotFound $e) { foreach (WikiPath::ALL_MARKUP_EXTENSIONS as $markupExtension) { if ($markupExtension == $contextExtension) { continue; } try { return FileSystems::closest($requestedPath, $this->getLastFileNameForFragment() . '.' . $contextExtension); } catch (ExceptionNotFound $e) { // not found, we let it go to the default if needed } } } return $this->getDefaultSlotContentPath(); } /** * @throws ExceptionNotFound if the page/markup fragment was not found (a container element does not have any also) */ public function getMarkupFetcher(): FetcherMarkup { if (isset($this->fetcherFragment)) { return $this->fetcherFragment; } /** * Rebuild the fragment if any */ $fragmentPath = $this->getFragmentPath(); $contextPath = $this->contextPath; try { $this->fetcherFragment = FetcherMarkup::createXhtmlMarkupFetcherFromPath($fragmentPath, $contextPath); } catch (ExceptionNotExists $e) { throw new ExceptionNotFound("The fragment path ($fragmentPath) was no found"); } return $this->fetcherFragment; } public function getElementId(): string { return $this->elementId; } public function getPathName() { return self::getPathNameFromElementId($this->getElementId()); } }