/** * Creates a new placed widget. Won't be saved automatically, save it to a * widget repository. * @param string $baseDirectory Base directory of all widgets. * @param string $dirName Name of the widget. * @param Document $document The document the widget is placed in. * @return PlacedWidget The placed widget. * @throws NotFoundException If no widget exists with the given dirName. */ public static function newPlacedWidget($baseDirectory, $dirName, Document $document) { $placedWidget = new PlacedWidget($baseDirectory); $placedWidget->setDocumentId($document->getId()); $placedWidget->widgetName = (string) $dirName; $placedWidget->id = 0; if (!file_exists($placedWidget->getWidgetCodeFile())) { throw new NotFoundException(); } return $placedWidget; }
/** * Gets the widget definition (containing the actual behaviour of the widget) * from the placed widget. * * If the widget code has been uninstalled, an instance of `NullWidget` is * returned. * @param PlacedWidget $placedWidget The placed widget. * @return WidgetDefinition The widget definition. * @throws LogicException If the widget code contains an error. */ public function getDefinition(PlacedWidget $placedWidget) { $dirName = $placedWidget->getDirectoryName(); if (!preg_match('/^[A-Za-z0-9\\-_]+$/', $dirName)) { // Not a valid name return new NullWidget("invalidName"); } // Load the widget if (!isset($this->loadedWidgets[$dirName])) { $this->currentlyLoadingWidgetName = $dirName; $file = $placedWidget->getWidgetCodeFile(); if (!file_exists($file)) { return new NullWidget($dirName); } require $file; $this->currentlyLoadingWidgetName = ""; } // Check if loaded if (!isset($this->loadedWidgets[$dirName])) { throw new LogicException("Widget {$dirName} contains an error: it didn't register itself."); } return $this->loadedWidgets[$dirName]; }