Page MenuHomeDevCentral

D4185.diff
No OneTemporary

D4185.diff

diff --git a/workspaces/src/Engines/Collection/FilesCollection.php b/workspaces/src/Engines/Collection/FilesCollection.php
--- a/workspaces/src/Engines/Collection/FilesCollection.php
+++ b/workspaces/src/Engines/Collection/FilesCollection.php
@@ -20,6 +20,8 @@
use Exception;
use Generator;
+use Keruald\OmniTools\IO\Directory;
+
/**
* Files Collection class
*
@@ -206,16 +208,9 @@
* @return int The number of documents
*/
public function count () {
- $dir = $this->getCurrentCollectionPath();
- $count = 0;
- $files = scandir($dir);
- foreach ($files as $file) {
- if (get_extension($file) == 'json') {
- $count++;
- }
- }
+ $dir = new Directory($this->getCurrentCollectionPath());
- return $count;
+ return $dir->countFiles("*.json");
}
/**
@@ -225,13 +220,13 @@
* CollectionDocument
*/
public function getAll () {
- $dir = $this->getCurrentCollectionPath();
- $files = scandir($dir);
+ $dir = new Directory($this->getCurrentCollectionPath());
+
+ $files = $dir->glob("*.json");
foreach ($files as $file) {
- if (get_extension($file) == 'json') {
- $documentId = get_filename($file);
- yield $this->get($documentId);
- }
+ $documentId = $file->getFileNameWithoutExtension();
+
+ yield $this->get($documentId);
}
}
@@ -241,15 +236,9 @@
* @return array The documents list
*/
public function getDocumentsList () {
- $dir = $this->getFilePath('');
- $files = scandir($dir);
- $documents = [];
- foreach ($files as $file) {
- if (get_extension($file) == 'json') {
- $documents[] = get_filename($file);
- }
- }
+ $dir = new Directory($this->getCurrentCollectionPath());
+ $files = $dir->glob("*.json");
- return $documents;
+ return array_map(fn ($file) => $file->getFileNameWithoutExtension(), $files);
}
}
diff --git a/workspaces/src/Engines/I18n/TextFileMessage.php b/workspaces/src/Engines/I18n/TextFileMessage.php
--- a/workspaces/src/Engines/I18n/TextFileMessage.php
+++ b/workspaces/src/Engines/I18n/TextFileMessage.php
@@ -17,6 +17,8 @@
namespace Waystone\Workspaces\Engines\I18n;
+use Keruald\OmniTools\IO\Directory;
+
use Exception;
/**
@@ -45,22 +47,23 @@
$this->filename = $filename;
//Finds relevant files
- $files = scandir($folder);
+ $dir = new Directory($folder);
+
+ $files = $dir->glob($filename . '-*.txt');
foreach ($files as $file) {
- if (str_starts_with($file, $filename . '-') && get_extension($file) == 'txt') {
- $lang = substr($file, strlen($filename) + 1, -4);
- if (str_contains($lang, '-')) {
- //The user have quux-lang.txt and quux-foo-lang.txt files
- continue;
- }
- $file = $folder . DIRECTORY_SEPARATOR . $file;
- $this->localizations[$lang] = file_get_contents($file);
+ $tokens = explode('-', $file->getFileNameWithoutExtension());
+ if (count($tokens) > 2) {
+ //The user have quux-lang.txt and quux-foo-lang.txt files
+ continue;
}
+ $lang = $tokens[1];
+
+ $this->localizations[$lang] = $file->read();
}
//Fallback if only one file is offered
- $file = $folder . DIRECTORY_SEPARATOR . $filename . '.txt';
- if (file_exists($file)) {
+ $file = $dir->getFile($filename . '.txt');
+ if ($file->exists()) {
if (count($this->localizations)) {
if (array_key_exists(Language::FALLBACK, $this->localizations)) {
trigger_error("Ignored file: $filename.txt, as $filename-" . Language::FALLBACK . ".txt already exists and is used for fallback purpose", E_USER_NOTICE);
@@ -69,8 +72,8 @@
trigger_error("You have $filename.txt and $filename-<lang>.txt files; you should have one or the other, but not both", E_USER_NOTICE);
}
+ $this->localizations[Language::FALLBACK] = $file->read();
- $this->localizations[Language::FALLBACK] = file_get_contents($file);
return;
}
diff --git a/workspaces/src/apps/documents/DocumentsApplication.php b/workspaces/src/apps/documents/DocumentsApplication.php
--- a/workspaces/src/apps/documents/DocumentsApplication.php
+++ b/workspaces/src/apps/documents/DocumentsApplication.php
@@ -19,28 +19,31 @@
use Waystone\Workspaces\Engines\Errors\ErrorHandling;
use Waystone\Workspaces\Engines\I18n\Language;
+use Keruald\OmniTools\IO\Directory;
+use Keruald\OmniTools\IO\File;
+
/**
* Documents application class
*/
class DocumentsApplication extends Application {
+
/**
* @var string the application name
*/
public static $name = "Documents";
- /**
- * Gets path to a document file
- */
- private function getFilePath ($file) {
+ private Directory $directory;
+
+ protected function onAfterInitialize () : void {
global $Config;
- return $Config['Content']['Workspaces']
- . DIRECTORY_SEPARATOR
- . $this->context->workspace->code
- . DIRECTORY_SEPARATOR
- . $this->context->configuration->path
- . DIRECTORY_SEPARATOR
- . $file;
+ $path = [
+ $Config["Content"]["Workspaces"],
+ $this->context->workspace->code,
+ $this->context->configuration->path,
+ ];
+
+ $this->directory = Directory::fromPathFragments($path);
}
/**
@@ -49,15 +52,9 @@
* @return array The documents list
*/
public function getDocumentsList () {
- $dir = $this->getFilePath('');
- $files = scandir($dir);
- $documents = [];
- foreach ($files as $file) {
- if (get_extension($file) == 'json') {
- $documents[] = get_filename($file);
- }
- }
- return $documents;
+ $files = $this->directory->glob("*.json");
+
+ return array_map(fn ($file) => $file->getFileNameWithoutExtension(), $files);
}
/**
@@ -67,8 +64,7 @@
* @return stdClass the document JSON representation
*/
public function getDocument ($docId) {
- $file = $this->getFilePath($docId . '.json');
- $data = file_get_contents($file);
+ $data = $this->directory->getFile($docId . ".json")->read();
$data = json_decode($data);
if ($data === null) {
diff --git a/workspaces/src/apps/staticcontent/StaticContentApplication.php b/workspaces/src/apps/staticcontent/StaticContentApplication.php
--- a/workspaces/src/apps/staticcontent/StaticContentApplication.php
+++ b/workspaces/src/apps/staticcontent/StaticContentApplication.php
@@ -17,6 +17,8 @@
use Waystone\Workspaces\Engines\Apps\Application;
+use Keruald\OmniTools\IO\Directory;
+
/**
* Static content application class
*
@@ -28,28 +30,39 @@
*/
public static $name = "StaticContent";
- private function getFilePath ($file) {
+ private Directory $directory;
+
+ protected function onAfterInitialize () : void {
global $Config;
+ $path = [
+ $Config["Content"]["Workspaces"],
+ $this->context->workspace->code,
+ $this->context->configuration->path,
+ ];
+
+ $this->directory = Directory::fromPathFragments($path);
+ }
+
+
+ private function getFilePath ($file) {
if ($file === "" || $file === NULL) {
$file = "index.html";
}
- return $Config['Content']['Workspaces']
- . DIRECTORY_SEPARATOR
- . $this->context->workspace->code
- . DIRECTORY_SEPARATOR
- . $this->context->configuration->path
- . DIRECTORY_SEPARATOR
- . $file;
+ return $this->directory->getFile($file);
}
public function serveFile ($file) {
- $path = $this->getFilePath($file);
+ $file = $this->getFilePath($file);
+
$smarty = $this->context->templateEngine;
- if (file_exists($path)) {
- switch ($ext = strtolower(get_extension($path))) {
+ if ($file->exists()) {
+ $path = $file->getPath();
+
+ $ext = $file->getExtension();
+ switch ($ext) {
case "html":
case "htm":
$smarty->assign('PAGE_TITLE', $title);
diff --git a/workspaces/src/includes/GlobalFunctions.php b/workspaces/src/includes/GlobalFunctions.php
--- a/workspaces/src/includes/GlobalFunctions.php
+++ b/workspaces/src/includes/GlobalFunctions.php
@@ -4,33 +4,6 @@
use Keruald\OmniTools\HTTP\Requests\Request;
-////////////////////////////////////////////////////////////////////////////////
-/// ///
-/// Misc helper functions ///
-/// ///
-////////////////////////////////////////////////////////////////////////////////
-
-/**
- * Gets file extension
- * @param string $file the file to get the extension
-* @return string the file extension
- */
-function get_extension ($file) {
- $dotPosition = strrpos($file, ".");
- return substr($file, $dotPosition + 1);
-}
-
-/**
- * Gets file name
- * @param string $file the file to get the extension
- * @return string the file name
- */
-function get_filename ($file) {
- //TODO: clear directory
- $dotPosition = strrpos($file, ".");
- return substr($file, 0, $dotPosition);
-}
-
////////////////////////////////////////////////////////////////////////////////
/// ///
/// URL helpers functions ///

File Metadata

Mime Type
text/plain
Expires
Sun, Aug 23, 11:12 (18 h, 17 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
4028105
Default Alt Text
D4185.diff (9 KB)

Event Timeline