# Plugin Depency via Composer im neuen Plugin System

**URL:** <https://forum.shopware.com/t/plugin-depency-via-composer-im-neuen-plugin-system/51216>\
**Category:** Programmierung\
**Created:** [19. Februar 2018 um 09:36 UTC](https://forum.shopware.com/t/plugin-depency-via-composer-im-neuen-plugin-system/51216 "2018-02-19T09:36:04Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![langnickel](https://avatars.discourse-cdn.com/v4/letter/l/3ec8ea/32.png) [@langnickel](https://forum.shopware.com/u/langnickel)\
**Post date:** [19. Februar 2018 um 09:36 UTC](https://forum.shopware.com/t/plugin-depency-via-composer-im-neuen-plugin-system/51216/1 "2018-02-19T09:36:04Z")

</div>

Ich möchte ein Abhängigkeite, die ich über Composer bezogen habe, in meinem Plugin registrieren. Ich habe den vendor-Folder und die autoload.php soweit vorliegen. Laut Doku muss ich nun einfach die autoload requiren/einbinden aber in welcher Methode mach ich das am Besten?

```
// https://developers.shopware.com/blog/2015/11/11/best-practices-of-shopware-plugin-development/#using-composer
public function registerMyComponents()
{
    require_once $this->Path() . '/vendor/autoload.php';
}

```

Soll ich dazu die getSubscribeEvents missbrauchen oder gibt es im neuen Plugin-System einen vorgeschriebenen Weg um Abhängigkeit per Composer einzubinden und dann normmal per use zu nutzen? Es gibt eine registerCommands&nbsp;in der Shopware\Components\Plugin-Basisklasse. Überschreiben/Erweitern?

---

<div class="post-metadata">

**Author:** ![simkli](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/simkli/32/13124_2.png) [@simkli](https://forum.shopware.com/u/simkli)\
**Post date:** [19. Februar 2018 um 09:45 UTC](https://forum.shopware.com/t/plugin-depency-via-composer-im-neuen-plugin-system/51216/2 "2018-02-19T09:45:07Z")

</div>

@Shyim‍ macht das bei seinem ShyimProfiler gleich ganz am Anfang der Plugin Hauptklasse:

[https://github.com/shyim/shopware-profiler/blob/master/ShyimProfiler.php#L18](https://github.com/shyim/shopware-profiler/blob/master/ShyimProfiler.php#L18)

Ich denke, dass kann man als „Best Practice“ ansehen 😃

Viele Grüße

---

<div class="post-metadata">

**Author:** ![langnickel](https://avatars.discourse-cdn.com/v4/letter/l/3ec8ea/32.png) [@langnickel](https://forum.shopware.com/u/langnickel)\
**Post date:** [19. Februar 2018 um 09:46 UTC](https://forum.shopware.com/t/plugin-depency-via-composer-im-neuen-plugin-system/51216/3 "2018-02-19T09:46:01Z")

</div>

Japp, kann man 😉 danke. Für den Fall dass die Repo irgendwann mal weg, hier mal als simples Beispiel:  
&nbsp;

```
// use statements ...

if (file_exists( __DIR__. '/vendor/autoload.php')) {
    require_once __DIR__. '/vendor/autoload.php';
}

class MyPlugin extends Plugin {

    // ...

}

```

&nbsp;

---

<div class="post-metadata">

**Author:** ![hhmarco73](https://avatars.discourse-cdn.com/v4/letter/h/ed8c4c/32.png) [@hhmarco73](https://forum.shopware.com/u/hhmarco73)\
**Post date:** [19. Februar 2018 um 13:15 UTC](https://forum.shopware.com/t/plugin-depency-via-composer-im-neuen-plugin-system/51216/4 "2018-02-19T13:15:36Z")

</div>

Die Vorgehensweise, das Vendor Verzeichnis direkt in der Datei einzubinden verstößt allerdings gegen eine PSR1 Regel:

> <https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-1-basic-coding-standard.md#23-side-effects>

Ich habe es für mich daher über einen Subscriber gelöst.

```
pluginPath = $pluginPath;
    }

    /**
     * @return array
     */
    public static function getSubscribedEvents(): array
    {
        return [
            'Enlight_Controller_Front_StartDispatch' => 'onFrontStartDispatch',
            'Shopware_Console_Add_Command' => 'onFrontStartDispatch',
        ];
    }

    /**
     * @noinspection PhpParamsInspection
     * @param EventArgs $args
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function onFrontStartDispatch(/** @noinspection PhpUnusedParameterInspection */ EventArgs $args)
    {
        /** @noinspection PhpIncludeInspection */
        require_once $this->pluginPath . '/vendor/autoload.php';
    }
}

```

In der services.xml dann:

```
            %PLUGIN_NAME.plugin_dir%

```

&nbsp;

---

<div class="post-metadata">

**Author:** ![simkli](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/simkli/32/13124_2.png) [@simkli](https://forum.shopware.com/u/simkli)\
**Post date:** [19. Februar 2018 um 13:37 UTC](https://forum.shopware.com/t/plugin-depency-via-composer-im-neuen-plugin-system/51216/5 "2018-02-19T13:37:32Z")

</div>

Hallo @hhmarco73‍:

> Die Vorgehensweise, das Vendor Verzeichnis direkt in der Datei einzubinden verstößt allerdings gegen eine PSR1 Regel:

Wieso? Es wird doch extra zuvor mit einer if-condition abgefangen. Dazu steht auf der von dir verlinkten Seite:

> // conditional declaration is \*not\* a side effect

=\> Es ist kein SideEffect oder etwa doch?

Viele Grüße

---

<div class="post-metadata">

**Author:** ![hhmarco73](https://avatars.discourse-cdn.com/v4/letter/h/ed8c4c/32.png) [@hhmarco73](https://forum.shopware.com/u/hhmarco73)\
**Post date:** [20. Februar 2018 um 12:55 UTC](https://forum.shopware.com/t/plugin-depency-via-composer-im-neuen-plugin-system/51216/6 "2018-02-20T12:55:07Z")

</div>

“// conditional declaration is \*not\* a side effect” bedeutet hier m. E., dass im globalen Namensraum erst geprüft wird, ob das&nbsp;Symbol schon vorhanden ist. Hier beim Einbinden des Vendor Verzeichnisses wird ja nur geprüft, ob die Datei vorhanden ist.

Ich denke aber auch, dass da grundsätzlich eher keine Problematik draus entsteht. Und mit Shopware 5.4 wird ja eh alles besser 😉

---

<div class="post-metadata">

**Author:** ![shyim](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/shyim/32/7681_2.png) [@shyim](https://forum.shopware.com/u/shyim)\
**Post date:** [20. Februar 2018 um 13:45 UTC](https://forum.shopware.com/t/plugin-depency-via-composer-im-neuen-plugin-system/51216/7 "2018-02-20T13:45:56Z")

</div>

Der richtige weg wäre so&nbsp;[Added new plugin method registerAutoloader for composer dependencies by shyim · Pull Request #1155 · shopware/shopware · GitHub](https://github.com/shopware/shopware/pull/1155) , jedoch kann das dauern ka wann das gemergt wird 😃

---

<div class="post-metadata">

**Author:** ![hhmarco73](https://avatars.discourse-cdn.com/v4/letter/h/ed8c4c/32.png) [@hhmarco73](https://forum.shopware.com/u/hhmarco73)\
**Post date:** [20. Februar 2018 um 14:36 UTC](https://forum.shopware.com/t/plugin-depency-via-composer-im-neuen-plugin-system/51216/8 "2018-02-20T14:36:01Z")

</div>

M. E. ist das aber auch noch nicht der komplett richtige/optimale&nbsp;Weg. Besser wäre wahrscheinlich, dass hier&nbsp;[GitHub - wikimedia/composer-merge-plugin: Merge one or more additional composer.json files at Composer runtime](https://github.com/wikimedia/composer-merge-plugin) irgend wie in Shopware nutzbar zu machen, wenn wir denn hier von der Installation von Abhängigkeiten via Composer sprechen.

---

<div class="post-metadata">

**Author:** ![langnickel](https://avatars.discourse-cdn.com/v4/letter/l/3ec8ea/32.png) [@langnickel](https://forum.shopware.com/u/langnickel)\
**Post date:** [20. Februar 2018 um 14:52 UTC](https://forum.shopware.com/t/plugin-depency-via-composer-im-neuen-plugin-system/51216/9 "2018-02-20T14:52:18Z")

</div>

In meinem Fall hab ich einfach nur PhpOffice/Spreadsheet einbinden müssen für ein internes Plugin. Das Paket PhpOffice/Spreadsheet wird nur per Composer bereitgestellt, daher die ursprüngliche Frage). Da es ein internes Plugin ist und nicht an andere weiter gegeben, kann ich an der Stelle den Weg des geringsten Widerstandes gehen. Trotzdem danke für die weiteren Vorschläge.

---

<div class="post-metadata">

**Author:** ![shyim](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/shyim/32/7681_2.png) [@shyim](https://forum.shopware.com/u/shyim)\
**Post date:** [20. Februar 2018 um 21:21 UTC](https://forum.shopware.com/t/plugin-depency-via-composer-im-neuen-plugin-system/51216/10 "2018-02-20T21:21:24Z")

</div>

@hhmarco73‍ schau dir einfach sw 5.4 an&nbsp; ![Sticking-out-tongue](https://forum.shopware.com/plugins/CKEditor/plugins/smiley/images/sticking-out-tongue.png "Sticking-out-tongue")

---

<div class="post-metadata">

**Author:** ![hhmarco73](https://avatars.discourse-cdn.com/v4/letter/h/ed8c4c/32.png) [@hhmarco73](https://forum.shopware.com/u/hhmarco73)\
**Post date:** [26. Februar 2018 um 10:35 UTC](https://forum.shopware.com/t/plugin-depency-via-composer-im-neuen-plugin-system/51216/11 "2018-02-26T10:35:18Z")

</div>

@Shyim‍, ich kenne die Optimierung von Shopware 5.4 hinsichtlich Composer;) Und da ich nur einen Shop betreue, bringt mir das höchstwahrscheinlich auch einige Vorteile. Für einen Plugin Hersteller wäre m. E. aber das Composer Merge Tool weiterhin eine gute Sache.&nbsp;

---

<div class="post-metadata">

**Author:** ![shyim](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/shyim/32/7681_2.png) [@shyim](https://forum.shopware.com/u/shyim)\
**Post date:** [26. Februar 2018 um 22:19 UTC](https://forum.shopware.com/t/plugin-depency-via-composer-im-neuen-plugin-system/51216/12 "2018-02-26T22:19:09Z")

</div>

Und welche?
