Skip to content

Commit

Permalink
Add methods to append / prepend datasources to an AutoDatasource inst…
Browse files Browse the repository at this point in the history
…ance

Used by the Blocks plugin to avoid nesting an autodatasource inside of another autodatasource.
  • Loading branch information
LukeTowers committed Sep 27, 2023
1 parent f01103a commit cfb8cdf
Showing 1 changed file with 40 additions and 44 deletions.
84 changes: 40 additions & 44 deletions modules/cms/classes/AutoDatasource.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php namespace Cms\Classes;
<?php

namespace Cms\Classes;

use ApplicationException;
use Cache;
use Exception;
use ApplicationException;
use Winter\Storm\Halcyon\Model;
use Winter\Storm\Halcyon\Processors\Processor;
use Winter\Storm\Halcyon\Datasource\Datasource;
use Winter\Storm\Halcyon\Exception\DeleteFileException;
use Winter\Storm\Halcyon\Datasource\DatasourceInterface;
use Winter\Storm\Halcyon\Exception\DeleteFileException;
use Winter\Storm\Halcyon\Model;
use Winter\Storm\Halcyon\Processors\Processor;

/**
* Datasource that loads from other data sources automatically
Expand Down Expand Up @@ -68,6 +70,28 @@ public function __construct(array $datasources, ?string $cacheKey = null)
$this->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
*/
Expand All @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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 = [];

Expand All @@ -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.');
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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) {
Expand Down Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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];
}
Expand Down

0 comments on commit cfb8cdf

Please sign in to comment.