1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-15 05:04:04 +00:00
tenancy/src/Database/Concerns/TenantRun.php
Abrar Ahmad 97ab483173
Completing PR #881 (#902)
* install PHP CS Fixer

* Fix styling

* remove StyleCI config

* use config from archtechx/template

* Fix styling

* added `php-cs-fixer`

* Update .php-cs-fixer.php

* added GitHub token

* Update ci.yml

* Update ci.yml

* Update ci.yml

* php-cs-fixer workflow same as template

Co-authored-by: Erik Gaal <me@erikgaal.nl>
Co-authored-by: erikgaal <erikgaal@users.noreply.github.com>
2022-07-20 15:28:45 +02:00

31 lines
617 B
PHP

<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Database\Concerns;
use Stancl\Tenancy\Contracts\Tenant;
trait TenantRun
{
/**
* Run a callback in this tenant's context.
* Atomic, safely reverts to previous context.
*/
public function run(callable $callback)
{
/** @var Tenant $this */
$originalTenant = tenant();
tenancy()->initialize($this);
$result = $callback($this);
if ($originalTenant) {
tenancy()->initialize($originalTenant);
} else {
tenancy()->end();
}
return $result;
}
}