Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Commit

Permalink
Code refactoring (#2)
Browse files Browse the repository at this point in the history
* Code refactoring
  • Loading branch information
FoxWS authored Sep 24, 2022
1 parent 170ac9a commit dff0218
Show file tree
Hide file tree
Showing 24 changed files with 462 additions and 586 deletions.
30 changes: 18 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

This package allows a single Laravel application to work with multiple domains/tenants.

It is intended to complement a multi-tenancy package such as [spatie/laravel-multitenancy](https://github.com/spatie/laravel-multitenancy), [archtechx/tenancy](https://github.com/archtechx/tenancy), etc.
It is intended to complement a multi-tenancy package such as [spatie/laravel-multitenancy](https://github.com/spatie/laravel-multitenancy) (tested), [archtechx/tenancy](https://github.com/archtechx/tenancy), etc.

It allows caching of configs, routes & views, and is made to be easy to install, as there is no need to modify the core of the Laravel Framework.

Expand All @@ -36,7 +36,6 @@ e.g. `app\Domain\Example\domain.json`:
```json
{
"name": "Example",
"enabled": true,
"domain": {
"local": "example.test",
"staging": "example.dev",
Expand All @@ -45,7 +44,7 @@ e.g. `app\Domain\Example\domain.json`:
}
```

> **NOTE:** The `domain` array matches the environment set in `.env`, e.g. `APP_ENV=local` will use `example.test` as it's base.
> **NOTE:** The `domain` array matches the environment set in `.env`, e.g. `APP_ENV=local` will use `example.test` as it's (route) base.
The structure of each domain should look like this, using `app\Domain\Example` as it's root path:

Expand All @@ -68,7 +67,7 @@ To interact with the domain(s), one may use the following:
| Helper | Description |
| --- | --- |
| `config('example.app.name')` | Would return the name of the application. |
| `route('example.home'` | Would return the route to `/`. |
| `route('example.home')` | Would return the route to `/`. |
| `view('example::home')` | Would return the `home.blade.php` located in views. |
| `domain('example')` | Would return a domain instance. |
| `<x-example::menu-component />` | Would return the `MenuComponent` located in components. |
Expand All @@ -80,27 +79,35 @@ When using Spatie's [laravel-multitenancy](https://github.com/spatie/laravel-mul
> **NOTE:** Please see [documentation](https://spatie.be/docs/laravel-multitenancy/v2/using-tasks-to-prepare-the-environment/creating-your-own-task) for details.
```php
namespace App\Support\Multitenancy\Tasks;
namespace App\Core\Support\Multitenancy\Tasks;

use Foxws\LaravelMultidomain\Contracts\RepositoryInterface;
use Foxws\LaravelMultidomain\Support\DomainRepository;
use Foxws\MultiDomain\Facades\MultiDomain;
use Foxws\MultiDomain\Providers\DomainServiceProvider;
use Spatie\Multitenancy\Models\Tenant;
use Spatie\Multitenancy\Tasks\SwitchTenantTask;

class SwitchDomainTask implements SwitchTenantTask
{
public function makeCurrent(Tenant $tenant): void
{
/** @var DomainRepository $repository */
$repository = app(RepositoryInterface::class);
$domains = MultiDomain::findByDomain($tenant->domain);

if ($domain = $repository->find($tenant->domain)) {
$domain->registerProviders();
foreach ($domains as $domain) {
app(DomainServiceProvider::class, compact('domain'))
->registerProviders();
}
}

public function forgetCurrent(): void
{
$domains = MultiDomain::findByDomain(
Tenant::current()->domain
);

foreach ($domains as $domain) {
app(DomainServiceProvider::class, compact('domain'))
->unregisterProviders();
}
}
}
```
Expand All @@ -125,7 +132,6 @@ Please review [our security policy](../../security/policy) on how to report secu

## Credits

- [foxws](https://github.com/foxws)
- [laravel-modules](https://github.com/nWidart/laravel-modules)
- [Spatie](https://github.com/spatie)
- [All Contributors](../../contributors)
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@
},
"autoload": {
"psr-4": {
"Foxws\\LaravelMultidomain\\": "src"
"Foxws\\MultiDomain\\": "src"
},
"files": [
"src/Helpers/functions.php"
]
},
"autoload-dev": {
"psr-4": {
"Foxws\\LaravelMultidomain\\Tests\\": "tests"
"Foxws\\MultiDomain\\Tests\\": "tests"
}
},
"scripts": {
Expand All @@ -63,10 +63,10 @@
"extra": {
"laravel": {
"providers": [
"Foxws\\LaravelMultidomain\\LaravelMultidomainServiceProvider"
"Foxws\\MultiDomain\\MultiDomainServiceProvider"
],
"aliases": {
"LaravelMultidomain": "Foxws\\LaravelMultidomain\\Facades\\LaravelMultidomain"
"MultiDomain": "Foxws\\MultiDomain\\Facades\\MultiDomain"
}
}
},
Expand Down
35 changes: 4 additions & 31 deletions config/multidomain.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,14 @@

/*
|--------------------------------------------------------------------------
| Domain path
| Domain caching
|--------------------------------------------------------------------------
|
| This defines the domain root path.
| This defines the domain caching.
|
*/

'path' => app_path('Domain'),
'cache_enabled' => env('MULTIDOMAIN_CACHE_ENABLED', false),

/*
|--------------------------------------------------------------------------
| Scan Path
|--------------------------------------------------------------------------
|
| This defines the path to scan for domains.
|
*/

'scan' => [
'enabled' => true,
'path' => app_path('Domain/*'),
],

/*
|--------------------------------------------------------------------------
| Caching
|--------------------------------------------------------------------------
|
| This defines config for setting up caching.
|
*/

'cache' => [
'enabled' => false,
'key' => 'multidomain',
'lifetime' => 60 * 60 * 24,
],
'cache_lifetime' => env('MULTIDOMAIN_CACHE_LIFETIME', 60 * 60 * 24 * 7),
];
23 changes: 0 additions & 23 deletions src/Commands/CacheCommand.php

This file was deleted.

18 changes: 10 additions & 8 deletions src/Commands/ClearCommand.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
<?php

namespace Foxws\LaravelMultidomain\Commands;
namespace Foxws\MultiDomain\Commands;

use Foxws\LaravelMultidomain\Contracts\RepositoryInterface;
use Foxws\LaravelMultidomain\Support\DomainRepository;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;

class ClearCommand extends Command
{
public $signature = 'multidomain:clear';

public $description = 'Clear the domain repository cache.';
public $description = 'Clear the multidomain cache.';

public function handle(): void
{
/** @var DomainRepository $repository */
$repository = app(RepositoryInterface::class);
$repository->removeCached();
$this->clearCache();

$this->info('Domains cached cleared.');
$this->info('Domain cache cleared.');
}

protected function clearCache(): bool
{
return Cache::forget('multidomain');
}
}
29 changes: 0 additions & 29 deletions src/Contracts/RepositoryInterface.php

This file was deleted.

121 changes: 0 additions & 121 deletions src/Domain.php

This file was deleted.

Loading

0 comments on commit dff0218

Please sign in to comment.