Initial implementation attempt for theme:sync command

This commit is contained in:
Luke Towers 2018-12-16 09:43:33 -06:00
parent a7bbfe2d9d
commit 5f78fd4df9
2 changed files with 148 additions and 16 deletions

View File

@ -34,7 +34,7 @@ class AutoDatasource extends Datasource implements DatasourceInterface
/**
* @var string The key for the datasource to perform CRUD operations on
*/
protected $activeDatasourceKey = '';
public $activeDatasourceKey = '';
/**
* Create a new datasource instance.
@ -87,11 +87,9 @@ class AutoDatasource extends Datasource implements DatasourceInterface
{
$result = false;
$keys = array_keys($this->datasources);
if (in_array($source, $keys)) {
// Get the datasource's cache index key
$cacheIndex = array_search($source, $keys);
$sourcePaths = $this->getSourcePaths($source);
if (!empty($paths)) {
// Generate the path
list($name, $extension) = $model->getFileNameParts();
$path = $this->makeFilePath($model->getObjectTypeDirName(), $name, $extension);
@ -99,7 +97,7 @@ class AutoDatasource extends Datasource implements DatasourceInterface
// Deleted paths are included as being handled by a datasource
// The functionality built on this will need to make sure they
// include deleted records when actually performing sycning actions
if (isset($this->pathCache[$cacheIndex][$path])) {
if (isset($sourcePaths[$path])) {
$result = true;
}
}
@ -107,6 +105,28 @@ class AutoDatasource extends Datasource implements DatasourceInterface
return $result;
}
/**
* 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)
{
$result = [];
$keys = array_keys($this->datasources);
if (in_array($source, $keys)) {
// Get the datasource's cache index key
$cacheIndex = array_search($source, $keys);
// Return the available paths
$result = $this->pathCache[$cacheIndex];
}
return $result;
}
/**
* Push the provided model to the specified datasource
*

View File

@ -1,10 +1,17 @@
<?php namespace System\Console;
use App;
use Config;
use Event;
use Exception;
use Cms\Classes\Theme;
use Cms\Classes\ThemeManager;
use Cms\Classes\Meta;
use Cms\Classes\Page;
use Cms\Classes\Layout;
use Cms\Classes\Content;
use Cms\Classes\Partial;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
@ -13,7 +20,7 @@ use Symfony\Component\Console\Input\InputArgument;
* Console command to sync a theme between the DB and Filesystem layers.
*
* theme:sync name --paths=file/to/sync.md,other/file/to/sync.md --target=filesystem --force
*
*
* - name defaults to the currently active theme
* - --paths defaults to all paths within the theme, otherwise comma-separated list of paths relative to the theme directory
* - --target defaults to "database", the source will whichever of filesystem vs database is not the target
@ -38,6 +45,26 @@ class ThemeSync extends Command
*/
protected $description = 'Sync an existing theme between the DB and Filesystem layers';
/**
* @var \October\Rain\Datasource\DatasourceInterface The theme's AutoDatasource instance
*/
protected $datasource;
/**
* @var string The datasource key that the sync is targeting
*/
protected $target;
/**
* @var string The datasource key that the sync is sourcing from
*/
protected $source;
/**
* @var array Models
*/
protected $halyconModels = [];
/**
* Execute the console command.
* @return void
@ -50,11 +77,7 @@ class ThemeSync extends Command
}
// Check to see if the DB layer is enabled
$enableDbLayer = Config::get('cms.enableDatabaseLayer', false);
if (is_null($enableDbLayer)) {
$enableDbLayer = !Config::get('app.debug');
}
if (!$enableDbLayer) {
if (!Theme::databaseLayerEnabled()) {
return $this->error("cms.enableDatabaseLayer is not enabled, enable it first and try again.");
}
@ -69,6 +92,7 @@ class ThemeSync extends Command
if (!$themeExists) {
return $this->error(sprintf('The theme %s does not exist.', $themeName));
}
$theme = Theme::load($themeName);
// Get the target and source datasources
$availableSources = ['filesystem', 'database'];
@ -80,28 +104,116 @@ class ThemeSync extends Command
if (!in_array($target, $availableSources)) {
$this->error(sprintf("Provided --target of %s is invalid. Allowed: database, filesystem"), $target);
}
$this->source = $source;
$this->target = $target;
// Get the paths
// @TODO: Use the model classes to call listInTheme instead to get all handled paths instead of the other way round
// @TODO: Will probably have to interact directly with the datasources to do the syncing, not sure how much AutoDatasource will be useful here
$paths = $this->option('paths') ?: null;
if ($paths) {
$paths = array_map('trim', explode(',', $paths));
} else {
$paths = $theme->getDatasource()->getSourcePaths($source);
// Get the Halcyon model classes to use when filtering the paths to be synced
$validModels = [];
$modelClasses = [
Meta::class,
Page::class,
Layout::class,
Content::class,
Partial::class,
];
/**
* @event system.console.theme.sync.getModelClasses
* Get the Halcyon model classes to use when filtering the paths to be synced
*
* Example usage:
*
* Event::listen('system.console.theme.sync.getModelClasses', function () {
* return [
* Meta::class,
* Page::class,
* Layout::class,
* Content::class,
* Partial::class,
* ];
* });
*
*/
$results = Event::fire('system.console.theme.sync.getAvailableModelClasses');
foreach ($results as $result) {
$modelClasses += $result;
}
foreach ($modelClasses as $class) {
$validModels[] = new $class;
}
foreach ($paths as $path => $exists) {
foreach ($validModels as $model) {
if (starts_with($path, $model->getObjectTypeDirName() . '/') &&
in_array(pathinfo($path, PATHINFO_EXTENSION), $model->getAllowedExtensions())
) {
// Skip to the next path
continue 2;
}
}
// If we've made it here, this path doesn't get to proceed
unset($paths[$path]);
}
unset($validModels);
}
// Confirm with the user
if (!$this->confirmToProceed(sprintf('This will REPLACE the provided paths from "themes/%s" on the %s with content from the %s', $themeName, $target, $source), function () { return true; })) {
if (!$this->confirmToProceed(sprintf('This will OVERWRITE the %s provided paths in "themes/%s" on the %s with content from the %s', count($paths), $themeName, $target, $source), function () { return true; })) {
return;
}
try {
// TODO: Actually implement the functionality
$this->info('Syncing files, please wait...');
$progress = $this->output->createProgressBar(count($paths));
$this->info(sprintf('The theme %s has been synced from the %s to the %s.', $themeName, $source, $target));
$this->datasource = $theme->getDatasource();
foreach ($paths as $path) {
// $this->datasource->pushToSource($this->getModelForPath($path), $target);
$progress->advance();
}
$progress->finish();
$this->info('');
$this->info(sprintf('The theme %s has been successfully synced from the %s to the %s.', $themeName, $source, $target));
}
catch (Exception $ex) {
$this->error($ex->getMessage());
}
}
/**
* Get the correct Halcyon model for the provided path from the source datasource
*
* @param string $path
* @return void
*/
protected function getModelForPath(string $path)
{
$originalSource = $this->datasource->activeDatasourceKey;
$this->datasource->activeDatasourceKey = $this->source;
// $model::load($this->theme, $fileName);
$this->datasource->activeDatasourceKey = $originalSource;
// return $model;
}
/**
* Get the console command arguments.
* @return array