Skip to content

Commit fcacaa2

Browse files
author
roadiz-ci
committed
Merge branch hotfix/v2.7.33
1 parent 8b510a1 commit fcacaa2

108 files changed

Lines changed: 3130 additions & 3155 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/run-test.yml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
runs-on: ubuntu-latest
2020
strategy:
2121
matrix:
22-
php-version: ['8.1', '8.2', '8.3']
22+
php-version: ['8.3', '8.4', '8.5']
2323
steps:
2424
- uses: shivammathur/setup-php@v2
2525
with:
@@ -35,9 +35,7 @@ jobs:
3535
${{ runner.os }}-php-${{ matrix.php-version }}-
3636
- name: Install Dependencies
3737
run: composer install --no-scripts --no-ansi --no-interaction --no-progress
38-
- name: Run Unit tests
39-
run: vendor/bin/phpunit -v --whitelist ./src tests
40-
- name: Run PHP Code Sniffer
41-
run: vendor/bin/phpcs -p ./src
4238
- name: Run PHPStan
4339
run: vendor/bin/phpstan analyse --no-progress -c phpstan.neon
40+
- name: Run Unit tests
41+
run: vendor/bin/phpunit -v --whitelist ./src tests

LICENSE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright © 2024 Ambroise Maupate
3+
Copyright © 2025 Ambroise Maupate
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
66

composer.json

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,39 @@
1818
}
1919
],
2020
"require": {
21-
"php": ">=8.1",
21+
"php": ">=8.3",
2222
"ext-json": "*",
2323
"ext-gd": "*",
2424
"ext-dom": "*",
2525
"ext-zip": "*",
2626
"ext-simplexml": "*",
2727
"ext-fileinfo": "*",
28-
"doctrine/orm": "~2.19.0",
28+
"doctrine/orm": "~2.20.0",
2929
"enshrined/svg-sanitize": "^0.22",
30-
"guzzlehttp/guzzle": "^7.2.0",
31-
"guzzlehttp/psr7": "^2.0",
32-
"intervention/image": "^2.5",
30+
"intervention/image": "^3.11",
3331
"league/flysystem": "^3.0",
34-
"monolog/monolog": "^1.24.0 || ^2.1.1",
35-
"symfony/asset": "6.4.*",
36-
"symfony/console": "6.4.*",
37-
"symfony/event-dispatcher": "6.4.*",
38-
"symfony/filesystem": "6.4.*",
39-
"symfony/finder": "6.4.*",
40-
"symfony/http-foundation": "6.4.*",
41-
"symfony/options-resolver": "6.4.*",
42-
"symfony/serializer": "6.4.*",
43-
"twig/twig": "^3.1"
32+
"monolog/monolog": "^3.9",
33+
"symfony/asset": "7.4.*",
34+
"symfony/console": "7.4.*",
35+
"symfony/event-dispatcher": "7.4.*",
36+
"symfony/filesystem": ">=7.1",
37+
"symfony/finder": "7.4.*",
38+
"symfony/http-foundation": "7.4.*",
39+
"symfony/http-client-contracts": "^3.5",
40+
"symfony/options-resolver": "7.4.*",
41+
"symfony/serializer": "7.4.*",
42+
"twig/twig": "^3.21"
4443
},
4544
"require-dev": {
45+
"api-platform/metadata": "^4.1.18",
46+
"doctrine/doctrine-bundle": "^2.8.1",
4647
"php-coveralls/php-coveralls": "^2.4",
47-
"phpunit/phpunit": "^9.5",
48-
"api-platform/metadata": "^3.2.12",
49-
"squizlabs/php_codesniffer": "^3.5",
50-
"phpstan/phpstan": "^1.5.3",
51-
"phpstan/phpstan-doctrine": "^1.3"
48+
"phpstan/phpstan": "^2.1.36",
49+
"phpstan/phpdoc-parser": "<2",
50+
"phpstan/phpstan-doctrine": "^2.0.13",
51+
"phpunit/phpunit": "^9.6",
52+
"symfony/http-client": "7.4.*",
53+
"symfony/process": "7.4.*"
5254
},
5355
"autoload": {
5456
"psr-4": {
@@ -68,8 +70,8 @@
6870
},
6971
"extra": {
7072
"branch-alias": {
71-
"dev-master": "2.3.x-dev",
72-
"dev-develop": "2.4.x-dev"
73+
"dev-master": "2.7.x-dev",
74+
"dev-develop": "2.8.x-dev"
7375
}
7476
}
7577
}

phpcs.xml.dist

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/AbstractDocumentFactory.php

