ShellHelper::rename()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace Lumener\Helpers;
4
5
use GuzzleHttp\Exception\GuzzleException;
6
use GuzzleHttp\Client;
7
8
class ShellHelper
9
{
10
    public static $LastError;
11
12
    /**
13
     * @param       $uri
14
     * @param array $params
15
     *
16
     * @return bool|mixed|\Psr\Http\Message\ResponseInterface
17
     */
18
    public static function get($uri, $params = [])
19
    {
20
        try {
21
            $client = new Client(); //GuzzleHttp\Client
22
            return $client->request('GET', $uri, $params);
23
        } catch (GuzzleException $e) {
24
            self::$LastError = $e->getMessage();
25
            return false;
26
        }
27
    }
28
29
    /**
30
     * copy files
31
     *
32
     * @param $string
33
     */
34
    public static function copy($source, $dest)
35
    {
36
        if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
37
            echo shell_exec("copy \"{$source}\" \"{$dest}\"");
38
        } else {
39
            echo shell_exec("cp \"{$source}\" \"{$dest}\"");
40
        }
41
    }
42
43
    /**
44
     * Replace portions of the file
45
     * @param  string $pattern
46
     * @param  string $replacement
47
     * @param  string $file
48
     * @return void
49
     */
50
    public static function replace($pattern, $replacement, $file)
51
    {
52
        if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
53
            // TODO: Test this on windows
54
            echo shell_exec("cat \"{$file}\" | %{_ -replace \"$pattern\",\"{$replacement}\"} > \"{$file}\"");
55
        } else {
56
            $rule = '%([/])%';
57
            $pattern = preg_replace($rule, '\\\\$0', $pattern);
58
            $replacement = preg_replace($rule, '\\\\$0', $replacement);
59
            $arg = escapeshellarg("s/{$pattern}/{$replacement}/g");
60
            echo shell_exec("LC_ALL=C sed -i -r \"{$arg}\" \"{$file}\"");
61
        }
62
    }
63
64
    /**
65
     * Execute the sed command
66
     *
67
     * @param $string
68
     */
69
    public static function rename($string, $filename)
70
    {
71
        if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
72
            // TODO: Test this on windows
73
            echo shell_exec("cat \"{$filename}\" | %{_ -replace \"([\\[\\)\\(\\\;\\{\\}]|^)'{$string}\",\"$1LUMENER_OVERRIDE_{$string}\"} > \"{$filename}\"");
74
        } else {
75
            echo shell_exec('LC_ALL=C sed -i -r \'s/([)(\;{}]|^)'.$string.'/\1LUMENER_OVERRIDE_'.$string.'/g\' '."\"{$filename}\"");
76
        }
77
    }
78
}
79