1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 01:34:03 +00:00
This commit is contained in:
Samuel Štancl 2019-08-16 23:38:08 +02:00
parent 5f1dc0b84d
commit 93c6361369
5 changed files with 230 additions and 1 deletions

View file

@ -0,0 +1,85 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="description" content="<?php echo e($page->description ?? $page->siteDescription); ?>">
<meta property="og:site_name" content="<?php echo e($page->siteName); ?>"/>
<meta property="og:title" content="<?php echo e($page->title ? $page->title . ' | ' : ''); ?><?php echo e($page->siteName); ?>"/>
<meta property="og:description" content="<?php echo e($page->description ?? $page->siteDescription); ?>"/>
<meta property="og:url" content="<?php echo e($page->getUrl()); ?>"/>
<meta property="og:image" content="/assets/img/logo.png"/>
<meta property="og:type" content="website"/>
<meta name="twitter:image:alt" content="<?php echo e($page->siteName); ?>">
<meta name="twitter:card" content="summary_large_image">
<?php if($page->docsearchApiKey && $page->docsearchIndexName): ?>
<meta name="generator" content="tighten_jigsaw_doc">
<?php endif; ?>
<title><?php echo e($page->siteName); ?><?php echo e($page->title ? ' | ' . $page->title : ''); ?></title>
<link rel="home" href="<?php echo e($page->baseUrl); ?>">
<link rel="icon" href="/favicon.ico">
<?php echo $__env->yieldPushContent('meta'); ?>
<?php if($page->production): ?>
<!-- Insert analytics code here -->
<?php endif; ?>
<link href="https://fonts.googleapis.com/css?family=Nunito+Sans:300,300i,400,400i,700,700i,800,800i" rel="stylesheet">
<link rel="stylesheet" href="<?php echo e(mix('css/main.css', 'assets/build')); ?>">
<?php if($page->docsearchApiKey && $page->docsearchIndexName): ?>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/docsearch.js@2/dist/cdn/docsearch.min.css" />
<?php endif; ?>
</head>
<body class="flex flex-col justify-between min-h-screen bg-grey-lightest text-grey-darkest leading-normal font-sans">
<header class="flex items-center shadow bg-white border-b h-24 mb-8 py-4" role="banner">
<div class="container flex items-center max-w-4xl mx-auto px-4 lg:px-8">
<div class="flex items-center">
<a href="/" title="<?php echo e($page->siteName); ?> home" class="inline-flex items-center">
<h1 class="text-lg md:text-2xl text-blue-darkest font-semibold hover:text-blue-dark my-0 pr-4"><?php echo e($page->siteName); ?></h1>
</a>
</div>
<div class="flex flex-1 justify-end items-center text-right md:pl-10">
<?php if($page->docsearchApiKey && $page->docsearchIndexName): ?>
<?php echo $__env->make('_nav.search-input', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>
<?php endif; ?>
</div>
</div>
<?php echo $__env->yieldContent('nav-toggle'); ?>
</header>
<main role="main" class="w-full flex-auto">
<?php echo $__env->yieldContent('body'); ?>
</main>
<script src="<?php echo e(mix('js/main.js', 'assets/build')); ?>"></script>
<?php echo $__env->yieldPushContent('scripts'); ?>
<footer class="bg-white text-center text-sm mt-12 py-4" role="contentinfo">
<ul class="flex flex-col md:flex-row justify-center list-reset">
<li class="md:mr-2">
&copy; <a href="https://github.com/stancl" title="Samuel Štancl">Samuel Štancl</a> <?php echo e(date('Y')); ?>.
</li>
<li>
Built with <a href="http://jigsaw.tighten.co" title="Jigsaw by Tighten">Jigsaw</a>
and <a href="https://tailwindcss.com" title="Tailwind CSS, a utility-first CSS framework">Tailwind CSS</a>.
</li>
</ul>
</footer>
</body>
</html>
<?php /**PATH /home/samuel/Projects/tenancy-docs/source/_layouts/master.blade.php ENDPATH**/ ?>

View file

@ -0,0 +1,15 @@
<?php $__env->startSection('body'); ?>
<div class="flex flex-col items-center mt-32 text-grey-darker">
<h1 class="text-6xl font-light leading-none mb-2">404</h1>
<h2 class="text-3xl">Page not found</h2>
<hr class="block w-full max-w-sm mx-auto my-8 border">
<p class="text-xl">
Need to update this page? See the <a title="404 Page Documentation" href="https://jigsaw.tighten.co/docs/custom-404-page/"> Jigsaw documentation</a>.
</p>
</div>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('_layouts.master', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /home/samuel/Projects/tenancy-docs/source/404.blade.php ENDPATH**/ ?>

View file

@ -44,7 +44,7 @@
<div class="container flex items-center max-w-4xl mx-auto px-4 lg:px-8">
<div class="flex items-center">
<a href="/" title="{{ $page->siteName }} home" class="inline-flex items-center">
<img class="h-8 md:h-10 mr-3" src="/assets/img/logo.svg" alt="{{ $page->siteName }} logo" />
{{-- <img class="h-8 md:h-10 mr-3" src="/assets/img/logo.svg" alt="{{ $page->siteName }} logo" /> --}}
<h1 class="text-lg md:text-2xl text-blue-darkest font-semibold hover:text-blue-dark my-0 pr-4">{{ $page->siteName }}</h1>
</a>

View file

@ -0,0 +1,127 @@
---
title: Tenancy Initialization
description: Tenancy Initialization with stancl/tenancy — A Laravel multi-database tenancy package that respects your code..
extends: _layouts.documentation
section: content
---
# Tenancy Initialization {#tenancy-initialization}
When you, or the `InitializeTenancy` middleware, run `tenancy()->init()`, the `bootstrap()` method is called.
This method switches database connection, Redis connection (if Redis tenancy is enabled), cache and filesystem root paths.
This page goes through the code that actually makes this happen. You don't have to read this page to use the package, but it will give you insight into the magic that's happening in the background, so that you can be more confident in it.
## Database tenancy {#database-tenancy}
`bootstrap()` runs the following method:
```php
public function switchDatabaseConnection()
{
$this->database->connect($this->getDatabaseName());
}
```
If `tenancy.database_name_key` is set and present in the current tenant's data, the `getDatabaseName()` returns the stored database_name. Otherwise it returns the prefix + uuid + suffix.
```php
public function getDatabaseName($tenant = []): string
{
$tenant = $tenant ?: $this->tenant;
if ($key = $this->app['config']['tenancy.database_name_key']) {
if (isset($tenant[$key])) {
return $tenant[$key];
}
}
return $this->app['config']['tenancy.database.prefix'] . $tenant['uuid'] . $this->app['config']['tenancy.database.suffix'];
}
```
This is passed as an argument to the `connect()` method. This method creates a new database connection and sets it as the default one.
```php
public function connect(string $database)
{
$this->createTenantConnection($database);
$this->useConnection('tenant');
}
public function createTenantConnection(string $database_name)
{
// Create the `tenancy` database connection.
$based_on = config('tenancy.database.based_on') ?: config('database.default');
config()->set([
'database.connections.tenant' => config('database.connections.' . $based_on),
]);
// Change DB name
$database_name = $this->getDriver() === 'sqlite' ? database_path($database_name) : $database_name;
config()->set(['database.connections.tenant.database' => $database_name]);
}
public function useConnection(string $connection)
{
// $this->database = Illuminate\Database\DatabaseManager
$this->database->setDefaultConnection($connection);
$this->database->reconnect($connection);
}
```
## Redis tenancy {#redis-tenancy}
The `bootstrap()` method calls `setPhpRedisPrefix()` if `tenancy.redis.tenancy` is `true`.
This method cycles through the `tenancy.redis.prefixed_connections` and sets their prefix to `tenancy.redis.prefix_base` + uuid.
```php
public function setPhpRedisPrefix($connections = ['default'])
{
// [...]
foreach ($connections as $connection) {
$prefix = $this->app['config']['tenancy.redis.prefix_base'] . $this->tenant['uuid'];
$client = Redis::connection($connection)->client();
try {
// [...]
$client->setOption($client::OPT_PREFIX, $prefix);
} catch (\Throwable $t) {
throw new PhpRedisNotInstalledException();
}
}
}
```
## Cache tenancy {#cache-tenancy}
`bootstrap()` calls `tagCache()` which replaces the `'cache'` key in the service container with a different `CacheManager`.
```php
public function tagCache()
{
// [...]
$this->app->extend('cache', function () {
return new \Stancl\Tenancy\CacheManager($this->app);
});
}
```
This `CacheManager` forwards all calls to the inner store, but also adds tag which "scope" the cache and allow for selective cache clearing:
```php
class CacheManager extends BaseCacheManager
{
public function __call($method, $parameters)
{
$tags = [config('tenancy.cache.tag_base') . tenant('uuid')];
if ($method === 'tags') {
if (\count($parameters) !== 1) {
throw new \Exception("Method tags() takes exactly 1 argument. {count($parameters)} passed.");
}
$names = $parameters[0];
$names = (array) $names; // cache()->tags('foo') https://laravel.com/docs/5.7/cache#removing-tagged-cache-items
return $this->store()->tags(\array_merge($tags, $names));
}
return $this->store()->tags($tags)->$method(...$parameters);
}
}
```
## Filesystem tenancy {#filesystem-tenancy}
todo

View file

@ -7,6 +7,8 @@ section: content
# Getting Started {#getting-started}
todo tenancy end, todo deleting specific cache tags
[**stancl/tenancy**](https://github.com/stancl/tenancy) is a Laravel multi-database tenancy package. It is designed in a way that requires you to make no changes to your codebase. Instead of applying traits on models and replacing every single reference to cache by a reference to a tenant-aware cache, the package lets you write your app without thinking about tenancy. It handles tenancy automatically.
> Note: Filesystem is the only thing that can be a little problematic. Be sure to read [that page](TODOfilesystem).