Passed
Push — master ( fa7ac3...812e1d )
by Ferry
04:19
created

Plugin::getAll()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 14
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 23
rs 9.4888
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: User
5
 * Date: 8/12/2019
6
 * Time: 11:59 PM
7
 */
8
9
namespace crocodicstudio\crudbooster\helpers;
10
11
use Illuminate\Support\Facades\Route;
12
13
class Plugin
14
{
15
16
    public static function has($key)
17
    {
18
        if(file_exists(app_path("CBPlugins/".$key))) return true;
19
        else return false;
20
    }
21
22
    public static function isNeedUpgrade($pluginKey, $versionToCompare)
23
    {
24
        $pluginJson = json_decode(file_get_contents(app_path("CBPlugins/".$pluginKey."/plugin.json")), true);
25
        if($pluginJson) {
26
            return version_compare($pluginJson['version'], $versionToCompare,"!=");
27
        }else{
28
            return false;
29
        }
30
    }
31
32
    public static function registerDefaultRoute($dir)
33
    {
34
        Route::group(['middleware' => ['web',\crocodicstudio\crudbooster\middlewares\CBDeveloper::class],
35
            'prefix'=>"developer/".getSetting('developer_path'),
36
            'namespace' => 'App\CBPlugins\\'.basename(dirname("./../".$dir)).'\Controllers'], function () use ($dir) {
37
            cb()->routeController("plugins/".basename(dirname("./../".$dir)),"\App\CBPlugins\\".basename(dirname("./../".$dir))."\Controllers\\".basename(dirname("./../".$dir))."Controller");
38
        });
39
    }
40
41
    public function getAll()
42
    {
43
        $plugins = scandir(app_path("CBPlugins"));
44
45
        $result = [];
46
        foreach($plugins as $plugin) {
47
            if($plugin != "." && $plugin != "..") {
48
                $basename = basename($plugin);
49
                $row = json_decode(file_get_contents(app_path("CBPlugins".DIRECTORY_SEPARATOR.$plugin.DIRECTORY_SEPARATOR."plugin.json")), true);
50
                if($row) {
51
                    $result[] = [
52
                        "name"=>$row['name'],
53
                        "version"=>$row['version'],
54
                        "icon"=>$row['icon'],
55
                        "url"=>route($basename."ControllerGetIndex")
56
                    ];
57
                }
58
            }
59
        }
60
61
        $result = collect($result)->sortBy("name")->values()->all();
62
63
        return $result;
64
    }
65
}