Passed
Push — master ( b40adf...1b4971 )
by George
02:45
created

Scaffold::build()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace Ghaskell\Scaffold;
4
5
use Ghaskell\Scaffold\Facades\Vibro;
6
use Illuminate\Database\Eloquent\Collection;
7
use Illuminate\Filesystem\Filesystem;
8
use Illuminate\Support\Str;
9
10
class Scaffold
11
{
12
    protected $model;
13
    protected $migration;
14
    protected $variables;
15
    protected $migrationFileName;
16
    protected $files;
17
18
    public $messages = [];
19
    public $created = [];
20
21
22
23
    public function __construct($migration)
0 ignored issues
show
Unused Code introduced by
The parameter $migration is not used and could be removed. ( Ignorable by Annotation )

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

23
    public function __construct(/** @scrutinizer ignore-unused */ $migration)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
24
    {
25
26
27
    }
28
29
    public static function make($migrationName)
30
    {
31
        $scaffold =  new Scaffold($migrationName);
32
        $scaffold->files = new Filesystem();
33
        $scaffold->files->requireOnce($migrationName);
34
        $migrationInstance = $scaffold->resolve($name = $scaffold->getMigrationName($migrationName)); //parse into instance
35
        $reflector = new \ReflectionClass($migrationInstance);  //reflect
36
        $migrationFileName = $reflector->getFileName();
37
        $scaffold->migration = $scaffold->files->get($migrationFileName);
38
        $scaffold->model = ModelPattern::make($scaffold->migration);
39
        return $scaffold;
40
41
    }
42
43
44
    /**
45
     * Resolve a migration instance from a file.
46
     *
47
     * @param  string  $file
48
     * @return object
49
     */
50
    public function resolve($file)
51
    {
52
        $class = Str::studly(implode('_', array_slice(explode('_', $file), 4)));
53
54
        return new $class;
55
    }
56
57
    /**
58
     * Get the name of the migration.
59
     *
60
     * @param  string  $path
61
     * @return string
62
     */
63
    public function getMigrationName($path)
64
    {
65
        return str_replace('.php', '', basename($path));
66
    }
67
68
    /**
69
     * Get all of the migration files in a given path.
70
     *
71
     * @param  string|array  $paths
72
     * @return array
73
     */
74
    public function getMigrationFiles($paths)
75
    {
76
        return Collection::make($paths)->flatMap(function ($path) {
77
            return $this->files->glob($path.'/*_*.php');
78
        })->filter()->sortBy(function ($file) {
79
            return $this->getMigrationName($file);
80
        })->values()->keyBy(function ($file) {
81
            return $this->getMigrationName($file);
82
        })->all();
83
    }
84
85
86
87
    public function build($stub, $data)
88
    {
89
        $path = $this->getStubPath($stub);
90
        return Vibro::compileFile($path, $data);
0 ignored issues
show
Bug introduced by
The method compileFile() does not exist on Ghaskell\Scaffold\Facades\Vibro. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

90
        return Vibro::/** @scrutinizer ignore-call */ compileFile($path, $data);
Loading history...
91
    }
92
93
    protected function getStub($stub)
94
    {
95
        $stub = Str::camel($stub);
96
        if($this->files->exists(app_path("Scaffold/stubs/$stub.stub"))) {
97
                return $this->files->get(app_path("Scaffold/stubs/$stub.stub"));
98
        } else {
99
            return false;
100
        }
101
    }
102
103
    protected function getStubPath($stub)
104
    {
105
        $stub = Str::camel($stub);
106
        return app_path("Scaffold/stubs/$stub.stub");
107
    }
108
109
    public function addRoutes()
110
    {
111
        foreach(config('scaffold.routes') as $key => $route)
112
        {
113
            $namespace = Str::studly($key);
114
            $routeContent = $this->files->get($route['fileName']);
115
            $uri = Str::plural(strtolower(preg_replace(
116
                '/([a-zA-Z])(?=[A-Z])/',
117
                '$1-', $this->model->name
118
            )));
119
120
121
            if($key === 'web') {
122
                $routeString = "\n\nRoute::resource('$uri', '$namespace\\{$this->model->name}Controller')->only(['index', 'show']);";
123
            } else {
124
                $routeString = "\n\nRoute::apiResource('$uri', '$namespace\\{$this->model->name}Controller');";
125
            }
126
127
            if (!str_contains($routeContent, $routeString)) {
128
                $this->files->append($route['fileName'], $routeString);
129
            }
130
        }
131
        return $this;
132
    }
133
134
    public function generate($file)
135
    {
136
            $target = Str::camel(str_replace('build', '', $file));
137
            $config = config("scaffold.files.$target");
138
            $pathPrefix = $config['path'];
139
            $variables = ['model' => $this->model];
140
141
            if(!empty(config("scaffold.variables"))) {
142
                foreach (config("scaffold.variables") as $variable => $value) {
143
                    $variables[$variable] = $this->compileVariable($value);
144
                }
145
            }
146
147
            $fileName = Vibro::compileFileName($config['fileNamePattern'], $variables);
0 ignored issues
show
Bug introduced by
The method compileFileName() does not exist on Ghaskell\Scaffold\Facades\Vibro. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

147
            /** @scrutinizer ignore-call */ 
148
            $fileName = Vibro::compileFileName($config['fileNamePattern'], $variables);
Loading history...
148
            $pathPrefix = base_path(Vibro::compileFileName("$pathPrefix", $variables));
149
            if($this->files->exists("$pathPrefix/$fileName")) {
150
                $this->messages[] = "File '$fileName' exists and was not overwritten.";
151
                return $this;
152
            }
153
154
//            dd($pathPrefix);
155
156
            if(!$this->files->isDirectory($pathPrefix)) {
157
                $this->files->makeDirectory("$pathPrefix");
158
            }
159
160
            $content = $this->build($target, $variables);
161
            if($content) {
162
                $this->files->put("$pathPrefix/$fileName", $content);
163
                $this->created[] = "$pathPrefix/$fileName";
164
            } else {
165
                $this->messages[] = "File stub $target not found.";
166
            }
167
168
            return $this;
169
170
    }
171
172
    public function compileVariable($string) {
173
        $model = $this->model;
0 ignored issues
show
Unused Code introduced by
The assignment to $model is dead and can be removed.
Loading history...
174
        ob_start();
175
        eval( "echo $string;");
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
176
        $output = ob_get_contents();
177
        ob_end_clean();
178
        return $output;
179
    }
180
181
182
183
}