Passed
Push — master ( db619f...0a9de2 )
by Ferry
04:28
created

Plugin::getAllThemes()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 0
dl 0
loc 11
rs 10
c 0
b 0
f 0
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 getAllThemes() {
42
        $plugins_from_user = $this->getAll();
43
        $plugins_from_master = $this->getAll(__DIR__."/../views/themes");
44
        $result = [];
45
        $plugins = array_merge($plugins_from_master, $plugins_from_user);
46
        foreach($plugins as $plugin) {
47
            if($plugin['type'] == "theme") {
48
                $result[] = $plugin;
49
            }
50
        }
51
        return $result;
52
    }
53
54
    public function getAll($path = null)
55
    {
56
        $path = ($path)?:app_path("CBPlugins");
57
        $plugins = scandir($path);
58
59
        $result = [];
60
        foreach($plugins as $plugin) {
61
            if($plugin != "." && $plugin != "..") {
62
                $basename = basename($plugin);
63
                $row = json_decode(file_get_contents($path.DIRECTORY_SEPARATOR.$plugin.DIRECTORY_SEPARATOR."plugin.json"), true);
64
                if($row) {
65
                    try {
66
                        $row['url'] = route($basename."ControllerGetIndex");
67
                    } catch (\Exception $e) {
68
                        $row['url'] = null;
69
                    }
70
                    $result[] = $row;
71
                }
72
            }
73
        }
74
75
        $result = collect($result)->sortBy("name")->values()->all();
76
77
        return $result;
78
    }
79
}