Skip to content

Commit

Permalink
porting to 5.5 - step 1
Browse files Browse the repository at this point in the history
  • Loading branch information
gecche committed Jun 23, 2018
1 parent 59410b8 commit bd8d299
Show file tree
Hide file tree
Showing 16 changed files with 165 additions and 284 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
# laravel-multidomain
A Laravel extension for using a laravel application on a multi domain setting

## Description

## Documentation

## Compability

v1.1 requires Laravel 5.5+
v1.0 requires Laravel 5.1+

## Tests
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,12 @@
"files": [
"src/Foundation/helpers.php"
]
},
"extra": {
"laravel": {
"providers": [
"Gecche\\Multidomain\\Foundation\\DomainConsoleServiceProvider"
]
}
}
}
33 changes: 0 additions & 33 deletions src/DomainServiceProvider.php

This file was deleted.

112 changes: 28 additions & 84 deletions src/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Cookie;

class Application extends \Illuminate\Foundation\Application {
class Application extends \Illuminate\Foundation\Application
{
/**
* The environment file to load during bootstrapping.
*
Expand All @@ -14,17 +15,18 @@ class Application extends \Illuminate\Foundation\Application {
protected $environmentFile = null;

/**
* Detect the application's current environment.
* Detect the application's current domain.
*
* @param array|string $envs
* @param array|string $envs
* @return string
*/
public function detectDomain() {
public function detectDomain()
{
$args = isset($_SERVER['argv']) ? $_SERVER['argv'] : null;

$domainDetector = new DomainDetector();
$fullDomain = $domainDetector->detect($args);
list($domain_scheme,$domain_name,$domain_port) = $domainDetector->split($fullDomain);
list($domain_scheme, $domain_name, $domain_port) = $domainDetector->split($fullDomain);
$this['full_domain'] = $fullDomain;
$this['domain'] = $domain_name;
$this['domain_scheme'] = $domain_scheme;
Expand All @@ -33,12 +35,12 @@ public function detectDomain() {
}

/**
* Get or check the current application environment.
* Get or check the current application domain.
*
* @param mixed
* @return string
*/
public function domain() {
public function domain()
{
if (count(func_get_args()) > 0) {
return in_array($this['domain'], func_get_args());
}
Expand All @@ -47,12 +49,13 @@ public function domain() {
}

/**
* Get or check the current application environment.
* Get or check the full current application domain with HTTP scheme and port.
*
* @param mixed
* @return string
*/
public function fullDomain() {
public function fullDomain()
{
if (count(func_get_args()) > 0) {
return in_array($this['full_domain'], func_get_args());
}
Expand All @@ -70,14 +73,26 @@ public function environmentFile()
return $this->environmentFile ?: $this->environmentFileDomain();
}

public function environmentFileDomain() {
/**
* Get the environment file of the current domain if it exists.
* The file has to be named .env.<DOMAIN>
* It returns the base .env file if a specific file does not exist.
*
* @return string
*/
public function environmentFileDomain()
{
$filePath = rtrim($this['path.base'], DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
$file = '.env.' . $this['domain'];
return file_exists($filePath.$file) ? $file : '.env';
return file_exists($filePath . $file) ? $file : '.env';
}

/**
* Get the path to the storage directory.
* Get the path to the storage directory of the current domain.
* The storage path is a folder in the main storage laravel folder
* with the sanitized domain name (dots are replaced with underscores)
* It is sanitized in order to avoid problems with dots in paths especially
* in the case of using array_dot notation.
*
* @return string
*/
Expand All @@ -90,76 +105,5 @@ public function domainStoragePath()
return $this->storagePath();
}

public function getLocale()
{
$locale = $this['config']->get('app.locale');
//SEssion $locale = $this['request']->cookie('lang') ? $this['request']->cookie('lang') : $locale;
$locale = $this['request']->cookie('lang') ? $this['request']->cookie('lang') : $locale;
return $locale;
}

/**
* Detect and set application localization environment (language).
* NOTE: Don't foreget to ADD/SET/UPDATE the locales array in app/config/app.php!
*
*/


// public function configureLocale()
// {
//
// // Set default locale.
// $mLocale = $this['config']->get( 'app.locale' );
//
// // Has a session locale already been set?
// if ( !$this['session']->has( 'locale' ) )
// {
// // No, a session locale hasn't been set.
// // Was there a cookie set from a previous visit?
// $mFromCookie = Cookie::get( 'lang', null );
// //$mFromCookie = array_get($_COOKIE,'lang',null);
//
// if ( $mFromCookie != null && in_array( $mFromCookie, $this['config']->get( 'app.langs' ) ) )
// {
// // Cookie was previously set and it's a supported locale.
// $mLocale = $mFromCookie;
// }
// else
// {
// // No cookie was set.
// // Attempt to get local from current URI.
// $mFromURI = $this['request']->segment( 1 );
// if ( $mFromURI != null && in_array( $mFromURI, $this['config']->get( 'app.langs' ) ) )
// {
// // supported locale
// $mLocale = $mFromURI;
// }
// else
// {
// // attempt to detect locale from browser.
// $mFromBrowser = substr( Request::server( 'http_accept_language' ), 0, 2 );
// if ( $mFromBrowser != null && in_array( $mFromBrowser, $this['config']->get( 'app.langs' ) ) )
// {
// // browser lang is supported, use it.
// $mLocale = $mFromBrowser;
// } // $mFromBrowser
// } // $mFromURI
// } // $mFromCookie
//
// $this['session']->put( 'locale', $mLocale );
//
// //$_COOKIE['lang'] = $mLocale;
// Cookie::forever( 'lang', $mLocale);
// } // Session?
// else
// {
// // session locale is available, use it.
// $mLocale = $this['session']->get( 'locale' );
// } // Session?
//
// // set application locale for current session.
// $this->setLocale( $mLocale );
//
// }
//
}
2 changes: 1 addition & 1 deletion src/Foundation/Bootstrap/DetectDomain.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function bootstrap(Application $app)
//Detect the domain
$app->detectDomain();

//Overrides the storage path if the domain staorge path exists
//Overrides the storage path if the domain stoarge path exists
$app->useStoragePath($app->domainStoragePath());

}
Expand Down
77 changes: 15 additions & 62 deletions src/Foundation/Console/AddDomainCommand.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
<?php namespace Gecche\Multidomain\Foundation\Console;

use Illuminate\Console\GeneratorCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Config;
use Illuminate\Support\Facades\Config;

class AddDomainCommand extends GeneratorCommand
{

use DomainCommandTrait;

protected $name = "domain:add";
protected $description = "Adds a domain to the framework.";
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'domain:add
{domain : The name of the domain to add to the framework}
{--domain_values= : The optional values for the domain variables to be stored in the env file (json object)}
{--force : Force the creation of domain storage dirs also if they already exist}';


protected $description = "Adds a domain to the framework by creating the specific .env file and storage dirs.";
protected $domain;

/*
* Se il file di ambiente esiste già viene semplicemente sovrascirtto con i nuovi valori passati dal comando (update)
*/
public function fire()
public function handle()
{
$this->domain = $this->argument('domain');

Expand Down Expand Up @@ -78,38 +86,6 @@ protected function createDomainEnvFile()
$this->files->put($domainEnvFilePath, $domainEnvFileContents);
}

protected function getVarsArray($path)
{
$envFileContents = $this->files->getArray($path);
$varsArray = array();
foreach ($envFileContents as $line) {
$lineArray = explode('=', $line);

if (count($lineArray) !== 2) {
continue;
}

$varsArray[$lineArray[0]] = trim($lineArray[1]);

}
return $varsArray;
}

protected function makeDomainEnvFileContents($domainValues)
{
$contents = '';
$previousKeyPrefix = '';
foreach ($domainValues as $key => $value) {
$keyPrefix = current(explode('_', $key));
if ($keyPrefix !== $previousKeyPrefix && !empty($contents)) {
$contents .= "\n";
}
$contents .= $key . '=' . $value . "\n";
$previousKeyPrefix = $keyPrefix;
}
return $contents;
}

public function createDomainStorageDirs()
{
$storageDirs = Config::get('domain.storage_dirs', array());
Expand Down Expand Up @@ -159,31 +135,8 @@ protected function addDomainToConfigFile($config) {
return $config;
}

protected function getArguments()
{
return [
["domain", InputArgument::REQUIRED, "The name of the domain to add to the framework."],
];
}

protected function getOptions()
{
return [
[
"domain_values",
null,
InputOption::VALUE_OPTIONAL,
"The optional values for the domain variables (json object).",
"{}"
],
[
"force",
null,
InputOption::VALUE_NONE,
"Force the creation of domain storage dirs also if they already exist"
],
];
}



}
4 changes: 2 additions & 2 deletions src/Foundation/Console/DomainCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class DomainCommand extends Command {
*
* @var string
*/
protected $name = 'domain';
protected $signature = 'domain';

/**
* The console command description.
Expand All @@ -23,7 +23,7 @@ class DomainCommand extends Command {
*
* @return void
*/
public function fire()
public function handle()
{
$this->line('<info>Current application domain:</info> <comment>'.$this->laravel['domain']. "--" . $this->laravel['domain_port'] . "--" . $this->laravel['domain_scheme'].'</comment>');
}
Expand Down
5 changes: 3 additions & 2 deletions src/Foundation/Console/DomainCommandTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ protected function updateConfigFile($opType = 'add')

protected function getVarsArray($path)
{
$envFileContents = $this->files->getArray($path);
$envFileContents = $this->files->get($path);
$envFileContentsArray = explode("\n",$envFileContents);
$varsArray = array();
foreach ($envFileContents as $line) {
foreach ($envFileContentsArray as $line) {
$lineArray = explode('=', $line);

if (count($lineArray) !== 2) {
Expand Down
Loading

0 comments on commit bd8d299

Please sign in to comment.