Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add list command #73

Merged
merged 1 commit into from
Feb 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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];
}

}