1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace LaravelFreelancerNL\Aranguent\Schema\Concerns; |
||
6 | |||
7 | use Closure; |
||
8 | use LaravelFreelancerNL\Aranguent\Schema\Blueprint; |
||
9 | |||
10 | trait UsesBlueprints |
||
11 | { |
||
12 | /** |
||
13 | * Create a new command set with a Closure. |
||
14 | * |
||
15 | * @param string $collection |
||
16 | * @return Blueprint |
||
17 | */ |
||
18 | 90 | protected function createBlueprint($table, Closure $callback = null) |
|
19 | { |
||
20 | 90 | $prefix = $this->connection->getConfig('prefix_indexes') |
|
21 | ? $this->connection->getConfig('prefix') |
||
22 | 90 | : ''; |
|
23 | |||
24 | 90 | if (isset($this->resolver)) { |
|
25 | return call_user_func($this->resolver, $table, $this->grammar, $this->schemaManager, $callback, $prefix); |
||
26 | } |
||
27 | |||
28 | 90 | return new Blueprint($table, $this->grammar, $this->schemaManager, $callback, $prefix); |
|
29 | } |
||
30 | |||
31 | /** |
||
32 | * Set the Schema Blueprint resolver callback. |
||
33 | * |
||
34 | * |
||
35 | * @return void |
||
36 | */ |
||
37 | public function blueprintResolver(Closure $resolver) |
||
38 | { |
||
39 | $this->resolver = $resolver; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
40 | } |
||
41 | |||
42 | 90 | protected function build($blueprint) |
|
43 | { |
||
44 | 90 | $blueprint->build($this->connection, $this->grammar); |
|
45 | } |
||
46 | |||
47 | /** |
||
48 | * Create a new collection on the schema. |
||
49 | * |
||
50 | * @param array<mixed> $config |
||
51 | */ |
||
52 | 67 | public function create($table, Closure $callback, array $config = []): void |
|
53 | { |
||
54 | 67 | $this->build(tap($this->createBlueprint($table), function ($blueprint) use ($callback, $config) { |
|
55 | 67 | $blueprint->create($config); |
|
56 | |||
57 | 67 | $callback($blueprint); |
|
58 | 67 | })); |
|
59 | } |
||
60 | |||
61 | /** |
||
62 | * Modify a table's schema. |
||
63 | * |
||
64 | * @param string $table |
||
65 | * @return void |
||
66 | */ |
||
67 | 82 | public function table($table, Closure $callback) |
|
68 | { |
||
69 | 82 | $this->build($this->createBlueprint($table, $callback)); |
|
70 | } |
||
71 | } |
||
72 |