-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPluginManager.php
More file actions
executable file
·132 lines (117 loc) · 4.5 KB
/
PluginManager.php
File metadata and controls
executable file
·132 lines (117 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
namespace Plugin\MPBC43;
use Eccube\Entity\Master\ProductStatus;
use Eccube\Entity\Master\SaleType;
use Eccube\Entity\Product;
use Eccube\Entity\ProductClass;
use Eccube\Plugin\AbstractPluginManager;
use Plugin\MPBC43\Entity\Config;
use Psr\Container\ContainerInterface;
class PluginManager extends AbstractPluginManager
{
/**
* プラグイン有効化時の処理
*
* @param array $meta
* @param ContainerInterface $container
*/
public function enable(array $meta, ContainerInterface $container)
{
$entityManager = $container->get('doctrine.orm.entity_manager');
// Ensure Config exists
$ConfigRepository = $entityManager->getRepository(Config::class);
$Config = $ConfigRepository->findOneBy([]);
if (!$Config) {
$Config = new Config();
$Config->setProductNameDisplayType('customer_input');
$entityManager->persist($Config);
$entityManager->flush($Config);
} else {
// 既存のConfigがあるが、product_name_display_typeが設定されていない場合のデフォルト値設定
if ($Config->getProductNameDisplayType() === null) {
$Config->setProductNameDisplayType('customer_input');
$entityManager->persist($Config);
$entityManager->flush($Config);
}
}
$Config = $this->createConfig($entityManager);
// Ensure a hidden base product with a single class exists for MPBC
$productRepo = $entityManager->getRepository(Product::class);
$Product = $productRepo->findOneBy(['name' => 'MPB_Product']);
if (!$Product) {
$statusRepo = $entityManager->getRepository(ProductStatus::class);
$saleTypeRepo = $entityManager->getRepository(SaleType::class);
$Product = new Product();
$Product->setName('MPB_Product');
// Make it saleable/visible (1 is typically 表示)
$ShowStatus = $statusRepo->find(1);
if ($ShowStatus) {
$Product->setStatus($ShowStatus);
}
// Minimal required class
$ProductClass = new ProductClass();
$ProductClass->setProduct($Product);
$ProductClass->setCode('MPB-CLASS');
$ProductClass->setStockUnlimited(true);
$ProductClass->setPrice02(1); // base price (will be overridden at runtime)
$ProductClass->setVisible(true);
// Use default sale type (usually 1: 通常商品) on ProductClass
$DefaultSaleType = $saleTypeRepo->find(1);
if ($DefaultSaleType) {
$ProductClass->setSaleType($DefaultSaleType);
}
$entityManager->persist($Product);
$entityManager->persist($ProductClass);
$entityManager->flush();
}
}
/**
* プラグイン無効化時の処理
*
* @param array $meta
* @param ContainerInterface $container
*/
public function disable(array $meta, ContainerInterface $container)
{
// 無効化時に必要な処理があればここに記述
}
/**
* プラグインアンインストール時の処理
*
* @param array $meta
* @param ContainerInterface $container
*/
public function uninstall(array $meta, ContainerInterface $container)
{
// アンインストール時に必要な処理があればここに記述
}
/**
* プラグインインストール時の処理
*
* @param array $meta
* @param ContainerInterface $container
*/
public function install(array $meta, ContainerInterface $container)
{
$entityManager = $container->get('doctrine.orm.entity_manager');
// データベーススキーマを更新
$this->createSchema($entityManager);
}
/**
* データベーススキーマ作成
*/
private function createSchema($entityManager)
{
$tool = new \Doctrine\ORM\Tools\SchemaTool($entityManager);
// プラグイン固有のエンティティのメタデータのみを取得
$configMetaData = $entityManager->getClassMetadata(Config::class);
try {
$tool->createSchema([$configMetaData]);
} catch (\Exception $e) {
// テーブルが既に存在する場合は無視
if (strpos($e->getMessage(), 'already exists') === false) {
throw $e;
}
}
}
}