Skip to content

Commit

Permalink
Added readFile function to FileUtils.
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentmuller committed Jun 3, 2024
1 parent 3b89eba commit c920522
Show file tree
Hide file tree
Showing 21 changed files with 515 additions and 176 deletions.
105 changes: 0 additions & 105 deletions public/css/table_log.css
Original file line number Diff line number Diff line change
Expand Up @@ -42,111 +42,6 @@ td.user {
white-space: nowrap;
}

/* level icons */
.icon-level {
width: 1.25em;
text-align: center;
}

.icon-level:before {
font-family: "Font Awesome 6 Free", sans-serif;
font-weight: 900;
font-style: normal;
padding-right: 4px;
content: '\f05a';
}

.icon-level.info {
color: var(--bs-info);
}

.icon-level.debug {
color: var(--bs-secondary);
}

.icon-level.warning {
color: var(--bs-warning);
}

.icon-level.alert,
.icon-level.critical,
.icon-level.emergency,
.icon-level.error {
color: var(--bs-danger);
}

.icon-level.all:before {
content: "\f5fd";
}

.icon-level.info:before,
.icon-level.debug:before,
.icon-level.error:before,
.icon-level.critical:before,
.icon-level.alert:before,
.icon-level.emergency:before {
content: "\f06a";
}

.icon-level.warning:before {
content: "\f071";
}

/* channel icons */
.icon-channel {
width: 1.25em;
text-align: center;
}

.icon-channel:before {
font-family: "Font Awesome 6 Free", sans-serif;
font-weight: 900;
font-style: normal;
padding-right: 6px;
content: '\f15b';
}

.icon-channel.all:before {
content: '\f7d9';
}

.icon-channel.application:before {
content: '\f5fc';
}

.icon-channel.request:before {
content: '\e13c';
}

.icon-channel.doctrine:before {
content: '\f1c0';
}

.icon-channel.cache:before {
content: '\f0a0';
}

.icon-channel.security:before {
content: '\f084';
}

.icon-channel.mailer:before {
font-weight: 400;
content: '\f0e0';
}

.icon-channel.php:before {
content: '\f121';
}

.icon-channel.console:before {
content: '\f11c';
}

.icon-channel.deprecation:before {
content: '\f188';
}

/* PHP array */
pre.highlight-php {
background-color: initial !important;
Expand Down
2 changes: 1 addition & 1 deletion public/vendor.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
},
{
"name": "html5sortable",
"version": "0.13.3",
"version": "0.14.0",
"source": "cdnjs",
"files": [
"html5sortable.js"
Expand Down
9 changes: 2 additions & 7 deletions src/Command/UpdateAssetsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -475,14 +475,9 @@ private function propertyExists(array $var, array|string $properties, bool $log
private function readFile(string $filename): string|false
{
$this->writeVeryVerbose(\sprintf('Load "%s".', $filename));
$content = \file_get_contents($filename);
if (!\is_string($content)) {
$this->writeError(\sprintf('Unable to get content of "%s".', $filename));

return false;
}
$content = FileUtils::readFile($filename);
if ('' === $content) {
$this->writeError(\sprintf('The content of "%s" is empty.', $filename));
$this->writeError(\sprintf('Unable to get content of "%s".', $filename));

return false;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Controller/AjaxController.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ public function license(
if (!FileUtils::exists($file)) {
return $this->jsonFalse(['message' => $this->trans('about.dialog.not_found')]);
}
$content = \file_get_contents($file);
if (!\is_string($content)) {
$content = FileUtils::readFile($file);
if ('' === $content) {
return $this->jsonFalse(['message' => $this->trans('about.dialog.not_loaded')]);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Entity/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public function getTitle(): ?string
/**
* Gets the title, the last name and the first name separate by a space character.
*/
public function getTitleAndFullName(): ?string
public function getTitleAndFullName(): string
{
return $this->concat($this->title, $this->getFullName());
}
Expand Down
4 changes: 4 additions & 0 deletions src/Form/Extension/FileTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public static function getExtendedTypes(): iterable
}

/**
* @psalm-param array<array-key, mixed> $options
*
* @psalm-return array<array-key, mixed>
*
* @psalm-suppress MixedAssignment
*/
protected function updateOptions(FormInterface $form, array $options): array
Expand Down
28 changes: 7 additions & 21 deletions src/Listener/ResponseListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,15 @@ private function buildCSP(): string
return '';
}

$this->replaceNonce($csp);
$values = ['nonce' => $this->service->getCspNonce()];
$result = \array_map(
static fn (array $subject): array => StringUtils::replace($values, $subject),
$csp
);

return \array_reduce(
\array_keys($csp),
fn (string $carry, string $key): string => \sprintf('%s%s %s;', $carry, $key, \implode(' ', $csp[$key])),
\array_keys($result),
fn (string $carry, string $key): string => \sprintf('%s%s %s;', $carry, $key, \implode(' ', $result[$key])),
''
);
}
Expand Down Expand Up @@ -170,22 +174,4 @@ private function loadFile(): array
$content
);
}

/**
* @param-out array<string, string[]> $csp
*
* @psalm-suppress ReferenceConstraintViolation
*/
private function replaceNonce(array &$csp): void
{
$nonce = $this->service->getCspNonce();
\array_walk_recursive(
$csp,
function (string &$value) use ($nonce): void {
if ('nonce' === $value) {
$value = $nonce;
}
}
);
}
}
8 changes: 4 additions & 4 deletions src/Pdf/PdfTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,8 @@ public function endRow(): static
throw new \OutOfRangeException(\sprintf('Invalid spanned cells: expected %d, %d given.', $count, $span));
}

$cells = $this->cells;
$parent = $this->parent;
$cells = $this->cells;
$columns = $this->columns;

// compute
Expand Down Expand Up @@ -848,10 +848,10 @@ private function checkRowStarted(): void
}

