refactoring
This commit is contained in:
parent
edbc6c55ae
commit
a7053dfef7
|
@ -1,7 +1,10 @@
|
||||||
{
|
{
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"files": [
|
"files": [
|
||||||
"src/functions/simple-router.php"
|
"src/routing.php",
|
||||||
|
"src/validators.php",
|
||||||
|
"src/parsers.php",
|
||||||
|
"src/actions.php"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
|
|
57
src/actions.php
Normal file
57
src/actions.php
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
class Action
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Home action.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function home():string
|
||||||
|
{
|
||||||
|
return response()
|
||||||
|
->httpCode(200)
|
||||||
|
->json([]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parser action.
|
||||||
|
*
|
||||||
|
* @param string $parser
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function parse(string $parser):string
|
||||||
|
{
|
||||||
|
$url = input()->get('url')->value;
|
||||||
|
|
||||||
|
if (!isValidUrl($url, getenv('API_HOSTNAME'))) {
|
||||||
|
return response()
|
||||||
|
->httpCode(400)
|
||||||
|
->json(['error' => 'Invalid URL']);
|
||||||
|
}
|
||||||
|
|
||||||
|
$parsers = [
|
||||||
|
'graby' => 'graby',
|
||||||
|
'opengraph' => 'opengraph',
|
||||||
|
];
|
||||||
|
|
||||||
|
if (!isset($parsers[$parser])) {
|
||||||
|
return response()
|
||||||
|
->httpCode(400)
|
||||||
|
->json(['error' => 'Invalid parser']);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$result = call_user_func_array($parsers[$parser], [$url]);
|
||||||
|
|
||||||
|
return response()
|
||||||
|
->httpCode(200)
|
||||||
|
->json($result);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
return response()
|
||||||
|
->httpCode(500)
|
||||||
|
->json(['error' => $e->getMessage()]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
37
src/parsers.php
Normal file
37
src/parsers.php
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use Graby\Graby;
|
||||||
|
use Fusonic\OpenGraph\Consumer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Graby.
|
||||||
|
*
|
||||||
|
* @param string $url
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
function graby(string $url) : array
|
||||||
|
{
|
||||||
|
$graby = new Graby();
|
||||||
|
$result = $graby->fetchContent($url);
|
||||||
|
|
||||||
|
if (isset($result['headers'])) {
|
||||||
|
unset($result['headers']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Opengraph
|
||||||
|
*
|
||||||
|
* @param string $url
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
function opengraph(string $url) : array
|
||||||
|
{
|
||||||
|
$consumer = new Consumer();
|
||||||
|
$object = $consumer->loadUrl($url);
|
||||||
|
|
||||||
|
return json_decode(json_encode($object), true);
|
||||||
|
}
|
26
src/validators.php
Normal file
26
src/validators.php
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validates an URL.
|
||||||
|
*
|
||||||
|
* @param string|null $url
|
||||||
|
* @param string|null $hostname
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
function isValidUrl( ? string $url, ? string $hostname) : bool
|
||||||
|
{
|
||||||
|
if ($url === null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (trim($url) === '') {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (filter_var($url, FILTER_VALIDATE_URL) === false) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return parse_url($url)['host'] !== $hostname;
|
||||||
|
}
|
|
@ -1,7 +1,5 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Graby\Graby;
|
|
||||||
use Fusonic\OpenGraph\Consumer;
|
|
||||||
use Pecee\SimpleRouter\SimpleRouter;
|
use Pecee\SimpleRouter\SimpleRouter;
|
||||||
|
|
||||||
require __DIR__.'/../vendor/autoload.php';
|
require __DIR__.'/../vendor/autoload.php';
|
||||||
|
@ -9,80 +7,6 @@ require __DIR__.'/../vendor/autoload.php';
|
||||||
$dotenv = Dotenv\Dotenv::create(__DIR__.'/../');
|
$dotenv = Dotenv\Dotenv::create(__DIR__.'/../');
|
||||||
$dotenv->load();
|
$dotenv->load();
|
||||||
|
|
||||||
function isValidUrl( ? string $url, ? string $hostname) : bool
|
SimpleRouter::get('/', 'Action@home');
|
||||||
{
|
SimpleRouter::get('/do/{parser}', 'Action@parse');
|
||||||
if ($url === null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (trim($url) === '') {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (filter_var($url, FILTER_VALIDATE_URL) === false) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return parse_url($url)['host'] !== $hostname;
|
|
||||||
}
|
|
||||||
|
|
||||||
function graby(string $url) : array
|
|
||||||
{
|
|
||||||
$graby = new Graby();
|
|
||||||
$result = $graby->fetchContent($url);
|
|
||||||
|
|
||||||
if (isset($result['headers'])) {
|
|
||||||
unset($result['headers']);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $result;
|
|
||||||
}
|
|
||||||
|
|
||||||
function opengraph(string $url) : array
|
|
||||||
{
|
|
||||||
$consumer = new Consumer();
|
|
||||||
$object = $consumer->loadUrl($url);
|
|
||||||
|
|
||||||
return json_decode(json_encode($object), true);
|
|
||||||
}
|
|
||||||
|
|
||||||
SimpleRouter::get('/', function() {
|
|
||||||
return response()
|
|
||||||
->httpCode(200)
|
|
||||||
->json([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
SimpleRouter::get('/do/{parser}', function ($parser) {
|
|
||||||
$url = input()->get('url')->value;
|
|
||||||
|
|
||||||
if (!isValidUrl($url, getenv('API_HOSTNAME'))) {
|
|
||||||
return response()
|
|
||||||
->httpCode(400)
|
|
||||||
->json(['error' => 'Invalid URL']);
|
|
||||||
}
|
|
||||||
|
|
||||||
$parsers = [
|
|
||||||
'graby' => 'graby',
|
|
||||||
'opengraph' => 'opengraph',
|
|
||||||
];
|
|
||||||
|
|
||||||
if (!isset($parsers[$parser])) {
|
|
||||||
return response()
|
|
||||||
->httpCode(400)
|
|
||||||
->json(['error' => 'Invalid parser']);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
$result = call_user_func_array($parsers[$parser], [$url]);
|
|
||||||
|
|
||||||
return response()
|
|
||||||
->httpCode(200)
|
|
||||||
->json($result);
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
return response()
|
|
||||||
->httpCode(500)
|
|
||||||
->json(['error' => $e->getMessage()]);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
SimpleRouter::start();
|
SimpleRouter::start();
|
||||||
|
|
Loading…
Reference in a new issue