Completed
Branch master (01bfb0)
by Nik
03:53
created
src/Application/Exception/BitrixException.php 1 patch
Indentation   +33 added lines, -33 removed lines patch added patch discarded remove patch
@@ -1,8 +1,8 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 /**
3
- * For the full copyright and license information, please view the LICENSE.md
4
- * file that was distributed with this source code.
5
- */
3
+     * For the full copyright and license information, please view the LICENSE.md
4
+     * file that was distributed with this source code.
5
+     */
6 6
 
7 7
 namespace Notamedia\ConsoleJedi\Application\Exception;
8 8
 
@@ -14,37 +14,37 @@  discard block
 block discarded – undo
14 14
  */
15 15
 class BitrixException extends \RuntimeException
16 16
 {
17
-	public static function hasException(\CMain $APPLICATION = null)
18
-	{
19
-		if (null === $APPLICATION)
20
-		{
21
-			$APPLICATION = $GLOBALS['APPLICATION'];
22
-		}
17
+    public static function hasException(\CMain $APPLICATION = null)
18
+    {
19
+        if (null === $APPLICATION)
20
+        {
21
+            $APPLICATION = $GLOBALS['APPLICATION'];
22
+        }
23 23
 
24
-		return is_object($APPLICATION->GetException());
25
-	}
24
+        return is_object($APPLICATION->GetException());
25
+    }
26 26
 
27
-	/**
28
-	 * Check for legacy bitrix exception, throws new self if any
29
-	 *
30
-	 * @param string $message [optional] Additional error message
31
-	 * @param \CMain $APPLICATION [optional] $APPLICATION instance
32
-	 * @throws static
33
-	 */
34
-	public static function generate($message = null, \CMain $APPLICATION = null)
35
-	{
36
-		if (null === $APPLICATION)
37
-		{
38
-			$APPLICATION = $GLOBALS['APPLICATION'];
39
-		}
27
+    /**
28
+     * Check for legacy bitrix exception, throws new self if any
29
+     *
30
+     * @param string $message [optional] Additional error message
31
+     * @param \CMain $APPLICATION [optional] $APPLICATION instance
32
+     * @throws static
33
+     */
34
+    public static function generate($message = null, \CMain $APPLICATION = null)
35
+    {
36
+        if (null === $APPLICATION)
37
+        {
38
+            $APPLICATION = $GLOBALS['APPLICATION'];
39
+        }
40 40
 
41
-		if ($ex = $APPLICATION->GetException())
42
-		{
43
-			throw new static($message ? $message . ': ' . $ex->GetString() : $ex->GetString());
44
-		}
45
-		else
46
-		{
47
-			throw new static($message ? $message : 'Unknown exception');
48
-		}
49
-	}
41
+        if ($ex = $APPLICATION->GetException())
42
+        {
43
+            throw new static($message ? $message . ': ' . $ex->GetString() : $ex->GetString());
44
+        }
45
+        else
46
+        {
47
+            throw new static($message ? $message : 'Unknown exception');
48
+        }
49
+    }
50 50
 }
51 51
\ No newline at end of file
Please login to merge, or discard this patch.
src/Application/Exception/ConfigurationException.php 1 patch
Indentation   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -1,8 +1,8 @@
 block discarded – undo
1 1
 <?php
2 2
 /**
3
- * For the full copyright and license information, please view the LICENSE.md
4
- * file that was distributed with this source code.
5
- */
3
+     * For the full copyright and license information, please view the LICENSE.md
4
+     * file that was distributed with this source code.
5
+     */
6 6
 
7 7
 namespace Notamedia\ConsoleJedi\Application\Exception;
8 8
 
Please login to merge, or discard this patch.
src/Application/Command/BitrixCommand.php 1 patch
Indentation   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -1,8 +1,8 @@
 block discarded – undo
1 1
 <?php
2 2
 /**
3
- * For the full copyright and license information, please view the LICENSE.md
4
- * file that was distributed with this source code.
5
- */
3
+     * For the full copyright and license information, please view the LICENSE.md
4
+     * file that was distributed with this source code.
5
+     */
6 6
 
7 7
 namespace Notamedia\ConsoleJedi\Application\Command;
8 8
 