Lines changed: 30 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use League\Flysystem\FilesystemOperator;
99
use League\Flysystem\MountManager;
1010
use Psr\Log\LoggerInterface;
11-
use Psr\Log\NullLogger;
1211
use RZ\Roadiz\Documents\Models\DocumentInterface;
1312
use RZ\Roadiz\Documents\Models\FileHashInterface;
1413
use RZ\Roadiz\Documents\Models\FolderInterface;
@@ -23,88 +22,69 @@
2322
*/
2423
abstract class AbstractDocumentFactory
2524
{
26-
private LoggerInterface $logger;
2725
private ?File $file = null;
2826
private ?FolderInterface $folder = null;
29-
private FilesystemOperator $documentsStorage;
30-
private DocumentFinderInterface $documentFinder;
3127

3228
public function __construct(
33-
FilesystemOperator $documentsStorage,
34-
DocumentFinderInterface $documentFinder,
35-
?LoggerInterface $logger = null
29+
protected readonly FilesystemOperator $documentsStorage,
30+
protected readonly DocumentFinderInterface $documentFinder,
31+
protected readonly LoggerInterface $logger,
3632
) {
3733
if (!$documentsStorage instanceof MountManager) {
3834
trigger_error('Document Storage must be a MountManager to address public and private files.', E_USER_WARNING);
3935
}
40-
$this->documentsStorage = $documentsStorage;
41-
$this->documentFinder = $documentFinder;
42-
$this->logger = $logger ?? new NullLogger();
4336
}
4437

45-
/**
46-
* @return File
47-
*/
4838
public function getFile(): File
4939
{
5040
if (null === $this->file) {
5141
throw new \BadMethodCallException('File should be defined before using it.');
5242
}
43+
5344
return $this->file;
5445
}
5546

5647
/**
57-
* @param File $file
5848
* @return $this
5949
*/
6050
public function setFile(File $file): static
6151
{
6252
$this->file = $file;
53+
6354
return $this;
6455
}
6556

66-
/**
67-
* @return FolderInterface|null
68-
*/
6957
public function getFolder(): ?FolderInterface
7058
{
7159
return $this->folder;
7260
}
7361

7462
/**
75-
* @param FolderInterface|null $folder
7663
* @return $this
7764
*/
7865
public function setFolder(?FolderInterface $folder = null): static
7966
{
8067
$this->folder = $folder;
68+
8169
return $this;
8270
}
8371

8472
/**
8573
* Special case for SVG without XML statement.
86-
*
87-
* @param DocumentInterface $document
8874
*/
8975
protected function parseSvgMimeType(DocumentInterface $document): void
9076
{
9177
if (
92-
($document->getMimeType() === 'text/plain' || $document->getMimeType() === 'text/html') &&
93-
preg_match('#\.svg$#', $document->getFilename())
78+
('text/plain' === $document->getMimeType() || 'text/html' === $document->getMimeType())
79+
&& preg_match('#\.svg$#', $document->getFilename())
9480
) {
9581
$this->logger->debug('Uploaded a SVG without xml declaration. Presuming it’s a valid SVG file.');
9682
$document->setMimeType('image/svg+xml');
9783
}
9884
}
9985

100-
/**
101-
* @return DocumentInterface
102-
*/
10386
abstract protected function createDocument(): DocumentInterface;
10487

105-
/**
106-
* @param DocumentInterface $document
107-
*/
10888
abstract protected function persistDocument(DocumentInterface $document): void;
10989

11090
protected function getHashAlgorithm(): string
@@ -116,22 +96,25 @@ protected function getHashAlgorithm(): string
11696
* Create a document from UploadedFile, Be careful, this method does not flush, only
11797
* persists current Document.
11898
*
119-
* @param bool $allowEmpty Default false, requires a local file to create new document entity
99+
* @param bool $allowEmpty Default false, requires a local file to create new document entity
120100
* @param bool $allowDuplicates Default false, always import new document even if file already exists
121-
* @return null|DocumentInterface
101+
*
122102
* @throws FilesystemException
123103
*/
124104
public function getDocument(bool $allowEmpty = false, bool $allowDuplicates = false): ?DocumentInterface
125105
{
126-
if ($allowEmpty === false) {
106+
if (false === $allowEmpty) {
127107
// Getter throw exception on null file
128108
$file = $this->getFile();
129109
} else {
130110
$file = $this->file;
131111
}
132112

133113
if (null === $file) {
134-
return null;
114+
$document = $this->createDocument();
115+
$this->persistDocument($document);
116+
117+
return $document;
135118
}
136119

137120
if ($file instanceof UploadedFile && !$file->isValid()) {
@@ -146,10 +129,10 @@ public function getDocument(bool $allowEmpty = false, bool $allowDuplicates = fa
146129
if (false !== $fileHash && !$allowDuplicates) {
147130
$existingDocument = $this->documentFinder->findOneByHashAndAlgorithm($fileHash, $this->getHashAlgorithm());
148131
if (null !== $existingDocument) {
149-
if (
150-
$existingDocument->isRaw() &&
151-
null !== $existingDownscaledDocument = $existingDocument->getDownscaledDocument()
152-
) {
132+
/*
133+
* If existing document is a RAW, serve its downscaled version
134+
*/
135+
if (null !== $existingDownscaledDocument = $existingDocument->getDownscaledDocument()) {
153136
$existingDocument = $existingDownscaledDocument;
154137
}
155138
if (null !== $this->folder) {
@@ -159,7 +142,11 @@ public function getDocument(bool $allowEmpty = false, bool $allowDuplicates = fa
159142
$this->logger->info(sprintf(
160143
'File %s already exists with same checksum, do not upload it twice.',
161144
$existingDocument->getFilename()
162-
));
145+
), [
146+
'path' => $existingDocument->getMountPath(),
147+
]);
148+
(new Filesystem())->remove($file->getPathname());
149+
163150
return $existingDocument;
164151
}
165152
}
@@ -175,8 +162,8 @@ public function getDocument(bool $allowEmpty = false, bool $allowDuplicates = fa
175162
$this->parseSvgMimeType($document);
176163

177164
if (
178-
$document instanceof FileHashInterface &&
179-
false !== $fileHash
165+
$document instanceof FileHashInterface
166+
&& false !== $fileHash
180167
) {
181168
$document->setFileHash($fileHash);
182169
$document->setFileHashAlgorithm($this->getHashAlgorithm());
@@ -196,8 +183,6 @@ public function getDocument(bool $allowEmpty = false, bool $allowDuplicates = fa
196183
/**
197184
* Updates a document from UploadedFile, Be careful, this method does not flush.
198185
*
199-
* @param DocumentInterface $document
200-
* @return DocumentInterface
201186
* @throws FilesystemException
202187
*/
203188
public function updateDocument(DocumentInterface $document): DocumentInterface
@@ -231,7 +216,7 @@ public function updateDocument(DocumentInterface $document): DocumentInterface
231216
}
232217
}
233218

234-
$document->setFolder(\mb_substr(hash("crc32b", date('YmdHi')), 0, 12));
219+
$document->setFolder(DocumentFolderGenerator::generateFolderName());
235220
}
236221

237222
$document->setFilename($this->getFileName());
@@ -247,9 +232,6 @@ public function updateDocument(DocumentInterface $document): DocumentInterface
247232
}
248233

249234
/**
250-
* @param File $localFile
251-
* @param DocumentInterface $document
252-
* @return void
253235
* @throws FilesystemException
254236
*/
255237
public function moveFile(File $localFile, DocumentInterface $document): void
@@ -267,9 +249,6 @@ public function moveFile(File $localFile, DocumentInterface $document): void
267249
}
268250
}
269251

270-
/**
271-
* @return string
272-
*/
273252
protected function getFileName(): string
274253
{
275254
$file = $this->getFile();
@@ -278,8 +257,8 @@ protected function getFileName(): string
278257
$fileName = $file->getClientOriginalName();
279258
} elseif (
280259
$file instanceof DownloadedFile
281-
&& $file->getOriginalFilename() !== null
282-
&& $file->getOriginalFilename() !== ''
260+
&& null !== $file->getOriginalFilename()
261+
&& '' !== $file->getOriginalFilename()
283262
) {
284263
$fileName = $file->getOriginalFilename();
285264
} else {
@@ -292,9 +271,6 @@ protected function getFileName(): string
292271
/**
293272
* Create a Document from an external URL.
294273
*
295-
* @param string $downloadUrl
296-
*
297-
* @return DocumentInterface|null
298274
* @throws FilesystemException
299275
*/
300276
public function getDocumentFromUrl(string $downloadUrl): ?DocumentInterface
@@ -303,6 +279,7 @@ public function getDocumentFromUrl(string $downloadUrl): ?DocumentInterface
303279
if (null !== $downloadedFile) {
304280
return $this->setFile($downloadedFile)->getDocument();
305281
}
282+
306283
return null;
307284
}
308285
}

0 commit comments

Comments
 (0)