• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

visavi / rotor / 27267213075

10 Jun 2026 09:35AM UTC coverage: 13.943% (+0.08%) from 13.868%
27267213075

push

github

visavi
Поправил кешировование настроек, тесты

8 of 14 new or added lines in 6 files covered. (57.14%)

2 existing lines in 1 file now uncovered.

807 of 5788 relevant lines covered (13.94%)

1.41 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/app/Http/Controllers/Admin/ModuleController.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace App\Http\Controllers\Admin;
6

7
use App\Models\Module;
8
use App\Models\ModuleRegistry;
9
use Illuminate\Http\RedirectResponse;
10
use Illuminate\Http\Request;
11
use Illuminate\Support\Facades\Artisan;
12
use Illuminate\Support\Facades\Http;
13
use Illuminate\View\View;
14
use ZipArchive;
15

16
class ModuleController extends AdminController
17
{
18
    /**
19
     * Главная страница
20
     */
21
    public function index(): View
×
22
    {
23
        $modules = Module::query()->get();
×
24
        $moduleInstall = [];
×
25
        foreach ($modules as $module) {
×
26
            $moduleInstall[$module->name] = $module;
×
27
        }
28

29
        $moduleNames = [];
×
30
        $modulesLoaded = glob(base_path('modules/*'), GLOB_ONLYDIR);
×
31
        foreach ($modulesLoaded as $module) {
×
32
            if (file_exists($module . '/module.php')) {
×
33
                $moduleNames[basename($module)] = include $module . '/module.php';
×
34
            }
35
        }
36

37
        $installed = array_intersect_key($moduleInstall, $moduleNames);
×
38
        $counts = [
×
39
            'all'           => count($moduleNames),
×
40
            'installed'     => count(array_filter($installed, fn ($m) => $m->active)),
×
41
            'disabled'      => count(array_filter($installed, fn ($m) => ! $m->active)),
×
42
            'not-installed' => count($moduleNames) - count($installed),
×
43
        ];
×
44

45
        $registryModules = ModuleRegistry::getAvailableModules();
×
46

47
        return view('admin/modules/index', compact('moduleInstall', 'moduleNames', 'counts', 'registryModules'));
×
48
    }
49

50
    /**
51
     * Просмотр модуля
52
     */
53
    public function module(Request $request): View
×
54
    {
55
        $moduleName = (string) $request->input('module');
×
56
        $modulePath = base_path('modules/' . $moduleName);
×
57

58
        if (! preg_match('|^[A-Z][\w\-]+$|', $moduleName) || ! file_exists($modulePath)) {
×
59
            abort(200, __('admin.modules.module_not_found'));
×
60
        }
61

62
        $moduleConfig = include $modulePath . '/module.php';
×
63
        $module = Module::query()->where('name', $moduleName)->first();
×
64

65
        if (file_exists($modulePath . '/screenshots')) {
×
66
            $moduleConfig['screenshots'] = glob($modulePath . '/screenshots/*.{gif,png,jpg,jpeg,webp}', GLOB_BRACE);
×
67
        }
68

69
        if (file_exists($modulePath . '/database/migrations')) {
×
70
            $migrations = [];
×
71
            foreach (glob($modulePath . '/database/migrations/*.php') as $migration) {
×
72
                $migrations[basename($migration)] = file_get_contents($migration);
×
73
            }
74
            $moduleConfig['migrations'] = $migrations;
×
75
        }
76

77
        if (file_exists($modulePath . '/resources/assets')) {
×
78
            $moduleConfig['symlink'] = Module::getLinkNameByPath($modulePath);
×
79
        }
80

81
        if (file_exists($modulePath . '/config.php')) {
×
82
            $moduleConfig['config'] = file_get_contents($modulePath . '/config.php');
×
83
        }
84

85
        if ($module && $module->settings) {
×
86
            $moduleConfig['settings'] = var_export($module->settings, true);
×
87
        }
88

89
        if (file_exists($modulePath . '/routes.php')) {
×
90
            $moduleConfig['routes'] = file_get_contents($modulePath . '/routes.php');
×
91
        }
92

93
        if (file_exists($modulePath . '/hooks.php')) {
×
94
            $moduleConfig['hooks'] = file_get_contents($modulePath . '/hooks.php');
×
95
        }
96

97
        if (file_exists($modulePath . '/helpers.php')) {
×
98
            $moduleConfig['helpers'] = file_get_contents($modulePath . '/helpers.php');
×
99
        }
100

101
        if (file_exists($modulePath . '/middleware.php')) {
×
102
            $moduleConfig['middleware'] = file_get_contents($modulePath . '/middleware.php');
×
103
        }
104

105
        $registryInfo = ModuleRegistry::getAvailableModules()[$moduleName] ?? null;
×
106

107
        return view('admin/modules/module', compact('module', 'moduleConfig', 'moduleName', 'registryInfo'));
×
108
    }
109

110
    /**
111
     * Установка модуля
112
     */
113
    public function install(Request $request): RedirectResponse
×
114
    {
115
        $moduleName = $request->input('module');
×
116
        $enable = int($request->input('enable'));
×
117
        $update = int($request->input('update'));
×
118
        $modulePath = base_path('modules/' . $moduleName);
×
119

120
        if (! preg_match('|^[A-Z][\w\-]+$|', $moduleName) || ! file_exists($modulePath)) {
×
121
            abort(200, __('admin.modules.module_not_found'));
×
122
        }
123

124
        $module = Module::query()->firstOrNew(['name' => $moduleName]);
×
125

126
        $moduleConfig = include $modulePath . '/module.php';
×
127
        $module->createSymlink();
×
128
        $module->migrate();
×
129

130
        Artisan::call('route:clear');
×
131
        $result = __('admin.modules.module_success_installed');
×
132

133
        if ($module->exists) {
×
134
            if ($update) {
×
135
                $module->update([
×
136
                    'version'    => $moduleConfig['version'],
×
137
                    'updated_at' => SITETIME,
×
138
                ]);
×
139
                $result = __('admin.modules.module_success_updated');
×
140
            }
141

142
            if ($enable) {
×
143
                $module->update([
×
144
                    'active'     => true,
×
145
                    'updated_at' => SITETIME,
×
146
                ]);
×
147
                $result = __('admin.modules.module_success_enabled');
×
148
            }
149
        } else {
150
            $module->fill([
×
151
                'version'    => $moduleConfig['version'],
×
152
                'updated_at' => SITETIME,
×
153
                'created_at' => SITETIME,
×
154
            ])->save();
×
155
        }
156

NEW
157
        clearCache(['modules', 'settings']);
×
158
        setFlash('success', $result);
×
159

160
        return redirect('admin/modules/module?module=' . $moduleName);
×
161
    }
162

163
    /**
164
     * Каталог модулей из реестров
165
     */
166
    public function marketplace(Request $request): View
×
167
    {
168
        $force = (bool) $request->input('refresh');
×
169
        $available = ModuleRegistry::getAvailableModules($force);
×
170

171
        $modules = Module::query()->get()->keyBy('name');
×
172
        $moduleNames = [];
×
173

174
        $modulesLoaded = glob(base_path('modules/*'), GLOB_ONLYDIR);
×
175
        foreach ($modulesLoaded as $module) {
×
176
            $moduleNames[] = basename($module);
×
177
        }
178

179
        $counts = ['all' => count($available), 'installed' => 0, 'disabled' => 0, 'not-installed' => 0];
×
180
        foreach ($available as $name => $info) {
×
181
            $localExists = in_array($name, $moduleNames, true);
×
182
            $installed = $modules->has($name) && $localExists;
×
183

184
            if ($installed) {
×
185
                $counts[$modules[$name]->active ? 'installed' : 'disabled']++;
×
186
            } else {
187
                $counts['not-installed']++;
×
188
            }
189
        }
190

191
        return view('admin/modules/marketplace', compact('available', 'modules', 'moduleNames', 'counts'));
×
192
    }
193

194
    /**
195
     * Форма загрузки модуля
196
     */
197
    public function upload(): View
×
198
    {
199
        return view('admin/modules/upload');
×
200
    }
201

202
    /**
203
     * Установка модуля из ZIP-файла
204
     */
205
    public function uploadZip(Request $request): RedirectResponse
×
206
    {
207
        if (! $request->hasFile('zip') || ! $request->file('zip')->isValid()) {
×
208
            setFlash('danger', __('admin.modules.upload_invalid_file'));
×
209

210
            return redirect()->route('admin.modules.upload');
×
211
        }
212

213
        try {
214
            $moduleName = $this->extractZip($request->file('zip')->getPathname());
×
215
        } catch (\Exception $e) {
×
216
            setFlash('danger', $e->getMessage());
×
217

218
            return redirect()->route('admin.modules.upload');
×
219
        }
220

221
        $isUpdate = Module::query()->where('name', $moduleName)->exists();
×
222
        $redirect = $isUpdate
×
223
            ? '/admin/modules/install?module=' . $moduleName . '&update=1'
×
224
            : '/admin/modules/module?module=' . $moduleName;
×
225

226
        return redirect($redirect)
×
227
            ->with('success', __('admin.modules.upload_success_extracted'));
×
228
    }
229

230
    /**
231
     * Установка модуля по URL
232
     */
233
    public function download(Request $request): RedirectResponse
×
234
    {
235
        $url = trim($request->input('url', ''));
×
236

237
        if (! filter_var($url, FILTER_VALIDATE_URL)) {
×
238
            setFlash('danger', __('admin.modules.download_invalid_url'));
×
239

240
            return redirect()->back();
×
241
        }
242

243
        $maxSize = (int) setting('filesize') * 1024;
×
244

245
        try {
246
            $response = Http::timeout(30)->get($url);
×
247

248
            if (! $response->ok()) {
×
249
                setFlash('danger', __('admin.modules.download_failed'));
×
250

251
                return redirect()->back();
×
252
            }
253

254
            $body = $response->body();
×
255

256
            if (substr($body, 0, 4) !== "PK\x03\x04") {
×
257
                setFlash('danger', __('admin.modules.download_not_zip'));
×
258

259
                return redirect()->back();
×
260
            }
261

262
            $contentLength = (int) $response->header('Content-Length');
×
263
            if (($contentLength > 0 && $contentLength > $maxSize) || strlen($body) > $maxSize) {
×
264
                setFlash('danger', __('admin.modules.download_too_large', ['size' => formatSize($maxSize)]));
×
265

266
                return redirect()->back();
×
267
            }
268

269
            $tempDir = storage_path('app/temp');
×
270
            if (! is_dir($tempDir)) {
×
271
                mkdir($tempDir, 0755, true);
×
272
            }
273
            $tempFile = $tempDir . '/rotor_module_' . uniqid() . '.zip';
×
274
            file_put_contents($tempFile, $body);
×
275

276
            try {
277
                $moduleName = $this->extractZip($tempFile);
×
278
            } finally {
279
                @unlink($tempFile);
×
280
            }
281
        } catch (\Exception $e) {
×
282
            setFlash('danger', $e->getMessage());
×
283

284
            return redirect()->back();
×
285
        }
286

287
        $isUpdate = Module::query()->where('name', $moduleName)->exists();
×
288
        $redirect = $isUpdate
×
289
            ? '/admin/modules/install?module=' . $moduleName . '&update=1'
×
290
            : '/admin/modules/module?module=' . $moduleName;
×
291

292
        return redirect($redirect)
×
293
            ->with('success', __('admin.modules.upload_success_extracted'));
×
294
    }
295

296
    /**
297
     * Распаковка ZIP-архива модуля
298
     */
299
    private function extractZip(string $zipPath): string
×
300
    {
301
        $zip = new ZipArchive();
×
302

303
        if ($zip->open($zipPath) !== true) {
×
304
            throw new \RuntimeException(__('admin.modules.zip_open_failed'));
×
305
        }
306

307
        $topDirs = [];
×
308
        for ($i = 0; $i < $zip->numFiles; $i++) {
×
309
            $name = $zip->getNameIndex($i);
×
310

311
            if (str_contains($name, '..')) {
×
312
                $zip->close();
×
313
                throw new \RuntimeException(__('admin.modules.zip_invalid_path'));
×
314
            }
315

316
            $parts = explode('/', $name);
×
317
            if ($parts[0] !== '') {
×
318
                $topDirs[$parts[0]] = true;
×
319
            }
320
        }
321

322
        if (count($topDirs) !== 1) {
×
323
            $zip->close();
×
324
            throw new \RuntimeException(__('admin.modules.zip_invalid_structure'));
×
325
        }
326

327
        $moduleName = array_key_first($topDirs);
×
328

329
        if (! preg_match('/^[A-Z][A-Za-z0-9]+$/', $moduleName)) {
×
330
            $zip->close();
×
331
            throw new \RuntimeException(__('admin.modules.zip_invalid_name'));
×
332
        }
333

334
        $targetPath = base_path('modules/' . $moduleName);
×
335

336
        // Существующий модуль уводим в резервную копию, чтобы чистая распаковка
337
        // не оставила старых файлов и можно было откатиться при сбое
338
        $backupPath = null;
×
339
        if (is_dir($targetPath)) {
×
340
            $backupPath = base_path('modules/.backup_' . $moduleName . '_' . time());
×
341
            if (! rename($targetPath, $backupPath)) {
×
342
                $zip->close();
×
343
                throw new \RuntimeException(__('admin.modules.zip_backup_failed'));
×
344
            }
345
        }
346

347
        if (! $zip->extractTo(base_path('modules/'))) {
×
348
            $zip->close();
×
349
            $this->restoreBackup($targetPath, $backupPath);
×
350
            throw new \RuntimeException(__('admin.modules.zip_extract_failed'));
×
351
        }
352
        $zip->close();
×
353

354
        $this->chmodRecursive($targetPath);
×
355

356
        if (! file_exists($targetPath . '/module.php')) {
×
357
            $this->restoreBackup($targetPath, $backupPath);
×
358
            throw new \RuntimeException(__('admin.modules.zip_no_module_file'));
×
359
        }
360

361
        if ($backupPath) {
×
362
            $this->deleteDirectory($backupPath);
×
363
        }
364

365
        return $moduleName;
×
366
    }
367

368
    /**
369
     * Удаление файлов модуля с диска
370
     */
371
    public function deleteFiles(Request $request): RedirectResponse
×
372
    {
373
        $moduleName = $request->input('module');
×
374
        $modulePath = base_path('modules/' . $moduleName);
×
375

376
        if (! preg_match('|^[A-Z][\w\-]+$|', $moduleName) || ! file_exists($modulePath)) {
×
377
            abort(200, __('admin.modules.module_not_found'));
×
378
        }
379

380
        if (Module::query()->where('name', $moduleName)->exists()) {
×
381
            abort(200, __('admin.modules.delete_files_not_uninstalled'));
×
382
        }
383

384
        $this->deleteDirectory($modulePath);
×
385

386
        Artisan::call('route:clear');
×
387
        setFlash('success', __('admin.modules.module_files_deleted'));
×
388

389
        return redirect()->route('admin.modules.index');
×
390
    }
391

392
    /**
393
     * Откат распаковки: удалить частично распакованное и вернуть резервную копию
394
     */
395
    private function restoreBackup(string $targetPath, ?string $backupPath): void
×
396
    {
397
        $this->deleteDirectory($targetPath);
×
398

399
        if ($backupPath && is_dir($backupPath)) {
×
400
            rename($backupPath, $targetPath);
×
401
        }
402
    }
403

404
    /**
405
     * Рекурсивно устанавливает права доступа (755 для директорий, 644 для файлов)
406
     */
UNCOV
407
    private function chmodRecursive(string $path): void
×
408
    {
409
        chmod($path, 0755);
×
410

411
        foreach (scandir($path) as $item) {
×
412
            if ($item === '.' || $item === '..') {
×
413
                continue;
×
414
            }
415

416
            $full = $path . '/' . $item;
×
417
            if (is_dir($full)) {
×
418
                $this->chmodRecursive($full);
×
419
            } else {
420
                chmod($full, 0644);
×
421
            }
422
        }
423
    }
424

425
    /**
426
     * Рекурсивно удаляет директорию, включая симлинки
427
     */
UNCOV
428
    private function deleteDirectory(string $path): void
×
429
    {
430
        if (is_link($path)) {
×
431
            unlink($path);
×
432

433
            return;
×
434
        }
435

436
        if (! is_dir($path)) {
×
437
            return;
×
438
        }
439

440
        foreach (scandir($path) as $item) {
×
441
            if ($item === '.' || $item === '..') {
×
442
                continue;
×
443
            }
444

445
            $full = $path . '/' . $item;
×
446
            is_dir($full) && ! is_link($full) ? $this->deleteDirectory($full) : unlink($full);
×
447
        }
448

449
        rmdir($path);
×
450
    }
451

452
    /**
453
     * Удаление/Выключение модуля
454
     */
455
    public function uninstall(Request $request): RedirectResponse
×
456
    {
457
        $moduleName = $request->input('module');
×
458
        $disable = int($request->input('disable'));
×
459
        $modulePath = base_path('modules/' . $moduleName);
×
460

461
        if (! preg_match('|^[A-Z][\w\-]+$|', $moduleName) || ! file_exists($modulePath)) {
×
462
            abort(200, __('admin.modules.module_not_found'));
×
463
        }
464

465
        $module = Module::query()->where('name', $moduleName)->first();
×
466
        if (! $module) {
×
467
            abort(200, __('admin.modules.module_not_found'));
×
468
        }
469

470
        $module->deleteSymlink();
×
471
        Artisan::call('route:clear');
×
472

473
        if ($disable) {
×
474
            $module->update([
×
475
                'active'     => false,
×
476
                'updated_at' => SITETIME,
×
477
            ]);
×
478
            $result = __('admin.modules.module_success_disabled');
×
479
        } else {
480
            if (env('MODULES_SAFE_MODE', false)) {
×
481
                setFlash('danger', __('admin.modules.safe_mode_enabled'));
×
482

483
                return redirect('admin/modules/module?module=' . $moduleName);
×
484
            }
485

486
            $module->rollback();
×
487
            $module->delete();
×
488
            $result = __('admin.modules.module_success_deleted');
×
489
        }
490

NEW
491
        clearCache(['modules', 'settings']);
×
492
        setFlash('success', $result);
×
493

494
        return redirect('admin/modules/module?module=' . $moduleName);
×
495
    }
496
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc