Skip to content

Commit

Permalink
Fixes execution on PHP8 - undefined state on clean
Browse files Browse the repository at this point in the history
  • Loading branch information
jaymoulin committed Apr 5, 2021
1 parent 8f36bba commit f4acc6a
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 71 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION ?= 1.21.0
VERSION ?= 1.21.1
CACHE ?= --no-cache=1
FULLVERSION ?= ${VERSION}
archs ?= amd64 arm32v6 arm64v8 i386
Expand Down
2 changes: 1 addition & 1 deletion converter
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env php
<?php
require_once 'vendor/autoload.php';
$sVersion = getenv('VERSION') ?: '1.21.0';
$sVersion = getenv('VERSION') ?: '1.21.1';
define('VERSION', <<<HEREDOC
Yamete $sVersion - Jay MOULIN <https://twitter.com/MoulinJay>
Expand Down
2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM php:8.0-alpine
COPY qemu-*-static /usr/bin/
ARG VERSION=1.21.0
ARG VERSION=1.21.1
LABEL maintainer="Jay MOULIN <https://jaymoulin.me/me/yamete> <https://twitter.com/MoulinJay>"
LABEL version=${VERSION}

Expand Down
2 changes: 1 addition & 1 deletion download
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env php
<?php
require_once 'vendor/autoload.php';
$sVersion = getenv('VERSION') ?: '1.21.0';
$sVersion = getenv('VERSION') ?: '1.21.1';
define('VERSION', <<<HEREDOC
Yamete $sVersion - Jay MOULIN <https://twitter.com/MoulinJay>
Expand Down
112 changes: 57 additions & 55 deletions src/Driver/CartoonSexComixCom.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,66 +11,68 @@
use PHPHtmlParser\Exceptions\StrictException;
use Yamete\DriverAbstract;

class CartoonSexComixCom extends DriverAbstract
{
private const DOMAIN = 'cartoonsexcomix.com';
private array $aMatches = [];

public function canHandle(): bool
if (!class_exists(CartoonSexComixCom::class)) {
class CartoonSexComixCom extends DriverAbstract
{
return (bool)preg_match(
'~^(?<scheme>https?)://(www\.)?(' . strtr($this->getDomain(), ['.' => '\.', '-' => '\-',]) .
')/(pictures|gallery|galleries|videos)/(?<album>[^/?]+)[/?]?~',
$this->sUrl,
$this->aMatches
);
}
private const DOMAIN = 'cartoonsexcomix.com';
private array $aMatches = [];

protected function getDomain(): string
{
return self::DOMAIN;
}
public function canHandle(): bool
{
return (bool)preg_match(
'~^(?<scheme>https?)://(www\.)?(' . strtr($this->getDomain(), ['.' => '\.', '-' => '\-',]) .
')/(pictures|gallery|galleries|videos)/(?<album>[^/?]+)[/?]?~',
$this->sUrl,
$this->aMatches
);
}

/**
* @return array
* @throws GuzzleException
* @throws ChildNotFoundException
* @throws CircularException
* @throws ContentLengthException
* @throws LogicalException
* @throws NotLoadedException
* @throws StrictException
*/
public function getDownloadables(): array
{
$iParamsPos = strpos($this->sUrl, '?');
$this->sUrl = $iParamsPos ? substr($this->sUrl, 0, $iParamsPos) : $this->sUrl;
$oRes = $this->getClient()->request('GET', $this->sUrl, ['http_errors' => false]);
$this->sUrl .= ($this->sUrl[strlen($this->sUrl) - 1] != '/') ? '/' : '';
$aReturn = [];
$index = 0;
foreach ($this->getDomParser()->loadStr((string)$oRes->getBody())->find($this->getSelector()) as $oLink) {
$sFilename = $oLink->getAttribute('href');
$sFilename = str_contains($sFilename, 'http')
? $sFilename
: (
str_contains($sFilename, '//')
? $this->aMatches['scheme'] . ':' . $sFilename
: $this->aMatches['scheme'] . '://www.' . $this->getDomain() . $sFilename);
$sBasename = $this->getFolder() . DIRECTORY_SEPARATOR . str_pad($index++, 5, '0', STR_PAD_LEFT)
. '-' . basename($sFilename);
$aReturn[$sBasename] = $sFilename;
protected function getDomain(): string
{
return self::DOMAIN;
}
return $aReturn;
}

protected function getSelector(): string
{
return '.my-gallery figure a';
}
/**
* @return array
* @throws GuzzleException
* @throws ChildNotFoundException
* @throws CircularException
* @throws ContentLengthException
* @throws LogicalException
* @throws NotLoadedException
* @throws StrictException
*/
public function getDownloadables(): array
{
$iParamsPos = strpos($this->sUrl, '?');
$this->sUrl = $iParamsPos ? substr($this->sUrl, 0, $iParamsPos) : $this->sUrl;
$oRes = $this->getClient()->request('GET', $this->sUrl, ['http_errors' => false]);
$this->sUrl .= ($this->sUrl[strlen($this->sUrl) - 1] != '/') ? '/' : '';
$aReturn = [];
$index = 0;
foreach ($this->getDomParser()->loadStr((string)$oRes->getBody())->find($this->getSelector()) as $oLink) {
$sFilename = $oLink->getAttribute('href');
$sFilename = str_contains($sFilename, 'http')
? $sFilename
: (
str_contains($sFilename, '//')
? $this->aMatches['scheme'] . ':' . $sFilename
: $this->aMatches['scheme'] . '://www.' . $this->getDomain() . $sFilename);
$sBasename = $this->getFolder() . DIRECTORY_SEPARATOR . str_pad($index++, 5, '0', STR_PAD_LEFT)
. '-' . basename($sFilename);
$aReturn[$sBasename] = $sFilename;
}
return $aReturn;
}

private function getFolder(): string
{
return implode(DIRECTORY_SEPARATOR, [$this->getDomain(), $this->aMatches['album']]);
protected function getSelector(): string
{
return '.my-gallery figure a';
}

private function getFolder(): string
{
return implode(DIRECTORY_SEPARATOR, [$this->getDomain(), $this->aMatches['album']]);
}
}
}
4 changes: 2 additions & 2 deletions src/DriverAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public function setUrl(string $sUrl): DriverInterface
*/
public function clean(): DriverInterface
{
unset($this->oDomParser);
unset($this->oClient);
$this->oDomParser = null;
$this->oClient = null;
return $this;
}

Expand Down
12 changes: 6 additions & 6 deletions src/PDF.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,24 @@ private function fullSizeImage(string $sFileName): void
try {
$this->Image($sFileName, 0, 0, $width, $height);
} catch (Exception $e) {
if (strpos($e->getMessage(), 'Not a PNG file') !== false) {
if (str_contains($e->getMessage(), 'Not a PNG file')) {
$sNewFilename = str_replace('.png', '.jpg', $sFileName);
rename($sFileName, $sNewFilename);
$this->fullSizeImage($sNewFilename);
} elseif (strpos($e->getMessage(), 'Not a JPEG file') !== false) {
} elseif (str_contains($e->getMessage(), 'Not a JPEG file')) {
$sNewFilename = str_replace('.jpg', '.gif', $sFileName);
rename($sFileName, $sNewFilename);
$this->fullSizeImage($sNewFilename);
} elseif (strpos($e->getMessage(), 'Not a GIF file') !== false or
} elseif (str_contains($e->getMessage(), 'Not a GIF file') or
(
strpos($e->getMessage(), 'Missing or incorrect image file') !== false and
strpos($e->getMessage(), '.gif') !== false
str_contains($e->getMessage(), 'Missing or incorrect image file') and
str_contains($e->getMessage(), '.gif')
)
) {
$sNewFilename = str_replace('.gif', '.png', $sFileName);
rename($sFileName, $sNewFilename);
$this->Image($sNewFilename, 0, 0, $width, $height);
} elseif (strpos($e->getMessage(), 'Unsupported image type: webp') !== false) {
} elseif (str_contains($e->getMessage(), 'Unsupported image type: webp')) {
$sNewFilename = str_replace('.webp', '.jpg', $sFileName);
$oImage = imagecreatefromwebp($sFileName);
imagejpeg($oImage, $sNewFilename, 100);
Expand Down
2 changes: 1 addition & 1 deletion src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function addDriverDirectory(string $sDirectory): Parser
public function parse(string $sUrl): bool|ResultIterator
{
foreach ($this->aDrivers as $oDriver) {
$oDriver->setUrl($sUrl);
$oDriver->clean()->setUrl($sUrl);
if ($oDriver->canHandle()) {
return new ResultIterator($oDriver);
}
Expand Down
5 changes: 2 additions & 3 deletions src/ResultIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@
*/
class ResultIterator implements Iterator, Countable
{
private $oDriver;
private $oIterator;
private DriverInterface $oDriver;
private ArrayIterator $oIterator;

public function __construct(DriverInterface $oDriver)
{
$this->oDriver = $oDriver;
$this->oIterator = new ArrayIterator($oDriver->getDownloadables());
$oDriver->clean();
}

public function current(): Downloadable
Expand Down

0 comments on commit f4acc6a

Please sign in to comment.