diff --git a/config/orbit.php b/config/orbit.php index 18a5d5e..4c3d290 100644 --- a/config/orbit.php +++ b/config/orbit.php @@ -17,6 +17,8 @@ 'enabled' => env('ORBIT_GIT_ENABLED', false), 'name' => env('ORBIT_GIT_NAME'), 'email' => env('ORBIT_GIT_EMAIL'), + 'root' => env('ORBIT_GIT_ROOT', base_path()), + 'binary' => env('ORBIT_GIT_BINARY', '/usr/bin/git'), ], ]; diff --git a/src/Facades/Orbit.php b/src/Facades/Orbit.php index b4e790d..b9bce0d 100644 --- a/src/Facades/Orbit.php +++ b/src/Facades/Orbit.php @@ -14,6 +14,7 @@ * @method static string getDatabasePath() * @method static string getGitName() * @method static string getGitEmail() + * @method static string getGitRoot() * * @see \Orbit\OrbitManager */ diff --git a/src/Listeners/ProcessGitTransaction.php b/src/Listeners/ProcessGitTransaction.php index 24cd50d..284fdce 100644 --- a/src/Listeners/ProcessGitTransaction.php +++ b/src/Listeners/ProcessGitTransaction.php @@ -2,18 +2,49 @@ namespace Orbit\Listeners; +use Exception; use Orbit\Events\OrbitalCreated; use Orbit\Events\OrbitalUpdated; +use Orbit\Facades\Orbit; +use Symplify\GitWrapper\Exception\GitException; +use Symplify\GitWrapper\GitWrapper; class ProcessGitTransaction { public function created(OrbitalCreated $event) { + $message = 'orbit: created ' . class_basename($event->model) . ' ' . $event->model->getKey(); + $this->commit($message); } public function updated(OrbitalUpdated $event) { + $message = 'orbit: updated ' . class_basename($event->model) . ' ' . $event->model->getKey(); + $this->commit($message); + } + + protected function commit(string $message) + { + $wrapper = new GitWrapper( + Orbit::getGitBinary() + ); + + $git = $wrapper->workingCopy( + Orbit::getGitRoot() + ); + + if (! $git->hasChanges()) { + return; + } + + $git->add( + config('orbit.paths.content') + ); + + $git->commit($message); + + $git->push(); } } diff --git a/src/OrbitManager.php b/src/OrbitManager.php index d574b68..ea0a87c 100644 --- a/src/OrbitManager.php +++ b/src/OrbitManager.php @@ -53,4 +53,14 @@ public function getGitEmail() return config('orbit.git.email'); } + + public function getGitRoot() + { + return config('orbit.git.root'); + } + + public function getGitBinary() + { + return config('orbit.git.binary'); + } }