Please login to merge, or discard this patch.
src/Application/Command/InitCommand.php 1 patch
Indentation   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -1,8 +1,8 @@
 block discarded – undo
1 1
 <?php
2 2
 /**
3
- * For the full copyright and license information, please view the LICENSE.md
4
- * file that was distributed with this source code.
5
- */
3
+     * For the full copyright and license information, please view the LICENSE.md
4
+     * file that was distributed with this source code.
5
+     */
6 6
 
7 7
 namespace Notamedia\ConsoleJedi\Application\Command;
8 8
 
Please login to merge, or discard this patch.
src/Environment/Command/InitCommand.php 1 patch
Indentation   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -1,8 +1,8 @@
 block discarded – undo
1 1
 <?php
2 2
 /**
3
- * For the full copyright and license information, please view the LICENSE.md
4
- * file that was distributed with this source code.
5
- */
3
+     * For the full copyright and license information, please view the LICENSE.md
4
+     * file that was distributed with this source code.
5
+     */
6 6
 
7 7
 namespace Notamedia\ConsoleJedi\Environment\Command;
8 8
 
Please login to merge, or discard this patch.
src/Module/Module.php 1 patch
Indentation   +394 added lines, -394 removed lines patch added patch discarded remove patch
@@ -1,8 +1,8 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 /**
3
- * For the full copyright and license information, please view the LICENSE.md
4
- * file that was distributed with this source code.
5
- */
3
+     * For the full copyright and license information, please view the LICENSE.md
4
+     * file that was distributed with this source code.
5
+     */
6 6
 
7 7
 namespace Notamedia\ConsoleJedi\Module;
8 8
 
@@ -18,395 +18,395 @@  discard block
 block discarded – undo
18 18
  */
19 19
 class Module
