Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Database\Migrations;
4
 
5
use Closure;
6
use Illuminate\Filesystem\Filesystem;
7
use Illuminate\Support\Str;
8
use InvalidArgumentException;
9
 
10
class MigrationCreator
11
{
12
    /**
13
     * The filesystem instance.
14
     *
15
     * @var \Illuminate\Filesystem\Filesystem
16
     */
17
    protected $files;
18
 
19
    /**
20
     * The custom app stubs directory.
21
     *
22
     * @var string
23
     */
24
    protected $customStubPath;
25
 
26
    /**
27
     * The registered post create hooks.
28
     *
29
     * @var array
30
     */
31
    protected $postCreate = [];
32
 
33
    /**
34
     * Create a new migration creator instance.
35
     *
36
     * @param  \Illuminate\Filesystem\Filesystem  $files
37
     * @param  string  $customStubPath
38
     * @return void
39
     */
40
    public function __construct(Filesystem $files, $customStubPath)
41
    {
42
        $this->files = $files;
43
        $this->customStubPath = $customStubPath;
44
    }
45
 
46
    /**
47
     * Create a new migration at the given path.
48
     *
49
     * @param  string  $name
50
     * @param  string  $path
51
     * @param  string|null  $table
52
     * @param  bool  $create
53
     * @return string
54
     *
55
     * @throws \Exception
56
     */
57
    public function create($name, $path, $table = null, $create = false)
58
    {
59
        $this->ensureMigrationDoesntAlreadyExist($name, $path);
60
 
61
        // First we will get the stub file for the migration, which serves as a type
62
        // of template for the migration. Once we have those we will populate the
63
        // various place-holders, save the file, and run the post create event.
64
        $stub = $this->getStub($table, $create);
65
 
66
        $path = $this->getPath($name, $path);
67
 
68
        $this->files->ensureDirectoryExists(dirname($path));
69
 
70
        $this->files->put(
71
            $path, $this->populateStub($stub, $table)
72
        );
73
 
74
        // Next, we will fire any hooks that are supposed to fire after a migration is
75
        // created. Once that is done we'll be ready to return the full path to the
76
        // migration file so it can be used however it's needed by the developer.
77
        $this->firePostCreateHooks($table, $path);
78
 
79
        return $path;
80
    }
81
 
82
    /**
83
     * Ensure that a migration with the given name doesn't already exist.
84
     *
85
     * @param  string  $name
86
     * @param  string  $migrationPath
87
     * @return void
88
     *
89
     * @throws \InvalidArgumentException
90
     */
91
    protected function ensureMigrationDoesntAlreadyExist($name, $migrationPath = null)
92
    {
93
        if (! empty($migrationPath)) {
94
            $migrationFiles = $this->files->glob($migrationPath.'/*.php');
95
 
96
            foreach ($migrationFiles as $migrationFile) {
97
                $this->files->requireOnce($migrationFile);
98
            }
99
        }
100
 
101
        if (class_exists($className = $this->getClassName($name))) {
102
            throw new InvalidArgumentException("A {$className} class already exists.");
103
        }
104
    }
105
 
106
    /**
107
     * Get the migration stub file.
108
     *
109
     * @param  string|null  $table
110
     * @param  bool  $create
111
     * @return string
112
     */
113
    protected function getStub($table, $create)
114
    {
115
        if (is_null($table)) {
116
            $stub = $this->files->exists($customPath = $this->customStubPath.'/migration.stub')
117
                            ? $customPath
118
                            : $this->stubPath().'/migration.stub';
119
        } elseif ($create) {
120
            $stub = $this->files->exists($customPath = $this->customStubPath.'/migration.create.stub')
121
                            ? $customPath
122
                            : $this->stubPath().'/migration.create.stub';
123
        } else {
124
            $stub = $this->files->exists($customPath = $this->customStubPath.'/migration.update.stub')
125
                            ? $customPath
126
                            : $this->stubPath().'/migration.update.stub';
127
        }
128
 
129
        return $this->files->get($stub);
130
    }
131
 
132
    /**
133
     * Populate the place-holders in the migration stub.
134
     *
135
     * @param  string  $stub
136
     * @param  string|null  $table
137
     * @return string
138
     */
139
    protected function populateStub($stub, $table)
140
    {
141
        // Here we will replace the table place-holders with the table specified by
142
        // the developer, which is useful for quickly creating a tables creation
143
        // or update migration from the console instead of typing it manually.
144
        if (! is_null($table)) {
145
            $stub = str_replace(
146
                ['DummyTable', '{{ table }}', '{{table}}'],
147
                $table, $stub
148
            );
149
        }
150
 
151
        return $stub;
152
    }
153
 
154
    /**
155
     * Get the class name of a migration name.
156
     *
157
     * @param  string  $name
158
     * @return string
159
     */
160
    protected function getClassName($name)
161
    {
162
        return Str::studly($name);
163
    }
164
 
165
    /**
166
     * Get the full path to the migration.
167
     *
168
     * @param  string  $name
169
     * @param  string  $path
170
     * @return string
171
     */
172
    protected function getPath($name, $path)
173
    {
174
        return $path.'/'.$this->getDatePrefix().'_'.$name.'.php';
175
    }
176
 
177
    /**
178
     * Fire the registered post create hooks.
179
     *
180
     * @param  string|null  $table
181
     * @param  string  $path
182
     * @return void
183
     */
184
    protected function firePostCreateHooks($table, $path)
185
    {
186
        foreach ($this->postCreate as $callback) {
187
            $callback($table, $path);
188
        }
189
    }
190
 
191
    /**
192
     * Register a post migration create hook.
193
     *
194
     * @param  \Closure  $callback
195
     * @return void
196
     */
197
    public function afterCreate(Closure $callback)
198
    {
199
        $this->postCreate[] = $callback;
200
    }
201
 
202
    /**
203
     * Get the date prefix for the migration.
204
     *
205
     * @return string
206
     */
207
    protected function getDatePrefix()
208
    {
209
        return date('Y_m_d_His');
210
    }
211
 
212
    /**
213
     * Get the path to the stubs.
214
     *
215
     * @return string
216
     */
217
    public function stubPath()
218
    {
219
        return __DIR__.'/stubs';
220
    }
221
 
222
    /**
223
     * Get the filesystem instance.
224
     *
225
     * @return \Illuminate\Filesystem\Filesystem
226
     */
227
    public function getFilesystem()
228
    {
229
        return $this->files;
230
    }
231
}