Test Setup Failed
Branch master (8be614)
by Php Easy Api
06:15 queued 03:31
created

Utils::glob()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 4
nop 2
dl 0
loc 15
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Resta\Support;
4
5
class Utils
6
{
7
    /**
8
     * @var array $bool
9
     */
10
    private static $bool = [];
11
12
    /**
13
     * string upper case
14
     *
15
     * @param $argument
16
     * @param bool $shift
17
     * @return array
18
     */
19
    public static function upperCase($argument,$shift=true)
20
    {
21
        if($shift){
22
            array_shift($argument);
23
        }
24
25
        return array_map(function($argument){
26
            return ucfirst($argument);
27
        },$argument);
28
    }
29
30
    /**
31
     * encrypt array data
32
     *
33
     * @param $class
34
     * @return string
35
     */
36
    public static function encryptArrayData($class)
37
    {
38
        //the serialized class data
39
        return md5(serialize($class));
40
    }
41
42
43
    /**
44
     * @param $argument
45
     * @return array|string
46
     */
47
    public static function strtolower($argument)
48
    {
49
        if(!is_array($argument)){
50
            return strtolower($argument);
51
        }
52
        return array_map(function($argument){
53
            return strtolower($argument);
54
        },$argument);
55
    }
56
57
    /**
58
     * @param array $data
59
     * @return string
60
     */
61
    public static function generatorNamespace($data=array())
62
    {
63
        return str_replace('.php','',implode("\\",$data));
64
    }
65
66
    /**
67
     * @param $class
68
     * @param bool $extension
69
     * @return mixed
70
     */
71
    public static function getPathFromNamespace($class,$extension=true)
72
    {
73
        if($extension){
74
            $default=root.'/'.str_replace("\\","/",$class).'.php';
75
        }
76
        else{
77
            $default=root.'/'.str_replace("\\","/",$class).'';
78
        }
79
80
        return str_replace("/App",'/src/app',$default);
81
    }
82
83
    /**
84
     * @param $namespace
85
     * @return bool
86
     */
87
    public static function isNamespaceExists($namespace)
88
    {
89
        return (is_string($namespace) && class_exists($namespace)) ? true : false;
90
    }
91
92
    /**
93
     * @param $class
94
     * @param $method
95
     * @return bool
96
     */
97
    public static function existMethod($class,$method)
98
    {
99
        return method_exists($class,$method);
100
    }
101
102
    /**
103
     * @param $first
104
     * @param $second
105
     * @return bool
106
     */
107
    public static function isArrayEqual($first,$second)
108
    {
109
        return (count( $first ) == count( $second ) && !array_diff( $first, $second ));
110
    }
111
112
    /**
113
     * @param $path
114
     * @return YamlManager
115
     */
116
    public static function yaml($path)
117
    {
118
        return new YamlManager($path);
119
    }
120
121
    /**
122
     * @param $path
123
     * @param bool $filename
124
     * @return array
125
     */
126
    public static function glob($path,$filename=false)
127
    {
128
        $configList = [];
129
130
        foreach (glob($path.'/*.php') as $config) {
131
132
            $configArray=str_replace(".php","",explode("/",$config));
133
            $configList[end($configArray)]=$config;
134
        }
135
136
        if($filename===true){
137
            return array_keys($configList);
138
        }
139
140
        return $configList;
141
    }
142
143
    /**
144
     * @param $path
145
     */
146
    public static function makeWritable($path)
147
    {
148
        $dir = new \DirectoryIterator($path);
149
150
        foreach ($dir as $item) {
151
152
            chmod($item->getPathname(), 0777);
153
154
            if ($item->isDir() && !$item->isDot()) {
155
                self::makeWritable($item->getPathname());
156
            }
157
        }
158
    }
159
160
    /**
161
     * @param $namespace
162
     * @param string $seperator
163
     * @return mixed
164
     */
165
    public static function getJustClassName($namespace,$seperator="\\")
166
    {
167
        $path = explode($seperator, $namespace);
168
        return array_pop($path);
169
    }
170
171
    /**
172
     * @param $class
173
     * @param array $param
174
     * @return bool
175
     */
176
    public static function changeClass($class,$param=array())
177
    {
178
        $executionPath=$class;
179
        $dt = fopen($executionPath, "r");
180
181
        if($dt!==false){
182
183
            $content = fread($dt, filesize($executionPath));
184
            fclose($dt);
185
186
            foreach ($param as $key=>$value){
187
                $content=str_replace($key,$value,$content);
188
            }
189
190
            $forWrite = fopen($executionPath, "w");
191
192
            if($forWrite!==false){
193
                fwrite($forWrite, $content);
194
                fclose($forWrite);
195
196
                return true;
197
            }
198
        }
199
200
        return false;
201
    }
202
203
    /**
204
     * @param $data
205
     * @param $callback
206
     * @return mixed
207
     */
208
    public static function returnCallback($data,$callback)
209
    {
210
        return call_user_func_array($callback,[$data]);
211
    }
212
213
    /**
214
     * @param $namespace
215
     * @return string
216
     */
217
    public static function getNamespace($namespace)
218
    {
219
        $rootDelete=str_replace(root.''.DIRECTORY_SEPARATOR.'src'.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'','',$namespace);
220
221
        return 'App\\'.self::generatorNamespace(
222
                explode(''.DIRECTORY_SEPARATOR.'',$rootDelete)
223
            );
224
225
    }
226
227
    /**
228
     * @param $callback
229
     * @return mixed
230
     */
231
    public static function callbackProcess($callback)
232
    {
233
        return (is_callable($callback)) ? call_user_func_array($callback,[app()]) : $callback;
234
    }
235
236
    /**
237
     * @param $array1
238
     * @param $array2
239
     * @return bool
240
     */
241
    public static function array_diff_key_recursive ($array1, $array2)
242
    {
243
        if(count($array1)!==count($array2)) self::$bool[]=false;
244
245
        foreach ($array1 as $array1_key=>$array1_value){
246
247
            if(!is_array($array1_value)){
248
                if(!array_key_exists($array1_key,$array2)) self::$bool[]=false;
249
            }
250
            else{
251
                if(!array_key_exists($array1_key,$array2)) self::$bool[]=false;
252
253
                if(!isset($array2[$array1_key]) OR !is_array($array2[$array1_key])) $array2[$array1_key]=[];
254
255
                if(isset($array1_value[0])) $array1_value=$array1_value[0];
256
257
                if(isset($array2[$array1_key][0])) $array2[$array1_key]=$array2[$array1_key][0];
258
259
                self::array_diff_key_recursive($array1_value,$array2[$array1_key]);
260
            }
261
        }
262
263
        if(in_array(false,self::$bool)){
264
            return false;
265
        }
266
        return true;
267
    }
268
269
    /**
270
     * @param $data
271
     * @return mixed
272
     */
273
    public static function slashToBackSlash($data)
274
    {
275
        return str_replace("/","\\",$data);
276
    }
277
278
    /**
279
     * get trace
280
     *
281
     * @param int $debug
282
     * @param null|string $key
283
     * @return mixed
284
     */
285
    public static function trace($debug=0,$key=null)
286
    {
287
        $trace = debug_backtrace();
288
289
        if($key===null){
290
            return $trace[$debug] ?? null;
291
        }
292
293
        return $trace[$debug][$key] ?? null;
294
    }
295
296
    /**
297
     * @return array
298
     */
299
    public static function getServiceConf()
300
    {
301
        if(property_exists(core(),'serviceConf') && defined('methodName')){
302
            return core()->serviceConf;
303
        }
304
        return [];
305
    }
306
307
308
    /**
309
     * @param $dir
310
     * @param bool $recursive
311
     * @param string $basedir
312
     * @param bool $include_dirs
313
     * @return array
314
     */
315
    public static function getAllFilesInDirectory($dir, $recursive = true, $basedir = '', $include_dirs = false)
316
    {
317
        if ($dir == '') {return array();} else {$results = array(); $subresults = array();}
318
        if (!is_dir($dir)) {$dir = dirname($dir);} // so a files path can be sent
319
        if ($basedir == '') {$basedir = realpath($dir).DIRECTORY_SEPARATOR;}
320
321
        $files = scandir($dir);
322
        foreach ($files as $key => $value){
323
            if ( ($value != '.') && ($value != '..') ) {
324
                $path = realpath($dir.DIRECTORY_SEPARATOR.$value);
325
                if (is_dir($path)) {
326
                    // optionally include directories in file list
327
                    if ($include_dirs) {$subresults[] = str_replace($basedir, '', $path);}
328
                    // optionally get file list for all subdirectories
329
                    if ($recursive) {
330
                        $subdirresults = self::getAllFilesInDirectory($path, $recursive, $basedir, $include_dirs);
331
                        $results = array_merge($results, $subdirresults);
332
                    }
333
                } else {
334
                    // strip basedir and add to subarray to separate file list
335
                    $subresults[] = str_replace($basedir, '', $path);
336
                }
337
            }
338
        }
339
        // merge the subarray to give the list of files then subdirectory files
340
        if (count($subresults) > 0) {$results = array_merge($subresults, $results);}
341
        return $results;
342
    }
343
344
    /**
345
     * @param $files
346
     * @param null|string $reelPath
347
     * @return array
348
     */
349
    public static function getPathWithPhpExtension($files,$reelPath=null)
350
    {
351
        $pathWithPhpList = [];
352
353
        foreach ($files as $file){
354
355
            if(preg_match('@(.*).php@is',$file,$pathWithPhp)){
356
357
                if($reelPath===null){
358
                    $pathWithPhpList[] = $pathWithPhp[0];
359
                }
360
                else{
361
                    $pathWithPhpList[] = $reelPath.'/'.$pathWithPhp[0];
362
                }
363
364
            }
365
        }
366
367
        return $pathWithPhpList;
368
    }
369
370
    /**
371
     * @return array
372
     */
373
    public static function getHttpMethods()
374
    {
375
        return [
376
            'get',
377
            'head',
378
            'post',
379
            'put',
380
            'delete',
381
            'connect',
382
            'options',
383
            'trace'
384
        ];
385
    }
386
387
    /**
388
     * @param $class
389
     * @return mixed
390
     */
391
    public static function resolverClass($class)
392
    {
393
        if(self::isNamespaceExists($class)){
394
            return app()->resolve($class);
0 ignored issues
show
Bug introduced by
The method resolve() does not exist on Resta\Contracts\ApplicationHelpersContracts. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

394
            return app()->/** @scrutinizer ignore-call */ resolve($class);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
395
        }
396
397
        return $class;
398
    }
399
400
    /**
401
     * @return array
402
     */
403
    public static function getRequestPathInfo()
404
    {
405
        if(is_null(BootStaticManager::getRequestPath())){
0 ignored issues
show
introduced by
The condition is_null(Resta\Support\Bo...ager::getRequestPath()) is always false.
Loading history...
406
            return explode("/",request()->getPathInfo());
407
        }
408
        return BootStaticManager::getRequestPath();
0 ignored issues
show
Bug Best Practice introduced by
The expression return Resta\Support\Boo...nager::getRequestPath() returns the type string which is incompatible with the documented return type array.
Loading history...
409
    }
410
411
    /**
412
     * @param $trace
413
     * @param null|string $remove
414
     * @return array
415
     */
416
    public static function removeTrace($trace,$remove=null)
417
    {
418
        $list = [];
419
420
        foreach($trace as $key=>$item){
421
422
            if(isset($item['file']) && !preg_match('@'.$remove.'@',$item['file'])){
423
                $list[$key] = $item;
424
            }
425
        }
426
427
        return array_values($list);
428
    }
429
}