20 20
 {
21
-	/** @var string */
22
-	private $name;
23
-
24
-	/** @var \CModule */
25
-	private $object;
26
-
27
-	/** @var bool */
28
-	private $beta = false;
29
-
30
-	/**
31
-	 * @param string $moduleName
32
-	 */
33
-	public function __construct($moduleName)
34
-	{
35
-		$this->name = $this->normalizeName($moduleName);
36
-	}
37
-
38
-	/**
39
-	 * @param string $moduleName
40
-	 * @return string
41
-	 */
42
-	protected function normalizeName($moduleName)
43
-	{
44
-		return preg_replace("/[^a-zA-Z0-9_.]+/i", "", trim($moduleName));
45
-	}
46
-
47
-	/**
48
-	 * @return \CModule
49
-	 */
50
-	protected function &getObject()
51
-	{
52
-		if (!isset($this->object))
53
-		{
54
-			$this->object = \CModule::CreateModuleObject($this->name);
55
-		}
56
-
57
-		if (!is_object($this->object) || !($this->object instanceof \CModule))
58
-		{
59
-			unset($this->object);
60
-			throw new Exception\ModuleNotFoundException('Module not found or incorrect', $this->name);
61
-		}
62
-
63
-		return $this->object;
64
-	}
65
-
66
-	/**
67
-	 * Checks for module and module object existence
68
-	 *
69
-	 * @return bool
70
-	 */
71
-	public function exist()
72
-	{
73
-		try
74
-		{
75
-			$this->getObject();
76
-		}
77
-		catch (Exception\ModuleNotFoundException $e)
78
-		{
79
-			return false;
80
-		}
81
-
82
-		return true;
83
-	}
84
-
85
-	/**
86
-	 * Check if module exists and installed
87
-	 *
88
-	 * @return bool
89
-	 */
90
-	public function isRegistered()
91
-	{
92
-		return ModuleManager::isModuleInstalled($this->name) && $this->exist();
93
-	}
94
-
95
-	/**
96
-	 * @return bool true for marketplace modules, false for kernel modules
97
-	 */
98
-	public function isThirdParty()
99
-	{
100
-		return strpos($this->name, '.') !== false;
101
-	}
102
-
103
-	/**
104
-	 * Install module
105
-	 *
106
-	 * @throws Exception\ModuleException
107
-	 * @throws BitrixException
108
-	 */
109
-	public function register()
110
-	{
111
-		if (!$this->isRegistered())
112
-		{
113
-			$moduleObject =& $this->getObject();
114
-
115
-			/**
116
-			 * It's important to check if module class defines InstallDB method (it must register module)
117
-			 * Thus absent InstallDB indicates that the module does not support automatic installation
118
-			 */
119
-			if ((new \ReflectionClass($moduleObject))->getMethod('InstallDB')->class !== get_class($moduleObject))
120
-			{
121
-				throw new Exception\ModuleInstallException(
122
-					'Missing InstallDB method. This module does not support automatic installation',
123
-					$this->name
124
-				);
125
-			}
126
-
127
-			if (!$moduleObject->InstallDB() && BitrixException::hasException())
128
-			{
129
-				throw new Exception\ModuleInstallException(
130
-					get_class($moduleObject) . '::InstallDB() returned false',
131
-					$this->name
132
-				);
133
-			}
134
-
135
-			$moduleObject->InstallEvents();
136
-
137
-			/** @noinspection PhpVoidFunctionResultUsedInspection */
138
-			if (!$moduleObject->InstallFiles() && BitrixException::hasException())
139
-			{
140
-				throw new Exception\ModuleInstallException(
141
-					get_class($moduleObject) . '::InstallFiles() returned false',
142
-					$this->name
143
-				);
144
-			}
145
-
146
-			if (!$this->isRegistered())
147
-			{
148
-				throw new Exception\ModuleInstallException(
149
-					'Module was not registered. Probably it does not support automatic installation.',
150
-					$this->name
151
-				);
152
-			}
153
-		}
154
-
155
-		return $this;
156
-	}
157
-
158
-	/**
159
-	 * Download module from marketplace
160
-	 *
161
-	 * @return $this
162
-	 */
163
-	public function load()
164
-	{
165
-		if (!$this->isRegistered())
166
-		{
167
-			if (!$this->exist())
168
-			{
169
-				require_once($_SERVER["DOCUMENT_ROOT"] . '/bitrix/modules/main/classes/general/update_client_partner.php');
170
-
171
-				if (!\CUpdateClientPartner::LoadModuleNoDemand(
172
-					$this->getName(),
173
-					$strError,
174
-					$this->isBeta() ? 'N' : 'Y',
175
-					LANGUAGE_ID)
176
-				)
177
-				{
178
-					throw new Exception\ModuleLoadException($strError, $this->getName());
179
-				}
180
-			}
181
-		}
182
-
183
-		return $this;
184
-	}
185
-
186
-	/**
187
-	 * Uninstall module
188
-	 *
189
-	 * @throws Exception\ModuleException
190
-	 * @throws BitrixException
191
-	 */
192
-	public function unRegister()
193
-	{
194
-		$moduleObject = $this->getObject();
195
-
196
-		if ($this->isRegistered())
197
-		{
198
-			/**
199
-			 * It's important to check if module class defines UnInstallDB method (it should unregister module)
200
-			 * Thus absent UnInstallDB indicates that the module does not support automatic uninstallation
201
-			 */
202
-			if ((new \ReflectionClass($moduleObject))->getMethod('UnInstallDB')->class !== get_class($moduleObject))
203
-			{
204
-				throw new Exception\ModuleUninstallException(
205
-					'Missing UnInstallDB method. This module does not support automatic uninstallation',
206
-					$this->name
207
-				);
208
-			}
209
-
210
-			/** @noinspection PhpVoidFunctionResultUsedInspection */
211
-			if (!$moduleObject->UnInstallFiles() && BitrixException::hasException())
212
-			{
213
-				throw new Exception\ModuleUninstallException(
214
-					get_class($moduleObject) . '::UnInstallFiles() returned false',
215
-					$this->name
216
-				);
217
-			}
218
-
219
-			$moduleObject->UnInstallEvents();
220
-
221
-			/** @noinspection PhpVoidFunctionResultUsedInspection */
222
-			if (!$moduleObject->UnInstallDB() && BitrixException::hasException())
223
-			{
224
-				throw new Exception\ModuleUninstallException(
225
-					get_class($moduleObject) . '::UnInstallFiles() returned false',
226
-					$this->name
227
-				);
228
-			}
229
-
230
-			if ($this->isRegistered())
231
-			{
232
-				throw new Exception\ModuleUninstallException('Module was not unregistered', $this->name);
233
-			}
234
-		}
235
-
236
-		return $this;
237
-	}
238
-
239
-	/**
240
-	 * Uninstall and remove module directory
241
-	 */
242
-	public function remove()
243
-	{
244
-		if ($this->isRegistered())
245
-		{
246
-			$this->unRegister();
247
-		}
248
-
249
-		$path = getLocalPath('modules/' . $this->getName());
250
-
251
-		if ($path)
252
-		{
253
-			(new Filesystem())->remove($_SERVER['DOCUMENT_ROOT'] . $path);
254
-		}
255
-
256
-		unset($this->object);
257
-
258
-		return $this;
259
-	}
260
-
261
-	/**
262
-	 * Update module
263
-	 *
264
-	 * It must be called repeatedly until the method returns false.
265
-	 * After each call php must be restarted (new process created) to update module class and function definitions.
266
-	 *
267
-	 * @param array $modulesUpdated [optional]
268
-	 * @return bool
269
-	 */
270
-	public function update(&$modulesUpdated = null)
271
-	{
272
-		require_once($_SERVER["DOCUMENT_ROOT"] . '/bitrix/modules/main/classes/general/update_client_partner.php');
273
-
274
-		if (!$this->isThirdParty())
275
-		{
276
-			throw new Exception\ModuleUpdateException('Kernel module updates are currently not supported.', $this->getName());
277
-		}
278
-
279
-		// ensures module existence
280
-		$this->getObject();
281
-
282
-		$errorMessage = $updateDescription = null;
283
-		$loadResult = \CUpdateClientPartner::LoadModulesUpdates(
284
-			$errorMessage,
285
-			$updateDescription,
286
-			LANGUAGE_ID,
287
-			$this->isBeta() ? 'N' : 'Y',
288
-			[$this->getName()],
289
-			true
290
-		);
291
-		switch ($loadResult)
292
-		{
293
-			// archive loaded
294
-			case "S":
295
-				return $this->update($modulesUpdated);
296
-
297
-			// error
298
-			case "E":
299
-				throw new Exception\ModuleUpdateException($errorMessage, $this->getName());
300
-
301
-			// finished installing updates
302
-			case "F":
303
-				return false;
304
-
305
-			// need to process loaded update
306
-			case 'U':
307
-				break;
308
-		}
309
-
310
-		/** @var string Temp directory with update files */
311
-		$updateDir = null;
312
-
313
-		if (!\CUpdateClientPartner::UnGzipArchive($updateDir, $errorMessage, true))
314
-		{
315
-			throw new Exception\ModuleUpdateException('[CL02] UnGzipArchive failed. ' . $errorMessage, $this->getName());
316
-		}
317
-
318
-		$this->validateUpdate($updateDir);
319
-
320
-		if (isset($updateDescription["DATA"]["#"]["NOUPDATES"]))
321
-		{
322
-			\CUpdateClientPartner::ClearUpdateFolder($_SERVER["DOCUMENT_ROOT"] . "/bitrix/updates/" . $updateDir);
323
-			return false;
324
-		}
325
-
326
-		$modulesUpdated = $updateDescr = [];
327
-		if (isset($updateDescription["DATA"]["#"]["ITEM"]))
328
-		{
329
-			foreach ($updateDescription["DATA"]["#"]["ITEM"] as $moduleInfo)
330
-			{
331
-				$modulesUpdated[$moduleInfo["@"]["NAME"]] = $moduleInfo["@"]["VALUE"];
332
-				$updateDescr[$moduleInfo["@"]["NAME"]] = $moduleInfo["@"]["DESCR"];
333
-			}
334
-		}
335
-
336
-		if (\CUpdateClientPartner::UpdateStepModules($updateDir, $errorMessage))
337
-		{
338
-			foreach ($modulesUpdated as $key => $value)
339
-			{
340
-				if (Option::set('main', 'event_log_marketplace', "Y") === "Y")
341
-				{
342
-					\CEventLog::Log("INFO", "MP_MODULE_DOWNLOADED", "main", $key, $value);
343
-				}
344
-			}
345
-		}
346
-		else
347
-		{
348
-			throw new Exception\ModuleUpdateException('[CL04] UpdateStepModules failed. ' . $errorMessage, $this->getName());
349
-		}
350
-
351
-		return true;
352
-	}
353
-
354
-	/**
355
-	 * Check update files
356
-	 * 
357
-	 * @param string $updateDir
358
-	 */
359
-	protected function validateUpdate($updateDir)
360
-	{
361
-		$errorMessage = null;
362
-		if (!\CUpdateClientPartner::CheckUpdatability($updateDir, $errorMessage))
363
-		{
364
-			throw new Exception\ModuleUpdateException('[CL03] CheckUpdatability failed. ' . $errorMessage, $this->getName());
365
-		}
366
-
367
-		if (isset($updateDescription["DATA"]["#"]["ERROR"]))
368
-		{
369
-			$errorMessage = "";
370
-			foreach ($updateDescription["DATA"]["#"]["ERROR"] as $errorDescription)
371
-			{
372
-				$errorMessage .= "[" . $errorDescription["@"]["TYPE"] . "] " . $errorDescription["#"];
373
-			}
374
-			throw new Exception\ModuleUpdateException($errorMessage, $this->getName());
375
-		}
376
-	}
377
-
378
-	/**
379
-	 * Returns module name
380
-	 *
381
-	 * @return string
382
-	 */
383
-	public function getName()
384
-	{
385
-		return $this->name;
386
-	}
387
-
388
-	/**
389
-	 * Beta releases allowed?
390
-	 *
391
-	 * @return boolean
392
-	 */
393
-	public function isBeta()
394
-	{
395
-		return $this->beta;
396
-	}
397
-
398
-	/**
399
-	 * Set beta releases installation
400
-	 *
401
-	 * @param boolean $beta
402
-	 */
403
-	public function setBeta($beta = true)
404
-	{
405
-		$this->beta = $beta;
406
-	}
407
-
408
-	public function getVersion()
409
-	{
410
-		return $this->getObject()->MODULE_VERSION;
411
-	}
21
+    /** @var string */
22
+    private $name;
23
+
24
+    /** @var \CModule */
25
+    private $object;
26
+
27
+    /** @var bool */
28
+    private $beta = false;
29
+
30
+    /**
31
+     * @param string $moduleName
32
+     */
33
+    public function __construct($moduleName)
34
+    {
35
+        $this->name = $this->normalizeName($moduleName);
36
+    }
37
+
38
+    /**
39
+     * @param string $moduleName
40
+     * @return string
41
+     */
42
+    protected function normalizeName($moduleName)
43
+    {
44
+        return preg_replace("/[^a-zA-Z0-9_.]+/i", "", trim($moduleName));
45
+    }
46
+
47
+    /**
48
+     * @return \CModule
49
+     */
50
+    protected function &getObject()
51
+    {
52
+        if (!isset($this->object))
53
+        {
54
+            $this->object = \CModule::CreateModuleObject($this->name);
55
+        }
56
+
57
+        if (!is_object($this->object) || !($this->object instanceof \CModule))
58
+        {
59
+            unset($this->object);
60
+            throw new Exception\ModuleNotFoundException('Module not found or incorrect', $this->name);
61
+        }
62
+
63
+        return $this->object;
64
+    }
65
+
66
+    /**
67
+     * Checks for module and module object existence
68
+     *
69
+     * @return bool
70
+     */
71
+    public function exist()
72
+    {
73
+        try
74
+        {
75
+            $this->getObject();
76
+        }
77
+        catch (Exception\ModuleNotFoundException $e)
78
+        {
79
+            return false;
80
+        }
81
+
82
+        return true;
83
+    }
84
+
85
+    /**
86
+     * Check if module exists and installed
87
+     *
88
+     * @return bool
89
+     */
90
+    public function isRegistered()
91
+    {
92
+        return ModuleManager::isModuleInstalled($this->name) && $this->exist();
93
+    }
94
+
95
+    /**
96
+     * @return bool true for marketplace modules, false for kernel modules
97
+     */
98
+    public function isThirdParty()
99
+    {
100
+        return strpos($this->name, '.') !== false;
101
+    }
102
+
103
+    /**
104
+     * Install module
105
+     *
106
+     * @throws Exception\ModuleException
107
+     * @throws BitrixException
108
+     */
109
+    public function register()
110
+    {
111
+        if (!$this->isRegistered())
112
+        {
113
+            $moduleObject =& $this->getObject();
114
+
115
+            /**
116
+             * It's important to check if module class defines InstallDB method (it must register module)
117
+             * Thus absent InstallDB indicates that the module does not support automatic installation
118
+             */
119
+            if ((new \ReflectionClass($moduleObject))->getMethod('InstallDB')->class !== get_class($moduleObject))
120
+            {
121
+                throw new Exception\ModuleInstallException(
122
+                    'Missing InstallDB method. This module does not support automatic installation',
123
+                    $this->name
124
+                );
125
+            }
126
+
127
+            if (!$moduleObject->InstallDB() && BitrixException::hasException())
128
+            {
129
+                throw new Exception\ModuleInstallException(
130
+                    get_class($moduleObject) . '::InstallDB() returned false',
131
+                    $this->name
132
+                );
133
+            }
134
+
135
+            $moduleObject->InstallEvents();
136
+
137
+            /** @noinspection PhpVoidFunctionResultUsedInspection */
138
+            if (!$moduleObject->InstallFiles() && BitrixException::hasException())
139
+            {
140
+                throw new Exception\ModuleInstallException(
141
+                    get_class($moduleObject) . '::InstallFiles() returned false',
142
+                    $this->name
143
+                );
144
+            }
145
+
146
+            if (!$this->isRegistered())
147
+            {
148
+                throw new Exception\ModuleInstallException(
149
+                    'Module was not registered. Probably it does not support automatic installation.',
150
+                    $this->name
151
+                );
152
+            }
153
+        }
154
+
155
+        return $this;
156
+    }
157
+
158
+    /**
159
+     * Download module from marketplace
160
+     *
161
+     * @return $this
162
+     */
163
+    public function load()
164
+    {
165
+        if (!$this->isRegistered())
166
+        {
167
+            if (!$this->exist())
168
+            {
169
+                require_once($_SERVER["DOCUMENT_ROOT"] . '/bitrix/modules/main/classes/general/update_client_partner.php');
170
+
171
+                if (!\CUpdateClientPartner::LoadModuleNoDemand(
172
+                    $this->getName(),
173
+                    $strError,
174
+                    $this->isBeta() ? 'N' : 'Y',
175
+                    LANGUAGE_ID)
176
+                )
177
+                {
178
+                    throw new Exception\ModuleLoadException($strError, $this->getName());
179
+                }
180
+            }
181
+        }
182
+
183
+        return $this;
184
+    }
185
+
186
+    /**
187
+     * Uninstall module
188
+     *
189
+     * @throws Exception\ModuleException
190
+     * @throws BitrixException
191
+     */
192
+    public function unRegister()
193
+    {
194
+        $moduleObject = $this->getObject();
195
+
196
+        if ($this->isRegistered())
197
+        {
198
+            /**
199
+             * It's important to check if module class defines UnInstallDB method (it should unregister module)
200
+             * Thus absent UnInstallDB indicates that the module does not support automatic uninstallation
201
+             */
202
+            if ((new \ReflectionClass($moduleObject))->getMethod('UnInstallDB')->class !== get_class($moduleObject))
203
+            {
204
+                throw new Exception\ModuleUninstallException(
205
+                    'Missing UnInstallDB method. This module does not support automatic uninstallation',
206
+                    $this->name
207
+                );
208
+            }
209
+
210
+            /** @noinspection PhpVoidFunctionResultUsedInspection */
211
+            if (!$moduleObject->UnInstallFiles() && BitrixException::hasException())
212
+            {
213
+                throw new Exception\ModuleUninstallException(
214
+                    get_class($moduleObject) . '::UnInstallFiles() returned false',
215
+                    $this->name
216
+                );
217
+            }
218
+
219
+            $moduleObject->UnInstallEvents();
220
+
221
+            /** @noinspection PhpVoidFunctionResultUsedInspection */
222
+            if (!$moduleObject->UnInstallDB() && BitrixException::hasException())
223
+            {
224
+                throw new Exception\ModuleUninstallException(
225
+                    get_class($moduleObject) . '::UnInstallFiles() returned false',
226
+                    $this->name
227
+                );
228
+            }
229
+
230
+            if ($this->isRegistered())
231
+            {
232
+                throw new Exception\ModuleUninstallException('Module was not unregistered', $this->name);
233
+            }
234
+        }
235
+
236
+        return $this;
237
+    }
238
+
239
+    /**
240
+     * Uninstall and remove module directory
241
+     */
242
+    public function remove()
243
+    {
244
+        if ($this->isRegistered())
245
+        {
246
+            $this->unRegister();
247
+        }
248
+
249
+        $path = getLocalPath('modules/' . $this->getName());
250
+
251
+        if ($path)
252
+        {
253
+            (new Filesystem())->remove($_SERVER['DOCUMENT_ROOT'] . $path);
254
+        }
255
+
256
+        unset($this->object);
257
+
258
+        return $this;
259
+    }
260
+
261
+    /**
262
+     * Update module
263
+     *
264
+     * It must be called repeatedly until the method returns false.
265
+     * After each call php must be restarted (new process created) to update module class and function definitions.
266
+     *
267
+     * @param array $modulesUpdated [optional]
268
+     * @return bool
269
+     */
270
+    public function update(&$modulesUpdated = null)
271
+    {
272
+        require_once($_SERVER["DOCUMENT_ROOT"] . '/bitrix/modules/main/classes/general/update_client_partner.php');
273
+
274
+        if (!$this->isThirdParty())
275
+        {
276
+            throw new Exception\ModuleUpdateException('Kernel module updates are currently not supported.', $this->getName());
277
+        }
278
+
279
+        // ensures module existence
280
+        $this->getObject();
281
+
282
+        $errorMessage = $updateDescription = null;
283
+        $loadResult = \CUpdateClientPartner::LoadModulesUpdates(
284
+            $errorMessage,
285
+            $updateDescription,
286
+            LANGUAGE_ID,
287
+            $this->isBeta() ? 'N' : 'Y',
288
+            [$this->getName()],
289
+            true
290
+        );
291
+        switch ($loadResult)
292
+        {
293
+            // archive loaded
294
+            case "S":
295
+                return $this->update($modulesUpdated);
296
+
297
+            // error
298
+            case "E":
299
+                throw new Exception\ModuleUpdateException($errorMessage, $this->getName());
300
+
301
+            // finished installing updates
302
+            case "F":
303
+                return false;
304
+
305
+            // need to process loaded update
306
+            case 'U':
307
+                break;
308
+        }
309
+
310
+        /** @var string Temp directory with update files */
311
+        $updateDir = null;
312
+
313
+        if (!\CUpdateClientPartner::UnGzipArchive($updateDir, $errorMessage, true))
314
+        {
315
+            throw new Exception\ModuleUpdateException('[CL02] UnGzipArchive failed. ' . $errorMessage, $this->getName());
316
+        }
317
+
318
+        $this->validateUpdate($updateDir);
319
+
320
+        if (isset($updateDescription["DATA"]["#"]["NOUPDATES"]))
321
+        {
322
+            \CUpdateClientPartner::ClearUpdateFolder($_SERVER["DOCUMENT_ROOT"] . "/bitrix/updates/" . $updateDir);
323
+            return false;
324
+        }
325
+
326
+        $modulesUpdated = $updateDescr = [];
327
+        if (isset($updateDescription["DATA"]["#"]["ITEM"]))
328
+        {
329
+            foreach ($updateDescription["DATA"]["#"]["ITEM"] as $moduleInfo)
330
+            {
331
+                $modulesUpdated[$moduleInfo["@"]["NAME"]] = $moduleInfo["@"]["VALUE"];
332
+                $updateDescr[$moduleInfo["@"]["NAME"]] = $moduleInfo["@"]["DESCR"];
333
+            }
334
+        }
335
+
336
+        if (\CUpdateClientPartner::UpdateStepModules($updateDir, $errorMessage))
337
+        {
338
+            foreach ($modulesUpdated as $key => $value)
339
+            {
340
+                if (Option::set('main', 'event_log_marketplace', "Y") === "Y")
341
+                {
342
+                    \CEventLog::Log("INFO", "MP_MODULE_DOWNLOADED", "main", $key, $value);
343
+                }
344
+            }
345
+        }
346
+        else
347
+        {
348
+            throw new Exception\ModuleUpdateException('[CL04] UpdateStepModules failed. ' . $errorMessage, $this->getName());
349
+        }
350
+
351
+        return true;
352
+    }
353
+
354
+    /**
355
+     * Check update files
356
+     * 
357
+     * @param string $updateDir
358
+     */
359
+    protected function validateUpdate($updateDir)
360
+    {
361
+        $errorMessage = null;
362
+        if (!\CUpdateClientPartner::CheckUpdatability($updateDir, $errorMessage))
363
+        {
364
+            throw new Exception\ModuleUpdateException('[CL03] CheckUpdatability failed. ' . $errorMessage, $this->getName());
365
+        }
366
+
367
+        if (isset($updateDescription["DATA"]["#"]["ERROR"]))
368
+        {
369
+            $errorMessage = "";
370
+            foreach ($updateDescription["DATA"]["#"]["ERROR"] as $errorDescription)
371
+            {
372
+                $errorMessage .= "[" . $errorDescription["@"]["TYPE"] . "] " . $errorDescription["#"];
373
+            }
374
+            throw new Exception\ModuleUpdateException($errorMessage, $this->getName());
375
+        }
376
+    }
377
+
378
+    /**
379
+     * Returns module name
380
+     *
381
+     * @return string
382
+     */
383
+    public function getName()
384
+    {
385
+        return $this->name;
386
+    }
387
+
388
+    /**
389
+     * Beta releases allowed?
390
+     *
391
+     * @return boolean
392
+     */
393
+    public function isBeta()
394
+    {
395
+        return $this->beta;
396
+    }
397
+
398
+    /**
399
+     * Set beta releases installation
400
+     *
401
+     * @param boolean $beta
402
+     */
403
+    public function setBeta($beta = true)
404
+    {
405
+        $this->beta = $beta;
406
+    }
407
+
408
+    public function getVersion()
409
+    {
410
+        return $this->getObject()->MODULE_VERSION;
411
+    }
412 412
 }
