Passed the buck to the AutoDatasource to implement pushToSource and removeFromSource, added type hinting to AutoDatasource parameters

This commit is contained in:
Luke Towers 2018-11-23 11:28:34 -06:00
parent 7ebd8b9ffc
commit 878bb890b9
2 changed files with 54 additions and 15 deletions

View File

@ -37,7 +37,7 @@ class AutoDatasource extends Datasource implements DatasourceInterface
* @param array $datasources Array of datasources to utilize. Lower indexes = higher priority ['datasourceName' => $datasource]
* @return void
*/
public function __construct($datasources)
public function __construct(array $datasources)
{
$this->datasources = $datasources;
@ -100,13 +100,37 @@ class AutoDatasource extends Datasource implements DatasourceInterface
return $result;
}
/**
* 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)
{
}
/**
* 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)
{
}
/**
* Get the appropriate datasource for the provided path
*
* @param string $path
* @return Datasource
*/
protected function getDatasourceForPath($path)
protected function getDatasourceForPath(string $path)
{
// Default to the last datasource provided
$datasourceIndex = count($this->datasources) - 1;
@ -144,7 +168,7 @@ class AutoDatasource extends Datasource implements DatasourceInterface
* ];
* @return array $paths ["$dirName/path/1.md", "$dirName/path/2.md"]
*/
protected function getValidPaths($dirName, $options = [])
protected function getValidPaths(string $dirName, $options = [])
{
// Initialize result set
$paths = [];
@ -185,7 +209,7 @@ class AutoDatasource extends Datasource implements DatasourceInterface
* @param string $extension
* @return string
*/
protected function makeFilePath($dirName, $fileName, $extension)
protected function makeFilePath(string $dirName, string $fileName, string $extension)
{
return $dirName . '/' . $fileName . '.' . $extension;
}
@ -209,7 +233,7 @@ class AutoDatasource extends Datasource implements DatasourceInterface
* @param string $extension
* @return mixed
*/
public function selectOne($dirName, $fileName, $extension)
public function selectOne(string $dirName, string $fileName, string $extension)
{
try {
$result = $this->getDatasourceForPath($this->makeFilePath($dirName, $fileName, $extension))->selectOne($dirName, $fileName, $extension);
@ -224,7 +248,7 @@ class AutoDatasource extends Datasource implements DatasourceInterface
* Returns all templates.
*
* @param string $dirName
* @param array $options Array of options, [
* @param array $options Array of options, [
* 'columns' => ['fileName', 'mtime', 'content'], // Only return specific columns
* 'extensions' => ['htm', 'md', 'twig'], // Extensions to search for
* 'fileMatch' => '*gr[ae]y', // Shell matching pattern to match the filename against using the fnmatch function
@ -234,7 +258,7 @@ class AutoDatasource extends Datasource implements DatasourceInterface
* ];
* @return array
*/
public function select($dirName, array $options = [])
public function select(string $dirName, $options = [])
{
// Handle fileName listings through just the cache
if (@$options['columns'] === ['fileName']) {
@ -282,7 +306,7 @@ class AutoDatasource extends Datasource implements DatasourceInterface
* @param string $content
* @return bool
*/
public function insert($dirName, $fileName, $extension, $content)
public function insert(string $dirName, string $fileName, string $extension, string $content)
{
// Insert only on the first datasource
$result = $this->getFirstDatasource()->insert($dirName, $fileName, $extension, $content);
@ -304,7 +328,7 @@ class AutoDatasource extends Datasource implements DatasourceInterface
* @param string $oldExtension Defaults to null
* @return int
*/
public function update($dirName, $fileName, $extension, $content, $oldFileName = null, $oldExtension = null)
public function update(string $dirName, string $fileName, string $extension, string $content, $oldFileName = null, $oldExtension = null)
{
$searchFileName = $oldFileName ?: $fileName;
$searchExt = $oldExtension ?: $extension;
@ -339,7 +363,7 @@ class AutoDatasource extends Datasource implements DatasourceInterface
* @param string $extension
* @return int
*/
public function delete($dirName, $fileName, $extension)
public function delete(string $dirName, string $fileName, string $extension)
{
try {
// Delete from only the first datasource
@ -375,7 +399,7 @@ class AutoDatasource extends Datasource implements DatasourceInterface
* @param string $extension
* @return int
*/
public function lastModified($dirName, $fileName, $extension)
public function lastModified(string $dirName, string $fileName, string $extension)
{
return $this->getDatasourceForPath($this->makeFilePath($dirName, $fileName, $extension))->lastModified($dirName, $fileName, $extension);
}

View File

@ -401,7 +401,10 @@ class Index extends Controller
$template = $this->loadTemplate($type, trim(Request::input('templatePath')));
if ($this->canCommitTemplate($template)) {
// @TODO: Implement commit logic
// Populate the filesystem with the template and then remove it from the db
$datasource = $this->getThemeDatasource();
$datasource->pushToSource($template, 'filesystem');
$datasource->removeFromSource($template, 'database');
Flash::success(Lang::get('cms::lang.editor.commit_success', ['type' => $type]));
}
@ -421,7 +424,9 @@ class Index extends Controller
$template = $this->loadTemplate($type, trim(Request::input('templatePath')));
if ($this->canResetTemplate($template)) {
// @TODO: Implement reset logic
// Remove the template from the DB
$datasource = $this->getThemeDatasource();
$datasource->removeFromSource($template, 'database');
Flash::success(Lang::get('cms::lang.editor.reset_success', ['type' => $type]));
}
@ -461,6 +466,16 @@ class Index extends Controller
return $result;
}
/**
* Get the active theme's datasource
*
* @return \October\Rain\Halcyon\Datasource\DatasourceInterface
*/
protected function getThemeDatasource()
{
return $this->theme->getDatasource();
}
/**
* Check to see if the provided template can be committed
* Only available in debug mode, the DB layer must be enabled, and the template must exist in the database
@ -474,7 +489,7 @@ class Index extends Controller
if (Config::get('app.debug', false) &&
Theme::databaseLayerEnabled() &&
Theme::getActiveTheme()->getDatasource()->sourceHasModel('database', $template)
$this->getThemeDatasource()->sourceHasModel('database', $template)
) {
$result = true;
}
@ -494,7 +509,7 @@ class Index extends Controller
$result = false;
if (Theme::databaseLayerEnabled()) {
$datasource = Theme::getActiveTheme()->getDatasource();
$datasource = $this->getThemeDatasource();
$result = $datasource->sourceHasModel('database', $template) && $datasource->sourceHasModel('filesystem', $template);
}