Skip to content

Commit

Permalink
Merge pull request #5 from itmundi/yaml
Browse files Browse the repository at this point in the history
Refactored import/export with yaml support
  • Loading branch information
Bob Olde Hampsink committed Oct 19, 2015
2 parents 8c544ac + 0a1e426 commit 183b4d9
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 89 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ Schematic allows you to synchronize your Craft setup over multiple environments

## Usage

Make sure you have your latest export stored at `./craft/config/schema.json`.
Make sure you have your latest export stored at `./craft/config/schema.yml`.

Then just run to import...

```
php ./craft/app/etc/console/yiic schematic import
./craft/app/etc/console/yiic schematic import
```

Optionally you can use --force to make the import delete any items which are not in the import file.
WARNING!! This will also delete any related content.

You can also generate a schema.json with
You can also generate a schema.yml with

```
php ./craft/app/etc/console/yiic schematic export
./craft/app/etc/console/yiic schematic export
```

## Hooks
Expand Down
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 183b4d9

Please sign in to comment.