1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 16:34:04 +00:00

ensure tenant will rollback

This commit is contained in:
bepsvpt 2020-11-23 17:41:18 +08:00
parent 126afcd0dd
commit 1634514069
No known key found for this signature in database
GPG key ID: 6700B07822B5F1BF

View file

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Stancl\Tenancy;
use Exception;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Traits\Macroable;
@ -109,6 +110,8 @@ class Tenancy
*
* @param callable $callback
* @return mixed
*
* @throw Exception
*/
public function central(callable $callback)
{
@ -116,15 +119,17 @@ class Tenancy
$this->end();
// This callback will usually not accept arguments, but the previous
// Tenant is the only value that can be useful here, so we pass that.
$result = $callback($previousTenant);
if ($previousTenant) {
$this->initialize($previousTenant);
try {
// This callback will usually not accept arguments, but the previous
// Tenant is the only value that can be useful here, so we pass that.
return $callback($previousTenant);
} catch (Exception $e) {
throw $e;
} finally {
if ($previousTenant) {
$this->initialize($previousTenant);
}
}
return $result;
}
/**