1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Migratio; |
4
|
|
|
|
5
|
|
|
use Migratio\Contract\SchemaCapsuleContract; |
6
|
|
|
|
7
|
|
|
class SchemaCapsule implements SchemaCapsuleContract |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var $config |
|
|
|
|
11
|
|
|
*/ |
12
|
|
|
protected $config; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var $file |
|
|
|
|
16
|
|
|
*/ |
17
|
|
|
protected $file; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var $table |
|
|
|
|
21
|
|
|
*/ |
22
|
|
|
protected $table; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var $wizardNamespace |
|
|
|
|
26
|
|
|
*/ |
27
|
|
|
protected $wizardNamespace; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var $wizardAlterNamespace |
|
|
|
|
31
|
|
|
*/ |
32
|
|
|
protected $wizardAlterNamespace; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* SchemaCapsule constructor. |
36
|
|
|
* @param $config |
37
|
|
|
* @param $file |
38
|
|
|
* @param $table |
39
|
|
|
*/ |
40
|
|
|
public function __construct($config,$file,$table) |
41
|
|
|
{ |
42
|
|
|
$this->config = $config; |
43
|
|
|
$this->file = $file; |
44
|
|
|
$this->table = $table; |
45
|
|
|
$this->wizardNamespace = $this->getWizardNamespace().'\Wizard'; |
46
|
|
|
$this->wizardAlterGroupNamespace = $this->getWizardNamespace().'\WizardAlterGroup'; |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param callable $callback |
51
|
|
|
* @return mixed|string |
52
|
|
|
*/ |
53
|
|
|
public function alter(callable $callback) |
54
|
|
|
{ |
55
|
|
|
return $this->callbackWizardInstance(__FUNCTION__,$callback); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param callable $callback |
60
|
|
|
* @return mixed|string |
61
|
|
|
*/ |
62
|
|
|
public function create(callable $callback) |
63
|
|
|
{ |
64
|
|
|
return $this->callbackWizardInstance(__FUNCTION__,$callback); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param $wizard |
69
|
|
|
*/ |
70
|
|
|
private function checkErrors($wizard) |
71
|
|
|
{ |
72
|
|
|
if(count($wizard->getNames())!==count($wizard->getTypes())){ |
73
|
|
|
$wizard->setError('name and types are not equal'); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
private function getWizardNamespace() |
81
|
|
|
{ |
82
|
|
|
return 'Migratio\GrammarStructure\\'.ucfirst($this->config['database']['driver']).'\Wizard'; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param $type |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
private function getWizardInstance($type) |
90
|
|
|
{ |
91
|
|
|
$wizard = ($type=="create") ? $this->wizardNamespace : $this->wizardAlterGroupNamespace; |
92
|
|
|
|
93
|
|
|
$wizard = new $wizard; |
94
|
|
|
|
95
|
|
|
$wizard->schemaType($type); |
96
|
|
|
|
97
|
|
|
$wizard->setTable($this->table); |
98
|
|
|
|
99
|
|
|
$wizard->setFile($this->file); |
100
|
|
|
|
101
|
|
|
return $wizard; |
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param $method |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
private function callbackWizardInstance($method,callable $callback) |
109
|
|
|
{ |
110
|
|
|
$wizardInstance = $this->getWizardInstance($method); |
111
|
|
|
|
112
|
|
|
$callbackReturn = call_user_func_array($callback,[$wizardInstance]); |
|
|
|
|
113
|
|
|
|
114
|
|
|
$this->checkErrors($wizardInstance); |
115
|
|
|
|
116
|
|
|
return $wizardInstance; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
|