|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace duxet\RethinkDB\Schema; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use duxet\Rethinkdb\Connection; |
|
7
|
|
|
use duxet\Rethinkdb\Schema\Blueprint; |
|
8
|
|
|
use r; |
|
9
|
|
|
|
|
10
|
|
|
class Builder extends \Illuminate\Database\Schema\Builder |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Create a new database Schema manager. |
|
14
|
|
|
* |
|
15
|
|
|
* @param Connection $connection |
|
16
|
|
|
*/ |
|
17
|
|
|
public function __construct(Connection $connection) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->connection = $connection; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Determine if the given table exists. |
|
24
|
|
|
* |
|
25
|
|
|
* @param string $table |
|
26
|
|
|
* |
|
27
|
|
|
* @return bool |
|
28
|
|
|
*/ |
|
29
|
|
|
public function hasTable($table) |
|
30
|
|
|
{ |
|
31
|
|
|
$conn = $this->connection->getConnection(); |
|
|
|
|
|
|
32
|
|
|
$db = r\db($this->connection->getDatabaseName()); |
|
33
|
|
|
$tables = $db->tableList()->run($conn); |
|
34
|
|
|
|
|
35
|
|
|
return in_array($table, $tables); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Create a new table on the schema. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $table |
|
42
|
|
|
* @param Closure $callback |
|
43
|
|
|
* |
|
44
|
|
|
* @return bool |
|
45
|
|
|
*/ |
|
46
|
|
|
public function create($table, Closure $callback = null) |
|
47
|
|
|
{ |
|
48
|
|
|
$blueprint = $this->createBlueprint($table); |
|
49
|
|
|
$blueprint->create(); |
|
50
|
|
|
if ($callback) { |
|
51
|
|
|
$callback($blueprint); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Drop a table from the schema. |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $table |
|
59
|
|
|
* |
|
60
|
|
|
* @return bool |
|
61
|
|
|
*/ |
|
62
|
|
|
public function drop($table) |
|
63
|
|
|
{ |
|
64
|
|
|
$blueprint = $this->createBlueprint($table); |
|
65
|
|
|
|
|
66
|
|
|
return $blueprint->drop(); |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Modify a table on the schema. |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $table |
|
73
|
|
|
* @param Closure $callback |
|
74
|
|
|
* |
|
75
|
|
|
* @return bool |
|
76
|
|
|
*/ |
|
77
|
|
|
public function table($table, Closure $callback) |
|
78
|
|
|
{ |
|
79
|
|
|
$blueprint = $this->createBlueprint($table); |
|
80
|
|
|
if ($callback) { |
|
81
|
|
|
$callback($blueprint); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Create a new command set with a Closure. |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $table |
|
89
|
|
|
* @param Closure $callback |
|
90
|
|
|
* |
|
91
|
|
|
* @return Blueprint |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function createBlueprint($table, Closure $callback = null) |
|
94
|
|
|
{ |
|
95
|
|
|
return new Blueprint($this->connection, $table); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: