Skip to content

Commit

Permalink
Merge pull request #158 from boboldehampsink/map_source_event
Browse files Browse the repository at this point in the history
Map source event
  • Loading branch information
bvangennep authored Sep 11, 2018
2 parents 280b95b + 750187e commit 97ab687
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 4.0.16 - 2018-09-10
### Added
- Added map_source event for mapping custom sources. See the Events section in the README for more.

## 4.0.15 - 2018-08-31
### Added
- Schematic can now parse environment variables in the schema file directly, without need for an override file
Expand Down
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ Then the environment variable `KEY_VALUE` needs to be set. The value of this env

Environment variables can also directly be used in the `schema.yml` file. Beware that if you do that, they will be overriden on export by their environment variable's values.

### Hooks
### Events

Custom converters can be injected with the `EVENT_RESOLVE_CONVERTER` event.
This can be especially useful for importing and exporting custom field types.
Expand All @@ -147,6 +147,22 @@ Event::on(Schematic::class, Schematic::EVENT_RESOLVE_CONVERTER, function (Conver
});
```

Custom source mappings can be injected with the `EVENT_MAP_SOURCE` event.
This can be especially useful for importing and exporting custom sources.

```php
Event::on(Schematic::class, Schematic::EVENT_MAP_SOURCE, function (SourceMappingEvent $event) {
list($sourceType, $sourceFrom) = explode(':', $event->source);
switch ($sourceType) {
case 'plugin-source':
$event->service = Craft::$app->customService;
$event->method = 'getCustomModelBy';
break;
}
});
```

### Caveats

Schematic uses handles to identify content. When a handle is changed in the schema file and import is run with force a new content type will be created with that handle, and the old content type will be deleted!
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "4.0.15",
"version": "4.0.16",
"name": "nerds-and-company/schematic",
"description": "Craft setup and sync tool",
"type": "craft-plugin",
Expand Down
14 changes: 14 additions & 0 deletions src/Behaviors/SourcesBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use yii\base\Behavior;
use craft\base\Model;
use NerdsAndCompany\Schematic\Schematic;
use NerdsAndCompany\Schematic\Events\SourceMappingEvent;

/**
* Schematic Sources Behavior.
Expand Down Expand Up @@ -94,6 +95,7 @@ public function getSource(string $fieldType, string $source = null, string $inde
/** @var Model $sourceObject */
$sourceObject = null;

// Get service and method by source
list($sourceType, $sourceFrom) = explode(':', $source);
switch ($sourceType) {
case 'editSite':
Expand Down Expand Up @@ -144,6 +146,18 @@ public function getSource(string $fieldType, string $source = null, string $inde
return $source;
}

// Send event
$plugin = Craft::$app->controller->module;
$event = new SourceMappingEvent([
'source' => $source,
'service' => $service ?? null,
'method' => $method ?? null,
]);
$plugin->trigger($plugin::EVENT_MAP_SOURCE, $event);
$service = $event->service;
$method = $event->method;

// Try service and method
if (isset($service) && isset($method) && isset($sourceFrom)) {
$method = $method.ucfirst($indexFrom);
try {
Expand Down
3 changes: 2 additions & 1 deletion src/Controllers/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ protected function getDataTypes(): array
$dataTypes = $this->applyExcludes($dataTypes);
}

//Import fields again after all sources have been imported
//Import fields and usergroups again after all sources have been imported
if (array_search('fields', $dataTypes) && count($dataTypes) > 1) {
$dataTypes[] = 'fields';
$dataTypes[] = 'userGroups';
}

return $dataTypes;
Expand Down
34 changes: 34 additions & 0 deletions src/Events/SourceMappingEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace NerdsAndCompany\Schematic\Events;

use yii\base\Event;

/**
* Schematic Source Mapping Event.
*
* Sync Craft Setups.
*
* @author Nerds & Company
* @copyright Copyright (c) 2015-2018, Nerds & Company
* @license MIT
*
* @see http://www.nerds.company
*/
class SourceMappingEvent extends Event
{
/**
* @var string
*/
public $source;

/**
* @var string
*/
public $service;

/**
* @var string
*/
public $method;
}
1 change: 1 addition & 0 deletions src/Schematic.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
class Schematic extends Plugin
{
const EVENT_RESOLVE_CONVERTER = 'resolve_converter';
const EVENT_MAP_SOURCE = 'map_source';

/**
* @var string
Expand Down

0 comments on commit 97ab687

Please sign in to comment.