22
33namespace Dentro \Patcher ;
44
5+ use Dentro \Patcher \Events \PatchEnded ;
6+ use Dentro \Patcher \Events \PatchStarted ;
57use Illuminate \Database \Migrations \Migrator ;
68
79class Patcher extends Migrator
@@ -50,23 +52,17 @@ public function runPending(array $migrations, array $options = []): void
5052 */
5153 protected function patch (string $ file , int $ batch , bool $ pretend ): void
5254 {
53- $ migration = $ this ->resolve (
54- $ name = $ this ->getMigrationName ($ file )
55- );
56-
57- $ migration ->setContainer (app ())->setCommand (app ('command.patcher ' ));
55+ $ migration = $ this ->resolvePath ($ file );
5856
59- if ($ pretend ) {
60- $ this ->pretendToRun ($ migration , 'patch ' );
57+ $ name = $ this ->getMigrationName ($ file );
6158
62- return ;
63- }
59+ $ migration ->setContainer (app ())->setCommand (app ('command.patcher ' ));
6460
6561 $ this ->note ("<comment>Patching:</comment> {$ name }" );
6662
6763 $ startTime = microtime (true );
6864
69- $ this ->runPatch ($ migration, ' patch ' );
65+ $ this ->runPatch ($ migration );
7066
7167 $ runTime = round (microtime (true ) - $ startTime , 2 );
7268
@@ -78,26 +74,36 @@ protected function patch(string $file, int $batch, bool $pretend): void
7874 /**
7975 * Run a migration inside a transaction if the database supports it.
8076 *
81- * @param object $migration
82- * @param string $method
83- *
77+ * @param object $patch
8478 * @return void
8579 * @throws \Throwable
8680 */
87- protected function runPatch (object $ migration , string $ method ): void
81+ protected function runPatch (object $ patch ): void
8882 {
8983 $ connection = $ this ->resolveConnection (
90- $ migration ->getConnection ()
84+ $ patch ->getConnection ()
9185 );
9286
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+ }
96102 }
97103 };
98104
99105 $ this ->getSchemaGrammar ($ connection )->supportsSchemaTransactions ()
100- && $ migration ->withinTransaction
106+ && $ patch ->withinTransaction
101107 ? $ connection ->transaction ($ callback )
102108 : $ callback ();
103109 }
0 commit comments