Refactor ThemeSync console command

This commit is contained in:
Samuel Georges 2019-06-01 13:42:29 +10:00
parent 8c398e7ad5
commit 6f021d5f20
1 changed files with 21 additions and 22 deletions

View File

@ -17,7 +17,7 @@ use Symfony\Component\Console\Input\InputArgument;
* *
* - name defaults to the currently active theme * - 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 * - --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 * - --target defaults to "filesystem", the source will whichever of filesystem vs database is not the target
* - --force bypasses the confirmation request * - --force bypasses the confirmation request
* *
* @package october\system * @package october\system
@ -90,13 +90,13 @@ class ThemeSync extends Command
// Get the target and source datasources // Get the target and source datasources
$availableSources = ['filesystem', 'database']; $availableSources = ['filesystem', 'database'];
$target = $this->option('target') ?: 'database'; $target = $this->option('target') ?: 'filesystem';
$source = 'filesystem'; $source = 'filesystem';
if ($target === 'filesystem') { if ($target === 'filesystem') {
$source = 'database'; $source = 'database';
} }
if (!in_array($target, $availableSources)) { if (!in_array($target, $availableSources)) {
return $this->error(sprintf("Provided --target of %s is invalid. Allowed: database, filesystem", $target)); return $this->error(sprintf("Provided --target of %s is invalid. Allowed: filesystem, database", $target));
} }
$this->source = $source; $this->source = $source;
$this->target = $target; $this->target = $target;
@ -107,7 +107,8 @@ class ThemeSync extends Command
if (!isset($userPaths)) { if (!isset($userPaths)) {
$paths = $themePaths; $paths = $themePaths;
} else { }
else {
$paths = []; $paths = [];
$userPaths = array_map('trim', explode(',', $userPaths)); $userPaths = array_map('trim', explode(',', $userPaths));
@ -121,8 +122,6 @@ class ThemeSync extends Command
} }
} }
} }
unset($userPaths);
unset($themePaths);
// Determine valid paths based on the models made available for syncing // Determine valid paths based on the models made available for syncing
$validPaths = []; $validPaths = [];
@ -144,24 +143,22 @@ class ThemeSync extends Command
* }); * });
* *
*/ */
$results = Event::fire('system.console.theme.sync.getAvailableModelClasses'); $eventResults = Event::fire('system.console.theme.sync.getAvailableModelClasses');
$validModels = []; $validModels = [];
foreach ($results as $result) { foreach ($eventResults as $result) {
if (!is_iterable($result)) { if (!is_array($result)) {
continue; continue;
} }
foreach ($result as $model) { foreach ($result as $modelClass) {
$class = new $model; $modelObj = new $modelClass;
if ($class instanceof \October\Rain\Halcyon\Model) { if ($modelObj instanceof \October\Rain\Halcyon\Model) {
$validModels[] = $class; $validModels[] = $modelObj;
} }
} }
unset($class);
} }
unset($results);
// Check each path and map it to a corresponding model // Check each path and map it to a corresponding model
foreach ($paths as $path) { foreach ($paths as $path) {
@ -178,8 +175,6 @@ class ThemeSync extends Command
} }
} }
} }
unset($paths);
unset($validModels);
if (count($validPaths) === 0) { if (count($validPaths) === 0) {
return $this->error(sprintf('No applicable paths found for %s.', $source)); return $this->error(sprintf('No applicable paths found for %s.', $source));
@ -205,7 +200,6 @@ class ThemeSync extends Command
$this->datasource->pushToSource($entity, $target); $this->datasource->pushToSource($entity, $target);
$progress->advance(); $progress->advance();
} }
unset($validPaths);
$progress->finish(); $progress->finish();
$this->info(''); $this->info('');
@ -225,13 +219,18 @@ class ThemeSync extends Command
* @param \Cms\Classes\Theme $theme * @param \Cms\Classes\Theme $theme
* @return \October\Rain\Halycon\Model * @return \October\Rain\Halycon\Model
*/ */
protected function getModelForPath(string $path, string $model, \Cms\Classes\Theme $theme) protected function getModelForPath($path, $modelClass, $theme)
{ {
$originalSource = $this->datasource->activeDatasourceKey; $originalSource = $this->datasource->activeDatasourceKey;
$this->datasource->activeDatasourceKey = $this->source; $this->datasource->activeDatasourceKey = $this->source;
$class = new $model; $modelObj = new $modelClass;
$entity = $model::load($theme, str_replace($class->getObjectTypeDirName() . '/', '', $path));
$entity = $modelClass::load(
$theme,
str_replace($modelObj->getObjectTypeDirName() . '/', '', $path)
);
if (!isset($entity)) { if (!isset($entity)) {
return null; return null;
} }
@ -260,7 +259,7 @@ class ThemeSync extends Command
{ {
return [ return [
['paths', null, InputOption::VALUE_REQUIRED, 'Comma-separated specific paths (relative to provided theme directory) to specificaly sync. Default is all paths. You may use regular expressions.'], ['paths', null, InputOption::VALUE_REQUIRED, 'Comma-separated specific paths (relative to provided theme directory) to specificaly sync. Default is all paths. You may use regular expressions.'],
['target', null, InputOption::VALUE_REQUIRED, 'The target of the sync, the other will be used as the source. Defaults to "database", can be "filesystem"'], ['target', null, InputOption::VALUE_REQUIRED, 'The target of the sync, the other will be used as the source. Defaults to "filesystem", can be "database"'],
['force', null, InputOption::VALUE_NONE, 'Force the operation to run.'], ['force', null, InputOption::VALUE_NONE, 'Force the operation to run.'],
]; ];
} }