Apps::getInfo()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 3
nop 1
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
  * osCommerce Online Merchant
4
  *
5
  * @copyright (c) 2016 osCommerce; https://www.oscommerce.com
6
  * @license MIT; https://www.oscommerce.com/license/mit.txt
7
  */
8
9
namespace OSC\OM;
10
11
use OSC\OM\OSCOM;
12
use OSC\OM\Registry;
13
14
class Apps
15
{
16
    public static function getAll()
17
    {
18
        $result = [];
19
20
        $apps_directory = OSCOM::BASE_DIR . 'Apps';
21
22
        if ($vdir = new \DirectoryIterator($apps_directory)) {
23
            foreach ($vdir as $vendor) {
24
                if (!$vendor->isDot() && $vendor->isDir()) {
25
                    if ($adir = new \DirectoryIterator($vendor->getPath() . '/' . $vendor->getFilename())) {
26
                        foreach ($adir as $app) {
27
                            if (!$app->isDot() && $app->isDir() && static::exists($vendor->getFilename() . '\\' . $app->getFilename())) {
28
                                if (($json = static::getInfo($vendor->getFilename() . '\\' . $app->getFilename())) !== false) {
29
                                    $result[] = $json;
30
                                }
31
                            }
32
                        }
33
                    }
34
                }
35
            }
36
        }
37
38
        return $result;
39
    }
40
41
    public static function getModules($type, $filter_vendor_app = null, $filter = null)
42
    {
43
        $result = [];
44
45
        if (!Registry::exists('ModuleType' . $type)) {
46
            $class = 'OSC\OM\Modules\\' . $type;
47
48
            if (!class_exists($class)) {
49
                trigger_error('OSC\OM\Apps::getModules(): ' . $type . ' module class not found in OSC\OM\Modules\\');
50
51
                return $result;
52
            }
53
54
            Registry::set('ModuleType' . $type, new $class());
55
        }
56
57
        $OSCOM_Type = Registry::get('ModuleType' . $type);
58
59
        $filter_vendor = $filter_app = null;
60
61 View Code Duplication
        if (isset($filter_vendor_app)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
            if (strpos($filter_vendor_app, '\\') !== false) {
63
                list($filter_vendor, $filter_app) = explode('\\', $filter_vendor_app, 2);
64
            } else {
65
                $filter_vendor = $filter_vendor_app;
66
            }
67
        }
68
69
        $vendor_directory = OSCOM::BASE_DIR . 'Apps';
70
71
        if (is_dir($vendor_directory)) {
72
            if ($vdir = new \DirectoryIterator($vendor_directory)) {
73
                foreach ($vdir as $vendor) {
74
                    if (!$vendor->isDot() && $vendor->isDir() && (!isset($filter_vendor) || ($vendor->getFilename() == $filter_vendor))) {
75
                        if ($adir = new \DirectoryIterator($vendor->getPath() . '/' . $vendor->getFilename())) {
76
                            foreach ($adir as $app) {
77
                                if (!$app->isDot() && $app->isDir() && (!isset($filter_app) || ($app->getFilename() == $filter_app)) && static::exists($vendor->getFilename() . '\\' . $app->getFilename()) && (($json = static::getInfo($vendor->getFilename() . '\\' . $app->getFilename())) !== false)) {
78
                                    if (isset($json['modules'][$type])) {
79
                                        $modules = $json['modules'][$type];
80
81
                                        if (isset($filter)) {
82
                                            $modules = $OSCOM_Type->filter($modules, $filter);
83
                                        }
84
85
                                        foreach ($modules as $key => $data) {
86
                                            $result = array_merge($result, $OSCOM_Type->getInfo($vendor->getFilename() . '\\' . $app->getFilename(), $key, $data));
87
                                        }
88
                                    }
89
                                }
90
                            }
91
                        }
92
                    }
93
                }
94
            }
95
        }
96
97
        return $result;
98
    }
99
100
    public static function exists($app)
101
    {
102
        if (strpos($app, '\\') !== false) {
103
            list($vendor, $app) = explode('\\', $app, 2);
104
105
            if (class_exists('OSC\Apps\\' . $vendor . '\\' . $app . '\\' . $app)) {
106
                if (is_subclass_of('OSC\Apps\\' . $vendor . '\\' . $app . '\\' . $app, 'OSC\OM\AppAbstract')) {
107
                    return true;
108
                } else {
109
                    trigger_error('OSC\OM\Apps::exists(): ' . $vendor . '\\' . $app . ' - App is not a subclass of OSC\OM\AppAbstract and cannot be loaded.');
110
                }
111
            }
112
        } else {
113
            trigger_error('OSC\OM\Apps::exists(): ' . $app . ' - Invalid format, must be: Vendor\App.');
114
        }
115
116
        return false;
117
    }
118
119
    public static function getModuleClass($module, $type)
120
    {
121
        if (!Registry::exists('ModuleType' . $type)) {
122
            $class = 'OSC\OM\Modules\\' . $type;
123
124
            if (!class_exists($class)) {
125
                trigger_error('OSC\OM\Apps::getModuleClass(): ' . $type . ' module class not found in OSC\OM\Modules\\');
126
127
                return false;
128
            }
129
130
            Registry::set('ModuleType' . $type, new $class());
131
        }
132
133
        $OSCOM_Type = Registry::get('ModuleType' . $type);
134
135
        return $OSCOM_Type->getClass($module);
136
    }
137
138
    public static function getInfo($app)
139
    {
140
        if (strpos($app, '\\') !== false) {
141
            list($vendor, $app) = explode('\\', $app, 2);
142
143
            $metafile = OSCOM::BASE_DIR . 'Apps/' . basename($vendor) . '/' . basename($app) . '/oscommerce.json';
144
145
            if (is_file($metafile) && (($json = json_decode(file_get_contents($metafile), true)) !== null)) {
146
                return $json;
147
            }
148
149
            trigger_error('OSC\OM\Apps::getInfo(): ' . $vendor . '\\' . $app . ' - Could not read App information in ' . $metafile . '.');
150
        } else {
151
            trigger_error('OSC\OM\Apps::getInfo(): ' . $app . ' - Invalid format, must be: Vendor\App.');
152
        }
153
154
        return false;
155
    }
156
157
    public static function getRouteDestination($route = null, $filter_vendor_app = null)
158
    {
159
        if (empty($route)) {
160
            $route = array_keys($_GET);
161
        }
162
163
        $result = $routes = [];
164
165
        if (empty($route)) {
166
            return $result;
167
        }
168
169
        $filter_vendor = $filter_app = null;
170
171 View Code Duplication
        if (isset($filter_vendor_app)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
172
            if (strpos($filter_vendor_app, '\\') !== false) {
173
                list($filter_vendor, $filter_app) = explode('\\', $filter_vendor_app, 2);
174
            } else {
175
                $filter_vendor = $filter_vendor_app;
176
            }
177
        }
178
179
        $vendor_directory = OSCOM::BASE_DIR . 'Apps';
180
181
        if (is_dir($vendor_directory)) {
182
            if ($vdir = new \DirectoryIterator($vendor_directory)) {
183
                foreach ($vdir as $vendor) {
184
                    if (!$vendor->isDot() && $vendor->isDir() && (!isset($filter_vendor) || ($vendor->getFilename() == $filter_vendor))) {
185
                        if ($adir = new \DirectoryIterator($vendor->getPath() . '/' . $vendor->getFilename())) {
186
                            foreach ($adir as $app) {
187
                                if (!$app->isDot() && $app->isDir() && (!isset($filter_app) || ($app->getFilename() == $filter_app)) && static::exists($vendor->getFilename() . '\\' . $app->getFilename()) && (($json = static::getInfo($vendor->getFilename() . '\\' . $app->getFilename())) !== false)) {
188
                                    if (isset($json['routes'][OSCOM::getSite()])) {
189
                                        $routes[$json['vendor'] . '\\' . $json['app']] = $json['routes'][OSCOM::getSite()];
190
                                    }
191
                                }
192
                            }
193
                        }
194
                    }
195
                }
196
            }
197
        }
198
199
        return call_user_func([
200
            'OSC\Sites\\' . OSCOM::getSite() . '\\' . OSCOM::getSite(),
201
            'resolveRoute'
202
        ], $route, $routes);
203
    }
204
}
205