Passed
Push — master ( 62919d...1f65fa )
by Iman
04:23
created

FileManipulator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 10
c 0
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A extractBetween() 0 6 1
A stringify() 0 14 4
A replaceBetweenMark() 0 14 1
A putCtrlContent() 0 3 1
A readCtrlContent() 0 3 1
1
<?php
2
3
namespace Crocodicstudio\Crudbooster\Modules\ModuleGenerator;
4
5
class FileManipulator
6
{
7
    public static function putCtrlContent($ctrl, $fileContent)
8
    {
9
        return file_put_contents(controller_path($ctrl), $fileContent);
10
    }
11
12
    public static function readCtrlContent($ctrl)
13
    {
14
        return file_get_contents(controller_path($ctrl));
15
    }
16
17
    public static function extractBetween($raw, $mark)
18
    {
19
        list($before, $_rest) = explode(cbStartMarker($mark), $raw);
20
        list($_middle, $after) = explode(cbEndMarker($mark), $_rest);
21
22
        return [trim($before), trim($_middle), trim($after)];
23
    }
24
25
    /**
26
     * @param $phpCode
27
     * @param $mark
28
     * @param $newCode
29
     * @return string
30
     */
31
    public static function replaceBetweenMark($phpCode, $mark, $newCode)
32
    {
33
        list($top, $_middle, $bottom) = self::extractBetween($phpCode, $mark);
34
        unset($_middle);
35
36
        $_code = $top."\n\n";
37
        $indent = str_repeat(' ', 8);
38
39
        $_code .= $indent.cbStartMarker($mark)."\n";
40
        $_code .= $indent.$newCode."\n";
41
        $_code .= $indent.cbEndMarker($mark)."\n\n";
42
        $_code .= $indent.$bottom;
43
44
        return $_code;
45
    }
46
47
    public static function stringify($input, $indent = "")
48
    {
49
        if (! is_array($input)) {
50
            return var_export($input, true);
51
        }
52
        $buffer = [];
53
        foreach ($input as $key => $value) {
54
            $buffer[] = $indent.var_export($key, true)."=>".self::stringify($value, ($indent."    "));
55
        }
56
        if (empty($buffer)) {
57
            return "[]";
58
        }
59
60
        return "[\n".implode(",\n", $buffer)."\n$indent]";
61
    }
62
}