413 413
\ No newline at end of file
Please login to merge, or discard this patch.
src/Module/Exception/ModuleInstallException.php 1 patch
Indentation   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -1,8 +1,8 @@
 block discarded – undo
1 1
 <?php
2 2
 /**
3
- * For the full copyright and license information, please view the LICENSE.md
4
- * file that was distributed with this source code.
5
- */
3
+     * For the full copyright and license information, please view the LICENSE.md
4
+     * file that was distributed with this source code.
5
+     */
6 6
 
7 7
 namespace Notamedia\ConsoleJedi\Module\Exception;
8 8
 
Please login to merge, or discard this patch.
src/Module/Exception/ModuleException.php 1 patch
Indentation   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -8,15 +8,15 @@
 block discarded – undo
8 8
 
9 9
 class ModuleException extends \RuntimeException
10 10
 {
11
-	/**
12
-	 * ModuleException constructor.
13
-	 *
14
-	 * @param string $message
15
-	 * @param int $moduleName
16
-	 * @param \Exception|null $previous
17
-	 */
18
-	public function __construct($message, $moduleName, \Exception $previous = null)
19
-	{
20
-		parent::__construct("[$moduleName] " . $message, 0, $previous);
21
-	}
11
+    /**
12
+     * ModuleException constructor.
13
+     *
14
+     * @param string $message
15
+     * @param int $moduleName
16
+     * @param \Exception|null $previous
17
+     */
18
+    public function __construct($message, $moduleName, \Exception $previous = null)
19
+    {
20
+        parent::__construct("[$moduleName] " . $message, 0, $previous);
21
+    }
22 22
 }
23 23
\ No newline at end of file
Please login to merge, or discard this patch.
src/Module/Exception/ModuleUninstallException.php 1 patch
Indentation   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -1,8 +1,8 @@
 block discarded – undo
1 1
 <?php
2 2
 /**
3
- * For the full copyright and license information, please view the LICENSE.md
4
- * file that was distributed with this source code.
5
- */
3
+     * For the full copyright and license information, please view the LICENSE.md
4
+     * file that was distributed with this source code.
5
+     */
6 6
 
7 7
 namespace Notamedia\ConsoleJedi\Module\Exception;
8 8
 
Please login to merge, or discard this patch.