These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of EC-CUBE |
||
5 | * |
||
6 | * Copyright(c) LOCKON CO.,LTD. All Rights Reserved. |
||
7 | * |
||
8 | * http://www.lockon.co.jp/ |
||
9 | * |
||
10 | * For the full copyright and license information, please view the LICENSE |
||
11 | * file that was distributed with this source code. |
||
12 | */ |
||
13 | |||
14 | namespace Eccube\Service; |
||
15 | |||
16 | use Doctrine\ORM\EntityManagerInterface; |
||
17 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
||
18 | use Symfony\Component\HttpKernel\DataCollector\MemoryDataCollector; |
||
19 | use Symfony\Component\DependencyInjection\ContainerInterface; |
||
20 | use Symfony\Component\HttpKernel\KernelEvents; |
||
21 | use Symfony\Component\HttpKernel\Event\PostResponseEvent; |
||
22 | |||
23 | class SystemService implements EventSubscriberInterface |
||
24 | { |
||
25 | const AUTO_MAINTENANCE = 'auto_maintenance'; |
||
26 | const AUTO_MAINTENANCE_UPDATE = 'auto_maintenance_update'; |
||
27 | |||
28 | /** |
||
29 | * メンテナンスモードを無効にする場合はtrue |
||
30 | * |
||
31 | * @var bool |
||
32 | 3 | */ |
|
33 | private $disableMaintenanceAfterResponse = false; |
||
34 | 3 | ||
35 | /** |
||
36 | * メンテナンスモードの識別子 |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | private $maintenanceMode = null; |
||
41 | |||
42 | 3 | /** |
|
43 | * @var EntityManagerInterface |
||
44 | 3 | */ |
|
45 | 3 | protected $entityManager; |
|
46 | |||
47 | 3 | /** |
|
48 | 3 | * @var ContainerInterface |
|
49 | */ |
||
50 | 3 | protected $container; |
|
51 | 3 | ||
52 | 3 | /** |
|
53 | * SystemService constructor. |
||
54 | * |
||
55 | * @param EntityManagerInterface $entityManager |
||
56 | * @param ContainerInterface $container |
||
57 | */ |
||
58 | public function __construct( |
||
59 | EntityManagerInterface $entityManager, |
||
60 | ContainerInterface $container |
||
61 | ) { |
||
62 | $this->entityManager = $entityManager; |
||
63 | $this->container = $container; |
||
64 | } |
||
65 | 3 | ||
66 | 3 | /** |
|
67 | 3 | * get DB version |
|
68 | * |
||
69 | 3 | * @return string |
|
70 | */ |
||
71 | public function getDbversion() |
||
72 | { |
||
73 | $rsm = new \Doctrine\ORM\Query\ResultSetMapping(); |
||
74 | $rsm->addScalarResult('v', 'v'); |
||
75 | |||
76 | $platform = $this->entityManager->getConnection()->getDatabasePlatform()->getName(); |
||
77 | switch ($platform) { |
||
78 | case 'sqlite': |
||
79 | $prefix = 'SQLite version '; |
||
80 | $func = 'sqlite_version()'; |
||
81 | break; |
||
82 | |||
83 | case 'mysql': |
||
84 | $prefix = 'MySQL '; |
||
85 | $func = 'version()'; |
||
86 | break; |
||
87 | |||
88 | case 'pgsql': |
||
89 | default: |
||
90 | $prefix = ''; |
||
91 | $func = 'version()'; |
||
92 | } |
||
93 | |||
94 | $version = $this->entityManager |
||
95 | ->createNativeQuery('select '.$func.' as v', $rsm) |
||
96 | ->getSingleScalarResult(); |
||
97 | |||
98 | return $prefix.$version; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Try to set new values memory_limit | return true |
||
103 | * |
||
104 | * @param string $memory | EX: 1536M |
||
105 | * |
||
106 | * @return bool |
||
107 | */ |
||
108 | public function canSetMemoryLimit($memory) |
||
109 | { |
||
110 | try { |
||
111 | $ret = ini_set('memory_limit', $memory); |
||
112 | } catch (\Exception $exception) { |
||
113 | return false; |
||
114 | } |
||
115 | |||
116 | return ($ret === false) ? false : true; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Get memory_limit | Megabyte |
||
121 | * |
||
122 | * @return float|int |
||
123 | */ |
||
124 | public function getMemoryLimit() |
||
125 | { |
||
126 | // Data type: bytes |
||
127 | $memoryLimit = (new MemoryDataCollector())->getMemoryLimit(); |
||
128 | if (-1 == $memoryLimit) { |
||
129 | return -1; |
||
130 | } |
||
131 | |||
132 | return ($memoryLimit == 0) ? 0 : ($memoryLimit / 1024) / 1024; |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * メンテナンスモードを切り替える |
||
137 | * |
||
138 | * - $isEnable = true の場合, $mode の文字列が記載された .maintenance ファイルを生成する |
||
139 | * - $isEnable = false の場合, $mode の文字列が記載された .maintenance ファイルを削除する |
||
140 | * |
||
141 | * @param bool $isEnable |
||
142 | * @param string $mode |
||
143 | */ |
||
144 | public function switchMaintenance($isEnable = false, $mode = self::AUTO_MAINTENANCE) |
||
145 | { |
||
146 | $isMaintenanceMode = $this->isMaintenanceMode(); |
||
147 | $path = $this->container->getParameter('eccube_content_maintenance_file_path'); |
||
148 | |||
149 | if ($isEnable && $isMaintenanceMode === false) { |
||
150 | file_put_contents($path, $mode); |
||
151 | } elseif ($isEnable === false && $isMaintenanceMode) { |
||
152 | $contents = file_get_contents($path); |
||
153 | if ($contents == $mode) { |
||
154 | unlink($path); |
||
155 | } |
||
156 | } |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * KernelEvents::TERMINATE で設定されるEvent |
||
161 | * |
||
162 | * @param PostResponseEvent $event |
||
163 | */ |
||
164 | public function disableMaintenanceEvent(PostResponseEvent $event) |
||
0 ignored issues
–
show
|
|||
165 | { |
||
166 | if ($this->disableMaintenanceAfterResponse) { |
||
167 | $this->switchMaintenance(false, $this->maintenanceMode); |
||
168 | } |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * メンテナンスモードを解除する |
||
173 | * |
||
174 | * KernelEvents::TERMINATE で解除のEventを設定し、メンテナンスモードを解除する |
||
175 | * |
||
176 | * @param string $mode |
||
177 | */ |
||
178 | public function disableMaintenance($mode = self::AUTO_MAINTENANCE) |
||
179 | { |
||
180 | $this->disableMaintenanceAfterResponse = true; |
||
181 | $this->maintenanceMode = $mode; |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * メンテナンスモードの状態を判定する |
||
186 | * |
||
187 | * @return bool |
||
188 | */ |
||
189 | public function isMaintenanceMode() |
||
190 | { |
||
191 | // .maintenanceが存在しているかチェック |
||
192 | return file_exists($this->container->getParameter('eccube_content_maintenance_file_path')); |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * {@inheritdoc} |
||
197 | */ |
||
198 | public static function getSubscribedEvents() |
||
199 | { |
||
200 | return [KernelEvents::TERMINATE => 'disableMaintenanceEvent']; |
||
201 | } |
||
202 | } |
||
203 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.