From 81f69de7ca74a8280ab8c7def894cc3cdb6bb0a7 Mon Sep 17 00:00:00 2001 From: sheychen Date: Sat, 10 Jun 2017 17:21:55 +0200 Subject: [PATCH] First MultiHost commit --- LICENSE | 21 ++++++++++++++++ composer.json | 18 +++++++++++++ exemple/Hosts.php | 24 ++++++++++++++++++ src/Host.php | 64 +++++++++++++++++++++++++++++++++++++++++++++++ src/HostTrait.php | 26 +++++++++++++++++++ 5 files changed, 153 insertions(+) create mode 100644 LICENSE create mode 100644 composer.json create mode 100644 exemple/Hosts.php create mode 100644 src/Host.php create mode 100644 src/HostTrait.php diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..59d50c5 --- /dev/null +++ b/LICENSE @@ -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. diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..b38ac3b --- /dev/null +++ b/composer.json @@ -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/" + } + } +} \ No newline at end of file diff --git a/exemple/Hosts.php b/exemple/Hosts.php new file mode 100644 index 0000000..8269c73 --- /dev/null +++ b/exemple/Hosts.php @@ -0,0 +1,24 @@ + 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 +); \ No newline at end of file diff --git a/src/Host.php b/src/Host.php new file mode 100644 index 0000000..5ff971b --- /dev/null +++ b/src/Host.php @@ -0,0 +1,64 @@ +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; + } +} \ No newline at end of file diff --git a/src/HostTrait.php b/src/HostTrait.php new file mode 100644 index 0000000..d767ad1 --- /dev/null +++ b/src/HostTrait.php @@ -0,0 +1,26 @@ +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); + } +} \ No newline at end of file