Skip to content

Commit

Permalink
Added support for importing/exporting asset transforms (#90)
Browse files Browse the repository at this point in the history
Added support for importing/exporting asset transforms
  • Loading branch information
Bob Olde Hampsink authored Apr 12, 2017
1 parent e193c72 commit 4949c64
Show file tree
Hide file tree
Showing 9 changed files with 447 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
###3.7.0###
- Added support for importing/exporting Asset Transforms
- Asset Sources now respect the force option

###3.6.1###
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Here is a list of all of the data types and their corresponding exclude paramete
| Data Type | Exlude Parameter |
| ------------- |-------------|
| Asset Sources | assetSources |
| Asset Transforms | assetTransforms |
| Category Groups | categoryGroups |
| Element Indexes | elementIndexSettings |
| Fields | fields |
Expand Down
3 changes: 3 additions & 0 deletions src/Console/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,9 @@ private function _setSchematicComponents()
'schematic_assetSources' => [
'class' => Service\AssetSources::class,
],
'schematic_assetTransforms' => [
'class' => Service\AssetTransforms::class,
],
'schematic_fields' => [
'class' => Service\Fields::class,
],
Expand Down
2 changes: 2 additions & 0 deletions src/Models/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*
* @property array $Locales
* @property array $assetSources
* @property array $assetTransforms
* @property array $fields
* @property array $globalSets
* @property array $plugins
Expand All @@ -44,6 +45,7 @@ protected function defineAttributes()
return [
'locales' => [AttributeType::Mixed, 'default' => []],
'assetSources' => [AttributeType::Mixed, 'default' => []],
'assetTransforms' => [AttributeType::Mixed, 'default' => []],
'fields' => [AttributeType::Mixed, 'default' => []],
'globalSets' => [AttributeType::Mixed, 'default' => []],
'plugins' => [AttributeType::Mixed, 'default' => []],
Expand Down
2 changes: 1 addition & 1 deletion src/Services/AssetSources.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Craft\AssetSourceModel;

/**
* Schematic Assets Service.
* Schematic Asset Sources Service.
*
* Sync Craft Setups.
*
Expand Down
152 changes: 152 additions & 0 deletions src/Services/AssetTransforms.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?php

namespace NerdsAndCompany\Schematic\Services;

use Craft\Craft;
use Craft\AssetTransformModel;

/**
* Schematic Asset Transforms Service.
*
* Sync Craft Setups.
*
* @author Nerds & Company
* @copyright Copyright (c) 2015-2017, Nerds & Company
* @license MIT
*
* @see http://www.nerds.company
*/
class AssetTransforms extends Base
{
/**
* @return AssetTransformsService
*/
private function getAssetTransformsService()
{
return Craft::app()->assetTransforms;
}

/**
* Export all asset transforms.
*
* @param AssetTransformModel[] $assetTransforms
*
* @return array
*/
public function export(array $assetTransforms = [])
{
Craft::log(Craft::t('Exporting Asset Transforms'));

$assetTransformDefinitions = [];

foreach ($assetTransforms as $assetTransform) {
$assetTransformDefinitions[$assetTransform->handle] = $this->getAssetTransformDefinition($assetTransform);
}

return $assetTransformDefinitions;
}

/**
* @param AssetTransformModel $assetTransform
*
* @return array
*/
private function getAssetTransformDefinition(AssetTransformModel $assetTransform)
{
return [
'name' => $assetTransform->name,
'width' => $assetTransform->width,
'height' => $assetTransform->height,
'format' => $assetTransform->format,
'dimensionChangeTime' => $assetTransform->dimensionChangeTime,
'mode' => $assetTransform->mode,
'position' => $assetTransform->position,
'quality' => $assetTransform->quality,
];
}

/**
* Import asset transform definitions.
*
* @param array $assetTransformDefinitions
* @param bool $force
*
* @return Result
*/
public function import(array $assetTransformDefinitions, $force = false)
{
Craft::log(Craft::t('Importing Asset Transforms'));

$this->resetCraftAssetTransformsServiceCache();
$assetTransforms = $this->getAssetTransformsService()->getAllTransforms('handle');

foreach ($assetTransformDefinitions as $assetTransformHandle => $assetTransformDefinition) {
$assetTransform = array_key_exists($assetTransformHandle, $assetTransforms)
? $assetTransforms[$assetTransformHandle]
: new AssetTransformModel();

unset($assetTransforms[$assetTransformHandle]);

$this->populateAssetTransform($assetTransform, $assetTransformDefinition, $assetTransformHandle);

if (!$this->getAssetTransformsService()->saveTransform($assetTransform)) { // Save asset transform via craft
$this->addErrors($assetTransform->getAllErrors());

continue;
}
}

if ($force) {
foreach ($assetTransforms as $assetTransform) {
$this->getAssetTransformsService()->deleteTransform($assetTransform->id);
}
}

return $this->getResultModel();
}

/**
* Populate asset transform.
*
* @param AssetTransformModel $assetTransform
* @param array $assetTransformDefinition
* @param string $assetTransformHandle
*
* @return AssetTransformModel
*/
private function populateAssetTransform(AssetTransformModel $assetTransform, array $assetTransformDefinition, $assetTransformHandle)
{
$assetTransform->setAttributes([
'handle' => $assetTransformHandle,
'name' => $assetTransformDefinition['name'],
'width' => $assetTransformDefinition['width'],
'height' => $assetTransformDefinition['height'],
'format' => $assetTransformDefinition['format'],
'dimensionChangeTime' => $assetTransformDefinition['dimensionChangeTime'],
'mode' => $assetTransformDefinition['mode'],
'position' => $assetTransformDefinition['position'],
'quality' => $assetTransformDefinition['quality'],
]);

return $assetTransform;
}

/**
* Reset craft fields service cache using reflection.
*/
private function resetCraftAssetTransformsServiceCache()
{
$obj = $this->getAssetTransformsService();
$refObject = new \ReflectionObject($obj);
if ($refObject->hasProperty('_fetchedAllTransforms')) {
$refProperty = $refObject->getProperty('_fetchedAllTransforms');
$refProperty->setAccessible(true);
$refProperty->setValue($obj, false);
}
if ($refObject->hasProperty('_transformsByHandle')) {
$refProperty = $refObject->getProperty('_transformsByHandle');
$refProperty->setAccessible(true);
$refProperty->setValue($obj, array());
}
}
}
10 changes: 10 additions & 0 deletions src/Services/Schematic.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Schematic extends BaseApplication
protected static $exportableDataTypes = [
'locales',
'assetSources',
'assetTransforms',
'fields',
'plugins',
'sections',
Expand Down Expand Up @@ -127,6 +128,9 @@ private function importDataModel(Data $model, $force)
$assetSources = $model->getAttribute('assetSources');
$assetSourcesImportResult = Craft::app()->schematic_assetSources->import($assetSources, $force);

$assetTransforms = $model->getAttribute('assetTransforms');
$assetTransformsImportResult = Craft::app()->schematic_assetTransforms->import($assetTransforms, $force);

$globalSets = $model->getAttribute('globalSets');
$globalSetsImportResult = Craft::app()->schematic_globalSets->import($globalSets, $force);

Expand Down Expand Up @@ -162,6 +166,7 @@ private function importDataModel(Data $model, $force)
$result->consume($pluginImportResult);
$result->consume($fieldImportResult);
$result->consume($assetSourcesImportResult);
$result->consume($assetTransformsImportResult);
$result->consume($globalSetsImportResult);
$result->consume($sectionImportResult);
$result->consume($categoryGroupImportResult);
Expand Down Expand Up @@ -228,6 +233,7 @@ private function exportDataModel($dataTypes = 'all')
}

$assetSources = Craft::app()->assetSources->getAllSources();
$assetTransforms = Craft::app()->assetTransforms->getAllTransforms();
$categoryGroups = Craft::app()->categories->getAllGroups();
$tagGroups = Craft::app()->tags->getAllTagGroups();

Expand All @@ -241,6 +247,10 @@ private function exportDataModel($dataTypes = 'all')
$export['assetSources'] = Craft::app()->schematic_assetSources->export($assetSources);
}

if (in_array('assetTransforms', $dataTypes)) {
$export['assetTransforms'] = Craft::app()->schematic_assetTransforms->export($assetTransforms);
}

if (in_array('fields', $dataTypes)) {
$fieldGroups = Craft::app()->fields->getAllGroups();
$export['fields'] = Craft::app()->schematic_fields->export($fieldGroups);
Expand Down
Loading

0 comments on commit 4949c64

Please sign in to comment.