Dependencies
This commit is contained in:
12
aldarien/url/.gitignore
vendored
Normal file
12
aldarien/url/.gitignore
vendored
Normal file
@ -0,0 +1,12 @@
|
||||
composer.phar
|
||||
/vendor/
|
||||
|
||||
# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
|
||||
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
|
||||
# composer.lock
|
||||
|
||||
.settings
|
||||
.buildpath
|
||||
.project
|
||||
|
||||
# Eclipse IDE
|
21
aldarien/url/LICENSE
Normal file
21
aldarien/url/LICENSE
Normal file
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2017 Aldarien
|
||||
|
||||
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.
|
2
aldarien/url/README.md
Normal file
2
aldarien/url/README.md
Normal file
@ -0,0 +1,2 @@
|
||||
# url
|
||||
Get relative path url
|
21
aldarien/url/app/Contract/URL.php
Normal file
21
aldarien/url/app/Contract/URL.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
namespace App\Contract;
|
||||
|
||||
use App\Definition\Contract;
|
||||
use App\Service\URL as URLService;
|
||||
|
||||
class URL
|
||||
{
|
||||
use Contract;
|
||||
|
||||
protected static function newInstance()
|
||||
{
|
||||
return new URLService();
|
||||
}
|
||||
public static function url($path = '', $variables = null)
|
||||
{
|
||||
$instance = self::getInstance();
|
||||
return $instance->url($path, $variables);
|
||||
}
|
||||
}
|
||||
?>
|
8
aldarien/url/app/Helper/functions.php
Normal file
8
aldarien/url/app/Helper/functions.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
function url($path = '', $variables = null) {
|
||||
return App\Contract\URL::url($path, $variables);
|
||||
}
|
||||
function baseUrl() {
|
||||
return url();
|
||||
}
|
||||
?>
|
76
aldarien/url/app/Service/URL.php
Normal file
76
aldarien/url/app/Service/URL.php
Normal file
@ -0,0 +1,76 @@
|
||||
<?php
|
||||
namespace App\Service;
|
||||
|
||||
use League\Uri\Http;
|
||||
use League\Uri\Components\Host;
|
||||
use League\Uri\Components\HierarchicalPath;
|
||||
|
||||
class URL
|
||||
{
|
||||
protected $root;
|
||||
protected $relative;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->root = $this->findRoot();
|
||||
$this->relative = $this->findRelative();
|
||||
}
|
||||
|
||||
protected function findRoot()
|
||||
{
|
||||
$base = $_SERVER['HTTP_HOST'] . ((isset($_SERVER['HTTP_PORT'])) ? ':' . $_SERVER['HTTP_PORT'] : '');
|
||||
$uri = Http::createFromString(\Sabre\Uri\resolve($_SERVER['REQUEST_SCHEME'] . '://' . $base, $_SERVER['SCRIPT_NAME']));
|
||||
$host = new Host($uri->getHost());
|
||||
if ($host->isAbsolute()) {
|
||||
return $host->getRegistrableDomain();
|
||||
}
|
||||
$base = $host . (($uri->getPort()) ? ':' . $uri->getPort() : '');
|
||||
return ($uri->getScheme() ?: 'http') . '://' . $base;
|
||||
}
|
||||
protected function findRelative()
|
||||
{
|
||||
$uri = Http::createFromString($_SERVER['SCRIPT_NAME']);
|
||||
$normalized = (new HierarchicalPath($uri->getPath()))->withoutLeadingSlash()->withoutTrailingSlash()->withoutDotSegments()->withoutEmptySegments();
|
||||
if ($normalized->getDirname() == '.') {
|
||||
return '';
|
||||
}
|
||||
return $normalized->getDirname();
|
||||
}
|
||||
|
||||
|
||||
public function url($path = '', $variables = null)
|
||||
{
|
||||
$uri = Http::createFromString($path);
|
||||
if ($uri->getHost() != $this->root and $uri->getHost() != '') {
|
||||
return $path;
|
||||
}
|
||||
|
||||
$uri = \Sabre\Uri\resolve($this->getBaseUrl(), $path);
|
||||
try {
|
||||
$host = new Host(Http::createFromString($uri)->getHost());
|
||||
} catch (\League\Uri\Exception $e) {
|
||||
$uri = \Sabre\Uri\resolve($this->getBaseUrl(), '../../') . '/' . basename($path);
|
||||
$host = new Host(Http::createFromString($uri)->getHost());
|
||||
}
|
||||
|
||||
$base = new Host(Http::createFromString($this->root)->getHost());
|
||||
if ($host . '' != $base . '') {
|
||||
$host = new Host(Http::createFromString($this->root)->getHost());
|
||||
$page = str_replace($this->root, '', $uri);
|
||||
$uri = \Sabre\Uri\resolve(Http::createFromString($this->root)->getScheme() . '://' . $host->getRegistrableDomain(). '/', $page);
|
||||
}
|
||||
|
||||
if ($variables != null) {
|
||||
$uri = \Sabre\Uri\resolve($uri, '?' . http_build_query($variables));
|
||||
}
|
||||
$uri = \Sabre\Uri\normalize($uri);
|
||||
|
||||
return $uri;
|
||||
}
|
||||
protected function getBaseUrl()
|
||||
{
|
||||
$url = \Sabre\Uri\normalize(trim($this->root . '/' . $this->relative, '/') . '/');
|
||||
return $url;
|
||||
}
|
||||
}
|
||||
?>
|
28
aldarien/url/composer.json
Normal file
28
aldarien/url/composer.json
Normal file
@ -0,0 +1,28 @@
|
||||
{
|
||||
"name" : "aldarien/url",
|
||||
"description" : "Get relative path uri",
|
||||
"type" : "library",
|
||||
"require" : {
|
||||
"aldarien/contract" : "dev-master",
|
||||
"aldarien/root" : "dev-master",
|
||||
"league/uri": "^5.2",
|
||||
"sabre/uri": "^2.1"
|
||||
},
|
||||
"require-dev" : {
|
||||
"phpunit/phpunit" : "^6.3"
|
||||
},
|
||||
"license" : "MIT",
|
||||
"authors" : [{
|
||||
"name" : "Aldarien",
|
||||
"email" : "jpvial@gmail.com"
|
||||
}
|
||||
],
|
||||
"autoload" : {
|
||||
"psr-4" : {
|
||||
"App\\" : "app"
|
||||
},
|
||||
"files": [
|
||||
"app/Helper/functions.php"
|
||||
]
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user