Skip to content

Commit

Permalink
wip [skip-ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
ryangjchandler committed Mar 21, 2021
1 parent 0ea529f commit 912d74e
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/Concerns/Orbital.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Support\Str;
use Illuminate\Filesystem\Filesystem;
use Orbit\Events\OrbitalCreated;
use Orbit\Events\OrbitalDeleted;
use Orbit\Events\OrbitalUpdated;
use ReflectionClass;

Expand Down Expand Up @@ -55,6 +56,17 @@ public static function bootOrbital()

return $status;
});

static::deleted(function (Model $model) {
$status = Orbit::driver(static::getOrbitalDriver())->delete(
$model,
static::getOrbitalPath()
);

OrbitalDeleted::dispatch($model);

return $status;
});
}

public static function getOrbitalSchema(Blueprint $table)
Expand Down
2 changes: 2 additions & 0 deletions src/Contracts/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ public function shouldRestoreCache(string $directory): bool;

public function save(Model $model, string $directory): bool;

public function delete(Model $model, string $directory): bool;

public function all(string $directory): Collection;
}
9 changes: 9 additions & 0 deletions src/Drivers/Markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ public function save(Model $model, string $directory): bool
return true;
}

public function delete(Model $model, string $directory): bool
{
$key = $model->getKey();

unlink($directory . DIRECTORY_SEPARATOR . $key . '.md');

return true;
}

public function all(string $directory): Collection
{
$collection = Collection::make();
Expand Down
18 changes: 18 additions & 0 deletions src/Events/OrbitalDeleted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Orbit\Events;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Events\Dispatchable;

class OrbitalDeleted
{
use Dispatchable;

public $model;

public function __construct(Model $model)
{
$this->model = $model;
}
}
10 changes: 9 additions & 1 deletion src/Listeners/ProcessGitTransaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use Orbit\Events\OrbitalCreated;
use Orbit\Events\OrbitalDeleted;
use Orbit\Events\OrbitalUpdated;
use Orbit\Facades\Orbit;
use Symplify\GitWrapper\Exception\GitException;
Expand All @@ -25,6 +26,13 @@ public function updated(OrbitalUpdated $event)
$this->commit($message);
}

public function deleted(OrbitalDeleted $event)
{
$message = 'orbit: deleted ' . class_basename($event->model) . ' ' . $event->model->getKey();

$this->commit($message);
}

protected function commit(string $message)
{
$wrapper = new GitWrapper(
Expand All @@ -40,7 +48,7 @@ protected function commit(string $message)
}

$git->add(
config('orbit.paths.content')
config('orbit.paths.content') . DIRECTORY_SEPARATOR . '*'
);

$git->commit($message);
Expand Down
2 changes: 2 additions & 0 deletions src/OrbitServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Support\ServiceProvider;
use Orbit\Contracts\Driver;
use Orbit\Events\OrbitalCreated;
use Orbit\Events\OrbitalDeleted;
use Orbit\Events\OrbitalUpdated;
use Orbit\Listeners\ProcessGitTransaction;

Expand Down Expand Up @@ -44,6 +45,7 @@ public function boot()
if ($this->app['config']->get('orbit.git.enabled')) {
Event::listen(OrbitalCreated::class, [ProcessGitTransaction::class, 'created']);
Event::listen(OrbitalUpdated::class, [ProcessGitTransaction::class, 'updated']);
Event::listen(OrbitalDeleted::class, [ProcessGitTransaction::class, 'deleted']);
}
}
}

0 comments on commit 912d74e

Please sign in to comment.