Completed
Push — master ( 662b61...b40adf )
by George
04:45 queued 02:21
created

Scaffold::parseColumnsFromMigration()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 17
nc 4
nop 0
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
    public static function arrayStringify(array $array)
86
    {
87
        $export = str_replace(['array (', ')', '&#40', '&#41'], ['[', ']', '(', ')'], var_export($array, true));
88
        $export = preg_replace("/ => \n[^\S\n]*\[/m", ' => [', $export);
89
        $export = preg_replace("/ => \[\n[^\S\n]*\]/m", ' => []', $export);
90
        $export = preg_replace('/[\r\n]+/', "\n", $export);
91
        $export = preg_replace('/[ \t]+/', ' ', $export);
92
        return $export; //don't fear the trailing comma
93
    }
94
95
    public function build($stub)
96
    {
97
        $path = $this->getStubPath($stub);
98
        return Vibro::compileFile($path, $this->model );
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

98
        return Vibro::/** @scrutinizer ignore-call */ compileFile($path, $this->model );
Loading history...
99
    }
100
101
    protected function getStub($stub)
102
    {
103
        $stub = Str::camel($stub);
104
        if($this->files->exists(app_path("Scaffold/stubs/$stub.stub"))) {
105
                return $this->files->get(app_path("Scaffold/stubs/$stub.stub"));
106
        } else {
107
            return false;
108
        }
109
    }
110
111
    protected function getStubPath($stub)
112
    {
113
        $stub = Str::camel($stub);
114
        return app_path("Scaffold/stubs/$stub.stub");
115
    }
116
117
    public function addRoutes()
118
    {
119
        foreach(config('scaffold.routes') as $key => $route)
120
        {
121
            $namespace = Str::studly($key);
122
            $routeContent = $this->files->get($route['fileName']);
123
            $uri = Str::plural(strtolower(preg_replace(
124
                '/([a-zA-Z])(?=[A-Z])/',
125
                '$1-', $this->model->name
126
            )));
127
            if($key = 'web') {
0 ignored issues
show
Unused Code introduced by
The assignment to $key is dead and can be removed.
Loading history...
128
                $routeString = "\n\nRoute::resource('$uri', '$namespace\\{$this->model->name}Controller')->only(['index', 'show']);";
129
            } else {
130
                $routeString = "\n\nRoute::apiResource('$uri', '$namespace\\{$this->model->name}Controller');";
131
            }
132
133
            if (!str_contains($routeContent, $routeString)) {
134
                $this->files->append($route['fileName'], $routeString);
135
            }
136
        }
137
        return $this;
138
    }
139
140
    public function __call($method, $arguments)
141
    {
142
        if (starts_with($method, 'build')) {
143
            $target = Str::camel(str_replace('build', '', $method));
144
            $config = config("scaffold.files.$target");
145
            $pathPrefix = $config['path'];
146
            $fileName = Vibro::compileFileName($config['fileNamePattern'], $this->model);
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

146
            /** @scrutinizer ignore-call */ 
147
            $fileName = Vibro::compileFileName($config['fileNamePattern'], $this->model);
Loading history...
147
148
            $path = app_path("$pathPrefix/$fileName");
149
            if($this->files->exists("$path")) {
150
                $this->messages[] = "File '$path' exists and was not overwritten.";
151
                return $this;
152
            }
153
154
            if(!$this->files->isDirectory(app_path($pathPrefix))) {
155
                $this->files->makeDirectory(app_path("$pathPrefix"));
156
            }
157
158
//            dd($this->model->fillable);
159
160
            $content = $this->build($target);
161
            if($content) {
162
                $this->files->put("$path", $content);
163
                $this->created[] = $path;
164
            } else {
165
                $this->messages[] = "File stub $target not found.";
166
            }
167
168
            return $this;
169
        }
170
    }
171
172
173
174
}