refactoring Service Layer

This commit is contained in:
aslan 2024-02-12 14:56:24 +03:00
parent 1f67f437f3
commit 94300bee31
2 changed files with 4 additions and 111 deletions

View File

@ -4,71 +4,11 @@ namespace App\Services;
use DiDom\Document; use DiDom\Document;
/*
class PageScrapper
{
private string $url;
private string $contentMarker;
public function __construct($url, $contentMarker = '<div class=["\']content_info["\']>')
{
$this->url = $url;
$this->contentMarker = $contentMarker;
}
public function getHTML()
{
$page = file_get_contents($this->url);
$strForPregMatch = "/" . "{$this->contentMarker}" . "(.*)<\/div>/is";
$arr = [];
$rez = preg_match_all($strForPregMatch, $page, $arr);
return $content = $arr[0][0];
}
public function normalizeURLFile($content)
{
$rez = preg_match_all('/<a href="(.*)">/isU', $content, $arr);
$arr[1] = array_unique($arr[1]);
foreach ($arr[1] as $el) {
if (!str_starts_with($el, 'https')) {
$content = str_replace($el, 'https://mkgtu.ru' . $el, $content);
}
}
$rez = preg_match_all('/src="(.*)">/isU', $content, $arr);
$arr[1] = array_unique($arr[1]);
foreach ($arr[1] as $el) {
if (!str_starts_with($el, 'https') && str_contains($el, 'upload')) {
$content = str_replace($el, 'https://mkgtu.ru' . $el, $content);
}
}
return $content;
}
public function cutHTML($content, $strForScissors)
{
$arr = [];
//<footer(.*)<\/footer>
//safdsaf sdfdasf<footer>--------------------------------fsdfdasf <\/footer> asdfdasf asdf
$rez = preg_match_all($strForScissors, $content, $arr);
//$arr[1][0] = '>--------------------------------fsdfdasf ';
$content = str_replace($arr[0], '', $content);
//safdsaf sdfdasf<footer<\/footer> asdfdasf asdf
return $content;
}
}
*/
class PageScrapper class PageScrapper
{ {
private string $url; private string $url;
private string $contentMarker; private string $contentMarker;
private Document $document;
public function __construct($url, $contentMarker) public function __construct($url, $contentMarker)
{ {
@ -76,16 +16,19 @@ class PageScrapper
$this->contentMarker = $contentMarker; $this->contentMarker = $contentMarker;
$this->document = new Document($this->url, true); $this->document = new Document($this->url, true);
} }
public function getFullHTML() public function getFullHTML()
{ {
return $this->document; return $this->document;
} }
public function printHTML() public function printHTML()
{ {
$rez = $this->document; $rez = $this->document;
$content = $rez->first($this->contentMarker)->html(); $content = $rez->first($this->contentMarker)->html();
return $content; return $content;
} }
public function normalizePath() public function normalizePath()
{ {
$rez = $this->document; $rez = $this->document;
@ -114,10 +57,6 @@ class PageScrapper
} }
foreach ($srclinks as $k => $srclink) { foreach ($srclinks as $k => $srclink) {
$src = $srclink->attr('src'); $src = $srclink->attr('src');
if (!str_contains($srclink->attr('src'), "https://")) { if (!str_contains($srclink->attr('src'), "https://")) {
@ -126,23 +65,6 @@ class PageScrapper
$html0 = str_replace(urldecode($unchanged), $changed, $html0); $html0 = str_replace(urldecode($unchanged), $changed, $html0);
} }
} }
// foreach ($srclinks as $k => $srclink) {
// $src = $srclink->attr('src');
//
// if (!str_contains($srclink->attr('src'), "https://")) {
//
//
// $tmp = explode('/', rawurldecode($src));
// foreach ( $tmp as $k => $v) {
// $tmp[$k] = rawurlencode($v);
// }
// $src = implode('/', $tmp);
//
//
// $html0 = str_replace($src, 'https://mkgtu.ru' . $src, $html0);
// }
// }
// str_replace('st yle', 'style', $html0);
return $html0; return $html0;
} }

View File

@ -1,29 +0,0 @@
<?php
namespace App\Services;
use Carbon\Carbon;
class WorkWithFiles
{
public static function saveFileToUploads($content)
{
$destinationPath = 'uploads';
$content->move($destinationPath, $content->getClientOriginalName());
}
public static function renameFile($content)
{
$nameChunks = explode('.', $content->getClientOriginalName());
array_splice($nameChunks, 1);
$timestamp = Carbon::now();
$name = implode('.', $nameChunks);
$newName = "{$name}{$timestamp}.{$content->getClientOriginalExtension()}";
$dir = __DIR__;
$path = "{$dir}/../../public/uploads/";
$oldName = $content->getClientOriginalName();
rename("{$path}{$oldName}", "{$path}{$newName}");
return "{$path}{$newName}";
}
}