1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-13 03:14:03 +00:00

[1.7.0] Add Events system (#93)

* Add TenantManagerEvents

* Apply fixes from StyleCI

* Fix typos, add tests

* end() events
This commit is contained in:
Samuel Štancl 2019-08-14 17:15:47 +02:00 committed by GitHub
parent d7358c588c
commit c1df467601
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 149 additions and 0 deletions

View file

@ -9,6 +9,8 @@ use Stancl\Tenancy\Exceptions\PhpRedisNotInstalledException;
trait BootstrapsTenancy
{
use TenantManagerEvents;
public $originalSettings = [];
/**
* Was tenancy initialized/bootstrapped?
@ -19,6 +21,10 @@ trait BootstrapsTenancy
public function bootstrap()
{
array_map(function ($listener) {
$listener($this);
}, $this->listeners['bootstrapping']);
$this->initialized = true;
$this->switchDatabaseConnection();
@ -27,6 +33,10 @@ trait BootstrapsTenancy
}
$this->tagCache();
$this->suffixFilesystemRootPaths();
array_map(function ($listener) {
$listener($this);
}, $this->listeners['bootstrapped']);
}
public function end()

View file

@ -0,0 +1,70 @@
<?php
namespace Stancl\Tenancy\Traits;
trait TenantManagerEvents
{
/**
* Event listeners.
*
* @var callable[][]
*/
protected $listeners = [
'bootstrapping' => [],
'bootstrapped' => [],
'ending' => [],
'ended' => [],
];
/**
* Register a listener that will be executed before tenancy is bootstrapped.
*
* @param callable $callback
* @return self
*/
public function bootstrapping(callable $callback)
{
$this->listeners['bootstrapping'][] = $callback;
return $this;
}
/**
* Register a listener that will be executed after tenancy is bootstrapped.
*
* @param callable $callback
* @return self
*/
public function bootstrapped(callable $callback)
{
$this->listeners['bootstrapped'][] = $callback;
return $this;
}
/**
* Register a listener that will be executed before tenancy is ended.
*
* @param callable $callback
* @return self
*/
public function ending(callable $callback)
{
$this->listeners['ending'][] = $callback;
return $this;
}
/**
* Register a listener that will be executed after tenancy is ended.
*
* @param callable $callback
* @return self
*/
public function ended(callable $callback)
{
$this->listeners['ended'][] = $callback;
return $this;
}
}