PHP code example of funkymed / tenant-aware-bundle
1. Go to this page and download the library: Download funkymed/tenant-aware-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
funkymed / tenant-aware-bundle example snippets
// src/Kernel.php
namespace App;
use Funkymed\TenantAwareBundle\TenantAwareKernel;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
class Kernel extends BaseKernel
{
use MicroKernelTrait;
private ?string $hostname;
public function __construct(
string $environment,
bool $debug,
string $hostname
) {
parent::__construct($environment, $debug);
$this->hostname = $hostname;
}
public function getCacheDir(): string
{
if ($this->getHostname()) {
return $this->getProjectDir().'/var/cache/'.$this->environment.'/'.$this->getHostname();
}
return $this->getProjectDir().'/var/cache/'.$this->environment;
}
public function getLogDir(): string
{
if ($this->getHostname()) {
return $this->getProjectDir().'/var/log/'.$this->getHostname();
}
return $this->getProjectDir().'/var/log';
}
public function getName()
{
return str_replace('-', '_', $this->getHostname());
}
public function getKernelParameters(): array
{
$parameters = parent::getKernelParameters();
$parameters['kernel.hostname'] = $this->getHostname();
return $parameters;
}
public function getHostname()
{
$hostname = $this->getHost();
return $hostname ? $hostname : $this->hostname;
}
public function getHost()
{
$possibleHostSources = array('HTTP_X_FORWARDED_HOST', 'HTTP_HOST', 'SERVER_NAME', 'SERVER_ADDR');
$sourceTransformations = array(
"HTTP_X_FORWARDED_HOST" => function ($value) {
$elements = explode(',', $value);
return trim(end($elements));
}
);
$host = '';
foreach ($possibleHostSources as $source) {
if (!empty($host)) {
break;
}
if (empty($_SERVER[$source])) {
continue;
}
$host = $_SERVER[$source];
if (array_key_exists($source, $sourceTransformations)) {
$host = $sourceTransformations[$source]($host);
}
}
// Remove port number from host
$host = preg_replace('/:\d+$/', '', $host);
return trim($host);
}
}
#!/usr/bin/env php
// bin/console
use App\Kernel;
use Funkymed\TenantAwareBundle\Command\TenantAwareApplication;
use Symfony\Component\Console\Input\ArgvInput;
if (!is_dir(dirname(__DIR__).'/vendor')) {
throw new LogicException('Dependencies are missing. Try running "composer install".');
}
if (!is_file(dirname(__DIR__).'/vendor/autoload_runtime.php')) {
throw new LogicException('Symfony Runtime is missing. Try running "composer new TenantAwareApplication($kernel);
};
// src/DependencyInjection/DependencyInjection/Compiler/Processor/DummyProcessor.php
namespace App\DependencyInjection\Compiler\Processor;
// use this as an exemple to create your own replacement configuration
class MyProcessor extends ProcessorAbstract
{
public function process()
{
// get current definition
$definition = $this->container->getDefinition('doctrine.dbal.default_connection');
$configuration = $definition->getArguments();
// update it from the tenant information
$configuration[0]["host"] = $this->tenant->getDatabaseHost();
$configuration[0]["dbname"] = $this->tenant->getDatabaseName();
$configuration[0]["user"] = $this->tenant->getDatabaseUser();
$configuration[0]["password"] = $this->tenant->getDatabasePassword();
// replace the current configuration everything is in the cache now
$definition->replaceArgument(0, $configuration[0]);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.