Shopware 6 開発
コアアーキテクチャ
Shopware 6 は PHP/Symfony プラットフォームです。コードを生成する前に、どのレイヤーを対象にしているかを明確にしてください。
| レイヤー | 技術 | 場所 |
|---|---|---|
| Core / PHP | PHP 8.2+, Symfony 7.4 (SW 6.7) / Symfony 7.0 (SW 6.6) | src/ — サービス、DAL、イベント |
| Storefront | Twig 3、SCSS、Vanilla JS、Vite 開発サーバー (6.7.11+) | src/Resources/views/storefront/ |
| Admin | Vue 3、Pinia、Meteor コンポーネント (mt-*)、Vite ビルド | src/Resources/app/administration/ |
| App System | manifest.xml (6.7 ではスキーマ 3.0)、App Scripts | 外部サーバーまたは Twig スクリプト (PHP 不要) |
プラグインとアプリシステムの違い: プラグインは、直接的な PHP/DB アクセスが必要なセルフホスト環境に使用します。アプリシステムは、SaaS/マルチテナント環境や Shopware Store への公開を目的とする場合に使用します。
バージョンは?
コードを生成する前に、対象バージョンを確認または推測してください。現在のラインは 6.7.13.x で、6.8 は 2027 年に計画されています。6.7 では多くのプラグイン向け API が変更されたため、6.6 で正しいコードが 6.7 では間違っていることがよくあります。
| トピック | SW 6.6 | SW 6.7 |
|---|---|---|
| 決済ハンドラー | Synchronous/AsynchronousPaymentHandlerInterface | AbstractPaymentHandler |
| 管理画面コンポーネント | sw-button、sw-card、sw-text-field | mt-button、mt-card、mt-text-field |
| 管理画面状態管理 | Vuex Shopware.State (非推奨) | Pinia Shopware.Store のみ |
| 管理画面ビルド | Webpack | Vite |
EntityExtension | getDefinitionClass() | 加えて抽象メソッド getEntityName() |
| プラグインカスタムエンティティ | entities.xml | 削除、EntityDefinition を使用 |
IdsCollection | Shopware\Core\Framework\Test\ | Shopware\Core\Test\Stub\Framework\ |
完全なリストは references/migration-6.7.md にあります。バージョンが不明な場合は、6.7 をターゲットとし、6.6 と異なる点に言及してください。
プラグイン構造
PluginName/
├── composer.json
├── src/
│ ├── PluginName.php # ブートストラップクラス
│ ├── Resources/
│ │ ├── config/
│ │ │ └── services.xml # Symfony DI コンテナ
│ │ ├── views/
│ │ │ └── storefront/ # Twig テンプレートオーバーライド
│ │ └── app/
│ │ └── administration/ # Vue.js 管理画面拡張
│ └── Migration/ # データベースマイグレーション
└── tests/
プラグインブートストラップ
<?php declare(strict_types=1);
namespace VendorName\PluginName;
use Shopware\Core\Framework\Plugin;
class PluginName extends Plugin {}
ライフサイクルアクション(例:決済方法の作成、アンインストール時のテーブル削除)が必要な場合にのみ、install()、activate()、deactivate()、uninstall() を拡張します。
5つの主要ワークフロー
1. サービス登録 (DI)
<!-- services.xml -->
<service id="VendorName\PluginName\Service\MyService">
<argument type="service" id="product.repository"/>
</service>
一般的なタグ: kernel.event_subscriber、twig.extension、console.command、messenger.message_handler、shopware.entity.definition、shopware.entity.extension、shopware.rule.definition、shopware.payment.method.sync、shopware.payment.method.async、shopware.cms.element。
2. イベントのリッスン (サブスクライバー)
class ProductSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return ['product.written' => 'onProductWritten'];
}
public function onProductWritten(EntityWrittenEvent $event): void
{
foreach ($event->getWriteResults() as $result) {
$id = $result->getPrimaryKey();
}
}
}
3. DAL 読み取り & 書き込み
// 読み取り
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('active', true));
$criteria->addAssociation('manufacturer');
$result = $this->productRepository->search($criteria, $context);
// 書き込み
$this->productRepository->upsert([
['id' => $id, 'name' => 'New Name'],
], $context);
4. ストアフロントテンプレートのオーバーライド
{# views/storefront/ 下の元のパスをミラーリング #}
{% sw_extends '@Storefront/storefront/page/product-detail/index.html.twig' %}
{% block page_product_detail_content %}
<div class="my-banner">カスタムコンテンツ</div>
{{ parent() }}
{% endblock %}
5. サービスのデコレーション
<service id="VendorName\PluginName\Decorator\MyDecorator"
decorates="original.service.id">
<argument type="service"
id="VendorName\PluginName\Decorator\MyDecorator.inner"/>
</service>
Karpathy の原則 — コーディング前に明確にする
コードを生成する前に、以下の前提を明確にしてください。
- バージョンは? SW 6.5 と 6.6 (API と Vue コンポーネントに差異あり)
- プラグインかアプリシステムか? プラグイン = PHPサーバー; アプリ = manifest.xml + 外部 / サーバーレス
- レイヤーは? PHP/Core、Storefront、Admin、またはヘッドレス / Store API
- 読み取りか書き込みか? リポジトリの
search()とupsert()/create()/update() - エンティティか拡張か? 新しいテーブルか、既存エンティティの拡張か
問題を解決する最小限のコードのみを記述してください。Shopware の DI とイベントシステムが複雑さの大部分を処理します。
DAL クイックリファレンス
// Criteria
$criteria->addFilter(new EqualsFilter('active', true));
$criteria->addFilter(new ContainsFilter('name', 'shirt'));
$criteria->addFilter(new RangeFilter('price', [RangeFilter::GTE => 10]));
$criteria->addAssociation('manufacturer');
$criteria->addSorting(new FieldSorting('name', FieldSorting::ASCENDING));
$criteria->setLimit(25)->setOffset(0);
// Context
$context = Context::createDefaultContext(); // システム
// または、ルート/イベントから SalesChannelContext を注入
// IDのみ (高速、ハイドレーションなし)
$ids = $this->repo->searchIds($criteria, $context)->getIds();
管理画面 Vue.js クイックリファレンス
// モジュール登録
Shopware.Module.register('my-module', {
type: 'plugin',
routes: { index: { component: 'my-module-index', path: 'index' } },
navigation: [{ label: 'my-module.title', path: 'my.module.index', icon: 'default-shopping-paper-bag' }],
});
// 既存コンポーネントのオーバーライド
Shopware.Component.override('sw-product-detail', {
methods: {
async saveProduct() {
await this.$super('saveProduct'); // 元の処理を呼び出す
},
},
});
CLI コマンド
bin/console plugin:install --activate PluginName
bin/console database:migrate --all PluginName
bin/console cache:clear
bin/console plugin:refresh
bin/build-administration.sh
bin/build-storefront.sh
bin/console theme:compile
php vendor/bin/phpunit --testsuite=unit
vendor/bin/phpstan analyse src --level=8
追加リソース
リファレンスファイル
特定の領域で作業する際は、これらを読み込んでください。
- references/dal.md — DAL: EntityDefinition、Criteria、Aggregations、カスタムフィールド、Entity Extensions (コアエンティティの拡張)
- references/admin.md — 管理画面: Vue モジュール、コンポーネント、オーバーライド、命名規則、ACL 権限、フィルター/インライン編集、検索設定
- references/storefront.md — ストアフロント: Twig 継承、SCSS/テーマ変数、JavaScriptプラグイン、コントローラー
- references/themes.md — テーマ: theme.json (設定フィールド、色、フォント、メディア)、SCSS Bootstrap オーバーライド、テーマ継承、ThemeInterface、CLI コマンド
- references/cart.md — カート: CartDataCollector、CartProcessor、CartValidator + カスタムエラー、割引ラインアイテム、価格操作、Tax Provider
- references/seo-mail.md — SEO: SeoUrlRoute、サイトマップ URL プロバイダー; メール: カスタムメールテンプレート (マイグレーション + 送信); ドキュメント (カスタムPDFタイプ); 注文ステートマシン (トランジション、イベント)
- references/plugin-structure.md — 完全なプラグイン構造: services.xml、ライフサイクルフック、composer.json、コンソールコマンド、スケジュールタスク
- references/api.md — Admin API & Store API: CRUD、バルク、フィルター; コンテキストトークンのライフサイクル、Cart/Checkout/Account Store API、TypeScript クライアントパターン
- references/testing.md — PHPUnit ユニット/統合、StaticEntityRepository、ProductBuilder、Jest、Cypress、assertSame と assertEquals の違い
- references/app-system.md — アプリシステム: manifest.xml、webhook HMAC 検証、登録ハンドシェイク、App Scripts (Twigベース、サーバーレス)
- references/security.md — セキュリティ: ルートスコープ、CSRF 保護、入力バリデーション、顧客による承認、SQLインジェクション防止
- references/integrations.md — 統合: Payment Handler (
AbstractPaymentHandler)、配送コスト (カートプロセッサー、calculator タグではない)、CMS Elements、Rule Builder 条件、Flow Builder イベント - references/performance.md — パフォーマンス: HTTP Cache (タグ、無効化)、Message Queue (非同期処理)、Elasticsearch/OpenSearch、オブジェクトキャッシュ
- references/devops.md — DevOps: 構造化ログ、PHPStan、php-cs-fixer、CI/CD、デプロイ、デバッグ、メディア処理、アップグレードの安全性
- references/migration-6.7.md — 6.6 から 6.7 への移行: Core/DAL、管理画面、ストアフロント、キャッシュ、API、ホスティング全体の破壊的変更、および 6.7.x で追加された機能 (Vite 開発サーバー、Twig UX コンポーネント、MCP サーバー)
- references/advanced.md — 高度なトピック: PHP Attributes エンティティ (SW 6.6.3+)、Flysystem (公開/非公開ファイルストレージ)、Redis (キャッシュ/キュー)、Rate Limiter (コンパイラパス + RateLimiter サービス)、Data Indexer、Field Inheritance (バリアント)、In-App Purchases
サンプル
examples/ ディレクトリには実際に動作するコードサンプルがあります。
- examples/custom-entity/ — 実行可能なプラグイン: Definition、Entity、Collection、Migration、製品へのバージョン付き外部キー、アンインストール時のクリーンアップ
- examples/storefront-subscriber/ — 実行可能なプラグイン: ページサブスクライバー、販売チャネルリポジトリ、Twig オーバーライド、スニペット