First MultiHost commit

master
sheychen 2017-06-10 17:21:55 +02:00
commit 81f69de7ca
5 changed files with 153 additions and 0 deletions

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2017 sheychen
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

18
composer.json Normal file
View File

@ -0,0 +1,18 @@
{
"name": "krutush/host",
"license": "MIT",
"authors": [
{
"name": "sheychen",
"email": "contact@clementbois.fr"
}
],
"require": {
"krutush/krutush": "dev-master"
},
"autoload": {
"psr-4": {
"Krutush\\Host\\": "src/"
}
}
}

24
exemple/Hosts.php Normal file
View File

@ -0,0 +1,24 @@
<?php return array(
'one' => array(
'data' => array(
'name' => 'Domain One',
'text' => 'One is the best.'
),
'url' => array(
'one.dev',
'www.one.dev',
'banana.fr'
)
),
'two' => array(
'data' => array(
'name' => 'Domain Two',
'text' => 'Two is better than One'
),
'url' => array(
'two.dev',
'www.banana.fr'
)
)
//So www.two.dev => Not Found
);

64
src/Host.php Normal file
View File

@ -0,0 +1,64 @@
<?php
namespace Krutush\Host;
class Host{
/**
* Hosts and data
* @var array
*/
private $hosts = array();
protected $url = false;
protected $domain = false;
private $isSet = false;
public function __construct(string $path = null){
if(isset($path))
$this->hosts = include($path);
}
public function getUrl(){
if($this->isSet === false){
$this->update();
}
return $this->url;
}
public function getDomain(){
if($this->isSet === false){
$this->update();
}
return $this->domain;
}
public function getData(string $key, string $domain = null){
$domain = $domain ?: $this->getDomain();
if(!isset($this->hosts[$domain]['data'][$key]))
return null;
return $this->hosts[$domain]['data'][$key];
}
public function update(){
$this->url = false;
$this->domain = false;
$this->isSet = true;
$host = $_SERVER['HTTP_HOST'] ?: $_SERVER['SERVER_NAME'];
if(!isset($host))
return false;
$host = preg_replace('/:\d+$/', '', $host);
foreach($this->hosts as $domain => $infos){
if(in_array($host, $infos['url'])){
$this->url = $host;
$this->domain = $domain;
return true;
}
}
return false;
}
}

26
src/HostTrait.php Normal file
View File

@ -0,0 +1,26 @@
<?php
namespace Krutush\Host;
use Krutush\Router;
trait HostTrait{
protected $host;
public function getHost() {
return clone $this->host;
}
public function __construct(array $data = array()){
parent::__construct($data);
$this->host = new Host(isset($data['host']) ? $data['host'] : (Path::isset('config') ? (Path::get('config').'/Hosts.php') : null));
}
public function run(string $uri = null, array $filters = array()){
if($this->host->getUrl() === false or $this->host->getDomain() === false)
$this->error();
$filters['domain'] = $this->host->getDomain();
parent::run($uri, $filters);
}
}