diff --git a/modules/cms/classes/AutoDatasource.php b/modules/cms/classes/AutoDatasource.php index c554b2f9ee..f68fb1861c 100644 --- a/modules/cms/classes/AutoDatasource.php +++ b/modules/cms/classes/AutoDatasource.php @@ -1,13 +1,15 @@ -postProcessor = new Processor; } + /** + * Append a datasource to the end of the list of datasources + */ + public function appendDatasource(string $key, DatasourceInterface $datasource): void + { + $this->datasources[$key] = $datasource; + $this->pathCache[] = Cache::rememberForever($datasource->getPathsCacheKey(), function () use ($datasource) { + return $datasource->getAvailablePaths(); + }); + } + + /** + * Prepend a datasource to the beginning of the list of datasources + */ + public function prependDatasource(string $key, DatasourceInterface $datasource): void + { + $this->datasources = array_prepend($this->datasources, $datasource, $key); + $this->pathCache = array_prepend($this->pathCache, Cache::rememberForever($datasource->getPathsCacheKey(), function () use ($datasource) { + return $datasource->getAvailablePaths(); + }), $key); + } + /** * Returns the in memory path cache map */ @@ -80,9 +104,8 @@ public function getPathCache(): array * Populate the local cache of paths available in each datasource * * @param boolean $refresh Default false, set to true to force the cache to be rebuilt - * @return void */ - public function populateCache($refresh = false) + public function populateCache(bool $refresh = false): void { $pathCache = []; foreach ($this->datasources as $datasource) { @@ -108,12 +131,8 @@ public function populateCache($refresh = false) /** * Check to see if the specified datasource has the provided Halcyon Model - * - * @param string $source The string key of the datasource to check - * @param Model $model The Halcyon Model to check for - * @return boolean */ - public function sourceHasModel(string $source, Model $model) + public function sourceHasModel(string $source, Model $model): bool { if (!$model->exists) { return false; @@ -141,11 +160,8 @@ public function sourceHasModel(string $source, Model $model) /** * Get the available paths for the specified datasource key - * - * @param string $source The string key of the datasource to check - * @return void */ - public function getSourcePaths(string $source) + public function getSourcePaths(string $source): array { $result = []; @@ -164,11 +180,9 @@ public function getSourcePaths(string $source) /** * Forces all operations in a provided closure to run within a selected datasource. * - * @param string $source - * @param \Closure $closure - * @return mixed + * @throws ApplicationException if the provided datasource key doesn't exist */ - public function usingSource(string $source, \Closure $closure) + public function usingSource(string $source, \Closure $closure): mixed { if (!array_key_exists($source, $this->datasources)) { throw new ApplicationException('Invalid datasource specified.'); @@ -191,12 +205,8 @@ public function usingSource(string $source, \Closure $closure) /** * Push the provided model to the specified datasource - * - * @param Model $model The Halcyon Model to push - * @param string $source The string key of the datasource to use - * @return void */ - public function pushToSource(Model $model, string $source) + public function pushToSource(Model $model, string $source): void { $this->usingSource($source, function () use ($model) { $datasource = $this->getActiveDatasource(); @@ -215,12 +225,8 @@ public function pushToSource(Model $model, string $source) /** * Remove the provided model from the specified datasource - * - * @param Model $model The Halcyon model to remove - * @param string $source The string key of the datasource to use - * @return void */ - public function removeFromSource(Model $model, string $source) + public function removeFromSource(Model $model, string $source): void { $this->usingSource($source, function () use ($model) { $datasource = $this->getActiveDatasource(); @@ -236,11 +242,8 @@ public function removeFromSource(Model $model, string $source) /** * Get the appropriate datasource for the provided path - * - * @param string $path - * @return Datasource */ - protected function getDatasourceForPath(string $path) + protected function getDatasourceForPath(string $path): DatasourceInterface { // Always return the active datasource when singleDatasourceMode is enabled if ($this->singleDatasourceMode) { @@ -283,7 +286,7 @@ protected function getDatasourceForPath(string $path) * ]; * @return array $paths ["$dirName/path/1.md", "$dirName/path/2.md"] */ - protected function getValidPaths(string $dirName, array $options = []) + protected function getValidPaths(string $dirName, array $options = []): array { // Initialize result set $paths = []; @@ -325,23 +328,16 @@ protected function getValidPaths(string $dirName, array $options = []) /** * Helper to make file path. - * - * @param string $dirName - * @param string $fileName - * @param string $extension - * @return string */ - protected function makeFilePath(string $dirName, string $fileName, string $extension) + protected function makeFilePath(string $dirName, string $fileName, string $extension): string { return ltrim($dirName . '/' . $fileName . '.' . $extension, '/'); } /** * Get the datasource for use with CRUD operations - * - * @return DatasourceInterface */ - protected function getActiveDatasource() + protected function getActiveDatasource(): DatasourceInterface { return $this->datasources[$this->activeDatasourceKey]; }