Skip to content

Commit

Permalink
Added basic logic
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-helldar committed Mar 15, 2024
1 parent 5eeb7cb commit 88a8e97
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 6 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
],
"require": {
"php": "^8.2",
"composer-runtime-api": "^2.2"
"composer-runtime-api": "^2.2",
"dragon-code/support": "^6.13"
},
"require-dev": {
"mockery/mockery": "^1.3.1",
Expand Down
11 changes: 11 additions & 0 deletions config/settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

return [
'schema' => [
/*
* Here you need to list all the names of the tables for which you need to dump data.
*/

'tables' => [],
]
];
6 changes: 1 addition & 5 deletions src/Concerns/About.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ protected function getPackageVersion(): string

protected function loadPackageName(): string
{
if (! is_null($this->packageName)) {
return $this->packageName;
}

return $this->packageName = Arr::ofFile($this->composer)->get('name');
return $this->packageName ??= Arr::ofFile($this->composer)->get('name');
}
}
27 changes: 27 additions & 0 deletions src/Listeners/DataDumpListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace DragonCode\LaravelDataDumper\Listeners;

use DragonCode\LaravelDataDumper\Service\Dumper;
use DragonCode\LaravelDataDumper\Service\Tables;
use Illuminate\Database\Events\SchemaDumped;

class DataDumpListener
{
public function __construct(
protected readonly Tables $tables,
protected readonly Dumper $dumper
) {
}

public function handle(SchemaDumped $event): void
{
$this->dumper->dump(
$event->connection,
$event->path,
$this->tables->dumpable()
);
}
}
22 changes: 22 additions & 0 deletions src/Service/Dumper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace DragonCode\LaravelDataDumper\Service;

use Illuminate\Database\Connection;

class Dumper
{
public function dump(Connection $connection, string $path, array $tables): void
{
foreach ($tables as $table) {
$this->export($connection, $path, $table);
}
}

protected function export(Connection $connection, string $path, string $table): void
{
// export table data to file
}
}
39 changes: 39 additions & 0 deletions src/Service/Tables.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace DragonCode\LaravelDataDumper\Service;

use stdClass;

class Tables
{
public function dumpable(): array
{
return array_intersect($this->flatten(), $this->toDump());
}

protected function toDump(): array
{
return config('database.schema.tables', []);
}

protected function flatten(): array
{
return array_map(function (array | stdClass $item) {
return is_array($item) ? $item['name'] : $item->name;
}, $this->getTables());
}

protected function getTables(): array
{
return method_exists($this->schema(), 'getAllTables')
? $this->schema()->getAllTables()
: $this->schema()->getTables();
}

protected function schema()
{
return app('db.schema');
}
}
15 changes: 15 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
namespace DragonCode\LaravelDataDumper;

use DragonCode\LaravelDataDumper\Concerns\About;
use DragonCode\LaravelDataDumper\Listeners\DataDumpListener;
use Illuminate\Database\Events\SchemaDumped;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider as BaseServiceProvider;

class ServiceProvider extends BaseServiceProvider
Expand All @@ -15,10 +18,22 @@ public function boot(): void
{
if ($this->app->runningInConsole()) {
$this->registerAbout();
$this->bootListener();
}
}

public function register(): void
{
$this->registerConfig();
}

protected function bootListener(): void
{
Event::listen(SchemaDumped::class, DataDumpListener::class);
}

protected function registerConfig(): void
{
$this->mergeConfigFrom(__DIR__ . '/../config/settings.php', 'database');
}
}

0 comments on commit 88a8e97

Please sign in to comment.