2. Usage

2.1. TenantContext

The TenantContext allows you to manage the currently used tenant.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<?php

// ..
use SWP\Component\MultiTenancy\Context\TenantContext;
use SWP\Component\MultiTenancy\Model\Tenant;

$tenant = new Tenant();
$tenantContext = new TenantContext();

$tenantContext->setTenant($tenant);

var_dump($tenantContext->getTenant());

Note

This service implements TenantContextInterface.

2.2. TenantProvider

The TenantProvider allows you to get all available tenants.

1
2
3
4
5
6
7
8
<?php

 // ..
use SWP\Component\MultiTenancy\Provider\TenantProvider;

$tenantProvider = new TenantProvider(/* SWP\Component\MultiTenancy\Repository\TenantRepositoryInterface repository */);

var_dump($tenantProvider->getAvailableTenants());

The getAvailableTenants method retrieves all tenants which have the enabled property set to true and have been inserted in the given repository.

Note

This service implements the TenantProviderInterface.

2.3. TenantResolver

The TenantResolver allows you to resolve the current tenant from the request.

1
2
3
4
5
6
7
8
<?php

// ..
use SWP\Component\MultiTenancy\Resolver\TenantResolver;

$tenantResolver = new TenantResolver('example.com', /* SWP\Component\MultiTenancy\Repository\TenantRepositoryInterface repository */);

var_dump($tenantResolver->resolve('tenant.example.com')); // will return an instance of TenantInterface.

The resolve method resolves the tenant based on the current subdomain name. For example, when the host is tenant.example.com, it will resolve the subdomain (tenant) and then it will search for the tenant in the given repository, by the resolved subdomain name. If the subdomain tenant is not found, it always returns the default tenant.

Note

This service implements the TenantResolverInterface.

2.4. TenantAwarePathBuilder

The TenantAwarePathBuilder responsibility is to build PHPCR base paths which are tenant-aware. This can build whatever path is needed by the provided paths’ names and the given context.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
<?php

// ..
use SWP\Component\MultiTenancy\PathBuilder\TenantAwarePathBuilder;
use SWP\Component\MultiTenancy\Context\TenantContext;
use SWP\Component\MultiTenancy\Model\Tenant;

$tenant = new Tenant();
$tenant->setSubdomain('example');
$tenant->setName('Example tenant');
$tenantContext = new TenantContext();
$tenantContext->setTenant($tenant);

$pathBuilder = new TenantAwarePathBuilder($tenantContext, '/swp');

var_dump($pathBuilder->build('routes')); // will return: /swp/example/routes.
var_dump($pathBuilder->build(['routes', 'content'])); // will return an array: ['/swp/example/routes', '/swp/example/routes']
var_dump($pathBuilder->build('/')); // will return: /swp/default

The build method method builds the PHPCR path. It accepts as a first argument, a string or an array of routes’ names. The second argument is the context for the given path(s) name(s).

In order to build the base paths, the TenantAwarePathBuilder’s construct requires an object of type TenantResolverInterface to be provided as a first argument, the object of type TenantContextInterface as a second argument, and the root base path as a third argument.

Note

This service implements the TenantResolverInterface.

2.5. TenantFactory

The TenantFactory allows you to create an objects of type TenantInterface.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<?php

 // ..
use SWP\Component\MultiTenancy\Model\Tenant;
use SWP\Component\MultiTenancy\Factory\TenantFactory;

$tenantFactory = new TenantFactory(Tenant::class);
$tenant = $tenantFactory->create();

var_dump($tenant);

2.6. OrganizationFactory

The OrganizationFactory allows you to create objects of type OrganizationInterface.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<?php

 // ..
use SWP\Component\MultiTenancy\Model\Organization;
use SWP\Component\MultiTenancy\Factory\OrganizationFactory;
use SWP\Component\Common\Generator\RandomStringGenerator;

$organizationFactory = new OrganizationFactory(Organization::class, new RandomStringGenerator());
$organization = $organizationFactory->create();
$organizationWithCode = $organizationFactory->createWithCode();

var_dump($organization);