From bbd4c6662252346117781111affcb13c30657980 Mon Sep 17 00:00:00 2001 From: Bob Olde Hampsink Date: Mon, 10 Sep 2018 13:09:15 +0200 Subject: [PATCH 1/7] Added map_source event --- src/Behaviors/SourcesBehavior.php | 14 ++++++++++++++ src/Events/SourceMappingEvent.php | 29 +++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 src/Events/SourceMappingEvent.php diff --git a/src/Behaviors/SourcesBehavior.php b/src/Behaviors/SourcesBehavior.php index 4ca0b2ea..22ef30a0 100644 --- a/src/Behaviors/SourcesBehavior.php +++ b/src/Behaviors/SourcesBehavior.php @@ -7,6 +7,7 @@ use yii\base\Behavior; use craft\base\Model; use NerdsAndCompany\Schematic\Schematic; +use NerdsAndCompany\Schematic\Events\SourceMappingEvent; /** * Schematic Sources Behavior. @@ -21,6 +22,8 @@ */ class SourcesBehavior extends Behavior { + const EVENT_MAP_SOURCE = 'map_source'; + /** * Recursively find sources in definition attributes. * @@ -144,6 +147,17 @@ public function getSource(string $fieldType, string $source = null, string $inde return $source; } + // Send event + $event = new SourceMappingEvent([ + 'service' => $service ?? null, + 'method' => $method ?? null, + ]); + $this->trigger(self::EVENT_MAP_SOURCE, $event); + if ($event->service && $event->method) { + $service = $event->service; + $method = $event->method; + } + if (isset($service) && isset($method) && isset($sourceFrom)) { $method = $method.ucfirst($indexFrom); try { diff --git a/src/Events/SourceMappingEvent.php b/src/Events/SourceMappingEvent.php new file mode 100644 index 00000000..2e65c695 --- /dev/null +++ b/src/Events/SourceMappingEvent.php @@ -0,0 +1,29 @@ + Date: Mon, 10 Sep 2018 13:32:51 +0200 Subject: [PATCH 2/7] Move event trigger to plugin --- src/Behaviors/SourcesBehavior.php | 13 ++++++------- src/Schematic.php | 1 + 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Behaviors/SourcesBehavior.php b/src/Behaviors/SourcesBehavior.php index 22ef30a0..eff82126 100644 --- a/src/Behaviors/SourcesBehavior.php +++ b/src/Behaviors/SourcesBehavior.php @@ -22,8 +22,6 @@ */ class SourcesBehavior extends Behavior { - const EVENT_MAP_SOURCE = 'map_source'; - /** * Recursively find sources in definition attributes. * @@ -97,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': @@ -148,16 +147,16 @@ public function getSource(string $fieldType, string $source = null, string $inde } // Send event + $plugin = Craft::$app->getPlugins()->getPlugin('schematic'); $event = new SourceMappingEvent([ 'service' => $service ?? null, 'method' => $method ?? null, ]); - $this->trigger(self::EVENT_MAP_SOURCE, $event); - if ($event->service && $event->method) { - $service = $event->service; - $method = $event->method; - } + $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 { diff --git a/src/Schematic.php b/src/Schematic.php index e8f707f0..2a6e867f 100644 --- a/src/Schematic.php +++ b/src/Schematic.php @@ -47,6 +47,7 @@ class Schematic extends Plugin { const EVENT_RESOLVE_CONVERTER = 'resolve_converter'; + const EVENT_MAP_SOURCE = 'map_source'; /** * @var string From 89c75eea33e5c37970a95fc5652ab8f60f95eccb Mon Sep 17 00:00:00 2001 From: Bob Olde Hampsink Date: Mon, 10 Sep 2018 13:45:27 +0200 Subject: [PATCH 3/7] Also pass on source --- src/Behaviors/SourcesBehavior.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Behaviors/SourcesBehavior.php b/src/Behaviors/SourcesBehavior.php index eff82126..e821af9c 100644 --- a/src/Behaviors/SourcesBehavior.php +++ b/src/Behaviors/SourcesBehavior.php @@ -149,6 +149,7 @@ public function getSource(string $fieldType, string $source = null, string $inde // Send event $plugin = Craft::$app->getPlugins()->getPlugin('schematic'); $event = new SourceMappingEvent([ + 'source' => $source, 'service' => $service ?? null, 'method' => $method ?? null, ]); From 077f416da50eb71abce9a8060255fa85f5376eb3 Mon Sep 17 00:00:00 2001 From: Bob Olde Hampsink Date: Mon, 10 Sep 2018 13:53:06 +0200 Subject: [PATCH 4/7] Added source to event object --- src/Events/SourceMappingEvent.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Events/SourceMappingEvent.php b/src/Events/SourceMappingEvent.php index 2e65c695..02c73db4 100644 --- a/src/Events/SourceMappingEvent.php +++ b/src/Events/SourceMappingEvent.php @@ -17,6 +17,11 @@ */ class SourceMappingEvent extends Event { + /** + * @var string + */ + public $source; + /** * @var string */ From 581cfa0625155c775d56a55789e3a2ad25eaa966 Mon Sep 17 00:00:00 2001 From: Bob Olde Hampsink Date: Mon, 10 Sep 2018 14:38:54 +0200 Subject: [PATCH 5/7] Updated docs --- CHANGELOG.md | 4 ++++ README.md | 18 +++++++++++++++++- composer.json | 2 +- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c982f21a..46df5b05 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 7e14b2dd..5976e0d4 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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! diff --git a/composer.json b/composer.json index 9f004676..84920d02 100644 --- a/composer.json +++ b/composer.json @@ -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", From 8f157053def04600ab75bb775918e04de99bab6d Mon Sep 17 00:00:00 2001 From: Bob Olde Hampsink Date: Mon, 10 Sep 2018 15:01:12 +0200 Subject: [PATCH 6/7] Import usergroups again after all sources have been imported --- src/Controllers/Base.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Controllers/Base.php b/src/Controllers/Base.php index 45101529..5ce0392a 100644 --- a/src/Controllers/Base.php +++ b/src/Controllers/Base.php @@ -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; From 750187ee2f50c98ba78cecae5a2c75a5c099b7e3 Mon Sep 17 00:00:00 2001 From: Bob Olde Hampsink Date: Tue, 11 Sep 2018 08:04:37 +0200 Subject: [PATCH 7/7] Get plugin class from within module --- src/Behaviors/SourcesBehavior.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Behaviors/SourcesBehavior.php b/src/Behaviors/SourcesBehavior.php index e821af9c..1282e035 100644 --- a/src/Behaviors/SourcesBehavior.php +++ b/src/Behaviors/SourcesBehavior.php @@ -147,7 +147,7 @@ public function getSource(string $fieldType, string $source = null, string $inde } // Send event - $plugin = Craft::$app->getPlugins()->getPlugin('schematic'); + $plugin = Craft::$app->controller->module; $event = new SourceMappingEvent([ 'source' => $source, 'service' => $service ?? null,