2016-04-29 13:20:17 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Cachet.
|
|
|
|
*
|
|
|
|
* (c) Alt Three Services Limited
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace CachetHQ\Cachet\Subscribers;
|
|
|
|
|
2016-08-03 12:34:43 +01:00
|
|
|
use CachetHQ\Cachet\Bus\Events\System\SystemWasInstalledEvent;
|
|
|
|
use CachetHQ\Cachet\Bus\Events\System\SystemWasResetEvent;
|
|
|
|
use CachetHQ\Cachet\Bus\Events\System\SystemWasUpdatedEvent;
|
2016-05-25 10:58:14 +01:00
|
|
|
use CachetHQ\Cachet\Settings\Cache;
|
2016-04-29 13:20:17 +01:00
|
|
|
use Carbon\Carbon;
|
2016-05-20 14:49:41 +01:00
|
|
|
use Exception;
|
2016-04-29 13:20:17 +01:00
|
|
|
use Illuminate\Console\Command;
|
|
|
|
use Illuminate\Contracts\Config\Repository;
|
|
|
|
use Illuminate\Contracts\Events\Dispatcher;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the command subscriber class.
|
|
|
|
*
|
|
|
|
* @author James Brooks <james@alt-three.com>
|
2016-05-20 14:49:41 +01:00
|
|
|
* @author Graham Campbell <graham@alt-three.com>
|
2016-04-29 13:20:17 +01:00
|
|
|
*/
|
|
|
|
class CommandSubscriber
|
|
|
|
{
|
|
|
|
/**
|
2016-05-25 10:58:14 +01:00
|
|
|
* The settings cache instance.
|
|
|
|
*
|
|
|
|
* @var \CachetHQ\Cachet\Settings\Cache
|
|
|
|
*/
|
|
|
|
protected $cache;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The config repository instance.
|
2016-04-29 13:20:17 +01:00
|
|
|
*
|
|
|
|
* @var \Illuminate\Contracts\Config\Repository
|
|
|
|
*/
|
|
|
|
protected $config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new command subscriber instance.
|
|
|
|
*
|
2016-05-25 10:58:14 +01:00
|
|
|
* @param \CachetHQ\Cachet\Settings\Cache $cache
|
2016-04-29 13:20:17 +01:00
|
|
|
* @param \Illuminate\Contracts\Config\Repository $config
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-05-25 10:58:14 +01:00
|
|
|
public function __construct(Cache $cache, Repository $config)
|
2016-04-29 13:20:17 +01:00
|
|
|
{
|
2016-05-25 10:58:14 +01:00
|
|
|
$this->cache = $cache;
|
2016-04-29 13:20:17 +01:00
|
|
|
$this->config = $config;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register the listeners for the subscriber.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Contracts\Events\Dispatcher $events
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function subscribe(Dispatcher $events)
|
|
|
|
{
|
2016-08-03 12:34:43 +01:00
|
|
|
$events->listen('command.installing', __CLASS__.'@fireInstallingCommand', 5);
|
|
|
|
$events->listen('command.updating', __CLASS__.'@fireUpdatingCommand', 5);
|
|
|
|
$events->listen('command.resetting', __CLASS__.'@fireResettingCommand', 5);
|
2016-04-29 13:20:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-03 12:34:43 +01:00
|
|
|
* Fire the installing command.
|
2016-04-29 13:20:17 +01:00
|
|
|
*
|
|
|
|
* @param \Illuminate\Console\Command $command
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-08-03 12:34:43 +01:00
|
|
|
public function fireInstallingCommand(Command $command)
|
|
|
|
{
|
|
|
|
$this->handleMainCommand($command);
|
|
|
|
|
|
|
|
event(new SystemWasInstalledEvent());
|
|
|
|
|
2016-08-08 08:39:20 +01:00
|
|
|
$command->info('System was installed!');
|
2016-08-03 12:34:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fire the updating command.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Console\Command $command
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function fireUpdatingCommand(Command $command)
|
|
|
|
{
|
|
|
|
$this->handleMainCommand($command);
|
|
|
|
|
|
|
|
event(new SystemWasUpdatedEvent());
|
|
|
|
|
2016-08-08 08:39:20 +01:00
|
|
|
$command->info('System was updated!');
|
2016-08-03 12:34:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fire the resetting command.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Console\Command $command
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function fireResettingCommand(Command $command)
|
|
|
|
{
|
|
|
|
$this->handleMainCommand($command);
|
|
|
|
|
|
|
|
event(new SystemWasResetEvent());
|
|
|
|
|
2016-08-08 08:39:20 +01:00
|
|
|
$command->info('System was reset!');
|
2016-08-03 12:34:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the main bulk of the command, clear the settings and backup the database.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Console\Command $command
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function handleMainCommand(Command $command)
|
2016-04-29 13:20:17 +01:00
|
|
|
{
|
2016-05-25 10:58:14 +01:00
|
|
|
$command->line('Clearing settings cache...');
|
|
|
|
|
|
|
|
$this->cache->clear();
|
|
|
|
|
|
|
|
$command->line('Settings cache cleared!');
|
|
|
|
|
2016-08-02 11:00:39 +01:00
|
|
|
// SQLite does not backup.
|
|
|
|
if ($this->config->get('database.default') === 'sqlite') {
|
|
|
|
$command->line('Backup skipped: SQLite is not supported.');
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-29 13:20:17 +01:00
|
|
|
$command->line('Backing up database...');
|
|
|
|
|
2016-05-20 14:49:41 +01:00
|
|
|
try {
|
|
|
|
$command->call('db:backup', [
|
|
|
|
'--compression' => 'gzip',
|
|
|
|
'--database' => $this->config->get('database.default'),
|
|
|
|
'--destination' => 'local',
|
|
|
|
'--destinationPath' => Carbon::now()->format('Y-m-d H.i.s'),
|
|
|
|
'--no-interaction' => true,
|
|
|
|
]);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$command->error($e->getMessage());
|
|
|
|
$command->line('Backup skipped!');
|
|
|
|
}
|
2016-04-29 13:20:17 +01:00
|
|
|
|
2016-05-20 14:49:41 +01:00
|
|
|
$command->line('Backup completed!');
|
2016-04-29 13:20:17 +01:00
|
|
|
}
|
|
|
|
}
|