|
<?php // copy everything after this line |
|
|
|
use Illuminate\Support\Facades\Artisan; |
|
use Statamic\Facades\File; |
|
use Statamic\Facades\YAML; |
|
|
|
class MigrateBetaFortyFive |
|
{ |
|
protected $console; |
|
|
|
public function __construct($console) |
|
{ |
|
$this->console = $console; |
|
} |
|
|
|
public function migrate() |
|
{ |
|
$this->console->line('<comment>[!]</comment> Any <comment>type: taxonomy</comment> fields will be renamed to <comment>type: terms</comment> and their <comment>taxonomy:</comment> options will be renamed to <comment>taxonomies</comment>.'); |
|
$this->console->line('<comment>[!]</comment> Any <comment>type: tags</comment> fields will be renamed to <comment>type: taggable</comment>.'); |
|
|
|
$blueprints = File::getFilesByTypeRecursively(resource_path('blueprints'), 'yaml'); |
|
$fieldsets = File::getFilesByTypeRecursively(resource_path('fieldsets'), 'yaml'); |
|
$files = $blueprints->merge($fieldsets); |
|
|
|
$converted = $files->reduce(function ($carry, $path) { |
|
$yaml = YAML::file($path)->parse(); |
|
$newYaml = $this->updateYaml($yaml); |
|
if ($yaml != $newYaml) { |
|
File::put($path, YAML::dump($newYaml)); |
|
$this->console->line("<info>[✓]</info> $path"); |
|
return ++$carry; |
|
} |
|
return $carry; |
|
}, 0); |
|
|
|
$this->console->info($converted . ' file(s) updated.'); |
|
} |
|
|
|
protected function updateYaml($array) |
|
{ |
|
foreach ($array as $key => &$value) { |
|
if (is_array($value)) { |
|
$value = $this->updateYaml($value); |
|
} |
|
if ($key === 'type' && $value == 'taxonomy') { |
|
$array = $this->replaceTaxonomy($array); |
|
} |
|
if ($key === 'type' && $value == 'tags') { |
|
$array['type'] = 'taggable'; |
|
} |
|
} |
|
|
|
return $array; |
|
} |
|
|
|
protected function replaceTaxonomy($array) |
|
{ |
|
if (isset($array['taxonomy'])) { |
|
$array['taxonomies'] = $array['taxonomy']; |
|
unset($array['taxonomy']); |
|
} |
|
$array['type'] = 'terms'; |
|
return $array; |
|
} |
|
} |
|
|
|
Artisan::command('migrate:beta45', function () { |
|
(new MigrateBetaFortyFive($this))->migrate(); |
|
}); |