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

Upgraded to Laravel 5.7 #3372

Merged
merged 7 commits into from
Dec 28, 2018
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
88 changes: 88 additions & 0 deletions app/Foundation/Exceptions/Displayers/SettingsDisplayer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?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\Foundation\Exceptions\Displayers;

use CachetHQ\Cachet\Settings\ReadException;
use Exception;
use GrahamCampbell\Exceptions\Displayers\DisplayerInterface;
use Illuminate\Http\Request;

class SettingsDisplayer implements DisplayerInterface
{
/**
* The request instance.
*
* @var \Illuminate\Http\Request
*/
protected $request;

/**
* Create a new redirect displayer instance.
*
* @param \Illuminate\Http\Request $request
*
* @return void
*/
public function __construct(Request $request)
{
$this->request = $request;
}

/**
* Get the error response associated with the given exception.
*
* @param \Exception $exception
* @param string $id
* @param int $code
* @param string[] $headers
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function display(Exception $exception, string $id, int $code, array $headers)
{
return cachet_redirect('setup');
}

/**
* Get the supported content type.
*
* @return string
*/
public function contentType()
{
return 'text/html';
}

/**
* Can we display the exception?
*
* @param \Exception $original
* @param \Exception $transformed
* @param int $code
*
* @return bool
*/
public function canDisplay(Exception $original, Exception $transformed, int $code)
{
return ($transformed instanceof ReadException) && !$this->request->is('setup*');
}

/**
* Do we provide verbose information about the exception?
*
* @return bool
*/
public function isVerbose()
{
return false;
}
}
2 changes: 1 addition & 1 deletion app/Http/Middleware/ReadyForUse.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function __construct(Repository $settings)
*/
public function handle(Request $request, Closure $next)
{
if (!$this->settings->get('app_name')) {
if (!$request->is('setup*') && !$this->settings->get('app_name')) {
return cachet_redirect('setup');
}

Expand Down
9 changes: 7 additions & 2 deletions app/Http/Middleware/SetupAlreadyCompleted.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace CachetHQ\Cachet\Http\Middleware;

use CachetHQ\Cachet\Settings\ReadException;
use CachetHQ\Cachet\Settings\Repository;
use Closure;
use Illuminate\Http\Request;
Expand Down Expand Up @@ -53,8 +54,12 @@ public function __construct(Repository $settings)
*/
public function handle(Request $request, Closure $next)
{
if ($this->settings->get('app_name')) {
return cachet_redirect('dashboard');
try {
if ($this->settings->get('app_name')) {
return cachet_redirect('dashboard');
}
} catch (ReadException $e) {
// not setup then!
}

return $next($request);
Expand Down
34 changes: 34 additions & 0 deletions app/Settings/ReadException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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\Settings;

use Exception;

/**
* This is the read exception class.
*
* @author Graham Campbell <[email protected]>
*/
class ReadException extends SettingsException
{
/**
* Create a new read exception instance.
*
* @param \Exception $e
*
* @return void
*/
public function __construct(Exception $e)
{
parent::__construct('Unable to read Cachet settings', $e);
}
}
57 changes: 44 additions & 13 deletions app/Settings/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace CachetHQ\Cachet\Settings;

use CachetHQ\Cachet\Models\Setting;
use Exception;

/**
* This is the settings repository class.
Expand Down Expand Up @@ -59,13 +60,19 @@ public function __construct(Setting $model)
/**
* Returns a setting from the database.
*
* @throws \CachetHQ\Cachet\Settings\ReadException
*
* @return array
*/
public function all()
{
return $this->model->all(['name', 'value'])->pluck('value', 'name')->map(function ($value, $name) {
return $this->castSetting($name, $value);
})->toArray();
try {
return $this->model->all(['name', 'value'])->pluck('value', 'name')->map(function ($value, $name) {
return $this->castSetting($name, $value);
})->toArray();
} catch (Exception $e) {
throw new ReadException($e);
}
}

/**
Expand All @@ -74,16 +81,22 @@ public function all()
* @param string $name
* @param string|null $value
*
* @throws \CachetHQ\Cachet\Settings\WriteException
*
* @return void
*/
public function set($name, $value)
{
$this->stale = true;

if ($value === null) {
$this->model->where('name', '=', $name)->delete();
} else {
$this->model->updateOrCreate(compact('name'), compact('value'));
try {
if ($value === null) {
$this->model->where('name', '=', $name)->delete();
} else {
$this->model->updateOrCreate(compact('name'), compact('value'));
}
} catch (Exception $e) {
throw new WriteException($e);
}
}

Expand All @@ -93,41 +106,59 @@ public function set($name, $value)
* @param string $name
* @param mixed $default
*
* @throws \CachetHQ\Cachet\Settings\ReadException
*
* @return mixed
*/
public function get($name, $default = null)
{
if ($setting = $this->model->where('name', '=', $name)->first()) {
return $this->castSetting($name, $setting->value);
try {
if ($setting = $this->model->where('name', '=', $name)->first()) {
return $this->castSetting($name, $setting->value);
}

return $default;
} catch (Exception $e) {
throw new ReadException($e);
}

return $default;
}

/**
* Deletes a setting.
*
* @param string $name
*
* @throws \CachetHQ\Cachet\Settings\WriteException
*
* @return void
*/
public function delete($name)
{
$this->stale = true;

$this->model->where('name', '=', $name)->delete();
try {
$this->model->where('name', '=', $name)->delete();
} catch (Exception $e) {
throw new WriteException($e);
}
}

/**
* Clear all settings.
*
* @throws \CachetHQ\Cachet\Settings\WriteException
*
* @return void
*/
public function clear()
{
$this->stale = true;

$this->model->query()->delete();
try {
$this->model->query()->delete();
} catch (Exception $e) {
throw new WriteException($e);
}
}

/**
Expand Down
35 changes: 35 additions & 0 deletions app/Settings/SettingsException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?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\Settings;

use Exception;

/**
* This is the settings exception class.
*
* @author Graham Campbell <[email protected]>
*/
class SettingsException extends Exception
{
/**
* Create a new write exception instance.
*
* @param string $m
* @param \Exception $e
*
* @return void
*/
public function __construct(string $m, Exception $e)
{
parent::__construct($m, 0, $e);
}
}
34 changes: 34 additions & 0 deletions app/Settings/WriteException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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\Settings;

use Exception;

/**
* This is the write exception class.
*
* @author Graham Campbell <[email protected]>
*/
class WriteException extends SettingsException
{
/**
* Create a new write exception instance.
*
* @param \Exception $e
*
* @return void
*/
public function __construct(Exception $e)
{
parent::__construct('Unable to write Cachet settings', $e);
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"graham-campbell/markdown": "^10.2",
"guzzlehttp/guzzle": "^6.3.3",
"jenssegers/date": "^3.4",
"laravel/framework": "5.6.*",
"laravel/framework": "5.7.*",
"laravel/tinker": "^1.0",
"laravolt/avatar": "^2.1",
"mccool/laravel-auto-presenter": "^7.1",
Expand Down
Loading