2
2
3
3
namespace Dentro \Patcher ;
4
4
5
+ use Dentro \Patcher \Events \PatchEnded ;
6
+ use Dentro \Patcher \Events \PatchStarted ;
5
7
use Illuminate \Database \Migrations \Migrator ;
6
8
7
9
class Patcher extends Migrator
@@ -50,23 +52,17 @@ public function runPending(array $migrations, array $options = []): void
50
52
*/
51
53
protected function patch (string $ file , int $ batch , bool $ pretend ): void
52
54
{
53
- $ migration = $ this ->resolve (
54
- $ name = $ this ->getMigrationName ($ file )
55
- );
56
-
57
- $ migration ->setContainer (app ())->setCommand (app ('command.patcher ' ));
55
+ $ migration = $ this ->resolvePath ($ file );
58
56
59
- if ($ pretend ) {
60
- $ this ->pretendToRun ($ migration , 'patch ' );
57
+ $ name = $ this ->getMigrationName ($ file );
61
58
62
- return ;
63
- }
59
+ $ migration ->setContainer (app ())->setCommand (app ('command.patcher ' ));
64
60
65
61
$ this ->note ("<comment>Patching:</comment> {$ name }" );
66
62
67
63
$ startTime = microtime (true );
68
64
69
- $ this ->runPatch ($ migration, ' patch ' );
65
+ $ this ->runPatch ($ migration );
70
66
71
67
$ runTime = round (microtime (true ) - $ startTime , 2 );
72
68
@@ -78,26 +74,36 @@ protected function patch(string $file, int $batch, bool $pretend): void
78
74
/**
79
75
* Run a migration inside a transaction if the database supports it.
80
76
*
81
- * @param object $migration
82
- * @param string $method
83
- *
77
+ * @param object $patch
84
78
* @return void
85
79
* @throws \Throwable
86
80
*/
87
- protected function runPatch (object $ migration , string $ method ): void
81
+ protected function runPatch (object $ patch ): void
88
82
{
89
83
$ connection = $ this ->resolveConnection (
90
- $ migration ->getConnection ()
84
+ $ patch ->getConnection ()
91
85
);
92
86
93
- $ callback = static function () use ($ migration , $ method ) {
94
- if (method_exists ($ migration , $ method )) {
95
- $ migration ->{$ method }();
87
+ $ dispatchEvent = function (object $ event ) {
88
+ $ this ->events ->dispatch ($ event );
89
+ };
90
+
91
+ $ callback = static function () use ($ patch , $ dispatchEvent ) {
92
+ if (method_exists ($ patch , 'patch ' )) {
93
+ if ($ patch instanceof Patch) {
94
+ $ dispatchEvent (new PatchStarted ($ patch ));
95
+ }
96
+
97
+ $ patch ->patch ();
98
+
99
+ if ($ patch instanceof Patch) {
100
+ $ dispatchEvent (new PatchEnded ($ patch ));
101
+ }
96
102
}
97
103
};
98
104
99
105
$ this ->getSchemaGrammar ($ connection )->supportsSchemaTransactions ()
100
- && $ migration ->withinTransaction
106
+ && $ patch ->withinTransaction
101
107
? $ connection ->transaction ($ callback )
102
108
: $ callback ();
103
109
}
0 commit comments