Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Converting relative urls to absolute
- Loading branch information
Showing
3 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.idea/ | ||
vendor/ | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: Jaeger <JaegerCode@gmail.com> | ||
* Date: 2017/9/26 | ||
* Converting relative urls to absolute | ||
*/ | ||
|
||
namespace QL\Ext; | ||
|
||
use QL\Contracts\PluginContract; | ||
use QL\QueryList; | ||
use phpUri; | ||
|
||
class AbsoluteUrl implements PluginContract | ||
{ | ||
public static function install(QueryList $queryList, ...$opt) | ||
{ | ||
$absoluteUrl = $opt[0] ?? 'absoluteUrl'; | ||
$absoluteUrlHelper = $opt[1] ?? 'absoluteUrlHelper'; | ||
|
||
// convert all link | ||
$queryList->bind($absoluteUrl,function ($url){ | ||
return AbsoluteUrl::convertAll($this,$url); | ||
}); | ||
|
||
// convert helper | ||
$queryList->bind($absoluteUrlHelper,function ($url,$relativeUrl){ | ||
return phpUri::parse($url)->join($relativeUrl); | ||
}); | ||
} | ||
|
||
public static function convertAll($ql,$url) | ||
{ | ||
$parser = phpUri::parse($url); | ||
$ql->find('a')->map(function($item) use ($parser,$ql){ | ||
$relativeUrl = $item->attr('href'); | ||
$absoluteUrl = $parser->join($relativeUrl); | ||
$item->attr('href',$absoluteUrl); | ||
}); | ||
$ql->setHtml($ql->find('')->html()); | ||
return $ql; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"require": { | ||
"pguardiario/phpuri": "^1.0" | ||
}, | ||
"require-dev": { | ||
"jaeger/querylist": "dev-master" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"QL\\Ext\\": "" | ||
} | ||
} | ||
} |