Skip to content

Commit

Permalink
Refactored import/export with yaml support
Browse files Browse the repository at this point in the history
  • Loading branch information
Bob Olde Hampsink committed Oct 19, 2015
1 parent 8c544ac commit c7045e6
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 85 deletions.
15 changes: 6 additions & 9 deletions consolecommands/SchematicCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,17 @@ class SchematicCommand extends BaseCommand
/**
* Imports the Craft datamodel.
*
* @param string $file json file containing the schema definition
* @param string $file yml file containing the schema definition
* @param bool $force if set to true items not in the import will be deleted
*/
public function actionImport($file = 'craft/config/schema.json', $force = false)
public function actionImport($file = 'craft/config/schema.yml', $force = false)
{
if (!IOHelper::fileExists($file)) {
$this->usageError(Craft::t('File not found.'));
exit(1);
}

$json = IOHelper::getFileContents($file);

$result = craft()->schematic->importFromJson($json, $force);
$result = craft()->schematic->importFromYaml($file, $force);

if (!$result->hasErrors()) {
echo Craft::t('Loaded schema from {file}', array('file' => $file))."\n";
Expand All @@ -47,12 +45,11 @@ public function actionImport($file = 'craft/config/schema.json', $force = false)
*
* @param string $file file to write the schema to
*/
public function actionExport($file = 'craft/config/schema.json')
public function actionExport($file = 'craft/config/schema.yml')
{
$schema = craft()->schematic->export();

IOHelper::writeToFile($file, json_encode($schema, JSON_PRETTY_PRINT));
craft()->schematic->exportToYaml($file);

echo Craft::t('Exported schema to {file}', array('file' => $file))."\n";
exit(0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Craft;

use Symfony\Component\Yaml\Yaml;

/**
* Schematic Exported Data Model.
* Schematic Data Model.
*
* Encapsulates data that has been exported via schematic.
*
Expand All @@ -13,22 +15,8 @@
*
* @link http://www.itmundi.nl
*/
class Schematic_ExportedDataModel extends BaseModel
class Schematic_DataModel extends BaseModel
{
/**
* Creates an Schematic_ExportedDataModel from JSON input.
*
* @param string $json The input JSON.
*
* @return Schematic_ExportedDataModel|null The new Schematic_ExportedDataMode on success, null on invalid JSON.
*/
public static function fromJson($json)
{
$data = json_decode($json, true);

return $data === null ? null : new static($data);
}

/**
* Define attributes.
*
Expand All @@ -48,12 +36,30 @@ protected function defineAttributes()
}

/**
* Returns a JSON representation of this model.
* Populate data model from yaml.
*
* @param string $yaml
*
* @return string
* @return Schematic_DataModel
*/
public function toJson()
public static function fromYaml($yaml)
{
return json_encode($this->getAttributes(), JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK);
$data = Yaml::parse($yaml);

return $data === null ? null : new static($data);
}

/**
* Populate yaml from data model.
*
* @param string $yaml
*
* @return Schematic_DataModel
*/
public static function toYaml($data)
{
$data = $data === null ? null : new static($data);

return Yaml::dump($data->attributes, 12);
}
}
98 changes: 42 additions & 56 deletions services/SchematicService.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,95 +16,81 @@
class SchematicService extends BaseApplicationComponent
{
/**
* Import from JSON.
* Import from Yaml file.
*
* @param string $json
* @param string $file
* @param bool $force if set to true items not included in import will be deleted
*
* @return Schematic_ResultModel
*/
public function importFromJson($json, $force = false)
public function importFromYaml($file, $force = false)
{
$exportedDataModel = Schematic_ExportedDataModel::fromJson($json);
$yaml = IOHelper::getFileContents($file);
$datamodel = Schematic_DataModel::fromYaml($yaml);

return $this->importFromExportedDataModel($exportedDataModel, $force);
return $this->importDataModel($datamodel, $force);
}

/**
* Load from JSON.
* Export to Yaml file.
*
* @param string $json
* @param string $file
*
* @return string
* @return bool
*/
public function loadFromJson($json)
public function exportToYaml($file)
{
$data = Schematic_ExportedDataModel::fromJson($json);
$datamodel = $this->exportDataModel();
$yaml = Schematic_DataModel::toYaml($datamodel);

return $data;
return IOHelper::writeToFile($file, $yaml);
}

/**
* Import from array.
* Import data model.
*
* @param array $array
* @param bool $force if set to true items not included in import will be deleted
* @param array $model
* @param bool $force if set to true items not in the import will be deleted
*
* @return Schematic_ResultModel
*/
public function importFromArray(array $array, $force = false)
{
$exportedDataModel = new Schematic_ExportedDataModel($array);

return $this->importFromExportedDataModel($exportedDataModel, $force);
}

/**
* Import from exported data model.
*
* @param Schematic_ExportedDataModel $model
* @param bool $force if set to true items not in the import will be deleted
*
* @return Schematic_ResultModel
*/
private function importFromExportedDataModel(Schematic_ExportedDataModel $model, $force)
private function importDataModel(array $model, $force)
{
// Import schema
$pluginImportResult = craft()->schematic_plugins->import($model->plugins);
$assetImportResult = craft()->schematic_assets->import($model->assets);
$fieldImportResult = craft()->schematic_fields->import($model->fields, $force);
$globalImportResult = craft()->schematic_globals->import($model->globals, $force);
$sectionImportResult = craft()->schematic_sections->import($model->sections, $force);
$userGroupImportResult = craft()->schematic_userGroups->import($model->userGroups, $force);

// Verify results
$result = new Schematic_ResultModel();

if ($model !== null) {
$pluginImportResult = craft()->schematic_plugins->import($model->plugins);
$assetImportResult = craft()->schematic_assets->import($model->assets);
$fieldImportResult = craft()->schematic_fields->import($model->fields, $force);
$globalImportResult = craft()->schematic_globals->import($model->globals, $force);
$sectionImportResult = craft()->schematic_sections->import($model->sections, $force);
$userGroupImportResult = craft()->schematic_userGroups->import($model->userGroups, $force);

$result->consume($pluginImportResult);
$result->consume($assetImportResult);
$result->consume($fieldImportResult);
$result->consume($globalImportResult);
$result->consume($sectionImportResult);
$result->consume($userGroupImportResult);

// run plugin imports through hook
$services = craft()->plugins->callFirst('registerMigrationService');
if (is_array($services)) {
foreach ($services as $handle => $service) {
$hookResult = $service->import($model->pluginData[$handle], $force);
$result->consume($hookResult);
}
$result->consume($pluginImportResult);
$result->consume($assetImportResult);
$result->consume($fieldImportResult);
$result->consume($globalImportResult);
$result->consume($sectionImportResult);
$result->consume($userGroupImportResult);

// Run plugin imports through hook
$services = craft()->plugins->callFirst('registerMigrationService');
if (is_array($services)) {
foreach ($services as $handle => $service) {
$hookResult = $service->import($model->pluginData[$handle], $force);
$result->consume($hookResult);
}
}

return $result;
}

/**
* Export.
* Export data model.
*
* @return array
*/
public function export()
private function exportDataModel()
{
$fieldGroups = craft()->fields->getAllGroups();
$sections = craft()->sections->getAllSections();
Expand All @@ -120,7 +106,7 @@ public function export()
'userGroups' => craft()->schematic_userGroups->export($userGroups),
);

// run plugin exports through hook
// Run plugin exports through hook
$services = craft()->plugins->callFirst('registerMigrationService');
if (is_array($services)) {
$export['pluginData'] = array();
Expand Down

0 comments on commit c7045e6

Please sign in to comment.