Skip to content

Commit 06748d7

Browse files
committed
[BETA] All types are accepted in the file class, but not all accept set/get functions.
1 parent 84da94f commit 06748d7

4 files changed

Lines changed: 24 additions & 13 deletions

File tree

src/imperazim/components/filesystem/File.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ public function __construct(
4343
} else {
4444
$directory = str_replace('//', '/', $directoryOrConfig . '/');
4545
}
46-
if (!in_array($fileType, self::getTypes())) {
47-
throw new FileSystemException("Invalid file type: $fileType");
48-
}
4946
$this->directoryOrConfig = $directory;
5047
$this->fileName = $fileName ?? '';
5148
$this->fileType = $fileType ?? self::TYPE_YML;
@@ -136,8 +133,9 @@ public static function serializeContent(string $extension, array $data): string
136133
'txt' => self::writeList(array_keys($data)),
137134
'ini' => self::writeIniFile($data)
138135
};
136+
} else {
137+
return self::writeList(array_keys($data));
139138
}
140-
throw new FileSystemException("Unsupported file type: {$extension}");
141139
}
142140

143141
/**
@@ -156,8 +154,9 @@ public static function deserializeContent(string $extension, string $fileContent
156154
'txt' => array_fill_keys(self::parseList($fileContent), true),
157155
'ini' => parse_ini_string($fileContent, true)
158156
};
157+
} else {
158+
return array_fill_keys(self::parseList($fileContent), true);
159159
}
160-
throw new FileSystemException("Unsupported file type: {$extension}");
161160
}
162161

163162
/**

src/imperazim/components/filesystem/Path.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,11 +270,13 @@ public static function getRecursiveFiles(string $folder): array {
270270

271271
foreach ($files as $file) {
272272
if ($file->isFile()) {
273+
$extension = $file->getExtension();
274+
$fileType = File::getTypeByExtension($extension);
273275
$filesInfo[] = [
274276
'directory' => $file->getPath(),
275-
'fileName' => $file->getBasename(),
276-
'fileType' => File::getTypeByExtension($file->getExtension()),
277-
'content' => File::deserializeContent($file->getExtension(), file_get_contents($file->getRealPath()))
277+
'fileName' => $file->getBasename('.' . $extension),
278+
'fileType' => $fileType,
279+
'content' => File::deserializeContent($extension, file_get_contents($file->getRealPath()))
278280
];
279281
}
280282
}

src/imperazim/components/filesystem/traits/FileExtensionTypes.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ public static function getExtensions(): array {
5858
public static function getTypeByExtension(string $extension): ?string {
5959
$extension = strtolower($extension);
6060
$type = array_search($extension, self::$typeToExtension, true);
61-
return $type !== false ? $type : null;
61+
if ($type !== false) {
62+
return $type;
63+
}
64+
return 'file:' . $extension;
6265
}
6366

6467
/**
@@ -68,8 +71,15 @@ public static function getTypeByExtension(string $extension): ?string {
6871
* @return string|null
6972
*/
7073
public static function getExtensionByType(string $type, bool $withPoint = false): ?string {
71-
$extension = self::$typeToExtension[$type] ?? null;
72-
return $extension !== null ? ($withPoint ? '.' . $extension : $extension) : null;
74+
if (isset(self::$typeToExtension[$type])) {
75+
$extension = self::$typeToExtension[$type];
76+
return $withPoint ? '.' . $extension : $extension;
77+
}
78+
if (str_starts_with($type, 'file:')) {
79+
$extension = substr($type, 5);
80+
return $withPoint ? '.' . $extension : $extension;
81+
}
82+
return null;
7383
}
74-
84+
7585
}

src/imperazim/components/plugin/PluginToolkit.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ public function saveRecursiveResources(?string $loadType = '--merge'): ?array {
351351
private function processFile(array $file, ?string $loadType): ?File {
352352
try {
353353
$fileName = $file['fileName'] ?? null;
354-
$fileType = $file['fileType'] ?? null;
354+
$fileType = $file['fileType'] ?? 'file:unknown';
355355
$fileContent = $file['content'] ?? null;
356356
$fileDirectory = $file['directory'] ?? null;
357357

0 commit comments

Comments
 (0)