/**
* @param PdfCell[] $cells
* @param PdfColumn[] $columns
* @psalm-param PdfCell[] $cells
* @psalm-param PdfColumn[] $columns
*
* @return array{
* @psalm-return array{
* 0: string[],
* 1: PdfStyle[],
* 2: PdfTextAlignment[],
Expand Down
9 changes: 3 additions & 6 deletions src/Report/MemoryImageReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use App\Controller\AbstractController;
use App\Pdf\Traits\PdfMemoryImageTrait;
use App\Service\ImageService;
use App\Utils\FileUtils;
use fpdf\PdfException;

/**
Expand Down Expand Up @@ -58,12 +59,8 @@ private function addImageGD(): void

private function addImageMemory(): void
{
if (!\file_exists($this->image)) {
throw PdfException::instance('Unable to get image.');
}

$data = \file_get_contents($this->image);
if (!\is_string($data)) {
$data = FileUtils::readFile($this->image);
if ('' === $data) {
throw PdfException::instance('Unable to get image content.');
}

Expand Down
3 changes: 1 addition & 2 deletions src/Service/LogService.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,11 @@ private function parseFile(): ?LogFile
* Decode the given JSON string.
*
* @psalm-return array<string, string>|null
*
* @psalm-suppress MixedReturnTypeCoercion
*/
private function parseJson(string $value): ?array
{
try {
/** @psalm-var array<string, string> */
return StringUtils::decodeJson($value);
} catch (\InvalidArgumentException) {
return null;
Expand Down
11 changes: 3 additions & 8 deletions src/Service/OpenWeatherCityUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,20 +127,14 @@ private function falseResult(string $message, array $parameters = []): array

/**
* @psalm-return OpenWeatherCityType[]|false
*
* @psalm-suppress MixedReturnTypeCoercion
*/
private function getFileContent(UploadedFile $file): array|false
{
if (!$file->isValid()) {
return false;
}
$filename = $file->getRealPath();
if (false === $filename) {
return false;
}
$content = \file_get_contents($filename);
if (false === $content) {
$content = FileUtils::readFile($file);
if ('' === $content) {
return false;
}
$content = \gzdecode($content);
Expand All @@ -149,6 +143,7 @@ private function getFileContent(UploadedFile $file): array|false
}

try {
/** @psalm-var OpenWeatherCityType[] */
return StringUtils::decodeJson($content);
} catch (\InvalidArgumentException) {
return false;
Expand Down
5 changes: 3 additions & 2 deletions src/Service/PdfLabelService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace App\Service;

use App\Pdf\PdfLabel;
use App\Utils\FileUtils;
use fpdf\PdfException;
use Psr\Cache\InvalidArgumentException;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
Expand Down Expand Up @@ -94,8 +95,8 @@ private function loadFile(?string $file = null): array
throw PdfException::format('Unable to find the file "%s".', $file);
}

$content = \file_get_contents($file);
if (!\is_string($content)) {
$content = FileUtils::readFile($file);
if ('' === $content) {
throw PdfException::format('Unable to get content of the file "%s".', $file);
}

Expand Down
5 changes: 0 additions & 5 deletions src/Table/Definition/log.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,5 @@
"field": "user",
"title": "log.fields.user",
"class": "user"
},
{
"field": "levelColor",
"alias": "color",
"visible": false
}
]
30 changes: 27 additions & 3 deletions src/Utils/FileUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ public static function decodeJson(string|\SplFileInfo $file, bool $assoc = true)
$file = self::realPath($file);

// file or url?
if (!self::isFile($file) && false === \filter_var($file, \FILTER_VALIDATE_URL)) {
if (!self::isFile($file) && !self::validateURL($file)) {
throw new \InvalidArgumentException(\sprintf("The file '%s' cannot be found.", $file));
}
$content = \file_get_contents($file);
if (false === $content) {
$content = self::readFile($file);
if ('' === $content) {
throw new \InvalidArgumentException(\sprintf("Unable to get content of the file '%s'.", $file));
}

Expand Down Expand Up @@ -273,6 +273,25 @@ public static function normalizeDirectory(string $path): string
return \str_replace(['\\', '/'], \DIRECTORY_SEPARATOR, $path);
}

/**
* Returns the content of a file as a string.
*
* @return string the content of the file; an empty string ("") on error
*/
public static function readFile(string|\SplFileInfo $file): string
{
$file = self::realPath($file);
if (!self::isFile($file) && !self::validateURL($file)) {
return '';
}

try {
return self::getFilesystem()->readFile($file);
} catch (IOException) {
return '';
}
}

/**
* Gets the real path of the given file.
*/
Expand Down Expand Up @@ -405,4 +424,9 @@ public static function tempFile(string $prefix = 'tmp', bool $deleteOnExit = tru
return null;
}
}

private static function validateURL(string $file): bool
{
return false !== \filter_var($file, \FILTER_VALIDATE_URL);
}
}
Loading

0 comments on commit c920522

Please sign in to comment.