From 81a6b00cbc67caaf96bdaa95d2a2d264b04c07c3 Mon Sep 17 00:00:00 2001 From: Fabrice Locher Date: Sat, 19 Sep 2020 13:32:30 +0200 Subject: [PATCH] Tenant aware tinker should define tenant interface Instead of defining the connection, we should also define the `Hyn\Tenancy\Contracts\Tenant` interface. This ensures that retrieving the current website via `app(\Hyn\Tenancy\Environment::class)->tenant();` as suggested in the docs is possible. --- docs/hyn/5.4/commands.md | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/docs/hyn/5.4/commands.md b/docs/hyn/5.4/commands.md index 9b41104e..47759534 100644 --- a/docs/hyn/5.4/commands.md +++ b/docs/hyn/5.4/commands.md @@ -76,6 +76,7 @@ namespace App\Traits; use Hyn\Tenancy\Contracts\Repositories\WebsiteRepository; use Hyn\Tenancy\Database\Connection; +use Hyn\Tenancy\Environment; use Illuminate\Database\Eloquent\ModelNotFoundException; use Symfony\Component\Console\Exception\RuntimeException; use Symfony\Component\Console\Input\InputOption; @@ -87,6 +88,11 @@ trait MutatesTinkerCommand */ private $connection; + /** + * @var Environment + */ + private $environment; + /** * @var WebsiteRepository */ @@ -95,9 +101,10 @@ trait MutatesTinkerCommand public function __construct() { parent::__construct(); - $this->setName('tenancy:' . $this->getName()); + $this->setName('tenancy:'.$this->getName()); $this->websites = app(WebsiteRepository::class); $this->connection = app(Connection::class); + $this->environment = app(Environment::class); } /** @@ -110,13 +117,15 @@ trait MutatesTinkerCommand public function handle() { $website_id = $this->option('website_id'); - try{ + try { $website = $this->websites->query()->where('id', $website_id)->firstOrFail(); $this->connection->set($website); - $this->info('Running Tinker on website_id: ' . $website_id); + $this->environment->tenant($website); + + $this->info('Running Tinker on website_id: '.$website_id); parent::handle(); - + $this->connection->purge(); } catch (ModelNotFoundException $e) { throw new RuntimeException(sprintf('The tenancy website_id=%d does not exist.', $website_id));