Skip to content

Commit

Permalink
add list command
Browse files Browse the repository at this point in the history
  • Loading branch information
xeno010 committed Feb 25, 2016
1 parent 5006366 commit 71b7151
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/BackupServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ function ($app) {
}
);

$this->commands(['command.backup:run', 'command.backup:clean']);
$this->app['command.backup:list'] = $this->app->share(
function ($app) {
return new Commands\ListCommand();
}
);

$this->commands(['command.backup:run', 'command.backup:clean', 'command.backup:list']);
}

/**
Expand All @@ -53,6 +59,7 @@ public function provides()
return [
'command.backup:run',
'command.backup:clean',
'command.backup:list',
];
}
}
76 changes: 76 additions & 0 deletions src/Commands/ListCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Spatie\Backup\Commands;

use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Storage;

class ListCommand extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'backup:list';

/**
* The console command description.
*
* @var string
*/
protected $description = 'List all backups';

public function fire()
{
$path = config('laravel-backup.destination.path');
$collection = new Collection();

foreach ($this->getTargetFileSystems() as $filesystem) {
$disk = Storage::disk($filesystem);

foreach ($disk->files($path) as $file) {
$collection->push([
'filesystem' => $filesystem,
'name' => $file,
'lastModified' => $disk->lastModified($file),
]);
}
}

$rows = $collection
->sortByDesc('lastModified')
->filter(function ($value) {
return ends_with($value['name'], '.zip');
})
->map(function ($value) {
$lastModified = Carbon::createFromTimestamp($value['lastModified']);

$value['lastModified'] = $lastModified;
$value['age'] = $lastModified->diffForHumans();

return $value;
});

$this->table(['Filesystem', 'Filename', 'Created at', 'Age'], $rows);
}

/**
* Get the filesystems to where the database should be dumped.
*
* @return array
*/
protected function getTargetFileSystems()
{
$fileSystems = config('laravel-backup.destination.filesystem');

if (is_array($fileSystems)) {
return $fileSystems;
}

return [$fileSystems];
}

}

0 comments on commit 71b7151

Please sign in to comment.