Browse Source

refactor Rels class into library and controller

pull/38/head
Aaron Parecki 7 years ago
parent
commit
932cbedf45
No known key found for this signature in database GPG Key ID: 276C2817346D6056
4 changed files with 95 additions and 47 deletions
  1. +2
    -1
      composer.json
  2. +8
    -46
      controllers/Rels.php
  3. +22
    -0
      lib/XRay.php
  4. +63
    -0
      lib/XRay/Rels.php

+ 2
- 1
composer.json View File

@ -18,7 +18,8 @@
"p3k\\XRay\\": "lib/XRay" "p3k\\XRay\\": "lib/XRay"
}, },
"files": [ "files": [
"lib/helpers.php"
"lib/helpers.php",
"lib/XRay.php"
] ]
}, },
"require-dev": { "require-dev": {

+ 8
- 46
controllers/Rels.php View File

@ -24,13 +24,15 @@ class Rels {
} }
public function fetch(Request $request, Response $response) { public function fetch(Request $request, Response $response) {
$opts = [];
if($request->get('timeout')) { if($request->get('timeout')) {
// We might make 2 HTTP requests, so each request gets half the desired timeout // We might make 2 HTTP requests, so each request gets half the desired timeout
$this->http->timeout = $request->get('timeout') / 2;
$opts['timeout'] = $request->get('timeout') / 2;
} }
if($request->get('max_redirects')) { if($request->get('max_redirects')) {
$this->http->max_redirects = (int)$request->get('max_redirects');
$opts['max_redirects'] = (int)$request->get('max_redirects');
} }
if($request->get('pretty')) { if($request->get('pretty')) {
@ -46,51 +48,11 @@ class Rels {
]); ]);
} }
// Attempt some basic URL validation
$scheme = parse_url($url, PHP_URL_SCHEME);
if(!in_array($scheme, ['http','https'])) {
return $this->respond($response, 400, [
'error' => 'invalid_url',
'error_description' => 'Only http and https URLs are supported'
]);
}
$host = parse_url($url, PHP_URL_HOST);
if(!$host) {
return $this->respond($response, 400, [
'error' => 'invalid_url',
'error_description' => 'The URL provided was not valid'
]);
}
$url = p3k\XRay\normalize_url($url);
$result = $this->http->get($url);
$html = $result['body'];
$mf2 = mf2\Parse($html, $result['url']);
$rels = $result['rels'];
if(isset($mf2['rels'])) {
$rels = array_merge($rels, $mf2['rels']);
}
// Resolve all relative URLs
foreach($rels as $rel=>$values) {
foreach($values as $i=>$value) {
$value = \mf2\resolveUrl($result['url'], $value);
$rels[$rel][$i] = $value;
}
}
if(count($rels) == 0)
$rels = new StdClass;
$xray = new p3k\XRay();
$xray->http = $this->http;
$res = $xray->rels($url, $opts);
return $this->respond($response, 200, [
'url' => $result['url'],
'code' => $result['code'],
'rels' => $rels
]);
return $this->respond($response, !empty($res['error']) ? 400 : 200, $res);
} }
} }

+ 22
- 0
lib/XRay.php View File

@ -0,0 +1,22 @@
<?php
namespace p3k;
class XRay {
public $http;
public function __construct() {
$this->http = new HTTP();
}
public function rels($url, $opts=[]) {
$rels = new XRay\Rels($this->http);
return $rels->parse($url, $opts);
}
public function parse($url, $opts=[]) {
$parser = new XRay\Parser($this->http);
return $parser->parse($url, $opts);
}
}

+ 63
- 0
lib/XRay/Rels.php View File

@ -0,0 +1,63 @@
<?php
namespace p3k\XRay;
class Rels {
private $http;
public function __construct($http) {
$this->http = $http;
}
public function parse($url, $opts=[]) {
if(isset($opts['timeout']))
$this->http->set_timeout($opts['timeout']);
if(isset($opts['max_redirects']))
$this->http->set_max_redirects($opts['max_redirects']);
$scheme = parse_url($url, PHP_URL_SCHEME);
if(!in_array($scheme, ['http','https'])) {
return [
'error' => 'invalid_url',
'error_description' => 'Only http and https URLs are supported'
];
}
$host = parse_url($url, PHP_URL_HOST);
if(!$host) {
return [
'error' => 'invalid_url',
'error_description' => 'The URL provided was not valid'
];
}
$url = normalize_url($url);
$result = $this->http->get($url);
$html = $result['body'];
$mf2 = \mf2\Parse($html, $result['url']);
$rels = $result['rels'];
if(isset($mf2['rels'])) {
$rels = array_merge($rels, $mf2['rels']);
}
// Resolve all relative URLs
foreach($rels as $rel=>$values) {
foreach($values as $i=>$value) {
$value = \mf2\resolveUrl($result['url'], $value);
$rels[$rel][$i] = $value;
}
}
if(count($rels) == 0)
$rels = new \StdClass;
return [
'url' => $result['url'],
'code' => $result['code'],
'rels' => $rels
];
}
}

Loading…
Cancel
Save