diff --git a/consolecommands/SchematicCommand.php b/consolecommands/SchematicCommand.php index 5b5f5fbe..e9876aa1 100644 --- a/consolecommands/SchematicCommand.php +++ b/consolecommands/SchematicCommand.php @@ -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"; @@ -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); } } diff --git a/models/Schematic_ExportedDataModel.php b/models/Schematic_DataModel.php similarity index 60% rename from models/Schematic_ExportedDataModel.php rename to models/Schematic_DataModel.php index 199f7b3f..2a132637 100644 --- a/models/Schematic_ExportedDataModel.php +++ b/models/Schematic_DataModel.php @@ -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. * @@ -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. * @@ -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); } } diff --git a/services/SchematicService.php b/services/SchematicService.php index e21e40a7..04d5eea2 100644 --- a/services/SchematicService.php +++ b/services/SchematicService.php @@ -16,83 +16,69 @@ 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); } } @@ -100,11 +86,11 @@ private function importFromExportedDataModel(Schematic_ExportedDataModel $model, } /** - * Export. + * Export data model. * * @return array */ - public function export() + private function exportDataModel() { $fieldGroups = craft()->fields->getAllGroups(); $sections = craft()->sections->getAllSections(); @@ -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();